10 Define a specific exception class to manage exceptions related to SalomeRunner
12 class SalomeRunnerException(Exception):
13 """Report error messages to the user interface of SalomeRunner."""
16 def __listDirectory(path):
18 for root, dirs, files in os.walk(path):
19 configFileNames = glob.glob(os.path.join(root,'*.cfg')) + glob.glob(os.path.join(root,'*.sh'))
20 allFiles += configFileNames
24 def __getConfigFileNamesDefault():
25 absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')
26 if not absoluteAppliPath:
29 envdDir = absoluteAppliPath + '/env.d'
30 if not os.path.isdir(envdDir):
33 return __listDirectory(envdDir)
36 def getConfigFileNames(args, checkExistence=False):
37 # special case: configuration files are provided by user
38 # Search for command-line argument(s) --config=file1,file2,..., filen
39 # Search for command-line argument(s) --config=dir1,dir2,..., dirn
40 configOptionPrefix = "--config="
41 configArgs = [ str(x) for x in args if str(x).startswith(configOptionPrefix) ]
43 if len(configArgs) == 0:
44 return __getConfigFileNamesDefault(), args, []
46 args = [ x for x in args if not x.startswith(configOptionPrefix) ]
47 allLists = [ x.replace(configOptionPrefix, '') for x in configArgs ]
51 for currentList in allLists:
52 elements = currentList.split(',')
54 elt = os.path.realpath(os.path.expanduser(elt))
55 if os.path.isdir(elt):
56 configFileNames += __listDirectory(elt)
58 if checkExistence and not os.path.isfile(elt):
61 configFileNames += [elt]
63 return configFileNames, args, unexisting
66 # Return an array of dictionaries {script_name: [list_of_its_args]}
67 def getScriptsAndArgs(args=[]):
68 # Syntax of args: script.py [args:a1,a2=val,an] ... script.py [args:a1,a2=val,an]
75 for i in range(len(args)):
78 if elt.startswith(argsPrefix):
79 if not currentKey or callPython:
80 raise SalomeRunnerException("args list must follow corresponding script file in command line.")
81 elt = elt.replace(argsPrefix, '')
82 scriptArgs[len(scriptArgs)-1][currentKey] = elt.split(",")
85 elif elt.startswith("python"):
87 elif os.path.isfile(elt) or os.path.isfile(elt+".py"):
88 if elt[-4:] != ".hdf":
90 currentScript = os.path.abspath(elt)
91 elif os.path.isfile(elt+".py"):
92 currentScript = os.path.abspath(elt+".py")
94 currentScript = os.path.abspath(elt) # python script not necessary has .py extension
96 if currentScript and callPython:
97 currentKey = "python "+currentScript
98 scriptArgs.append({currentKey:[]})
101 if not os.access(currentScript, os.X_OK):
102 currentKey = "python "+currentScript
103 scriptArgs.append({currentKey:[]})
107 fn = open(currentScript)
108 for i in xrange(10): # read only 10 first lines
110 if re.search("#!.*python"):
117 if not ispython and currentScript[-3:] == ".py":
118 currentKey = "python "+currentScript
120 currentKey = currentScript
122 scriptArgs.append({currentKey:[]})
127 # Formatting scripts and args as a Bash-like command-line:
128 # script1.py [args] ; script2.py [args] ; ...
129 def formatScriptsAndArgs(scriptArgs=[]):
131 for sc_dict in scriptArgs:
132 for script, sc_args in sc_dict.items(): # single entry
135 cmd = cmd + " " + " ".join(sc_args)
138 command = "; ".join(["%s"%x for x in commands])
142 # Ensure OMNIORB_USER_PATH is defined. This variable refers to a the folder in which
143 # SALOME will write omniOrb configuration files.
144 # If OMNIORB_USER_PATH is already set, only checks write access to associated directory ;
145 # an exception is raised if check fails. It allows users for choosing a specific folder.
146 # Else the function sets OMNIORB_USER_PATH this way:
147 # - If APPLI environment variable is set, OMNIORB_USER_PATH is set to ${APPLI}/USERS.
148 # The function does not check USERS folder existence or wrute access. This folder
149 # must exist ; this is the case if SALOME virtual application has been create using
150 # appli_gen.py script.
151 # - Else OMNIORB_USER_PATH is set to user home directory.
152 def setOmniOrbUserPath():
153 omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
155 if not os.access(omniorbUserPath, os.W_OK):
156 raise Exception("Unable to get write access to directory: %s"%omniorbUserPath)
159 homePath = os.path.realpath(os.path.expanduser('~'))
160 #defaultOmniorbUserPath = os.path.join(homePath, ".salomeConfig/USERS")
161 defaultOmniorbUserPath = homePath
162 if os.getenv("APPLI"):
163 defaultOmniorbUserPath = os.path.join(homePath, os.getenv("APPLI"), "USERS")
165 os.environ["OMNIORB_USER_PATH"] = defaultOmniorbUserPath