]> SALOME platform Git repositories - modules/yacs.git/blob - bin/salomeContext.py
Salome HOME
Some fixes on salome kill.
[modules/yacs.git] / bin / salomeContext.py
1 #! /usr/bin/env python3
2 # Copyright (C) 2013-2019  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import os
22 import sys
23 import logging
24 import configparser
25
26 from parseConfigFile import parseConfigFile
27
28 import tempfile
29 import pickle
30 import subprocess
31 import platform
32
33 from salomeContextUtils import SalomeContextException
34
35 def usage():
36   msg = '''\
37 Usage: salome [command] [options] [--config=<file,folder,...>]
38
39 Commands:
40 =========
41     start           Start a new SALOME instance.
42     context         Initialize SALOME context. Current environment is extended.
43     shell           Initialize SALOME context, attached to the last created SALOME
44                     instance if any, and executes scripts passed as command arguments.
45                     User works in a Shell terminal. SALOME environment is set but
46                     application is not started.
47     connect         Connect a Python console to the active SALOME instance.
48     remote          run command in SALOME environment from remote call, ssh or rsh.
49     kill <port(s)>  Terminate SALOME instances running on given ports for current user.
50                     Port numbers must be separated by blank characters.
51     killall         Terminate *all* SALOME running instances for current user.
52                     Do not start a new one.
53     test            Run SALOME tests.
54     info            Display some information about SALOME.
55     doc <module(s)> Show online module documentation (if available).
56                     Module names must be separated by blank characters.
57     help            Show this message.
58
59 If no command is given, default is start.
60
61 Command options:
62 ================
63     Use salome <command> --help to show help on command. Available for the
64     following commands: start, shell, connect, test, info.
65
66 --config=<file,folder,...>
67 ==========================
68     Initialize SALOME context from a list of context files and/or a list
69     of folders containing context files. The list is comma-separated, without
70     any blank characters.
71 '''
72
73   print(msg)
74 #
75
76 """
77 The SalomeContext class in an API to configure SALOME context then
78 start SALOME using a single python command.
79
80 """
81 class SalomeContext:
82   """
83   Initialize context from a list of configuration files
84   identified by their names.
85   These files should be in appropriate .cfg format.
86   """
87   def __init__(self, configFileNames=0):
88     self.getLogger().setLevel(logging.INFO)
89     #it could be None explicitly (if user use multiples setVariable...for standalone)
90     if configFileNames is None:
91        return
92     configFileNames = configFileNames or []
93     if len(configFileNames) == 0:
94       raise SalomeContextException("No configuration files given")
95
96     reserved=['PATH', 'DYLD_FALLBACK_LIBRARY_PATH', 'DYLD_LIBRARY_PATH', 'LD_LIBRARY_PATH', 'PYTHONPATH', 'MANPATH', 'PV_PLUGIN_PATH', 'INCLUDE', 'LIBPATH', 'SALOME_PLUGINS_PATH', 'LIBRARY_PATH', 'QT_PLUGIN_PATH']
97     for filename in configFileNames:
98       basename, extension = os.path.splitext(filename)
99       if extension == ".cfg":
100         self.__setContextFromConfigFile(filename, reserved)
101       else:
102         self.getLogger().error("Unrecognized extension for configuration file: %s", filename)
103   #
104
105   def __loadEnvModules(self, env_modules):
106     modulecmd = os.getenv('LMOD_CMD')
107     if not modulecmd:
108       raise SalomeContextException("Module environment not present")
109       return
110     try:
111       out, err = subprocess.Popen([modulecmd, "python", "load"] + env_modules, stdout=subprocess.PIPE).communicate()
112       exec(out)  # define specific environment variables
113     except:
114       raise SalomeContextException("Failed to load env modules: %s ..." % ' '.join(env_modules))
115       pass
116   #
117
118   def runSalome(self, args):
119     import os
120     # Run this module as a script, in order to use appropriate Python interpreter
121     # according to current path (initialized from context files).
122     env_modules_option = "--with-env-modules="
123     env_modules_l = [x for x in args if x.startswith(env_modules_option)]
124     if env_modules_l:
125       env_modules = env_modules_l[-1][len(env_modules_option):].split(',')
126       self.__loadEnvModules(env_modules)
127       args = [x for x in args if not x.startswith(env_modules_option)]
128     else:
129       env_modules = os.getenv("SALOME_ENV_MODULES", None)
130       if env_modules:
131         self.__loadEnvModules(env_modules.split(','))
132
133     absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')
134     env_copy = os.environ.copy()
135     selfBytes= pickle.dumps(self, protocol=0)
136     argsBytes= pickle.dumps(args, protocol=0)
137     proc = subprocess.Popen(['python3', os.path.join(absoluteAppliPath,"bin","salome","salomeContext.py"), selfBytes.decode(), argsBytes.decode()], shell=False, close_fds=True, env=env_copy)
138     out, err = proc.communicate()
139     return out, err, proc.returncode
140   #
141
142   """Append value to PATH environment variable"""
143   def addToPath(self, value):
144     self.addToVariable('PATH', value)
145   #
146
147   """Append value to LD_LIBRARY_PATH environment variable"""
148   def addToLdLibraryPath(self, value):
149     if platform.system() == 'Windows':
150       self.addToVariable('PATH', value)
151     elif platform.system() == 'Darwin':
152       if "LAPACK" in value:
153         self.addToVariable('DYLD_FALLBACK_LIBRARY_PATH', value)
154       else:
155         self.addToVariable('DYLD_LIBRARY_PATH', value)
156     else:
157       self.addToVariable('LD_LIBRARY_PATH', value)
158   #
159
160   """Append value to DYLD_LIBRARY_PATH environment variable"""
161   def addToDyldLibraryPath(self, value):
162     self.addToVariable('DYLD_LIBRARY_PATH', value)
163   #
164
165   """Append value to PYTHONPATH environment variable"""
166   def addToPythonPath(self, value):
167     self.addToVariable('PYTHONPATH', value)
168   #
169
170   """Set environment variable to value"""
171   def setVariable(self, name, value, overwrite=False):
172     env = os.getenv(name, '')
173     if env and not overwrite:
174       self.getLogger().error("Environment variable already existing (and not overwritten): %s=%s", name, value)
175       return
176
177     if env:
178       self.getLogger().debug("Overwriting environment variable: %s=%s", name, value)
179
180     value = os.path.expandvars(value) # expand environment variables
181     self.getLogger().debug("Set environment variable: %s=%s", name, value)
182     os.environ[name] = value
183   #
184
185   """Unset environment variable"""
186   def unsetVariable(self, name):
187     if os.environ.has_key(name):
188       del os.environ[name]
189   #
190
191   """Append value to environment variable"""
192   def addToVariable(self, name, value, separator=os.pathsep):
193     if value == '':
194       return
195
196     value = os.path.expandvars(value) # expand environment variables
197     self.getLogger().debug("Add to %s: %s", name, value)
198     env = os.getenv(name, None)
199     if env is None:
200       os.environ[name] = value
201     else:
202       os.environ[name] = value + separator + env
203   #
204
205   ###################################
206   # This begins the private section #
207   ###################################
208
209   def __parseArguments(self, args):
210     if len(args) == 0 or args[0].startswith("-"):
211       return None, args
212
213     command = args[0]
214     options = args[1:]
215
216     availableCommands = {
217       'start'   : '_runAppli',
218       'context' : '_setContext',
219       'shell'   : '_runSession',
220       'remote'  : '_runRemote',
221       'connect' : '_runConsole',
222       'kill'    : '_kill',
223       'killall' : '_killAll',
224       'test'    : '_runTests',
225       'info'    : '_showInfo',
226       'doc'     : '_showDoc',
227       'help'    : '_usage',
228       'coffee'  : '_makeCoffee',
229       'car'     : '_getCar',
230       }
231
232     if command not in availableCommands:
233       command = "start"
234       options = args
235
236     return availableCommands[command], options
237   #
238
239   """
240   Run SALOME!
241   Args consist in a mandatory command followed by optional parameters.
242   See usage for details on commands.
243   """
244   def _startSalome(self, args):
245     import os
246     import sys
247     try:
248       from setenv import add_path
249       absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH')
250       path = os.path.realpath(os.path.join(absoluteAppliPath, "bin", "salome"))
251       add_path(path, "PYTHONPATH")
252       path = os.path.realpath(os.path.join(absoluteAppliPath, "bin", "salome", "appliskel"))
253       add_path(path, "PYTHONPATH")
254
255     except:
256       pass
257
258     command, options = self.__parseArguments(args)
259     sys.argv = options
260
261     if command is None:
262       if args and args[0] in ["-h","--help","help"]:
263         usage()
264         return 0
265       # try to default to "start" command
266       command = "_runAppli"
267
268     try:
269       res = getattr(self, command)(options) # run appropriate method
270       return res or 0
271     except SystemExit as ex:
272       if ex.code != 0:
273         self.getLogger().error("SystemExit %s in method %s.", ex.code, command)
274       return ex.code
275     except SalomeContextException as e:
276       self.getLogger().error(e)
277       return 1
278     except Exception:
279       self.getLogger().error("Unexpected error:")
280       import traceback
281       traceback.print_exc()
282       return 1
283   #
284
285   def __setContextFromConfigFile(self, filename, reserved=None):
286     if reserved is None:
287       reserved = []
288     try:
289       unsetVars, configVars, reservedDict = parseConfigFile(filename, reserved)
290     except SalomeContextException as e:
291       msg = "%s"%e
292       self.getLogger().error(msg)
293       return 1
294
295     # unset variables
296     for var in unsetVars:
297       self.unsetVariable(var)
298
299     # set context
300     for reserved in reservedDict:
301       a = [_f for _f in reservedDict[reserved] if _f] # remove empty elements
302       a = [ os.path.realpath(x) for x in a ]
303       reformattedVals = os.pathsep.join(a)
304       if reserved in ["INCLUDE", "LIBPATH"]:
305         self.addToVariable(reserved, reformattedVals, separator=' ')
306       else:
307         self.addToVariable(reserved, reformattedVals)
308       pass
309
310     for key,val in configVars:
311       self.setVariable(key, val, overwrite=True)
312       pass
313
314     pythonpath = os.getenv('PYTHONPATH','').split(os.pathsep)
315     pythonpath = [ os.path.realpath(x) for x in pythonpath ]
316     sys.path[:0] = pythonpath
317   #
318
319   def _runAppli(self, args=None):
320     if args is None:
321       args = []
322     # Initialize SALOME environment
323     sys.argv = ['runSalome'] + args
324     import setenv
325     setenv.main(True, exeName="salome start")
326
327     import runSalome
328     runSalome.runSalome()
329     return 0
330   #
331
332   def _setContext(self, args=None):
333     salome_context_set = os.getenv("SALOME_CONTEXT_SET")
334     if salome_context_set:
335       print("***")
336       print("*** SALOME context has already been set.")
337       print("*** Enter 'exit' (only once!) to leave SALOME context.")
338       print("***")
339       return 0
340
341     os.environ["SALOME_CONTEXT_SET"] = "yes"
342     print("***")
343     print("*** SALOME context is now set.")
344     print("*** Enter 'exit' (only once!) to leave SALOME context.")
345     print("***")
346
347     cmd = ["/bin/bash"]
348     proc = subprocess.Popen(cmd, shell=False, close_fds=True)
349     proc.communicate()
350     return proc.returncode
351   #
352
353   def _runSession(self, args=None):
354     if args is None:
355       args = []
356     sys.argv = ['runSession'] + args
357     import runSession
358     params, args = runSession.configureSession(args, exe="salome shell")
359
360     sys.argv = ['runSession'] + args
361     import setenv
362     setenv.main(True)
363
364     return runSession.runSession(params, args)
365   #
366
367   def _runRemote(self, args=None):
368     if args is None:
369       args = []
370 #   complete salome environment 
371     sys.argv = ['runRemote']
372     import setenv
373     setenv.main(True)
374
375     import runRemote
376     return runRemote.runRemote(args)
377   #
378
379   def _runConsole(self, args=None):
380     if args is None:
381       args = []
382     # Initialize SALOME environment
383     sys.argv = ['runConsole']
384     import setenv
385     setenv.main(True)
386
387     import runConsole
388     return runConsole.connect(args)
389   #
390
391   def _kill(self, args=None):
392     if args is None:
393       args = []
394     ports = args
395     if not ports:
396       print("Port number(s) not provided to command: salome kill <port(s)>")
397       return 1
398
399     import subprocess
400     import setenv
401     setenv.main(True)
402     if os.getenv("NSHOST") == "no_host":
403       os.unsetenv("NSHOST")
404     for port in ports:
405       proc = subprocess.Popen(["killSalomeWithPort.py", port])
406       proc.communicate()
407
408     return 0
409   #
410
411   def _killAll(self, unused=None):
412     import setenv
413     setenv.main(True)
414     if os.getenv("NSHOST") == "no_host":
415       os.unsetenv("NSHOST")
416     try:
417       import PortManager # mandatory
418       import subprocess
419       ports = PortManager.getBusyPorts()['this']
420
421       if ports:
422         for port in ports:
423           proc = subprocess.Popen(["killSalomeWithPort.py", port])
424           proc.communicate()
425     except ImportError:
426       # :TODO: should be declared obsolete
427       from killSalome import killAllPorts
428       killAllPorts()
429       pass
430     return 0
431   #
432
433   def _runTests(self, args=None):
434     if args is None:
435       args = []
436     sys.argv = ['runTests']
437     import setenv
438     setenv.main(True)
439
440     import runTests
441     return runTests.runTests(args, exe="salome test")
442   #
443
444   def _showSoftwareVersions(self, softwares=None):
445     config = configparser.SafeConfigParser()
446     absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH')
447     filename = os.path.join(absoluteAppliPath, "sha1_collections.txt")
448     versions = {}
449     max_len = 0
450     with open(filename) as f:
451       for line in f:
452         try:
453           software, version, sha1 = line.split()
454           versions[software.upper()] = version
455           if len(software) > max_len:
456             max_len = len(software)
457         except:
458           pass
459         pass
460       pass
461     if softwares:
462       for soft in softwares:
463         if soft.upper() in versions:
464           print(soft.upper().rjust(max_len), versions[soft.upper()])
465     else:
466       import collections
467       od = collections.OrderedDict(sorted(versions.items()))
468       for name, version in od.items():
469         print(name.rjust(max_len), versions[name])
470     pass
471
472   def _showInfo(self, args=None):
473     if args is None:
474       args = []
475
476     usage = "Usage: salome info [options]"
477     epilog  = """\n
478 Display some information about SALOME.\n
479 Available options are:
480     -p,--ports                     Show the list of busy ports (running SALOME instances).
481     -s,--softwares [software(s)]   Show the list and versions of SALOME softwares.
482                                    Software names must be separated by blank characters.
483                                    If no software is given, show version of all softwares.
484     -v,--version                   Show running SALOME version.
485     -h,--help                      Show this message.
486 """
487     if not args:
488       args = ["--version"]
489
490     if "-h" in args or "--help" in args:
491       print(usage + epilog)
492       return 0
493
494     if "-p" in args or "--ports" in args:
495       import PortManager
496       ports = PortManager.getBusyPorts()
497       this_ports = ports['this']
498       other_ports = ports['other']
499       if this_ports or other_ports:
500           print("SALOME instances are running on the following ports:")
501           if this_ports:
502               print("   This application:", this_ports)
503           else:
504               print("   No SALOME instances of this application")
505           if other_ports:
506               print("   Other applications:", other_ports)
507           else:
508               print("   No SALOME instances of other applications")
509       else:
510           print("No SALOME instances are running")
511
512     if "-s" in args or "--softwares" in args:
513       if "-s" in args:
514         index = args.index("-s")
515       else:
516         index = args.index("--softwares")
517       indexEnd=index+1
518       while indexEnd < len(args) and args[indexEnd][0] != "-":
519         indexEnd = indexEnd + 1
520       self._showSoftwareVersions(softwares=args[index+1:indexEnd])
521
522     if "-v" in args or "--version" in args:
523       print("Running with python", platform.python_version())
524       return self._runAppli(["--version"])
525
526     return 0
527   #
528
529   def _showDoc(self, args=None):
530     if args is None:
531       args = []
532
533     modules = args
534     if not modules:
535       print("Module(s) not provided to command: salome doc <module(s)>")
536       return 1
537
538     appliPath = os.getenv("ABSOLUTE_APPLI_PATH")
539     if not appliPath:
540       raise SalomeContextException("Unable to find application path. Please check that the variable ABSOLUTE_APPLI_PATH is set.")
541     baseDir = os.path.join(appliPath, "share", "doc", "salome")
542     for module in modules:
543       docfile = os.path.join(baseDir, "gui", module.upper(), "index.html")
544       if not os.path.isfile(docfile):
545         docfile = os.path.join(baseDir, "tui", module.upper(), "index.html")
546       if not os.path.isfile(docfile):
547         docfile = os.path.join(baseDir, "dev", module.upper(), "index.html")
548       if os.path.isfile(docfile):
549         out, err = subprocess.Popen(["xdg-open", docfile]).communicate()
550       else:
551         print("Online documentation is not accessible for module:", module)
552
553   def _usage(self, unused=None):
554     usage()
555   #
556
557   def _makeCoffee(self, unused=None):
558     print("                        (")
559     print("                          )     (")
560     print("                   ___...(-------)-....___")
561     print("               .-\"\"       )    (          \"\"-.")
562     print("         .-\'``\'|-._             )         _.-|")
563     print("        /  .--.|   `\"\"---...........---\"\"`   |")
564     print("       /  /    |                             |")
565     print("       |  |    |                             |")
566     print("        \\  \\   |                             |")
567     print("         `\\ `\\ |                             |")
568     print("           `\\ `|            SALOME           |")
569     print("           _/ /\\            4 EVER           /")
570     print("          (__/  \\             <3            /")
571     print("       _..---\"\"` \\                         /`\"\"---.._")
572     print("    .-\'           \\                       /          \'-.")
573     print("   :               `-.__             __.-\'              :")
574     print("   :                  ) \"\"---...---\"\" (                 :")
575     print("    \'._               `\"--...___...--\"`              _.\'")
576     print("      \\\"\"--..__                              __..--\"\"/")
577     print("       \'._     \"\"\"----.....______.....----\"\"\"     _.\'")
578     print("          `\"\"--..,,_____            _____,,..--\"\"`")
579     print("                        `\"\"\"----\"\"\"`")
580     print("")
581     print("                    SALOME is working for you; what else?")
582     print("")
583   #
584
585   def _getCar(self, unused=None):
586     print("                                              _____________")
587     print("                                  ..---:::::::-----------. ::::;;.")
588     print("                               .\'\"\"\"\"\"\"                  ;;   \\  \":.")
589     print("                            .\'\'                          ;     \\   \"\\__.")
590     print("                          .\'                            ;;      ;   \\\\\";")
591     print("                        .\'                              ;   _____;   \\\\/")
592     print("                      .\'                               :; ;\"     \\ ___:\'.")
593     print("                    .\'--...........................    : =   ____:\"    \\ \\")
594     print("               ..-\"\"                               \"\"\"\'  o\"\"\"     ;     ; :")
595     print("          .--\"\"  .----- ..----...    _.-    --.  ..-\"     ;       ;     ; ;")
596     print("       .\"\"_-     \"--\"\"-----\'\"\"    _-\"        .-\"\"         ;        ;    .-.")
597     print("    .\'  .\'   SALOME             .\"         .\"              ;       ;   /. |")
598     print("   /-./\'         4 EVER <3    .\"          /           _..  ;       ;   ;;;|")
599     print("  :  ;-.______               /       _________==.    /_  \\ ;       ;   ;;;;")
600     print("  ;  / |      \"\"\"\"\"\"\"\"\"\"\".---.\"\"\"\"\"\"\"          :    /\" \". |;       ; _; ;;;")
601     print(" /\"-/  |                /   /                  /   /     ;|;      ;-\" | ;\';")
602     print(":-  :   \"\"\"----______  /   /              ____.   .  .\"\'. ;;   .-\"..T\"   .")
603     print("\'. \"  ___            \"\":   \'\"\"\"\"\"\"\"\"\"\"\"\"\"\"    .   ; ;    ;; ;.\" .\"   \'--\"")
604     print(" \",   __ \"\"\"  \"\"---... :- - - - - - - - - \' \'  ; ;  ;    ;;\"  .\"")
605     print("  /. ;  \"\"\"---___                             ;  ; ;     ;|.\"\"")
606     print(" :  \":           \"\"\"----.    .-------.       ;   ; ;     ;:")
607     print("  \\  \'--__               \\   \\        \\     /    | ;     ;;")
608     print("   \'-..   \"\"\"\"---___      :   .______..\\ __/..-\"\"|  ;   ; ;")
609     print("       \"\"--..       \"\"\"--\"        m l s         .   \". . ;")
610     print("             \"\"------...                  ..--\"\"      \" :")
611     print("                        \"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"    \\        /")
612     print("                                               \"------\"")
613     print("")
614     print("                                Drive your simulation properly with SALOME!")
615     print("")
616   #
617
618   # Add the following two methods since logger is not pickable
619   # Ref: http://stackoverflow.com/questions/2999638/how-to-stop-attributes-from-being-pickled-in-python
620   def __getstate__(self):
621     d = dict(self.__dict__)
622     if hasattr(self, '_logger'):
623       del d['_logger']
624     return d
625   #
626   def __setstate__(self, d):
627     self.__dict__.update(d) # I *think* this is a safe way to do it
628   #
629   # Excluding self._logger from pickle operation imply using the following method to access logger
630   def getLogger(self):
631     if not hasattr(self, '_logger'):
632       self._logger = logging.getLogger(__name__)
633       #self._logger.setLevel(logging.DEBUG)
634       #self._logger.setLevel(logging.WARNING)
635       self._logger.setLevel(logging.ERROR)
636     return self._logger
637   #
638
639 if __name__ == "__main__":
640   if len(sys.argv) == 3:
641     context = pickle.loads(sys.argv[1].encode())
642     args = pickle.loads(sys.argv[2].encode())
643
644     status = context._startSalome(args)
645     sys.exit(status)
646   else:
647     usage()
648 #