]> SALOME platform Git repositories - modules/kernel.git/blob - bin/salomeLauncherUtils.py
Salome HOME
CMake: doing proper string comparison in IF clause
[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):
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   for currentList in allLists:
50     elements = currentList.split(',')
51     for elt in elements:
52       elt = os.path.realpath(os.path.expanduser(elt))
53       if os.path.isdir(elt):
54         configFileNames += __listDirectory(elt)
55       else:
56         configFileNames += [elt]
57
58   return configFileNames, args
59 #
60
61 # Return an array of dictionaries {script_name: [list_of_its_args]}
62 def getScriptsAndArgs(args=[]):
63   # Syntax of args: script.py [args:a1,a2=val,an] ... script.py [args:a1,a2=val,an]
64   scriptArgs = []
65   currentKey = None
66   argsPrefix = "args:"
67   callPython = False
68
69   for i in range(len(args)):
70     elt = args[i]
71
72     if elt.startswith(argsPrefix):
73       if not currentKey or callPython:
74         raise SalomeRunnerException("args list must follow corresponding script file in command line.")
75       elt = elt.replace(argsPrefix, '')
76       scriptArgs[len(scriptArgs)-1][currentKey] = elt.split(",")
77       currentKey = None
78       callPython = False
79     elif elt.startswith("python"):
80       callPython = True
81     elif os.path.isfile(elt) or os.path.isfile(elt+".py"):
82       if elt[-3:] == ".py":
83         currentScript = os.path.abspath(elt)
84       else:
85         currentScript = None
86         if elt[-4:] != ".hdf":
87           currentScript = os.path.abspath(elt+".py")
88       if currentScript and callPython:
89         currentKey = "python "+currentScript
90         scriptArgs.append({currentKey:[]})
91         callPython = False
92       elif currentScript:
93         if not os.access(currentScript, os.X_OK):
94           currentKey = "python "+currentScript
95           scriptArgs.append({currentKey:[]})
96         else:
97           currentKey = currentScript
98           scriptArgs.append({currentKey:[]})
99   # end for loop
100   return scriptArgs
101 #
102
103 # Formatting scripts and args as a Bash-like command-line:
104 # script1.py [args] ; script2.py [args] ; ...
105 def formatScriptsAndArgs(scriptArgs=[]):
106     commands = []
107     for sc_dict in scriptArgs:
108       for script, sc_args in sc_dict.items(): # single entry
109         cmd = script
110         if sc_args:
111           cmd = cmd + " " + " ".join(sc_args)
112         commands.append(cmd)
113
114     command = "; ".join(["%s"%x for x in commands])
115     return command
116 #