Salome HOME
Merge tag 'V8_3_0a2' into ngr/python3_dev
[modules/kernel.git] / bin / salomeContextUtils.py.in
1 #! /usr/bin/env python
2
3 # Copyright (C) 2013-2016  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
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.
9 #
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.
14 #
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
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 import os
23 import sys
24 import glob
25 import subprocess
26 import re
27 import socket
28 import json
29
30 """
31 Define a specific exception class to manage exceptions related to SalomeContext
32 """
33 class SalomeContextException(Exception):
34   """Report error messages to the user interface of SalomeContext."""
35 #
36
37 def __listDirectory(path):
38   allFiles = []
39   for root, dirs, files in os.walk(path):
40     cfgFiles = glob.glob(os.path.join(root,'*.cfg'))
41     allFiles += cfgFiles
42
43   return allFiles
44 #
45
46 def __getConfigFileNamesDefault():
47   absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')
48   if not absoluteAppliPath:
49     return []
50
51   envdDir = absoluteAppliPath + '/env.d'
52   if not os.path.isdir(envdDir):
53     return []
54
55   return __listDirectory(envdDir)
56 #
57
58 def __getEnvironmentFileNames(args, optionPrefix, checkExistence):
59   # special case: extra configuration/environment files are provided by user
60   # Search for command-line argument(s) <optionPrefix>=file1,file2,..., filen
61   # Search for command-line argument(s) <optionPrefix>=dir1,dir2,..., dirn
62   configArgs = [ str(x) for x in args if str(x).startswith(optionPrefix) ]
63
64   args = [ x for x in args if not x.startswith(optionPrefix) ]
65   allLists = [ x.replace(optionPrefix, '') for x in configArgs ]
66
67   configFileNames = []
68   unexisting = []
69   for currentList in allLists:
70     elements = currentList.split(',')
71     for elt in elements:
72       elt = os.path.realpath(os.path.expanduser(elt))
73       if os.path.isdir(elt):
74         configFileNames += __listDirectory(elt)
75       else:
76         if checkExistence and not os.path.isfile(elt):
77           unexisting += [elt]
78         else:
79           configFileNames += [elt]
80
81   return configFileNames, args, unexisting
82 #
83
84 def getConfigFileNames(args, checkExistence=False):
85   configOptionPrefix = "--config="
86   configArgs = [ str(x) for x in args if str(x).startswith(configOptionPrefix) ]
87   if len(configArgs) == 0:
88     configFileNames, unexist = __getConfigFileNamesDefault(), []
89   else:
90     # get configuration filenames
91     configFileNames, args, unexist = __getEnvironmentFileNames(args, configOptionPrefix, checkExistence)
92
93   return configFileNames, args, unexist
94 #
95
96 def __getScriptPath(scriptName, searchPathList):
97   scriptName = os.path.expanduser(scriptName)
98   if os.path.isabs(scriptName):
99     return scriptName
100
101   if searchPathList is None or len(searchPathList) == 0:
102     return None
103
104   for path in searchPathList:
105     fullName = os.path.join(path, scriptName)
106     if os.path.isfile(fullName) or os.path.isfile(fullName+".py"):
107       return fullName
108
109   return None
110 #
111
112 class ScriptAndArgs:
113   # script: the command to be run, e.g. python <script.py>
114   # args: its input parameters
115   # out: its output parameters
116   def __init__(self, script=None, args=None, out=None):
117     self.script = script
118     self.args = args
119     self.out = out
120   #
121   def __repr__(self):
122     msg = "\n# Script: %s\n"%self.script
123     msg += "     * Input: %s\n"%self.args
124     msg += "     * Output: %s\n"%self.out
125     return msg
126   #
127 #
128 class ScriptAndArgsObjectEncoder(json.JSONEncoder):
129   def default(self, obj):
130     if isinstance(obj, ScriptAndArgs):
131       # to be easily parsed in GUI module (SalomeApp_Application)
132       # Do not export output arguments
133       return {obj.script:obj.args or []}
134     else:
135       return json.JSONEncoder.default(self, obj)
136 #
137
138 def getShortAndExtraArgs(args=None):
139   if args is None:
140     args = []
141   try:
142     pos = args.index("--") # raise a ValueError if not found
143     short_args = args[:pos]
144     extra_args = args[pos:] # include "--"
145   except ValueError:
146     short_args = args
147     extra_args = []
148     pass
149
150   return short_args, extra_args
151 #
152
153 # Return an array of ScriptAndArgs objects
154 def getScriptsAndArgs(args=None, searchPathList=None):
155   if args is None:
156     args = []
157   short_args, extra_args = getShortAndExtraArgs(args)
158   args = short_args
159
160   if searchPathList is None:
161     searchPathList = sys.path
162
163   # Syntax of args: script.py [args:a1,a2=val,an] ... script.py [args:a1,a2=val,an]
164   scriptArgs = []
165   currentKey = None
166   argsPrefix = "args:"
167   outPrefix = "out:"
168   callPython = False
169   afterArgs = False
170   currentScript = None
171
172   for i in range(len(args)):
173     elt = os.path.expanduser(args[i])
174     isDriver = (elt == "driver") # special case for YACS scheme execution
175
176     if elt.startswith(argsPrefix):
177       if not currentKey or callPython:
178         raise SalomeContextException("args list must follow corresponding script file in command line.")
179       elt = elt.replace(argsPrefix, '')
180       # Special process if some items of 'args:' list are themselves lists
181       # Note that an item can be a list, but not a list of lists...
182       # So we can have something like this:
183       # myscript.py args:['file1','file2'],val1,"done",[1,2,3],[True,False],"ok"
184       # With such a call, an elt variable contains the string representing ['file1','file2'],val1,"done",[1,2,3],[True,False],"ok" that is '[file1,file2],val1,done,[1,2,3],[True,False],ok'
185       # We have to split elt to obtain: ['[file1,file2]','val1','done','[1,2,3]','[True,False]','ok']
186       contains_list = re.findall('(\[[^\]]*\])', elt)
187       if contains_list:
188         extracted_args = []
189         x = elt.split(",")
190         # x is ['[file1', 'file2]', 'val1', 'done', '[1', '2', '3]', '[True', 'False]', 'ok']
191         list_begin_indices = [i for i in range(len(x)) if x[i].startswith('[')] # [0, 4, 7]
192         list_end_indices = [i for i in range(len(x)) if x[i].endswith(']')] # [1, 6, 8]
193         start = 0
194         for lbeg, lend in zip(list_begin_indices,list_end_indices): # [(0, 1), (4, 6), (7, 8)]
195           if lbeg > start:
196             extracted_args += x[start:lbeg]
197             pass
198           extracted_args += [','.join(x[lbeg:lend+1])]
199           start = lend+1
200           pass
201         if start < len(x):
202           extracted_args += x[start:len(x)]
203           pass
204         scriptArgs[len(scriptArgs)-1].args = extracted_args
205         pass
206       else: # a single split is enough
207         scriptArgs[len(scriptArgs)-1].args = [os.path.expanduser(x) for x in elt.split(",")]
208         pass
209       currentKey = None
210       callPython = False
211       afterArgs = True
212     elif elt.startswith(outPrefix):
213       if (not currentKey and not afterArgs) or callPython:
214         raise SalomeContextException("out list must follow both corresponding script file and its args in command line.")
215       elt = elt.replace(outPrefix, '')
216       scriptArgs[len(scriptArgs)-1].out = [os.path.expanduser(x) for x in elt.split(",")]
217       currentKey = None
218       callPython = False
219       afterArgs = False
220     elif elt.startswith("python"):
221       callPython = True
222       afterArgs = False
223     else:
224       if not os.path.isfile(elt) and not os.path.isfile(elt+".py"):
225         eltInSearchPath = __getScriptPath(elt, searchPathList)
226         if eltInSearchPath is None or (not os.path.isfile(eltInSearchPath) and not os.path.isfile(eltInSearchPath+".py")):
227           if elt[-3:] == ".py":
228             raise SalomeContextException("Script not found: %s"%elt)
229           scriptArgs.append(ScriptAndArgs(script=elt))
230           continue
231         elt = eltInSearchPath
232
233       if elt[-4:] != ".hdf":
234         if elt[-3:] == ".py" or isDriver:
235           currentScript = os.path.abspath(elt)
236         elif os.path.isfile(elt+".py"):
237           currentScript = os.path.abspath(elt+".py")
238         else:
239           currentScript = os.path.abspath(elt) # python script not necessary has .py extension
240         pass
241
242       if currentScript and callPython:
243         currentKey = "@PYTHONBIN@ "+currentScript
244         scriptArgs.append(ScriptAndArgs(script=currentKey))
245         callPython = False
246       elif currentScript:
247         if isDriver:
248           currentKey = currentScript
249           scriptArgs.append(ScriptAndArgs(script=currentKey))
250           callPython = False
251         elif not os.access(currentScript, os.X_OK):
252           currentKey = "@PYTHONBIN@ "+currentScript
253           scriptArgs.append(ScriptAndArgs(script=currentKey))
254         else:
255           ispython = False
256           try:
257             fn = open(currentScript)
258             for i in range(10): # read only 10 first lines
259               ln = fn.readline()
260               if re.search("#!.*python"):
261                 ispython = True
262                 break
263               pass
264             fn.close()
265           except:
266             pass
267           if not ispython and currentScript[-3:] == ".py":
268             currentKey = "@PYTHONBIN@ "+currentScript
269           else:
270             currentKey = currentScript
271             pass
272           scriptArgs.append(ScriptAndArgs(script=currentKey))
273       # CLOSE elif currentScript
274       afterArgs = False
275   # end for loop
276
277   if len(extra_args) > 1: # syntax: -- program [options] [arguments]
278     command = extra_args[1]
279     command_args = extra_args[2:]
280     scriptArgs.append(ScriptAndArgs(script=command, args=command_args))
281     pass
282
283   return scriptArgs
284 #
285
286 # Formatting scripts and args as a Bash-like command-line:
287 # script1.py [args] ; script2.py [args] ; ...
288 # scriptArgs is a list of ScriptAndArgs objects; their output parameters are omitted
289 def formatScriptsAndArgs(scriptArgs=None):
290     if scriptArgs is None:
291       return ""
292     commands = []
293     for sa_obj in scriptArgs:
294       cmd = sa_obj.script
295       if sa_obj.args:
296         cmd = " ".join([cmd]+sa_obj.args)
297       commands.append(cmd)
298
299     sep = " ; "
300     if sys.platform == "win32":
301       sep = " & "
302     command = sep.join(["%s"%x for x in commands])
303     return command
304 #
305
306 # Ensure OMNIORB_USER_PATH is defined. This variable refers to a folder in which
307 # SALOME will write omniOrb configuration files.
308 # If OMNIORB_USER_PATH is already set, only checks write access to associated directory ;
309 # an exception is raised if check fails. It allows users for choosing a specific folder.
310 # Else the function sets OMNIORB_USER_PATH this way:
311 # - If APPLI environment variable is set, OMNIORB_USER_PATH is set to ${APPLI}/USERS.
312 #   The function does not check USERS folder existence or write access. This folder
313 #   must exist ; this is the case if SALOME virtual application has been created using
314 #   appli_gen.py script.
315 # - Else OMNIORB_USER_PATH is set to user home directory.
316 def setOmniOrbUserPath():
317   omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
318   if omniorbUserPath:
319     if not os.access(omniorbUserPath, os.W_OK):
320       raise Exception("Unable to get write access to directory: %s"%omniorbUserPath)
321     pass
322   else:
323     homePath = os.path.realpath(os.path.expanduser('~'))
324     #defaultOmniorbUserPath = os.path.join(homePath, ".salomeConfig/USERS")
325     defaultOmniorbUserPath = homePath
326     if os.getenv("APPLI"):
327       defaultOmniorbUserPath = os.path.join(homePath, os.getenv("APPLI"), "USERS")
328       pass
329     os.environ["OMNIORB_USER_PATH"] = defaultOmniorbUserPath
330 #
331
332 def getHostname():
333   return socket.gethostname().split('.')[0]
334 #