Salome HOME
Increment version: 9.12.0
[modules/kernel.git] / bin / launchConfigureParser.py
index 1a5984cd73b7e2f200edece1cd0e00d19db603cd..5df821118b1ee84b7622dac613842526ef15046c 100644 (file)
@@ -1,5 +1,5 @@
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
 #
 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -84,6 +84,8 @@ useport_nam    = "useport"
 salomecfgname  = "salome"
 salomeappname  = "SalomeApp"
 script_nam     = "pyscript"
+verbosity_nam  = "verbosity"
+on_demand_nam  = "on_demand"
 
 # possible choices for the "embedded" and "standalone" parameters
 embedded_choices   = [ "registry", "study", "moduleCatalog", "cppContainer", "SalomeAppEngine" ]
@@ -91,7 +93,7 @@ standalone_choices = [ "registry", "study", "moduleCatalog", "cppContainer"]
 
 # values of boolean type (must be '0' or '1').
 # xml_parser.boolValue() is used for correct setting
-boolKeys = ( gui_nam, splash_nam, logger_nam, file_nam, xterm_nam, portkill_nam, killall_nam, except_nam, pinter_nam, shutdown_servers_nam, launcher_only_nam )
+boolKeys = ( gui_nam, splash_nam, logger_nam, file_nam, xterm_nam, portkill_nam, killall_nam, except_nam, pinter_nam, shutdown_servers_nam, launcher_only_nam, on_demand_nam )
 intKeys = ( interp_nam, )
 strKeys = ( launcher_nam )
 
@@ -119,7 +121,7 @@ def version():
             match = re.search( r':\s+([a-zA-Z0-9.]+)\s*$', v )
             if match :
                 return match.group( 1 )
-    except:
+    except Exception:
         pass
     return ''
 
@@ -325,7 +327,7 @@ class xml_parser:
             else:
                 if isinstance( strloc, bytes):
                     strloc = strloc.decode().strip()
-        except:
+        except Exception:
             pass
         return strloc
 
@@ -435,7 +437,7 @@ class xml_parser:
                     pass
                 pass
             pass
-        except:
+        except Exception:
             if verbose(): print("Configure parser: Error : can not read configuration file %s" % absfname)
         pass
 
@@ -583,6 +585,13 @@ Python file arguments, if any, must be comma-separated (without blank characters
                       dest="log_file",
                       help=help_str)
 
+    # Use gui-log-file for specific user actions in GUI. Default: nothing.
+    help_str = "Log specific user actions in GUI to <gui_log_file>"
+    pars.add_argument("--gui-log-file",
+                      metavar="<gui_log_file>",
+                      dest="gui_log_file",
+                      help=help_str)
+
     # Configuration XML file. Default: see defaultUserFile() function
     help_str = "Parse application settings from the <file> "
     help_str += "instead of default %s" % defaultUserFile()
@@ -817,6 +826,27 @@ Python file arguments, if any, must be comma-separated (without blank characters
                       dest="language",
                       help=help_str)
 
+    # Verbosity
+    help_str  = "Level of verbosity"
+    pars.add_argument("-V",
+                      "--verbose",
+                      metavar="<2/1/0>",
+                      dest="verbosity",
+                      default="0",
+                      help=help_str)
+
+    # On demand
+    help_str  = "Use installed salome on-demand extensions."
+    help_str += "0 to run without salome extensions [default], "
+    help_str += "1 to run only installed salome extensions. "
+    pars.add_argument("--on-demand",
+                      dest="on_demand",
+                      metavar="<0/1>",
+                      action=StoreBooleanAction,
+                      default=False,
+                      help=help_str)
+
+
     # Positional arguments (hdf file, python file)
     pars.add_argument("arguments", nargs=argparse.REMAINDER)
 
@@ -832,7 +862,7 @@ Python file arguments, if any, must be comma-separated (without blank characters
 args = {}
 #def get_env():
 #args = []
-def get_env(appname=salomeappname, cfgname=salomecfgname, exeName=None):
+def get_env(appname=salomeappname, cfgname=salomecfgname, exeName=None, keepEnvironment=True):
     ###
     # Collect launch configuration files:
     # - The environment variable "<appname>Config" (SalomeAppConfig) which can
@@ -857,21 +887,21 @@ def get_env(appname=salomeappname, cfgname=salomecfgname, exeName=None):
     global args
     config_var = appname+'Config'
 
+    ############################
+    # parse command line options
+    pars = CreateOptionParser(exeName=exeName)
+    cmd_opts = pars.parse_args(sys.argv[1:])
+    ############################
+
     # check KERNEL_ROOT_DIR
     kernel_root_dir = os.environ.get("KERNEL_ROOT_DIR", None)
-    if kernel_root_dir is None:
+    if kernel_root_dir is None and not cmd_opts.on_demand:
         print("""
         For each SALOME module, the environment variable <moduleN>_ROOT_DIR must be set.
         KERNEL_ROOT_DIR is mandatory.
         """)
         sys.exit(1)
 
-    ############################
-    # parse command line options
-    pars = CreateOptionParser(exeName=exeName)
-    cmd_opts = pars.parse_args(sys.argv[1:])
-    ############################
-
     # Process --print-port option
     if cmd_opts.print_port:
         from searchFreePort import searchFreePort
@@ -895,25 +925,24 @@ def get_env(appname=salomeappname, cfgname=salomecfgname, exeName=None):
         else:
             dirs += re.split('[;|:]', os.getenv(config_var))
 
-    gui_available = False
-    if os.getenv("GUI_ROOT_DIR"):
-        gui_resources_dir = os.path.join(os.getenv("GUI_ROOT_DIR"),'share','salome','resources','gui')
-        if os.path.isdir(gui_resources_dir):
-            gui_available = True
-            dirs.append(gui_resources_dir)
-        pass
-    if not gui_available:
-        kernel_resources_dir = os.path.join(os.getenv("KERNEL_ROOT_DIR"),'bin','salome','appliskel')
-        if os.getenv("KERNEL_ROOT_DIR") and os.path.isdir( kernel_resources_dir ):
-          dirs.append(kernel_resources_dir)
-        pass
-    os.environ[config_var] = os.pathsep.join(dirs)
+    if not keepEnvironment and not cmd_opts.on_demand:
+        if os.getenv("GUI_ROOT_DIR") and os.path.isdir(os.getenv("GUI_ROOT_DIR")):
+            gui_resources_dir = os.path.join(os.getenv("GUI_ROOT_DIR"),'share','salome','resources','gui')
+            if os.path.isdir(gui_resources_dir):
+                dirs.append(gui_resources_dir)
+            pass
+        else:
+            kernel_resources_dir = os.path.join(os.getenv("KERNEL_ROOT_DIR"),'bin','salome','appliskel')
+            if os.getenv("KERNEL_ROOT_DIR") and os.path.isdir( kernel_resources_dir ):
+              dirs.append(kernel_resources_dir)
+            pass
+        os.environ[config_var] = os.pathsep.join(dirs)
 
     dirs.reverse() # reverse order, like in "path" variable - FILO-style processing
 
     try:
         dirs.remove('') # to remove empty dirs if the variable terminate by ":" or if there are "::" inside
-    except:
+    except Exception:
         pass
 
     _opts = {} # associative array of options to be filled
@@ -927,7 +956,7 @@ def get_env(appname=salomeappname, cfgname=salomecfgname, exeName=None):
             try:
                 p = xml_parser(filename, _opts, [])
                 _opts = p.opts
-            except:
+            except Exception:
                 if verbose(): print("Configure parser: Error : can not read configuration file %s" % filename)
             pass
 
@@ -945,7 +974,7 @@ def get_env(appname=salomeappname, cfgname=salomecfgname, exeName=None):
         try:
             p = xml_parser(user_config, _opts, [])
             _opts = p.opts
-        except:
+        except Exception:
             if verbose(): print('Configure parser: Error : can not read user configuration file')
             user_config = ""
 
@@ -987,12 +1016,13 @@ def get_env(appname=salomeappname, cfgname=salomecfgname, exeName=None):
     args["session_gui"] = False
     args[batch_nam] = False
     args["study_hdf"] = None
+    args["gui_log_file"] = None
     if cmd_opts.gui is not None:
         args[gui_nam] = cmd_opts.gui
     if cmd_opts.batch is not None:
         args[batch_nam] = True
 
-    if not gui_available:
+    if ( not os.getenv("GUI_ROOT_DIR") or not os.path.isdir(os.getenv("GUI_ROOT_DIR")) ) and not cmd_opts.on_demand:
         args[gui_nam] = False
 
     if args[gui_nam]:
@@ -1014,6 +1044,13 @@ def get_env(appname=salomeappname, cfgname=salomecfgname, exeName=None):
         else:
             args[file_nam] = [cmd_opts.log_file]
 
+    # GUI Log file
+    if os.environ.get("GUI_LOG_FILE") is not None:
+        args["gui_log_file"] = os.environ["GUI_LOG_FILE"]
+
+    if cmd_opts.gui_log_file is not None:
+        args["gui_log_file"] = cmd_opts.gui_log_file
+
     # Naming Service port log file
     if cmd_opts.ns_port_log_file is not None:
         args["ns_port_log_file"] = cmd_opts.ns_port_log_file
@@ -1030,11 +1067,14 @@ def get_env(appname=salomeappname, cfgname=salomecfgname, exeName=None):
     if args[gui_nam] and args["session_gui"]:
         new_args = []
         for sa_obj in args[script_nam]:  # args[script_nam] is a list of ScriptAndArgs objects
-            script = re.sub(r'^python.*\s+', r'', sa_obj.script)
+            script = re.sub(r'^python. *\s+', r'', sa_obj.script)
             new_args.append(ScriptAndArgs(script=script, args=sa_obj.args, out=sa_obj.out))
         #
         args[script_nam] = new_args
 
+    args[verbosity_nam] = cmd_opts.verbosity
+    args[on_demand_nam] = cmd_opts.on_demand
+
     # xterm
     if cmd_opts.xterm is not None:
         args[xterm_nam] = cmd_opts.xterm
@@ -1133,23 +1173,24 @@ def get_env(appname=salomeappname, cfgname=salomecfgname, exeName=None):
 
     # now modify SalomeAppConfig environment variable
     # to take into account the SALOME modules
-    if os.sys.platform == 'win32':
-        dirs = re.split('[;]', os.environ[config_var] )
-    else:
-        dirs = re.split('[;|:]', os.environ[config_var] )
-    for module in args[modules_nam]:
-        if module not in ["KERNEL", "GUI", ""] and os.getenv("{0}_ROOT_DIR".format(module)):
-            d1 = os.path.join(os.getenv("{0}_ROOT_DIR".format(module)),"share","salome","resources",module.lower())
-            d2 = os.path.join(os.getenv("{0}_ROOT_DIR".format(module)),"share","salome","resources")
-            #if os.path.exists( "%s/%s.xml"%(d1, appname) ):
-            if os.path.exists( os.path.join(d1,"{0}.xml".format(salomeappname)) ):
-                dirs.append( d1 )
-            #elif os.path.exists( "%s/%s.xml"%(d2, appname) ):
-            elif os.path.exists( os.path.join(d2,"{0}.xml".format(salomeappname)) ):
-                dirs.append( d2 )
+    if not args[on_demand_nam]:
+        if os.sys.platform == 'win32':
+            dirs = re.split('[;]', os.environ[config_var] )
         else:
-            # print("* '"+m+"' should be deleted from ",args[modules_nam])
-            pass
+            dirs = re.split('[;|:]', os.environ[config_var] )
+        for module in args[modules_nam]:
+            if module not in ["KERNEL", "GUI", ""] and os.getenv("{0}_ROOT_DIR".format(module)):
+                d1 = os.path.join(os.getenv("{0}_ROOT_DIR".format(module)),"share","salome","resources",module.lower())
+                d2 = os.path.join(os.getenv("{0}_ROOT_DIR".format(module)),"share","salome","resources")
+                #if os.path.exists( "%s/%s.xml"%(d1, appname) ):
+                if os.path.exists( os.path.join(d1,"{0}.xml".format(salomeappname)) ):
+                    dirs.append( d1 )
+                #elif os.path.exists( "%s/%s.xml"%(d2, appname) ):
+                elif os.path.exists( os.path.join(d2,"{0}.xml".format(salomeappname)) ):
+                    dirs.append( d2 )
+            else:
+                # print("* '"+m+"' should be deleted from ",args[modules_nam])
+                pass
 
     # Test
     if cmd_opts.test_script_file is not None:
@@ -1184,6 +1225,8 @@ def get_env(appname=salomeappname, cfgname=salomecfgname, exeName=None):
         args[lang_nam] = cmd_opts.language
 
     # return arguments
-    os.environ[config_var] = os.pathsep.join(dirs)
+    if not keepEnvironment:
+        os.environ[config_var] = os.pathsep.join(dirs)
+
     # print("Args: ", args)
     return args