5 from parseConfigFile import parseConfigFile
6 from parseConfigFile import convertEnvFileToConfigFile
14 The SalomeRunner class in an API to configure SALOME environment then
15 start SALOME using a single python command.
20 Initialize environment from a list of configuration files
21 identified by their names.
22 These files should be in appropriate (new .cfg) format.
23 However you can give old .sh environment files; in this case,
24 the SalomeRunner class will try to automatically convert them
25 to .cfg format before setting the environment.
27 def __init__(self, configFileNames=[]):
28 for filename in configFileNames:
29 basename, extension = os.path.splitext(filename)
30 if extension == ".cfg":
31 self.__setEnvironmentFromConfigFile(filename)
32 elif extension == ".sh":
33 temp = tempfile.NamedTemporaryFile(suffix='.cfg')
35 convertEnvFileToConfigFile(filename, temp.name)
36 self.__setEnvironmentFromConfigFile(temp.name)
38 # Automatically cleans up the file
41 self._getLogger().warning("Unrecognized extension for configuration file: %s", filename)
45 # Run this module as a script, in order to use appropriate Python interpreter
46 # according to current path (initialized from environment files).
47 absoluteAppliPath = os.environ['ABSOLUTE_APPLI_PATH']
48 proc = subprocess.Popen(['python', absoluteAppliPath+'/bin/salome/salomeRunner.py', pickle.dumps(self), pickle.dumps(args)], shell=False, close_fds=True)
52 """Append value to PATH environment variable"""
53 def addToPath(self, value):
54 self.__addToReserved( 'PATH', value)
57 """Append value to LD_LIBRARY_PATH environment variable"""
58 def addToLdLibraryPath(self, value):
59 self.__addToReserved('LD_LIBRARY_PATH', value)
62 """Append value to PYTHONPATH environment variable"""
63 def addToPythonPath(self, value):
64 self.__addToReserved('PYTHONPATH', value)
67 """Set environment variable to value"""
68 def setEnviron(self, name, value, overwrite=False):
69 env = os.getenv(name, '')
70 if env and not overwrite:
71 self._getLogger().warning("Environment variable already existing and not overwritten: %s", name)
75 self._getLogger().info("Overwriting environment variable: %s", name)
77 value = os.path.expandvars(value) # expand environment variables
78 self._getLogger().debug("Set environment variable: %s=%s", name, value)
79 os.environ[name] = value
82 ###################################
83 # This begins the private section #
84 ###################################
86 def _usage(self, unused=[]):
87 exeName = os.path.splitext(os.path.basename(__file__))[0]
90 Usage: %s [command] [options] [--config=file1,...,filen]
93 start Launches SALOME virtual application [DEFAULT]
94 shell Executes a script under SALOME application environment
95 connect Connects a Python console to the active SALOME session
96 killall Kill all SALOME running sessions
97 info Display some information about SALOME
98 help Show this message
99 coffee Yes! SALOME can also make coffee!!"\
106 def __parseArguments(self, args):
107 if len(args) == 0 or args[0].startswith("-"):
113 availableCommands = {
114 'start' : '_runAppli',
115 'shell' : '_runSession',
116 'connect' : '_runConsole',
117 'killall': '_killAll',
120 'coffee' : '_makeCoffee'
123 if not command in availableCommands.keys():
124 self._getLogger().error("Unrecognized command: %s.", command)
128 return availableCommands[command], options
133 Args consist in a mandatory command followed by optionnal parameters.
134 See usage for details on commands.
136 def _getStarted(self, args):
137 command, options = self.__parseArguments(args)
141 if args and args[0] in ["-h","--help","help"]:
144 # try to default to "start" command
145 command = "_runAppli"
148 getattr(self, command)(options) # run appropriate method
149 except AttributeError:
150 self._getLogger().error("Method %s is not implemented.", command)
154 def __setEnvironmentFromConfigFile(self, filename):
155 configVars, reservedDict = parseConfigFile(filename, reserved=['PATH', 'LD_LIBRARY_PATH', 'PYTHONPATH'])
158 for reserved in reservedDict:
159 a = filter(None, reservedDict[reserved]) # remove empty elements
160 reformattedVals = ':'.join(a)
161 self.__addToReserved(reserved, reformattedVals)
164 for key,val in configVars:
165 self.setEnviron(key, val, overwrite=True)
168 sys.path[:0] = os.environ['PYTHONPATH'].split(':')
171 def __addToReserved(self, name, value):
175 value = os.path.expandvars(value) # expand environment variables
176 self._getLogger().debug("Add to %s: %s", name, value)
177 env = os.getenv(name, None)
179 os.environ[name] = value
181 os.environ[name] = value + os.pathsep + env
184 def _runAppli(self, args=[]):
185 # Initialize SALOME environment
186 sys.argv = ['runSalome'] + args
191 runSalome.runSalome()
194 def _runSession(self, args=[]):
195 sys.argv = ['runSession'] + args
197 runSession.configureSession(args)
204 # if exe does not contain any slashes (/), search in PATH
205 # if exe contains slashes:
206 # if exe begins with a slash, use this absolute path
207 # else build absolute path relative to current working directory
208 if (os.sep in exe) and (exe[0] is not os.sep):
209 args[0] = os.getcwd() + os.sep + exe
211 proc = subprocess.Popen(args, shell=False, close_fds=True)
214 absoluteAppliPath = os.environ['ABSOLUTE_APPLI_PATH']
215 cmd = ["/bin/bash", "--rcfile", absoluteAppliPath + "/.bashrc" ]
216 proc = subprocess.Popen(cmd, shell=False, close_fds=True)
220 def _runConsole(self, args=[]):
221 # Initialize SALOME environment
222 sys.argv = ['runConsole'] + args
230 def _killAll(self, args=[]):
231 #self._runAppli(['-k'] + args)
232 from killSalome import killAllPorts
236 def _showInfo(self, args=[]):
237 print "Running with python", platform.python_version()
238 self._runAppli(["--version"])
241 def _makeCoffee(self, args=[]):
244 print " ___...(-------)-....___"
245 print " .-\"\" ) ( \"\"-."
246 print " .-\'``\'|-._ ) _.-|"
247 print " / .--.| `\"\"---...........---\"\"` |"
255 print " _..---\"\"` \\ /`\"\"---.._"
256 print " .-\' \\ / \'-."
257 print " : `-.__ __.-\' :"
258 print " : ) \"\"---...---\"\" ( :"
259 print " \'._ `\"--...___...--\"` _.\'"
260 print " \\\"\"--..__ __..--\"\"/"
261 print " \'._ \"\"\"----.....______.....----\"\"\" _.\'"
262 print " `\"\"--..,,_____ _____,,..--\"\"`"
263 print " `\"\"\"----\"\"\"`"
267 # Add the following two methods since logger is not pickable
268 # Ref: http://stackoverflow.com/questions/2999638/how-to-stop-attributes-from-being-pickled-in-python
269 def __getstate__(self):
270 d = dict(self.__dict__)
274 def __setstate__(self, d):
275 self.__dict__.update(d) # I *think* this is a safe way to do it
277 # Excluding self._logger from pickle operation imply using the following method to access logger
278 def _getLogger(self):
279 if not hasattr(self, '_logger'):
280 self._logger = logging.getLogger(__name__)
281 #self._logger.setLevel(logging.DEBUG)
287 if __name__ == "__main__":
288 if len(sys.argv) == 3:
289 runner = pickle.loads(sys.argv[1])
290 args = pickle.loads(sys.argv[2])
291 runner._getStarted(args)
293 SalomeRunner()._usage()