Salome HOME
Fix CPPUNIT detection when its library directory contains -l (like -L/usr/lib/x86_64...
[modules/kernel.git] / bin / salomeLauncherUtils.py
1 #! /usr/bin/env python
2
3 import os
4 import sys
5 import glob
6 import subprocess
7
8 """
9 Define a specific exception class to manage exceptions related to SalomeRunner
10 """
11 class SalomeRunnerException(Exception):
12   """Report error messages to the user interface of SalomeRunner."""
13 #
14
15 def __listDirectory(path):
16   allFiles = []
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
20   return allFiles
21 #
22
23 def __getConfigFileNamesDefault():
24   absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')
25   if not absoluteAppliPath:
26     return []
27
28   envdDir = absoluteAppliPath + '/env.d'
29   if not os.path.isdir(envdDir):
30     return []
31
32   return __listDirectory(envdDir)
33 #
34
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) ]
41
42   if len(configArgs) == 0:
43     return __getConfigFileNamesDefault(), args, []
44
45   args = [ x for x in args if not x.startswith(configOptionPrefix) ]
46   allLists = [ x.replace(configOptionPrefix, '') for x in configArgs ]
47
48   configFileNames = []
49   unexisting = []
50   for currentList in allLists:
51     elements = currentList.split(',')
52     for elt in elements:
53       elt = os.path.realpath(os.path.expanduser(elt))
54       if os.path.isdir(elt):
55         configFileNames += __listDirectory(elt)
56       else:
57         if checkExistence and not os.path.isfile(elt):
58           unexisting += [elt]
59         else:
60           configFileNames += [elt]
61
62   return configFileNames, args, unexisting
63 #
64
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]
68   scriptArgs = []
69   currentKey = None
70   argsPrefix = "args:"
71   callPython = False
72
73   for i in range(len(args)):
74     elt = args[i]
75
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(",")
81       currentKey = None
82       callPython = False
83     elif elt.startswith("python"):
84       callPython = True
85     elif os.path.isfile(elt) or os.path.isfile(elt+".py"):
86       if elt[-3:] == ".py":
87         currentScript = os.path.abspath(elt)
88       else:
89         currentScript = None
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:[]})
95         callPython = False
96       elif currentScript:
97         if not os.access(currentScript, os.X_OK):
98           currentKey = "python "+currentScript
99           scriptArgs.append({currentKey:[]})
100         else:
101           currentKey = currentScript
102           scriptArgs.append({currentKey:[]})
103   # end for loop
104   return scriptArgs
105 #
106
107 # Formatting scripts and args as a Bash-like command-line:
108 # script1.py [args] ; script2.py [args] ; ...
109 def formatScriptsAndArgs(scriptArgs=[]):
110     commands = []
111     for sc_dict in scriptArgs:
112       for script, sc_args in sc_dict.items(): # single entry
113         cmd = script
114         if sc_args:
115           cmd = cmd + " " + " ".join(sc_args)
116         commands.append(cmd)
117
118     command = "; ".join(["%s"%x for x in commands])
119     return command
120 #
121
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")
134   if omniorbUserPath:
135     if not os.access(omniorbUserPath, os.W_OK):
136       raise Exception("Unable to get write access to directory: %s"%omniorbUserPath)
137     pass
138   else:
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")
144       pass
145     os.environ["OMNIORB_USER_PATH"] = defaultOmniorbUserPath
146 #