From 1cb97ddd3ce3e6c17e74db6a4772c38a1ebd2115 Mon Sep 17 00:00:00 2001 From: crouzet Date: Fri, 22 Feb 2019 14:38:42 +0100 Subject: [PATCH] =?utf8?q?remplacement=20des=20appels=20syst=C3=A8me=20?= =?utf8?q?=C3=A0=20lsb=5Frelease=20par=20l'utilisation=20du=20module=20pla?= =?utf8?q?tform?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- commands/config.py | 4 +- src/architecture.py | 110 +++++++++++------------------ src/internal_config/distrib.pyconf | 90 +++-------------------- 3 files changed, 51 insertions(+), 153 deletions(-) diff --git a/commands/config.py b/commands/config.py index 25d61d2..3df71fe 100644 --- a/commands/config.py +++ b/commands/config.py @@ -162,8 +162,8 @@ class ConfigManager: # set platform parameters dist_name = src.architecture.get_distribution(codes=distrib_cfg.DISTRIBUTIONS) - dist_version = src.architecture.get_distrib_version(dist_name, codes=distrib_cfg.VERSIONS) - dist_version_full = src.architecture.get_infosys() + dist_version = src.architecture.get_distrib_version(dist_name) + dist_version_full = src.architecture.get_version_XY() dist = dist_name + dist_version var['dist_name'] = dist_name diff --git a/src/architecture.py b/src/architecture.py index a638427..5b8711b 100644 --- a/src/architecture.py +++ b/src/architecture.py @@ -44,29 +44,6 @@ def get_user(): import pwd return pwd.getpwuid(os.getuid())[0] -def _lsb_release(args): - '''Get system information with lsb_release. - - :param args str: The arguments to give to lsb_release. - :return: The distribution. - :rtype: str - ''' - try: - path = '/usr/local/bin:/usr/bin:/bin' - lsb_path = os.getenv("LSB_PATH") - if lsb_path is not None: - path = lsb_path + ":" + path - - from subprocess import Popen, PIPE - 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.exit(-1) def get_distribution(codes): '''Gets the code for the distribution @@ -79,58 +56,40 @@ def get_distribution(codes): if is_windows(): return "Win" - # Call to lsb_release - distrib = _lsb_release('-si') - if codes is not None and distrib in codes: - distrib = codes[distrib] - else: + # else get linux distribution description from platform, and encode it with code + lin_distrib = platform.dist()[0].lower() + distrib="not found" + for dist in codes: + if dist in lin_distrib: + distrib = codes[dist] + break + if distrib=="not found": 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.exit(-1) return distrib -def get_infosys(): +def get_version_XY(): """ - 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. + Return major and minor version of the distribution + from a CentOS example, returns '7.6' + extracted from platform.dist() """ - 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 + dist_version=platform.dist()[1].split('.') + if len(dist_version)==1: + version = dist_version[0] + else: + version = dist_version[0] + "." + dist_version[1] + return version -def get_distrib_version(distrib, codes): - '''Gets the version of the distribution + +def get_distrib_version(distrib): + '''Return the sat encoded version of the distribution + This code is used in config to apend the name of the application directories + withdistribution info" :param distrib str: The distribution on which the version will be found. - :param codes L{Mapping}: The map containing distribution correlation table. :return: The version of the distribution on which salomeTools is running, regarding the distribution correlation table contained in codes variable. @@ -140,14 +99,25 @@ def get_distrib_version(distrib, codes): if is_windows(): return platform.release() - # Call to lsb_release - version = _lsb_release('-sr') - if distrib in codes: - if version in codes[distrib]: - version = codes[distrib][version] + # get version from platform + dist_version=platform.dist()[1].split('.') + # encode it (conform to src/internal_config/distrib.pyconf VERSIONS dist if distrib == "CO": - version=version[0] #for centos, we only care for major version + version=dist_version[0] # for centos, we only care for major version + elif distrib == "UB": + # for ubuntu, we care for major + minor version + version=dist_version[0] + "." + dist_version[1] + elif distrib == "DB": + if len(dist_version[0]) == 1: + version="0"+dist_version[0] + else: + version=dist_version[0] # unstable, and version >= 10 + elif distrib == "MG": + version="0"+dist_version[0] + else: + version=dist_version[0] + return version def get_python_version(): diff --git a/src/internal_config/distrib.pyconf b/src/internal_config/distrib.pyconf index dde0785..43ce36b 100644 --- a/src/internal_config/distrib.pyconf +++ b/src/internal_config/distrib.pyconf @@ -6,85 +6,13 @@ # If no code is found an error will be raised. DISTRIBUTIONS : { - "bullxServer": "BS" - "CentOS": "CO" - "Debian": "DB" - "Fedora": "FD" - "LinuxMint": "MN" - "Mageia": "MG" - "MandrivaLinux": "MD" - "RedHatEnterpriseServer": "CO" - "RedHatEnterpriseWorkstation" : "CO" - "Ubuntu": "UB" - "openSUSE project":"OS" + "centos": "CO" + "debian": "DB" + "fedora": "FD" + "linuxmint": "MN" + "mageia": "MG" + "mandriva": "MD" + "redhat": "CO" + "ubuntu": "UB" + "opensuse":"OS" } - -# versions substitution -# Code to use for release, to the replace value returned by: lsb_release -sr -# If no code is found the value is used. -VERSIONS : -{ - - # Do not do anything for the ubuntu machines (CEA descision). - - "DB": - { - "unstable": "sid" - "5.0": "05" - "4.0": "04" - "6.0.7" : "06" - "6.0.10": "06" - "7.1" : "07" - "7.11" : "07" - "7.2" : "07" - "7.3" : "07" - "7.5" : "07" - "8.1" : "08" - "8.2" : "08" - "8.3" : "08" - "8.4" : "08" - "8.5" : "08" - "8.6" : "08" - "8.7" : "08" - "8.8" : "08" - "9.1" : "09" - "9.2" : "09" - "9.3" : "09" - "9.4" : "09" - "9.5" : "09" - "9.6" : "09" - "9.7" : "09" - "9.8" : "09" - } - "MD": - { - "2008.0": "08" - "2010.0": "10" - "2010.1": "10" - "2010.2": "10" - "2007.1": "07" - "2006.0": "06" - "4" : "06" - } - "MG": - { - "3": "03" - "4": "04" - "5": "05" - "6": "06" - } - "CO": - { - "7.1.1503": "7" - "7.2.1511": "7" - "7.3.1611": "7" - "6.1" : "6" - "6.2" : "6" - "6.3" : "6" - "6.4" : "6" - "6.5" : "6" - "6.6" : "6" - "6.7" : "6" - } -} - -- 2.39.2