3 # Copyright (C) 2013-2014 CEA/DEN, EDF R&D, OPEN CASCADE
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
29 Define a specific exception class to manage exceptions related to SalomeRunner
31 class SalomeRunnerException(Exception):
32 """Report error messages to the user interface of SalomeRunner."""
35 def __listDirectory(path):
37 for root, dirs, files in os.walk(path):
38 configFileNames = glob.glob(os.path.join(root,'*.cfg')) + glob.glob(os.path.join(root,'*.sh'))
39 allFiles += configFileNames
43 def __getConfigFileNamesDefault():
44 absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')
45 if not absoluteAppliPath:
48 envdDir = absoluteAppliPath + '/env.d'
49 if not os.path.isdir(envdDir):
52 return __listDirectory(envdDir)
55 def getConfigFileNames(args, checkExistence=False):
56 # special case: configuration files are provided by user
57 # Search for command-line argument(s) --config=file1,file2,..., filen
58 # Search for command-line argument(s) --config=dir1,dir2,..., dirn
59 configOptionPrefix = "--config="
60 configArgs = [ str(x) for x in args if str(x).startswith(configOptionPrefix) ]
62 if len(configArgs) == 0:
63 return __getConfigFileNamesDefault(), args, []
65 args = [ x for x in args if not x.startswith(configOptionPrefix) ]
66 allLists = [ x.replace(configOptionPrefix, '') for x in configArgs ]
70 for currentList in allLists:
71 elements = currentList.split(',')
73 elt = os.path.realpath(os.path.expanduser(elt))
74 if os.path.isdir(elt):
75 configFileNames += __listDirectory(elt)
77 if checkExistence and not os.path.isfile(elt):
80 configFileNames += [elt]
82 return configFileNames, args, unexisting
85 # Return an array of dictionaries {script_name: [list_of_its_args]}
86 def getScriptsAndArgs(args=[]):
87 # Syntax of args: script.py [args:a1,a2=val,an] ... script.py [args:a1,a2=val,an]
94 for i in range(len(args)):
97 if elt.startswith(argsPrefix):
98 if not currentKey or callPython:
99 raise SalomeRunnerException("args list must follow corresponding script file in command line.")
100 elt = elt.replace(argsPrefix, '')
101 scriptArgs[len(scriptArgs)-1][currentKey] = elt.split(",")
104 elif elt.startswith("python"):
106 elif os.path.isfile(elt) or os.path.isfile(elt+".py"):
107 if elt[-4:] != ".hdf":
108 if elt[-3:] == ".py":
109 currentScript = os.path.abspath(elt)
110 elif os.path.isfile(elt+".py"):
111 currentScript = os.path.abspath(elt+".py")
113 currentScript = os.path.abspath(elt) # python script not necessary has .py extension
115 if currentScript and callPython:
116 currentKey = "@PYTHONBIN@ "+currentScript
117 scriptArgs.append({currentKey:[]})
120 if not os.access(currentScript, os.X_OK):
121 currentKey = "@PYTHONBIN@ "+currentScript
122 scriptArgs.append({currentKey:[]})
126 fn = open(currentScript)
127 for i in xrange(10): # read only 10 first lines
129 if re.search("#!.*python"):
136 if not ispython and currentScript[-3:] == ".py":
137 currentKey = "@PYTHONBIN@ "+currentScript
139 currentKey = currentScript
141 scriptArgs.append({currentKey:[]})
146 # Formatting scripts and args as a Bash-like command-line:
147 # script1.py [args] ; script2.py [args] ; ...
148 def formatScriptsAndArgs(scriptArgs=[]):
150 for sc_dict in scriptArgs:
151 for script, sc_args in sc_dict.items(): # single entry
154 cmd = cmd + " " + " ".join(sc_args)
157 if sys.platform == "win32":
159 command = sep.join(["%s"%x for x in commands])
163 # Ensure OMNIORB_USER_PATH is defined. This variable refers to a the folder in which
164 # SALOME will write omniOrb configuration files.
165 # If OMNIORB_USER_PATH is already set, only checks write access to associated directory ;
166 # an exception is raised if check fails. It allows users for choosing a specific folder.
167 # Else the function sets OMNIORB_USER_PATH this way:
168 # - If APPLI environment variable is set, OMNIORB_USER_PATH is set to ${APPLI}/USERS.
169 # The function does not check USERS folder existence or wrute access. This folder
170 # must exist ; this is the case if SALOME virtual application has been create using
171 # appli_gen.py script.
172 # - Else OMNIORB_USER_PATH is set to user home directory.
173 def setOmniOrbUserPath():
174 omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
176 if not os.access(omniorbUserPath, os.W_OK):
177 raise Exception("Unable to get write access to directory: %s"%omniorbUserPath)
180 homePath = os.path.realpath(os.path.expanduser('~'))
181 #defaultOmniorbUserPath = os.path.join(homePath, ".salomeConfig/USERS")
182 defaultOmniorbUserPath = homePath
183 if os.getenv("APPLI"):
184 defaultOmniorbUserPath = os.path.join(homePath, os.getenv("APPLI"), "USERS")
186 os.environ["OMNIORB_USER_PATH"] = defaultOmniorbUserPath