X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=bin%2FsalomeContextUtils.py.in;h=4835bcd25fd4a899c7f83b9306013b2b8ae36d39;hb=8aabfc9256249b3d60820832ac3450978a5a0f19;hp=dedf4056563b674893c4d9989920982c8b0b38a1;hpb=c2cdc6d4d86ed3be28bbaf635e6eaa0851709df1;p=modules%2Fkernel.git diff --git a/bin/salomeContextUtils.py.in b/bin/salomeContextUtils.py.in index dedf40565..4835bcd25 100644 --- a/bin/salomeContextUtils.py.in +++ b/bin/salomeContextUtils.py.in @@ -1,6 +1,6 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 -# Copyright (C) 2013-2014 CEA/DEN, EDF R&D, OPEN CASCADE +# Copyright (C) 2013-2016 CEA/DEN, EDF R&D, OPEN CASCADE # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -40,12 +40,6 @@ def __listDirectory(path): cfgFiles = glob.glob(os.path.join(root,'*.cfg')) allFiles += cfgFiles - shFiles = glob.glob(os.path.join(root,'*.sh')) - for f in shFiles: - no_ext = os.path.splitext(f)[0] - if not os.path.isfile(no_ext+".cfg"): - allFiles.append(f) - return allFiles # @@ -61,18 +55,14 @@ def __getConfigFileNamesDefault(): return __listDirectory(envdDir) # -def getConfigFileNames(args, checkExistence=False): - # special case: configuration files are provided by user - # Search for command-line argument(s) --config=file1,file2,..., filen - # Search for command-line argument(s) --config=dir1,dir2,..., dirn - configOptionPrefix = "--config=" - configArgs = [ str(x) for x in args if str(x).startswith(configOptionPrefix) ] +def __getEnvironmentFileNames(args, optionPrefix, checkExistence): + # special case: extra configuration/environment files are provided by user + # Search for command-line argument(s) =file1,file2,..., filen + # Search for command-line argument(s) =dir1,dir2,..., dirn + configArgs = [ str(x) for x in args if str(x).startswith(optionPrefix) ] - if len(configArgs) == 0: - return __getConfigFileNamesDefault(), args, [] - - args = [ x for x in args if not x.startswith(configOptionPrefix) ] - allLists = [ x.replace(configOptionPrefix, '') for x in configArgs ] + args = [ x for x in args if not x.startswith(optionPrefix) ] + allLists = [ x.replace(optionPrefix, '') for x in configArgs ] configFileNames = [] unexisting = [] @@ -91,6 +81,18 @@ def getConfigFileNames(args, checkExistence=False): return configFileNames, args, unexisting # +def getConfigFileNames(args, checkExistence=False): + configOptionPrefix = "--config=" + configArgs = [ str(x) for x in args if str(x).startswith(configOptionPrefix) ] + if len(configArgs) == 0: + configFileNames, unexist = __getConfigFileNamesDefault(), [] + else: + # get configuration filenames + configFileNames, args, unexist = __getEnvironmentFileNames(args, configOptionPrefix, checkExistence) + + return configFileNames, args, unexist +# + def __getScriptPath(scriptName, searchPathList): scriptName = os.path.expanduser(scriptName) if os.path.isabs(scriptName): @@ -111,7 +113,7 @@ class ScriptAndArgs: # script: the command to be run, e.g. python # args: its input parameters # out: its output parameters - def __init__(self, script = None, args = None, out = None): + def __init__(self, script=None, args=None, out=None): self.script = script self.args = args self.out = out @@ -133,10 +135,28 @@ class ScriptAndArgsObjectEncoder(json.JSONEncoder): return json.JSONEncoder.default(self, obj) # +def getShortAndExtraArgs(args=None): + if args is None: + args = [] + try: + pos = args.index("--") # raise a ValueError if not found + short_args = args[:pos] + extra_args = args[pos:] # include "--" + except ValueError: + short_args = args + extra_args = [] + pass + + return short_args, extra_args +# + # Return an array of ScriptAndArgs objects def getScriptsAndArgs(args=None, searchPathList=None): if args is None: args = [] + short_args, extra_args = getShortAndExtraArgs(args) + args = short_args + if searchPathList is None: searchPathList = sys.path @@ -157,7 +177,35 @@ def getScriptsAndArgs(args=None, searchPathList=None): if not currentKey or callPython: raise SalomeContextException("args list must follow corresponding script file in command line.") elt = elt.replace(argsPrefix, '') - scriptArgs[len(scriptArgs)-1].args = [os.path.expanduser(x) for x in elt.split(",")] + # Special process if some items of 'args:' list are themselves lists + # Note that an item can be a list, but not a list of lists... + # So we can have something like this: + # myscript.py args:['file1','file2'],val1,"done",[1,2,3],[True,False],"ok" + # With such a call, an elt variable contains the string representing ['file1','file2'],val1,"done",[1,2,3],[True,False],"ok" that is '[file1,file2],val1,done,[1,2,3],[True,False],ok' + # We have to split elt to obtain: ['[file1,file2]','val1','done','[1,2,3]','[True,False]','ok'] + contains_list = re.findall('(\[[^\]]*\])', elt) + if contains_list: + extracted_args = [] + x = elt.split(",") + # x is ['[file1', 'file2]', 'val1', 'done', '[1', '2', '3]', '[True', 'False]', 'ok'] + list_begin_indices = [i for i in range(len(x)) if x[i].startswith('[')] # [0, 4, 7] + list_end_indices = [i for i in range(len(x)) if x[i].endswith(']')] # [1, 6, 8] + start = 0 + for lbeg, lend in zip(list_begin_indices,list_end_indices): # [(0, 1), (4, 6), (7, 8)] + if lbeg > start: + extracted_args += x[start:lbeg] + pass + extracted_args += [','.join(x[lbeg:lend+1])] + start = lend+1 + pass + if start < len(x): + extracted_args += x[start:len(x)] + pass + scriptArgs[len(scriptArgs)-1].args = extracted_args + pass + else: # a single split is enough + scriptArgs[len(scriptArgs)-1].args = [os.path.expanduser(x) for x in elt.split(",")] + pass currentKey = None callPython = False afterArgs = True @@ -173,16 +221,18 @@ def getScriptsAndArgs(args=None, searchPathList=None): callPython = True afterArgs = False else: + file_extension = os.path.splitext(elt)[-1] if not os.path.isfile(elt) and not os.path.isfile(elt+".py"): eltInSearchPath = __getScriptPath(elt, searchPathList) if eltInSearchPath is None or (not os.path.isfile(eltInSearchPath) and not os.path.isfile(eltInSearchPath+".py")): - if elt[-3:] == ".py": + if file_extension == ".py": raise SalomeContextException("Script not found: %s"%elt) + scriptArgs.append(ScriptAndArgs(script=elt)) continue elt = eltInSearchPath - if elt[-4:] != ".hdf": - if elt[-3:] == ".py" or isDriver: + if file_extension != ".hdf": + if file_extension == ".py" or isDriver: currentScript = os.path.abspath(elt) elif os.path.isfile(elt+".py"): currentScript = os.path.abspath(elt+".py") @@ -195,6 +245,7 @@ def getScriptsAndArgs(args=None, searchPathList=None): scriptArgs.append(ScriptAndArgs(script=currentKey)) callPython = False elif currentScript: + script_extension = os.path.splitext(currentScript)[-1] if isDriver: currentKey = currentScript scriptArgs.append(ScriptAndArgs(script=currentKey)) @@ -206,7 +257,7 @@ def getScriptsAndArgs(args=None, searchPathList=None): ispython = False try: fn = open(currentScript) - for i in xrange(10): # read only 10 first lines + for i in range(10): # read only 10 first lines ln = fn.readline() if re.search("#!.*python"): ispython = True @@ -215,7 +266,7 @@ def getScriptsAndArgs(args=None, searchPathList=None): fn.close() except: pass - if not ispython and currentScript[-3:] == ".py": + if not ispython and script_extension == ".py": currentKey = "@PYTHONBIN@ "+currentScript else: currentKey = currentScript @@ -224,6 +275,13 @@ def getScriptsAndArgs(args=None, searchPathList=None): # CLOSE elif currentScript afterArgs = False # end for loop + + if len(extra_args) > 1: # syntax: -- program [options] [arguments] + command = extra_args[1] + command_args = extra_args[2:] + scriptArgs.append(ScriptAndArgs(script=command, args=command_args)) + pass + return scriptArgs # @@ -247,7 +305,7 @@ def formatScriptsAndArgs(scriptArgs=None): return command # -# Ensure OMNIORB_USER_PATH is defined. This variable refers to a the folder in which +# Ensure OMNIORB_USER_PATH is defined. This variable refers to a folder in which # SALOME will write omniOrb configuration files. # If OMNIORB_USER_PATH is already set, only checks write access to associated directory ; # an exception is raised if check fails. It allows users for choosing a specific folder. @@ -264,13 +322,23 @@ def setOmniOrbUserPath(): raise Exception("Unable to get write access to directory: %s"%omniorbUserPath) pass else: - homePath = os.path.realpath(os.path.expanduser('~')) - #defaultOmniorbUserPath = os.path.join(homePath, ".salomeConfig/USERS") - defaultOmniorbUserPath = homePath - if os.getenv("APPLI"): - defaultOmniorbUserPath = os.path.join(homePath, os.getenv("APPLI"), "USERS") - pass - os.environ["OMNIORB_USER_PATH"] = defaultOmniorbUserPath + # Must be in /tmp (or equivalent) to handle application concurrency + try: + import tempfile + temp = tempfile.NamedTemporaryFile() + temp_dir = os.path.dirname(temp.name) + temp.close() + if not os.access(temp_dir, os.W_OK): + raise Exception("Unable to get write access to directory: %s"%temp_dir) + os.environ["OMNIORB_USER_PATH"] = temp_dir + except: + homePath = os.path.realpath(os.path.expanduser('~')) + #defaultOmniorbUserPath = os.path.join(homePath, ".salomeConfig/USERS") + defaultOmniorbUserPath = homePath + if os.getenv("APPLI"): + defaultOmniorbUserPath = os.path.join(homePath, os.getenv("APPLI"), "USERS") + pass + os.environ["OMNIORB_USER_PATH"] = defaultOmniorbUserPath # def getHostname():