Salome HOME
mutex on StudyBuilder
[modules/kernel.git] / bin / launchConfigureParser.py
index 2176d4e7fab2dfdb9f18b91d1c3bb3f74a6c0f4e..06b9645d8b95798d861000f2b86540fd2f084741 100755 (executable)
@@ -1,5 +1,5 @@
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 #
 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -26,9 +26,7 @@ import xml.sax
 import optparse
 import types
 
-from salome_utils import verbose, setVerbose, getPortNumber, getHomeDir
-
-from salomeLauncherUtils import getScriptsAndArgs
+from salome_utils import verbose, getPortNumber, getHomeDir
 
 # names of tags in XML configuration file
 doc_tag = "document"
@@ -62,16 +60,13 @@ pinter_nam     = "pinter"
 batch_nam      = "batch"
 test_nam       = "test"
 play_nam       = "play"
+lang_nam       = "language"
 gdb_session_nam = "gdb_session"
 ddd_session_nam = "ddd_session"
 valgrind_session_nam = "valgrind_session"
 shutdown_servers_nam = "shutdown_servers"
 foreground_nam = "foreground"
 wake_up_session_nam = "wake_up_session"
-siman_nam = "siman"
-siman_study_nam = "siman_study"
-siman_scenario_nam = "siman_scenario"
-siman_user_nam = "siman_user"
 
 # values in XML configuration file giving specific module parameters (<module_name> section)
 # which are stored in opts with key <module_name>_<parameter> (eg SMESH_plugins)
@@ -105,11 +100,13 @@ def version():
     try:
         filename = None
         root_dir = os.environ.get( 'KERNEL_ROOT_DIR', '' ) # KERNEL_ROOT_DIR or "" if not found
-        if root_dir and os.path.exists( root_dir + "/bin/salome/VERSION" ):
-            filename = root_dir + "/bin/salome/VERSION"
+        version_file = os.path.join(root_dir, 'bin', 'salome', 'VERSION')
+        if root_dir and os.path.exists( version_file ):
+            filename = version_file
         root_dir = os.environ.get( 'GUI_ROOT_DIR', '' )    # GUI_ROOT_DIR "" if not found
-        if root_dir and os.path.exists( root_dir + "/bin/salome/VERSION" ):
-            filename = root_dir + "/bin/salome/VERSION"
+        version_file = os.path.join(root_dir, 'bin', 'salome', 'VERSION')
+        if root_dir and os.path.exists( version_file ):
+            filename = version_file
         if filename:
             str = open( filename, "r" ).readline() # str = "THIS IS SALOME - SALOMEGUI VERSION: 3.0.0"
             match = re.search( r':\s+([a-zA-Z0-9.]+)\s*$', str )
@@ -126,8 +123,19 @@ def version():
 def version_id(fname):
     major = minor = release = dev1 = dev2 = 0
     vers = fname.split(".")
-    if len(vers) > 0: major = int(vers[0])
-    if len(vers) > 1: minor = int(vers[1])
+    if len(vers) > 0:
+      try:
+        major = int(vers[0])
+      except ValueError:
+        # If salome version given is DEV, the call to int('DEV') will fail with
+        # a ValueError exception
+        pass
+    try:
+      if len(vers) > 1: minor = int(vers[1])
+    except ValueError:
+      # If salome version given is 7.DEV, the call to int('DEV') will fail with
+      # a ValueError exception
+      pass
     if len(vers) > 2:
         mr = re.search(r'^([0-9]+)([A-Z]|RC)?([0-9]*)',vers[2], re.I)
         if mr:
@@ -160,18 +168,13 @@ def version_id(fname):
 ###
 def defaultUserFile(appname=salomeappname, cfgname=salomecfgname):
     v = version()
-    if v: v = ".%s" % v
-    if sys.platform == "win32":
-      filename = os.path.join(getHomeDir(), "%s.xml%s" % (appname, v))
-    else:
-        if cfgname:
-            filename = os.path.join(getHomeDir(), ".config", cfgname, "%src%s" % (appname, v))
-            pass
-        else:
-            filename = os.path.join(getHomeDir(), ".%src%s" % (appname, v))
-            pass
-        pass
-    return filename
+    filetmpl = sys.platform == "win32" and "{0}.xml.{1}" or "{0}rc.{1}"
+    paths = []
+    paths.append(getHomeDir())
+    paths.append(".config")
+    if cfgname: paths.append(cfgname)
+    paths.append(filetmpl.format(appname, v))
+    return os.path.join(*paths)
 
 ###
 # Get user configuration file name
@@ -195,19 +198,20 @@ def userFile(appname, cfgname):
     if not id0: return None                      # bad version id -> can't detect appropriate file
 
     # ... get all existing user preferences files
-    if sys.platform == "win32":
-        files = glob.glob(os.path.join(getHomeDir(), "%s.xml.*" % appname))
-    else:
-        files = []
-        if cfgname:
-            # Since v6.6.0 - in ~/.config/salome directory, without dot prefix
-            files += glob.glob(os.path.join(getHomeDir(), ".config", cfgname, "%src.*" % appname))
-            # Since v6.5.0 - in ~/.config/salome directory, dot-prefixed (backward compatibility)
-            files += glob.glob(os.path.join(getHomeDir(), ".config", cfgname, ".%src.*" % appname))
-            pass
-        # old style (before v6.5.0) - in ~ directory, dot-prefixed
-        files += glob.glob(os.path.join(getHomeDir(), ".%src.*" % appname))
+    filetmpl1 = sys.platform == "win32" and "{0}.xml.*" or "{0}rc.*"
+    filetmpl2 = sys.platform == "win32" and filetmpl1 or "." + filetmpl1
+    files = []
+    if cfgname:
+        # Since v6.6.0 - in ~/.config/salome directory, without dot prefix
+        files += glob.glob(os.path.join(getHomeDir(), ".config", cfgname, filetmpl1.format(appname)))
+        # Since v6.5.0 - in ~/.config/salome directory, dot-prefixed (backward compatibility)
+        if filetmpl2 and filetmpl2 != filetmpl1:
+            files += glob.glob(os.path.join(getHomeDir(), ".config", cfgname, filetmpl2.format(appname)))
         pass
+    # old style (before v6.5.0) - in ~ directory, dot-prefixed
+    if filetmpl2 and filetmpl2 != filetmpl1:
+        files += glob.glob(os.path.join(getHomeDir(), filetmpl2.format(appname)))
+    pass
 
     # ... loop through all files and find most appopriate file (with closest id)
     appr_id   = -1
@@ -215,9 +219,9 @@ def userFile(appname, cfgname):
     for f in files:
         ff = os.path.basename( f )
         if sys.platform == "win32":
-            match = re.search( r'^%s\.xml\.([a-zA-Z0-9.]+)$'%appname, ff )
+            match = re.search( r'^{0}\.xml\.([a-zA-Z0-9.]+)$'.format(appname), ff )
         else:
-            match = re.search( r'^\.?%src\.([a-zA-Z0-9.]+)$'%appname, ff )
+            match = re.search( r'^\.?{0}rc\.([a-zA-Z0-9.]+)$'.format(appname), ff )
         if match:
             ver = version_id(match.group(1))
             if not ver: continue                 # bad version id -> skip file
@@ -306,6 +310,15 @@ class xml_parser:
         return strloc
         pass
 
+    def strValue( self, str ):
+        strloc = str
+        try:
+            if isinstance(strloc, types.UnicodeType): strloc = strloc.encode().strip()
+            else: strloc = strloc.strip()
+        except:
+            pass
+        return strloc
+
     def startElement(self, name, attrs):
         self.space.append(name)
         self.current = None
@@ -318,7 +331,7 @@ class xml_parser:
         # either "launch" or module name -- set section_name
         if self.space == [doc_tag, sec_tag] and nam_att in attrs.getNames():
             section_name = attrs.getValue( nam_att )
-            if section_name == lanch_nam:
+            if section_name in [lanch_nam, lang_nam]:
                 self.section = section_name # launch section
             elif self.opts.has_key( modules_nam ) and \
                  section_name in self.opts[ modules_nam ]:
@@ -340,19 +353,20 @@ class xml_parser:
                 key = nam
             else:                         # key for <module> section
                 key = self.section + "_" + nam
+            key = self.strValue( key )
             if nam in boolKeys:
                 self.opts[key] = self.boolValue( val )  # assign boolean value: 0 or 1
             elif nam in intKeys:
                 self.opts[key] = self.intValue( val )   # assign integer value
             elif nam in listKeys:
-                self.opts[key] = filter( lambda a: a.strip(), re.split( "[:;,]", val ) ) # assign list value: []
+                self.opts[key] = [ self.strValue( a ) for a in re.split( "[:;,]", val ) ] # assign list value: []
             else:
-                self.opts[key] = val
+                self.opts[key] = self.strValue( val ) # string value
             pass
         pass
 
     def endElement(self, name):
-        p = self.space.pop()
+        self.space.pop()
         self.current = None
         if self.section != section_to_skip and name == sec_tag:
             self.section = section_to_skip
@@ -483,7 +497,9 @@ def store_boolean (option, opt, value, parser, *args):
         for attribute in args:
             setattr(parser.values, attribute, value)
 
-def CreateOptionParser (theAdditionalOptions=[]):
+def CreateOptionParser (theAdditionalOptions=None, exeName=None):
+    if theAdditionalOptions is None:
+        theAdditionalOptions = []
     # GUI/Terminal. Default: GUI
     help_str = "Launch without GUI (in the terminal mode)."
     o_t = optparse.Option("-t",
@@ -728,7 +744,8 @@ def CreateOptionParser (theAdditionalOptions=[]):
     help_str += "0 to keep the standalone servers as daemon [default]. "
     help_str += "This option is only useful in batchmode "
     help_str += "(terminal mode or without showing desktop)."
-    o_shutdown = optparse.Option("--shutdown-servers",
+    o_shutdown = optparse.Option("-w",
+                                 "--shutdown-servers",
                                  metavar="<1/0>",
                                  #type="choice", choices=boolean_choices,
                                  type="string",
@@ -750,7 +767,7 @@ def CreateOptionParser (theAdditionalOptions=[]):
     # wake up session
     help_str  = "Wake up a previously closed session. "
     help_str += "The session object is found in the naming service pointed by the variable OMNIORB_CONFIG. "
-    help_str += "If this variable is not setted, the last configuration is taken. "
+    help_str += "If this variable is not set, the last configuration is taken. "
     o_wake_up = optparse.Option("--wake-up-session",
                                 action="store_true",
                                 dest="wake_up_session", default=False,
@@ -777,39 +794,13 @@ def CreateOptionParser (theAdditionalOptions=[]):
                              dest="use_port",
                                    help=help_str)
 
-    # SIMAN launch mode
-    help_str = "Special mode for interacting with SIMAN."
-    o_siman = optparse.Option("--siman",
-                              action="store_true",
-                              dest="siman",
-                              help=help_str)
-
-    # SIMAN study
-    help_str = "SIMAN study identifier."
-    o_siman_study = optparse.Option("--siman-study",
-                                    metavar="<id>",
-                                    type="string",
-                                    action="store",
-                                    dest="siman_study",
-                                    help=help_str)
-
-    # SIMAN scenario
-    help_str = "SIMAN scenario identifier."
-    o_siman_scenario = optparse.Option("--siman-scenario",
-                                       metavar="<id>",
-                                       type="string",
-                                       action="store",
-                                       dest="siman_scenario",
-                                       help=help_str)
-
-    # SIMAN user
-    help_str = "SIMAN user identifier."
-    o_siman_user = optparse.Option("--siman-user",
-                                   metavar="<id>",
-                                   type="string",
-                                   action="store",
-                                   dest="siman_user",
-                                   help=help_str)
+    help_str  = "Force application language. By default, a language specified in "
+    help_str += "the user's preferences is used."
+    o_lang = optparse.Option("-a",
+                             "--language",
+                             action="store",
+                             dest="language",
+                             help=help_str)
 
     # All options
     opt_list = [o_t,o_g, # GUI/Terminal
@@ -840,10 +831,7 @@ def CreateOptionParser (theAdditionalOptions=[]):
                 o_wake_up,
                 o_slm,   # Server launch mode
                 o_port,  # Use port
-                o_siman,         # Siman launch mode
-                o_siman_study,   # Siman study
-                o_siman_scenario,# Siman scenario
-                o_siman_user,    # Siman user
+                o_lang,  # Language
                 ]
 
     #std_options = ["gui", "desktop", "log_file", "resources",
@@ -853,9 +841,12 @@ def CreateOptionParser (theAdditionalOptions=[]):
 
     opt_list += theAdditionalOptions
 
-    a_usage = """%prog [options] [STUDY_FILE] [PYTHON_FILE [args] [PYTHON_FILE [args]...]]
+    if not exeName:
+      exeName = "%prog"
+
+    a_usage = """%s [options] [STUDY_FILE] [PYTHON_FILE [args] [PYTHON_FILE [args]...]]
 Python file arguments, if any, must be comma-separated (without blank characters) and prefixed by "args:" (without quotes), e.g. myscript.py args:arg1,arg2=val,...
-"""
+"""%exeName
     version_str = "Salome %s" % version()
     pars = optparse.OptionParser(usage=a_usage, version=version_str, option_list=opt_list)
 
@@ -871,7 +862,7 @@ Python file arguments, if any, must be comma-separated (without blank characters
 args = {}
 #def get_env():
 #args = []
-def get_env(theAdditionalOptions=[], appname=salomeappname, cfgname=salomecfgname):
+def get_env(theAdditionalOptions=None, appname=salomeappname, cfgname=salomecfgname, exeName=None):
     ###
     # Collect launch configuration files:
     # - The environment variable "<appname>Config" (SalomeAppConfig) which can
@@ -894,27 +885,24 @@ def get_env(theAdditionalOptions=[], appname=salomeappname, cfgname=salomecfgnam
     #   specified in configuration file(s)
     ###
 
+    if theAdditionalOptions is None:
+        theAdditionalOptions = []
+
     global args
     config_var = appname+'Config'
 
-    separator = ":"
-    if os.sys.platform == 'win32':
-        separator = ";"
-
     # check KERNEL_ROOT_DIR
-    try:
-        kernel_root_dir=os.environ["KERNEL_ROOT_DIR"]
-    except:
+    kernel_root_dir = os.environ.get("KERNEL_ROOT_DIR", None)
+    if kernel_root_dir is None:
         print """
         For each SALOME module, the environment variable <moduleN>_ROOT_DIR must be set.
         KERNEL_ROOT_DIR is mandatory.
         """
         sys.exit(1)
-        pass
 
     ############################
     # parse command line options
-    pars = CreateOptionParser(theAdditionalOptions)
+    pars = CreateOptionParser(theAdditionalOptions, exeName=exeName)
     (cmd_opts, cmd_args) = pars.parse_args(sys.argv[1:])
     ############################
 
@@ -937,20 +925,23 @@ def get_env(theAdditionalOptions=[], appname=salomeappname, cfgname=salomecfgnam
     dirs = []
     if os.getenv(config_var):
         if sys.platform == 'win32':
-            dirs += re.split(';', os.getenv(config_var))
+            dirs += re.split(os.pathsep, os.getenv(config_var))
         else:
             dirs += re.split('[;|:]', os.getenv(config_var))
 
-    gui_available = True
-    if os.getenv("GUI_ROOT_DIR") and os.path.isdir( os.getenv("GUI_ROOT_DIR") + "/share/salome/resources/gui" ):
-        dirs += [os.getenv("GUI_ROOT_DIR") + "/share/salome/resources/gui"]
+    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
-    else:
-        gui_available = False
-        if os.getenv("KERNEL_ROOT_DIR") and os.path.isdir( os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/appliskel" ):
-            dirs += [os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/appliskel"]
+    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] = separator.join(dirs)
+    os.environ[config_var] = os.pathsep.join(dirs)
 
     dirs.reverse() # reverse order, like in "path" variable - FILO-style processing
 
@@ -1000,15 +991,15 @@ def get_env(theAdditionalOptions=[], appname=salomeappname, cfgname=salomecfgnam
     # set default values for options which are NOT set in config files
     for aKey in listKeys:
         if not args.has_key( aKey ):
-            args[aKey]=[]
+            args[aKey] = []
 
     for aKey in boolKeys:
         if not args.has_key( aKey ):
-            args[aKey]=0
+            args[aKey] = 0
 
     if args[file_nam]:
         afile=args[file_nam]
-        args[file_nam]=[afile]
+        args[file_nam] = [afile]
 
     args[appname_nam] = appname
 
@@ -1067,13 +1058,13 @@ def get_env(theAdditionalOptions=[], appname=salomeappname, cfgname=salomecfgnam
             args["study_hdf"] = arg
 
     # Python scripts
+    from salomeContextUtils import getScriptsAndArgs, ScriptAndArgs
     args[script_nam] = getScriptsAndArgs(cmd_args)
-    new_args = []
     if args[gui_nam] and args["session_gui"]:
-        for d in args[script_nam]:
-            for s, a in d.items():
-                v = re.sub(r'^python.*\s+', r'', s)
-                new_args.append({v:a})
+        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)
+            new_args.append(ScriptAndArgs(script=script, args=sa_obj.args, out=sa_obj.out))
         #
         args[script_nam] = new_args
 
@@ -1159,20 +1150,10 @@ def get_env(theAdditionalOptions=[], appname=salomeappname, cfgname=salomecfgnam
     if cmd_opts.wake_up_session is not None:
         args[wake_up_session_nam] = cmd_opts.wake_up_session
 
-    # siman options
-    if cmd_opts.siman is not None:
-        args[siman_nam] = cmd_opts.siman
-    if cmd_opts.siman_study is not None:
-        args[siman_study_nam] = cmd_opts.siman_study
-    if cmd_opts.siman_scenario is not None:
-        args[siman_scenario_nam] = cmd_opts.siman_scenario
-    if cmd_opts.siman_user is not None:
-        args[siman_user_nam] = cmd_opts.siman_user
-
     ####################################################
     # Add <theAdditionalOptions> values to args
     for add_opt in theAdditionalOptions:
-        cmd = "args[\"%s\"] = cmd_opts.%s"%(add_opt.dest,add_opt.dest)
+        cmd = "args[\"{0}\"] = cmd_opts.{0}".format(add_opt.dest)
         exec(cmd)
     ####################################################
 
@@ -1187,15 +1168,15 @@ def get_env(theAdditionalOptions=[], appname=salomeappname, cfgname=salomecfgnam
         dirs = re.split('[;]', os.environ[config_var] )
     else:
         dirs = re.split('[;|:]', os.environ[config_var] )
-    for m in args[modules_nam]:
-        if m not in ["KERNEL", "GUI", ""] and os.getenv("%s_ROOT_DIR"%m):
-            d1 = os.getenv("%s_ROOT_DIR"%m) + "/share/salome/resources/" + m.lower()
-            d2 = os.getenv("%s_ROOT_DIR"%m) + "/share/salome/resources"
+    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( "%s/%s.xml"%(d1, salomeappname) ):
+            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( "%s/%s.xml"%(d2, salomeappname) ):
+            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]
@@ -1226,7 +1207,14 @@ def get_env(theAdditionalOptions=[], appname=salomeappname, cfgname=salomecfgnam
             sys.exit(1)
         args[useport_nam] = cmd_opts.use_port
 
+    if cmd_opts.language is not None:
+        langs = args["language_languages"] if "language_languages" in args else []
+        if cmd_opts.language not in langs:
+            print "Error: unsupported language: %s" % cmd_opts.language
+            sys.exit(1)
+        args[lang_nam] = cmd_opts.language
+
     # return arguments
-    os.environ[config_var] = separator.join(dirs)
+    os.environ[config_var] = os.pathsep.join(dirs)
     #print "Args: ", args
     return args