Salome HOME
add dist_ref
[tools/sat.git] / src / architecture.py
index 8e16c226caaf641fea149f215317bcf798ac38cb..a638427078c67b6117beb1c82ae92ce779e06fd0 100644 (file)
@@ -37,7 +37,7 @@ def get_user():
     '''
     # In windows case, the USERNAME environment variable has to be set
     if is_windows():
-        if not os.environ.has_key('USERNAME'):
+        if not 'USERNAME' in os.environ:
             raise Exception('USERNAME environment variable not set')
         return os.environ['USERNAME']
     else: # linux
@@ -58,16 +58,14 @@ def _lsb_release(args):
             path = lsb_path + ":" + path
         
         from subprocess import Popen, PIPE
-        res = Popen(['lsb_release', args], env={'PATH': path},
-                     stdout=PIPE).communicate()[0][:-1]
+        res = Popen(['lsb_release', args], env={'PATH': path}, stdout=PIPE).communicate()[0][:-1]
         # in case of python3, convert byte to str
         if isinstance(res, bytes):
             res = res.decode()
         return res
     except OSError:
         sys.stderr.write(_(u"lsb_release not installed\n"))
-        sys.stderr.write(_(u"You can define $LSB_PATH to give"
-                           " the path to lsb_release\n"))
+        sys.stderr.write(_(u"You can define $LSB_PATH to give the path to lsb_release\n"))
         sys.exit(-1)
 
 def get_distribution(codes):
@@ -87,12 +85,46 @@ def get_distribution(codes):
         distrib = codes[distrib]
     else:
         sys.stderr.write(_(u"Unknown distribution: '%s'\n") % distrib)
-        sys.stderr.write(_(u"Please add your distribution to"
-                           " src/internal_config/distrib.pyconf\n"))
+        sys.stderr.write(_(u"Please add your distribution to src/internal_config/distrib.pyconf\n"))
         sys.exit(-1)
 
     return distrib
 
+def get_infosys():
+    """
+    from a CentOS example,
+    returns '7.6'  from  command 'lsb_release -ds'
+    extracted from 'CentOS Linux release 7.6.1810 (Core)'
+    and also for RedHat fedora etc.
+    """
+    import re
+    osys = ""
+    version = ""
+    architecture = ""
+    osys_value = "Unknown"
+    os_dict = {"mandrivalinux":"MD",
+               "centos":"CO",
+               "RedHatEnterpriseServer":"CO",
+               "RedHatEnterpriseWorkstation":"CO",
+               "fedora":"FD",
+               "ubuntu":"UB",
+               "debian":"DB",
+               "mageia":"MG",}
+    # lsb_cmd = "lsb_release -ds"
+    args = "-ds"
+    output = _lsb_release(args)
+    # print("lsb_release output %s" % output)
+    regexp = r"(^[0-9]+([.]?[0-9]+)+)"
+    for an_item in output.replace('"','').split():
+        if re.match(regexp, an_item) is not None and not version:
+            version = ".".join(an_item.split(".")[:2])
+        else:
+            for sub_item in os_dict.keys():
+                if sub_item == an_item.lower():
+                    osys = an_item
+                    osys_value = os_dict[sub_item]
+        if version and osys: break
+    return version
 
 def get_distrib_version(distrib, codes):
     '''Gets the version of the distribution
@@ -114,6 +146,8 @@ def get_distrib_version(distrib, codes):
         if version in codes[distrib]:
             version = codes[distrib][version]
 
+    if distrib == "CO":
+        version=version[0]  #for centos, we only care for major version
     return version
 
 def get_python_version():
@@ -139,4 +173,4 @@ def get_nb_proc():
         nb_proc=multiprocessing.cpu_count()
     except :
         nb_proc=int(os.sysconf('SC_NPROCESSORS_ONLN'))
-    return nb_proc
\ No newline at end of file
+    return nb_proc