]> SALOME platform Git repositories - tools/sat.git/commitdiff
Salome HOME
developpement d'une option check_system pour git config
authorcrouzet <nicolas.crouzet@cea.fr>
Mon, 3 Feb 2020 15:03:50 +0000 (16:03 +0100)
committercrouzet <nicolas.crouzet@cea.fr>
Mon, 3 Feb 2020 15:03:50 +0000 (16:03 +0100)
commands/config.py
src/product.py
src/system.py

index 6be16dba9fb0617ab1c41d3c8a9bc5efb3f9181e..4b2be310e53136ff2e6a094cb634c6d662b9d162 100644 (file)
@@ -57,6 +57,8 @@ parser.add_option('', 'show_install', 'boolean', 'show_install',
     _("Optional: synthetic list of all install directories in the application"))
 parser.add_option('', 'show_properties', 'boolean', 'show_properties',
     _("Optional: synthetic list of all properties used in the application"))
+parser.add_option('', 'check_system', 'boolean', 'check_system',
+    _("Optional: check if system products are installed"))
 parser.add_option('c', 'copy', 'boolean', 'copy',
     _("""Optional: copy a config file to the personal config files directory.
 WARNING: the included files are not copied.
@@ -784,6 +786,35 @@ def show_patchs(config, logger):
   else:
     logger.write("No patchs found\n", 1)
 
+def check_install_system(config, logger):
+  '''Check the installation of all (declared) system products
+
+  :param config Config: the global configuration.
+  :param logger Logger: The logger instance to use for the display
+  '''
+  # get the command to use for checking the system dependencies
+  # (either rmp or apt)
+  check_cmd=src.system.get_pkg_check_cmd()
+  logger.write("\nCheck the system dependencies declared in the application\n",1)
+  pkgmgr=check_cmd[0]
+  for product in sorted(config.APPLICATION.products):
+    try:
+      product_info = src.product.get_product_config(config, product)
+      if src.product.product_is_native(product_info):
+        # if the product is native, get (in two dictionnaries the runtime and compile time 
+        # system dependencies with the status (OK/KO)
+        run_pkg,build_pkg=src.product.check_system_dep(check_cmd, product_info)
+        #logger.write("\n*** %s ***\n" % product, 1)
+        for pkg in run_pkg:
+            logger.write(run_pkg[pkg], 1)
+        for pkg in build_pkg:
+            logger.write(build_pkg[pkg], 1)
+        #  logger.write(src.printcolors.printcInfo("    %s\n" % i), 1)
+
+    except Exception as e:
+      msg = "problem with the check of system prerequisite %s\n%s\n" % (product, str(e))
+      logger.error(msg)
+
 
 def show_install_dir(config, logger):
   '''Prints all the used installed directories in the application.
@@ -1145,3 +1176,7 @@ def run(args, runner, logger):
         logger.write("\n", 2, False)
         show_properties(runner.cfg, logger)
 
+    # check system prerequisites
+    if options.check_system:
+       check_install_system(runner.cfg, logger)
+       pass 
index fe194f77950a1f781b2a002ffff80de6623937d0..2d829dc29f046e9c0e3c1c859d3a7ca7d1147710 100644 (file)
@@ -1172,6 +1172,36 @@ def product_test_property(product_info, property_name, property_value):
     result = eval(eval_expression)
     return result
 
+def check_system_dep(check_cmd, product_info):
+    """Search for system dependencies, check if installed
+    :param check_cmd Config: The command to use for checking (rpm/apt)
+    :param product_info Config: The configuration specific to the product
+    :rtype: two dictionnaries for runtime and compile time dependencies with text status
+    """
+    runtime_dep={}
+    build_dep={}
+    if "system_info" in product_info:
+        if check_cmd[0]=="rpm":
+            if "rpm" in product_info.system_info:
+                for pkg in product_info.system_info.rpm:
+                    runtime_dep[pkg]=src.system.check_system_pkg(check_cmd,pkg)
+            if "rpm_dev" in product_info.system_info:
+                for pkg in product_info.system_info.rpm_dev:
+                    build_dep[pkg]=src.system.check_system_pkg(check_cmd,pkg)
+        if check_cmd[0]=="apt":
+            if "apt" in product_info.system_info:
+                for pkg in product_info.system_info.apt:
+                    runtime_dep[pkg]=src.system.check_system_pkg(check_cmd,pkg)
+            if "apt_dev" in product_info.system_info:
+                for pkg in product_info.system_info.apt_dev:
+                    build_dep[pkg]=src.system.check_system_pkg(check_cmd,pkg)
+    #for pkg in runtime_dep:
+    #    print "CNC check for ", pkg
+    #    runtime_dep[pkg]=src.system.check_system_pkg(check_cmd,pkg)
+    #for pkg in build_dep:
+    #    build_dep[pkg]=src.system.check_system_pkg(check_cmd,pkg)
+    #print "CNC runtime_dep=", runtime_dep
+    return runtime_dep,build_dep
 
 
 def get_product_components(product_info):
index 73f9c18385b495dabdfc59f5fa1ee03bf5f515b6..1c11d5ce4f996649c9f47cde33e94f4efd18d9e2 100644 (file)
@@ -322,3 +322,58 @@ def svn_extract(user,
                           stdout=logger.logTxtFile,
                           stderr=subprocess.STDOUT)
     return (res == 0)
+
+def get_pkg_check_cmd():
+    '''Build the command to use for checking if a linux package is installed or not.'''
+    # 1- search for an installed package manager (rpm on rh, apt on db)
+    cmd_which_rpm=["which", "rpm"]
+    cmd_which_apt=["which", "apt"]
+    with open(os.devnull, 'w') as devnull:
+        # 1) we search for apt (debian based systems)
+        completed=subprocess.call(cmd_which_apt,stdout=devnull, stderr=subprocess.STDOUT)
+        if completed==0:
+            cmd_is_package_installed=["apt", "list", "--installed"]
+        else:
+            # 2) if apt not found search for rpm (redhat)
+            completed=subprocess.call(cmd_which_rpm,stdout=devnull, stderr=subprocess.STDOUT) # only 3.8! ,capture_output=True)
+            if completed==0:
+                cmd_is_package_installed=["rpm", "-q"]
+            else:
+                # no package manager was found
+                raise src.SatException("Error : command failed because sat was not able to find apt or rpm")
+    return cmd_is_package_installed
+
+def check_system_pkg(check_cmd,pkg):
+    '''Check if a package is installed
+    :param check_cmd list: the list of command to use system package manager
+    :param user str: the pkg name to check
+    :rtype: str
+    :return: a string with package name with status un message
+    '''
+    # build command
+    cmd_is_package_installed=[]
+    for cmd in check_cmd:
+        cmd_is_package_installed.append(cmd)
+    cmd_is_package_installed.append(pkg)
+    if check_cmd[0]=="apt":
+        # special treatment for apt
+        # (some debian packages have version numbers in their name, and also
+        # apt do not return status)
+        cmd_is_package_installed[-1]+="*" # we don't specify in pyconf the exact name because of version numbers
+        cmd_is_package_installed.append('|')
+        cmd_is_package_installed.append('grep') # add a grep to get an exit status
+        cmd_is_package_installed.append(cmd) 
+        
+    p=subprocess.Popen(cmd_is_package_installed, 
+                       stdout=subprocess.PIPE, 
+                       stderr=subprocess.PIPE)
+    output, err = p.communicate()
+    rc = p.returncode
+    if rc==0:
+        msg_status="   - "+pkg + " : " + src.printcolors.printcSuccess("OK") +\
+                  " (" +  output.replace('\n',' ') + ")\n" # remove output trailing \n
+    else:
+        msg_status="   - "+pkg + " : " + src.printcolors.printcError("KO") +\
+                   " (package is not installed!)\n"
+    return msg_status
+