9 Define a specific exception class to manage exceptions related to SalomeRunner
11 class SalomeRunnerException(Exception):
12 """Report error messages to the user interface of SalomeRunner."""
15 def __listDirectory(path):
17 for root, dirs, files in os.walk(path):
18 configFileNames = glob.glob(os.path.join(root,'*.cfg')) + glob.glob(os.path.join(root,'*.sh'))
19 allFiles += configFileNames
23 def __getConfigFileNamesDefault():
24 absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')
25 if not absoluteAppliPath:
28 envdDir = absoluteAppliPath + '/env.d'
29 if not os.path.isdir(envdDir):
32 return __listDirectory(envdDir)
35 def getConfigFileNames(args, checkExistence=False):
36 # special case: configuration files are provided by user
37 # Search for command-line argument(s) --config=file1,file2,..., filen
38 # Search for command-line argument(s) --config=dir1,dir2,..., dirn
39 configOptionPrefix = "--config="
40 configArgs = [ str(x) for x in args if str(x).startswith(configOptionPrefix) ]
42 if len(configArgs) == 0:
43 return __getConfigFileNamesDefault(), args, []
45 args = [ x for x in args if not x.startswith(configOptionPrefix) ]
46 allLists = [ x.replace(configOptionPrefix, '') for x in configArgs ]
50 for currentList in allLists:
51 elements = currentList.split(',')
53 elt = os.path.realpath(os.path.expanduser(elt))
54 if os.path.isdir(elt):
55 configFileNames += __listDirectory(elt)
57 if checkExistence and not os.path.isfile(elt):
60 configFileNames += [elt]
62 return configFileNames, args, unexisting
65 # Return an array of dictionaries {script_name: [list_of_its_args]}
66 def getScriptsAndArgs(args=[]):
67 # Syntax of args: script.py [args:a1,a2=val,an] ... script.py [args:a1,a2=val,an]
73 for i in range(len(args)):
76 if elt.startswith(argsPrefix):
77 if not currentKey or callPython:
78 raise SalomeRunnerException("args list must follow corresponding script file in command line.")
79 elt = elt.replace(argsPrefix, '')
80 scriptArgs[len(scriptArgs)-1][currentKey] = elt.split(",")
83 elif elt.startswith("python"):
85 elif os.path.isfile(elt) or os.path.isfile(elt+".py"):
87 currentScript = os.path.abspath(elt)
90 if elt[-4:] != ".hdf":
91 currentScript = os.path.abspath(elt+".py")
92 if currentScript and callPython:
93 currentKey = "python "+currentScript
94 scriptArgs.append({currentKey:[]})
97 if not os.access(currentScript, os.X_OK):
98 currentKey = "python "+currentScript
99 scriptArgs.append({currentKey:[]})
101 currentKey = currentScript
102 scriptArgs.append({currentKey:[]})
107 # Formatting scripts and args as a Bash-like command-line:
108 # script1.py [args] ; script2.py [args] ; ...
109 def formatScriptsAndArgs(scriptArgs=[]):
111 for sc_dict in scriptArgs:
112 for script, sc_args in sc_dict.items(): # single entry
115 cmd = cmd + " " + " ".join(sc_args)
118 command = "; ".join(["%s"%x for x in commands])
122 # Ensure OMNIORB_USER_PATH is defined. This variable refers to a the folder in which
123 # SALOME will write omniOrb configuration files.
124 # If OMNIORB_USER_PATH is already set, only checks write access to associated directory ;
125 # an exception is raised if check fails. It allows users for choosing a specific folder.
126 # Else the function sets OMNIORB_USER_PATH this way:
127 # - If APPLI environment variable is set, OMNIORB_USER_PATH is set to ${APPLI}/USERS.
128 # The function does not check USERS folder existence or wrute access. This folder
129 # must exist ; this is the case if SALOME virtual application has been create using
130 # appli_gen.py script.
131 # - Else OMNIORB_USER_PATH is set to user home directory.
132 def setOmniOrbUserPath():
133 omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
135 if not os.access(omniorbUserPath, os.W_OK):
136 raise Exception("Unable to get write access to directory: %s"%omniorbUserPath)
139 homePath = os.path.realpath(os.path.expanduser('~'))
140 #defaultOmniorbUserPath = os.path.join(homePath, ".salomeConfig/USERS")
141 defaultOmniorbUserPath = homePath
142 if os.getenv("APPLI"):
143 defaultOmniorbUserPath = os.path.join(homePath, os.getenv("APPLI"), "USERS")
145 os.environ["OMNIORB_USER_PATH"] = defaultOmniorbUserPath