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 SalomeContext
31 class SalomeContextException(Exception):
32 """Report error messages to the user interface of SalomeContext."""
35 def __listDirectory(path):
37 for root, dirs, files in os.walk(path):
38 cfgFiles = glob.glob(os.path.join(root,'*.cfg'))
41 shFiles = glob.glob(os.path.join(root,'*.sh'))
43 no_ext = os.path.splitext(f)[0]
44 if not os.path.isfile(no_ext+".cfg"):
50 def __getConfigFileNamesDefault():
51 absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')
52 if not absoluteAppliPath:
55 envdDir = absoluteAppliPath + '/env.d'
56 if not os.path.isdir(envdDir):
59 return __listDirectory(envdDir)
62 def getConfigFileNames(args, checkExistence=False):
63 # special case: configuration files are provided by user
64 # Search for command-line argument(s) --config=file1,file2,..., filen
65 # Search for command-line argument(s) --config=dir1,dir2,..., dirn
66 configOptionPrefix = "--config="
67 configArgs = [ str(x) for x in args if str(x).startswith(configOptionPrefix) ]
69 if len(configArgs) == 0:
70 return __getConfigFileNamesDefault(), args, []
72 args = [ x for x in args if not x.startswith(configOptionPrefix) ]
73 allLists = [ x.replace(configOptionPrefix, '') for x in configArgs ]
77 for currentList in allLists:
78 elements = currentList.split(',')
80 elt = os.path.realpath(os.path.expanduser(elt))
81 if os.path.isdir(elt):
82 configFileNames += __listDirectory(elt)
84 if checkExistence and not os.path.isfile(elt):
87 configFileNames += [elt]
89 return configFileNames, args, unexisting
92 def __getScriptPath(scriptName, searchPathList):
93 if searchPathList is None or len(searchPathList) == 0:
96 for path in searchPathList:
97 fullName = os.path.join(path, scriptName)
98 if os.path.isfile(fullName) or os.path.isfile(fullName+".py"):
104 # Return an array of dictionaries {script_name: [list_of_its_args]}
105 def getScriptsAndArgs(args=[], searchPathList=None):
106 if searchPathList is None:
107 searchPathList = sys.path
109 # Syntax of args: script.py [args:a1,a2=val,an] ... script.py [args:a1,a2=val,an]
116 for i in range(len(args)):
119 if elt.startswith(argsPrefix):
120 if not currentKey or callPython:
121 raise SalomeContextException("args list must follow corresponding script file in command line.")
122 elt = elt.replace(argsPrefix, '')
123 scriptArgs[len(scriptArgs)-1][currentKey] = elt.split(",")
126 elif elt.startswith("python"):
129 if not os.path.isfile(elt) and not os.path.isfile(elt+".py"):
130 eltInSearchPath = __getScriptPath(elt, searchPathList)
131 if eltInSearchPath is None or (not os.path.isfile(eltInSearchPath) and not os.path.isfile(eltInSearchPath+".py")):
132 if elt[-3:] == ".py":
133 raise SalomeContextException("Script not found: %s"%elt)
135 elt = eltInSearchPath
137 if elt[-4:] != ".hdf":
138 if elt[-3:] == ".py":
139 currentScript = os.path.abspath(elt)
140 elif os.path.isfile(elt+".py"):
141 currentScript = os.path.abspath(elt+".py")
143 currentScript = os.path.abspath(elt) # python script not necessary has .py extension
145 if currentScript and callPython:
146 currentKey = "@PYTHONBIN@ "+currentScript
147 scriptArgs.append({currentKey:[]})
150 if not os.access(currentScript, os.X_OK):
151 currentKey = "@PYTHONBIN@ "+currentScript
152 scriptArgs.append({currentKey:[]})
156 fn = open(currentScript)
157 for i in xrange(10): # read only 10 first lines
159 if re.search("#!.*python"):
166 if not ispython and currentScript[-3:] == ".py":
167 currentKey = "@PYTHONBIN@ "+currentScript
169 currentKey = currentScript
171 scriptArgs.append({currentKey:[]})
176 # Formatting scripts and args as a Bash-like command-line:
177 # script1.py [args] ; script2.py [args] ; ...
178 def formatScriptsAndArgs(scriptArgs=[]):
180 for sc_dict in scriptArgs:
181 for script, sc_args in sc_dict.items(): # single entry
184 cmd = cmd + " " + " ".join(sc_args)
187 if sys.platform == "win32":
189 command = sep.join(["%s"%x for x in commands])
193 # Ensure OMNIORB_USER_PATH is defined. This variable refers to a the folder in which
194 # SALOME will write omniOrb configuration files.
195 # If OMNIORB_USER_PATH is already set, only checks write access to associated directory ;
196 # an exception is raised if check fails. It allows users for choosing a specific folder.
197 # Else the function sets OMNIORB_USER_PATH this way:
198 # - If APPLI environment variable is set, OMNIORB_USER_PATH is set to ${APPLI}/USERS.
199 # The function does not check USERS folder existence or write access. This folder
200 # must exist ; this is the case if SALOME virtual application has been created using
201 # appli_gen.py script.
202 # - Else OMNIORB_USER_PATH is set to user home directory.
203 def setOmniOrbUserPath():
204 omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
206 if not os.access(omniorbUserPath, os.W_OK):
207 raise Exception("Unable to get write access to directory: %s"%omniorbUserPath)
210 homePath = os.path.realpath(os.path.expanduser('~'))
211 #defaultOmniorbUserPath = os.path.join(homePath, ".salomeConfig/USERS")
212 defaultOmniorbUserPath = homePath
213 if os.getenv("APPLI"):
214 defaultOmniorbUserPath = os.path.join(homePath, os.getenv("APPLI"), "USERS")
216 os.environ["OMNIORB_USER_PATH"] = defaultOmniorbUserPath