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 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 def __getScriptPath(scriptName, searchPathList):
86 if searchPathList is None or len(searchPathList) == 0:
89 for path in searchPathList:
90 fullName = os.path.join(path, scriptName)
91 if os.path.isfile(fullName) or os.path.isfile(fullName+".py"):
97 # Return an array of dictionaries {script_name: [list_of_its_args]}
98 def getScriptsAndArgs(args=[], searchPathList=None):
99 if searchPathList is None:
100 searchPathList = sys.path
102 # Syntax of args: script.py [args:a1,a2=val,an] ... script.py [args:a1,a2=val,an]
109 for i in range(len(args)):
112 if elt.startswith(argsPrefix):
113 if not currentKey or callPython:
114 raise SalomeContextException("args list must follow corresponding script file in command line.")
115 elt = elt.replace(argsPrefix, '')
116 scriptArgs[len(scriptArgs)-1][currentKey] = elt.split(",")
119 elif elt.startswith("python"):
122 if not os.path.isfile(elt) and not os.path.isfile(elt+".py"):
123 eltInSearchPath = __getScriptPath(elt, searchPathList)
124 if eltInSearchPath is None or (not os.path.isfile(eltInSearchPath) and not os.path.isfile(eltInSearchPath+".py")):
125 if elt[-3:] == ".py":
126 raise SalomeContextException("Script not found: %s"%elt)
128 elt = eltInSearchPath
130 if elt[-4:] != ".hdf":
131 if elt[-3:] == ".py":
132 currentScript = os.path.abspath(elt)
133 elif os.path.isfile(elt+".py"):
134 currentScript = os.path.abspath(elt+".py")
136 currentScript = os.path.abspath(elt) # python script not necessary has .py extension
138 if currentScript and callPython:
139 currentKey = "@PYTHONBIN@ "+currentScript
140 scriptArgs.append({currentKey:[]})
143 if not os.access(currentScript, os.X_OK):
144 currentKey = "@PYTHONBIN@ "+currentScript
145 scriptArgs.append({currentKey:[]})
149 fn = open(currentScript)
150 for i in xrange(10): # read only 10 first lines
152 if re.search("#!.*python"):
159 if not ispython and currentScript[-3:] == ".py":
160 currentKey = "@PYTHONBIN@ "+currentScript
162 currentKey = currentScript
164 scriptArgs.append({currentKey:[]})
169 # Formatting scripts and args as a Bash-like command-line:
170 # script1.py [args] ; script2.py [args] ; ...
171 def formatScriptsAndArgs(scriptArgs=[]):
173 for sc_dict in scriptArgs:
174 for script, sc_args in sc_dict.items(): # single entry
177 cmd = cmd + " " + " ".join(sc_args)
180 if sys.platform == "win32":
182 command = sep.join(["%s"%x for x in commands])
186 # Ensure OMNIORB_USER_PATH is defined. This variable refers to a the folder in which
187 # SALOME will write omniOrb configuration files.
188 # If OMNIORB_USER_PATH is already set, only checks write access to associated directory ;
189 # an exception is raised if check fails. It allows users for choosing a specific folder.
190 # Else the function sets OMNIORB_USER_PATH this way:
191 # - If APPLI environment variable is set, OMNIORB_USER_PATH is set to ${APPLI}/USERS.
192 # The function does not check USERS folder existence or wrute access. This folder
193 # must exist ; this is the case if SALOME virtual application has been create using
194 # appli_gen.py script.
195 # - Else OMNIORB_USER_PATH is set to user home directory.
196 def setOmniOrbUserPath():
197 omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
199 if not os.access(omniorbUserPath, os.W_OK):
200 raise Exception("Unable to get write access to directory: %s"%omniorbUserPath)
203 homePath = os.path.realpath(os.path.expanduser('~'))
204 #defaultOmniorbUserPath = os.path.join(homePath, ".salomeConfig/USERS")
205 defaultOmniorbUserPath = homePath
206 if os.getenv("APPLI"):
207 defaultOmniorbUserPath = os.path.join(homePath, os.getenv("APPLI"), "USERS")
209 os.environ["OMNIORB_USER_PATH"] = defaultOmniorbUserPath