Salome HOME
sat #18867 : pour les url des bases git : substitution des references par leur valeur...
[tools/sat.git] / commands / application.py
index 47e562e004562757e670536e519aa844de809882..f219c0b2d8a5c7b0b4a705c2cf357bae98d7d80b 100644 (file)
@@ -28,7 +28,7 @@ import src
 
 parser = src.options.Options()
 parser.add_option('n', 'name', 'string', 'name',
-    _('Optional: The name of the application (default is APPLI.name or '
+    _('Optional: The name of the application (default is APPLICATION.virtual_app.name or '
       'runAppli)'))
 parser.add_option('c', 'catalog', 'string', 'catalog',
     _('Optional: The resources catalog to use'))
@@ -42,6 +42,9 @@ parser.add_option('', 'gencat', 'string', 'gencat',
 parser.add_option('m', 'module', 'list2', 'modules',
     _("Optional: the restricted list of module(s) to include in the "
       "application"))
+parser.add_option('', 'use_mesa', 'boolean', 'use_mesa',
+    _("Optional: Create a launcher that will use mesa products\n\t"
+      "It can be usefull whan salome is used on a remote machine through ssh"))
 
 ##
 # Creates an alias for runAppli.
@@ -69,7 +72,7 @@ def add_module_to_appli(out, module, has_gui, module_path, logger, flagline):
 
 ##
 # Creates the config file to create an application with the list of modules.
-def create_config_file(config, modules, env_file, logger):
+def create_config_file(config, modules, env_files, logger):
 
     samples = ""
     if 'SAMPLES' in config.APPLICATION.products:
@@ -79,22 +82,34 @@ def create_config_file(config, modules, env_file, logger):
     f = open(config_file, "w")
 
     f.write('<application>\n')
-    f.write('<prerequisites path="%s"/>\n' % env_file)
+    for env_file in env_files:
+        if env_file.endswith("cfg"):
+            f.write('<context path="%s"/>\n' % env_file)
+        else:   
+            f.write('<prerequisites path="%s"/>\n' % env_file)
+
     f.write('<resources path="CatalogResources.xml"/>\n')
     f.write('<modules>\n')
 
     flagline = False
     for m in modules:
         mm = src.product.get_product_config(config, m)
+        # do not include in virtual application application module!
+        if src.get_property_in_product_cfg(mm, "is_salome_application") == "yes":
+            continue
+        # do not include products that do not compile
+        if not src.product.product_compiles(mm):
+            continue
+        #obsolete?
         if src.product.product_is_smesh_plugin(mm):
             continue
 
-        if 'install_dir' in mm and bool(mm.install_dir) :
+        if 'install_dir' in mm and bool(mm.install_dir):
             if src.product.product_is_cpp(mm):
                 # cpp module
                 for aa in src.product.get_product_components(mm):
-                    install_dir = os.path.join(config.APPLICATION.workdir,
-                                               "INSTALL")
+                    install_dir=os.path.join(config.APPLICATION.workdir,
+                                             config.INTERNAL.config.install_dir)
                     mp = os.path.join(install_dir, aa)
                     flagline = add_module_to_appli(f,
                                                    aa,
@@ -118,8 +133,8 @@ def create_config_file(config, modules, env_file, logger):
 ##
 # Customizes the application by editing SalomeApp.xml.
 def customize_app(config, appli_dir, logger):
-    if 'configure' not in config.APPLI \
-        or len(config.APPLI.configure) == 0:
+    if 'configure' not in config.APPLICATION.virtual_app \
+        or len(config.APPLICATION.virtual_app.configure) == 0:
         return
 
     # shortcut to get an element (section or parameter) from parent.
@@ -152,9 +167,9 @@ def customize_app(config, appli_dir, logger):
     assert document is not None, "document tag not found"
 
     logger.write("\n", 4)
-    for section_name in config.APPLI.configure:
-        for parameter_name in config.APPLI.configure[section_name]:
-            parameter_value = config.APPLI.configure[section_name][parameter_name]
+    for section_name in config.APPLICATION.virtual_app.configure:
+        for parameter_name in config.APPLICATION.virtual_app.configure[section_name]:
+            parameter_value = config.APPLICATION.virtual_app.configure[section_name][parameter_name]
             logger.write("  configure: %s/%s = %s\n" % (section_name,
                                                         parameter_name,
                                                         parameter_value), 4)
@@ -231,7 +246,7 @@ def create_application(config, appli_dir, catalog, logger, display=True):
                                    SALOME_modules)
     
     if retcode == 0:
-        cmd = src.printcolors.printcLabel("%s/runAppli" % appli_dir)
+        cmd = src.printcolors.printcLabel("%s/salome" % appli_dir)
 
     if display:
         logger.write("\n", 3, False)
@@ -244,11 +259,9 @@ def get_SALOME_modules(config):
     l_modules = []
     for product in config.APPLICATION.products:
         product_info = src.product.get_product_config(config, product)
-        if src.product.product_is_SALOME(product_info):
+        if (src.product.product_is_salome(product_info) or 
+               src.product.product_is_generated(product_info)):
             l_modules.append(product)
-        if src.product.product_is_generated(product_info):
-            if "component_name" in product_info:
-                l_modules.append(product_info.component_name)
     return l_modules
 
 ##
@@ -262,6 +275,18 @@ def generate_launch_file(config, appli_dir, catalog, logger, l_SALOME_modules):
     
     write_step(logger, _("Creating environment files"))
     status = src.KO_STATUS
+
+    # build the application (the name depends upon salome version
+    env_file = os.path.join(config.APPLICATION.workdir, "env_launch")
+    VersionSalome = src.get_salome_version(config)
+    if VersionSalome>=820:
+        # for salome 8+ we use a salome context file for the virtual app
+        app_shell=["cfg", "bash"]
+        env_files=[env_file+".cfg", env_file+".sh"]
+    else:
+        app_shell=["bash"]
+        env_files=[env_file+".sh"]
+
     try:
         import environ
         # generate only shells the user wants (by default bash, csh, batch)
@@ -269,15 +294,15 @@ def generate_launch_file(config, appli_dir, catalog, logger, l_SALOME_modules):
         # with the current system.
         environ.write_all_source_files(config,
                                        logger,
+                                       shells=app_shell,
                                        silent=True)
         status = src.OK_STATUS
     finally:
         logger.write(src.printcolors.printc(status) + "\n", 2, False)
 
-    # build the application
-    env_file = os.path.join(config.APPLICATION.workdir, "env_launch.sh")
+
     write_step(logger, _("Building application"), level=2)
-    cf = create_config_file(config, l_SALOME_modules, env_file, logger)
+    cf = create_config_file(config, l_SALOME_modules, env_files, logger)
 
     # create the application directory
     os.makedirs(appli_dir)
@@ -298,44 +323,6 @@ def generate_launch_file(config, appli_dir, catalog, logger, l_SALOME_modules):
     return retcode
 
 
-##
-# Generates a launcher that sources Salome's python and calls original launcher
-def generate_sourcing_launcher(config, appli_dir, logger) :
-
-    # Rename original launcher
-    launcher_name = os.path.join( appli_dir,
-                                  "bin",
-                                  "salome",
-                                  config.APPLI.launch_alias_name )
-    original_launcher = launcher_name + "-original"
-    os.rename( launcher_name, original_launcher )
-    
-    # Open new launcher
-    f = open(launcher_name, "w")
-    
-    # Write the set up of the environment
-    env = src.environment.SalomeEnviron( config,
-                                         src.fileEnviron.get_file_environ(
-                                                                        f,
-                                                                        "bash",
-                                                                        {},
-                                                                        config))
-    env.set_a_product( "Python", logger)
-    
-    # Write the call to the original launcher
-    f.write( "\n\n")
-    f.write( "# This is the call to the original launcher\n")
-    f.write( original_launcher + " $*" )
-    f.write( "\n\n")
-    
-    # Write the cleaning of the environment
-    env.finish(True)
-    
-    # Close new launcher
-    f.close()
-    os.chmod(launcher_name, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
-
-   
 
 ##
 # Generates the catalog from a list of machines.
@@ -352,47 +339,51 @@ def generate_catalog(machines, config, logger):
     user = getpass.getuser()
 
     catfile = src.get_tmp_filename(config, "CatalogResources.xml")
-    catalog = file(catfile, "w")
-    catalog.write("<!DOCTYPE ResourcesCatalog>\n<resources>\n")
-    for k in machines:
-        logger.write("    ssh %s " % (k + " ").ljust(20, '.'), 4)
-        logger.flush()
-
-        ssh_cmd = 'ssh -o "StrictHostKeyChecking no" %s %s' % (k, cmd)
-        p = subprocess.Popen(ssh_cmd, shell=True,
-                stdin=subprocess.PIPE,
-                stdout=subprocess.PIPE,
-                stderr=subprocess.PIPE)
-        p.wait()
-
-        if p.returncode != 0:
-            logger.write(src.printcolors.printc(src.KO_STATUS) + "\n", 4)
-            logger.write("    " + src.printcolors.printcWarning(p.stderr.read()),
-                         2)
-        else:
-            logger.write(src.printcolors.printc(src.OK_STATUS) + "\n", 4)
-            lines = p.stdout.readlines()
-            freq = lines[0][:-1].split(':')[-1].split('.')[0].strip()
-            nb_proc = len(lines) -1
-            memory = lines[-1].split(':')[-1].split()[0].strip()
-            memory = int(memory) / 1000
-
-            catalog.write("    <machine\n")
-            catalog.write("        protocol=\"ssh\"\n")
-            catalog.write("        nbOfNodes=\"1\"\n")
-            catalog.write("        mode=\"interactif\"\n")
-            catalog.write("        OS=\"LINUX\"\n")
-            catalog.write("        CPUFreqMHz=\"%s\"\n" % freq)
-            catalog.write("        nbOfProcPerNode=\"%s\"\n" % nb_proc)
-            catalog.write("        memInMB=\"%s\"\n" % memory)
-            catalog.write("        userName=\"%s\"\n" % user)
-            catalog.write("        name=\"%s\"\n" % k)
-            catalog.write("        hostname=\"%s\"\n" % k)
-            catalog.write("    >\n")
-            catalog.write("    </machine>\n")
-
-    catalog.write("</resources>\n")
-    catalog.close()
+    with open(catfile, 'w') as catalog:
+        catalog.write("<!DOCTYPE ResourcesCatalog>\n<resources>\n")
+        for k in machines:
+            if not src.architecture.is_windows(): 
+                logger.write("    ssh %s " % (k + " ").ljust(20, '.'), 4)
+                logger.flush()
+
+                ssh_cmd = 'ssh -o "StrictHostKeyChecking no" %s %s' % (k, cmd)
+                p = subprocess.Popen(ssh_cmd, shell=True,
+                        stdin=subprocess.PIPE,
+                        stdout=subprocess.PIPE,
+                        stderr=subprocess.PIPE)
+                p.wait()
+
+                machine_access = (p.returncode == 0) 
+                if not machine_access:
+                    logger.write(src.printcolors.printc(src.KO_STATUS) + "\n", 4)
+                    logger.write("    " + src.printcolors.printcWarning(p.stderr.read()),
+                                 2)
+                else:
+                    logger.write(src.printcolors.printc(src.OK_STATUS) + "\n", 4)
+                    lines = p.stdout.readlines()
+                    freq = lines[0][:-1].split(':')[-1].split('.')[0].strip()
+                    nb_proc = len(lines) -1
+                    memory = lines[-1].split(':')[-1].split()[0].strip()
+                    memory = int(memory) / 1000
+
+                catalog.write("    <machine\n")
+                catalog.write("        protocol=\"ssh\"\n")
+                catalog.write("        nbOfNodes=\"1\"\n")
+                catalog.write("        mode=\"interactif\"\n")
+                catalog.write("        OS=\"LINUX\"\n")
+
+                if (not src.architecture.is_windows()) and machine_access:
+                    catalog.write("        CPUFreqMHz=\"%s\"\n" % freq)
+                    catalog.write("        nbOfProcPerNode=\"%s\"\n" % nb_proc)
+                    catalog.write("        memInMB=\"%s\"\n" % memory)
+
+                catalog.write("        userName=\"%s\"\n" % user)
+                catalog.write("        name=\"%s\"\n" % k)
+                catalog.write("        hostname=\"%s\"\n" % k)
+                catalog.write("    >\n")
+                catalog.write("    </machine>\n")
+
+        catalog.write("</resources>\n")
     return catfile
 
 ##################################################
@@ -425,9 +416,9 @@ def run(args, runner, logger):
     application = src.printcolors.printcLabel(runner.cfg.VARS.application)
     logger.write(_("Building application for %s\n") % application, 1)
 
-    # if section APPLI does not exists create one
-    if "APPLI" not in runner.cfg:
-        msg = _("The section APPLI is not defined in the product.")
+    # if section APPLICATION.virtual_app does not exists create one
+    if "virtual_app" not in runner.cfg.APPLICATION:
+        msg = _("The section APPLICATION.virtual_app is not defined in the product. Use sat launcher in state")
         logger.write(src.printcolors.printcError(msg), 1)
         logger.write("\n", 1)
         return 1
@@ -439,16 +430,20 @@ def run(args, runner, logger):
 
     # set list of modules
     if options.modules:
-        runner.cfg.APPLI['modules'] = options.modules
+        runner.cfg.APPLICATION.virtual_app['modules'] = options.modules
+
+    # activate mesa use in the generated application
+    if options.use_mesa:
+        src.activate_mesa_property(runner.cfg)
 
     # set name and application_name
     if options.name:
-        runner.cfg.APPLI['name'] = options.name
-        runner.cfg.APPLI['application_name'] = options.name + "_appdir"
+        runner.cfg.APPLICATION.virtual_app['name'] = options.name
+        runner.cfg.APPLICATION.virtual_app['application_name'] = options.name + "_appdir"
     
-    application_name = src.get_cfg_param(runner.cfg.APPLI,
+    application_name = src.get_cfg_param(runner.cfg.APPLICATION.virtual_app,
                                          "application_name",
-                                         runner.cfg.APPLI.name + "_appdir")
+                                         runner.cfg.APPLICATION.virtual_app.name + "_appdir")
     appli_dir = os.path.join(target_dir, application_name)
 
     src.printcolors.print_value(logger,
@@ -466,18 +461,18 @@ def run(args, runner, logger):
         catalog_src = options.gencat
         catalog = generate_catalog(options.gencat.split(","),
                                    runner.cfg,logger)
-    elif 'catalog' in runner.cfg.APPLI:
+    elif 'catalog' in runner.cfg.APPLICATION.virtual_app:
         # use catalog specified in the product
-        if runner.cfg.APPLI.catalog.endswith(".xml"):
+        if runner.cfg.APPLICATION.virtual_app.catalog.endswith(".xml"):
             # catalog as a file
-            catalog = runner.cfg.APPLI.catalog
+            catalog = runner.cfg.APPLICATION.virtual_app.catalog
         else:
             # catalog as a list of computers
-            catalog_src = runner.cfg.APPLI.catalog
+            catalog_src = runner.cfg.APPLICATION.virtual_app.catalog
             mlist = filter(lambda l: len(l.strip()) > 0,
-                           runner.cfg.APPLI.catalog.split(","))
+                           runner.cfg.APPLICATION.virtual_app.catalog.split(","))
             if len(mlist) > 0:
-                catalog = generate_catalog(runner.cfg.APPLI.catalog.split(","),
+                catalog = generate_catalog(runner.cfg.APPLICATION.virtual_app.catalog.split(","),
                                            runner.cfg, logger)
 
     # display which catalog is used