on which SAT is running
'''
-import os, sys, platform
+import os, sys
+from platform import system,python_version,release
+
+# linux_distribution is removed from platform module in python 3.8+
+# we have to use distro module, which is not standard.
+# write an error message if distro is not installed
+try:
+ from platform import linux_distribution
+except:
+ try:
+ from distro import linux_distribution
+ except:
+ print ("\nError :\n"
+ " linux_distribution was removed from platform module in Python 3.8+\n"
+ " For python 3.8+ sat requires distro module to get information on linux distribution.\n"
+ " Please install distro module with : pip install distro")
+ sys.exit(-1)
+
def is_windows():
'''method that checks windows OS
:rtype: boolean
'''
- return platform.system() == 'Windows'
+ return system() == 'Windows'
def get_user():
'''method that gets the username that launched sat
:rtype: str
'''
- # In windows case, the USERNAME environment variable has to be set
- if is_windows():
- if not 'USERNAME' in os.environ:
- raise Exception('USERNAME environment variable not set')
- return os.environ['USERNAME']
- else: # linux
- 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)
+ try :
+ if is_windows():
+ # In windows case, the USERNAME environment variable has to be set
+ user_name=os.environ['USERNAME']
+ else: # linux
+ import pwd
+ user_name=pwd.getpwuid(os.getuid())[0]
+ except :
+ user_name="Unknown"
+ return user_name
+
def get_distribution(codes):
'''Gets the code for the distribution
:rtype: str
'''
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:
+ return "W"
+
+ # else get linux distribution description from platform, and encode it with code
+ lin_distrib = linux_distribution()[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.stderr.write(_(u"Please add your distribution to src/internal_config/distrib.pyconf\n"))
sys.exit(-1)
return distrib
-# Added by Lioka RAZAFINDRAZAKA
-# <run_shell> and <get_infosys> functions are used to get info from shell command
-def run_shell(sh_cmd, pipe=True):
- import subprocess
- if pipe: popen = subprocess.Popen(sh_cmd, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- else: popen = subprocess.Popen(sh_cmd, shell=True, close_fds=True)
- out_put = popen.communicate()
- return out_put[0], out_put[1], popen.returncode
-
-def get_infosys():
- import re, socket
- 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"
- output, errdata, return_code = run_shell(lsb_cmd)
- 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
- import platform
- architecture = platform.architecture()[0][:2]
- infosys = "_".join([osys,version,architecture,"bits",socket.gethostname(),osys_value+version])
- return version, infosys
-
-def get_distrib_version(distrib, codes):
- '''Gets the version of the distribution
+def get_version_XY():
+ """
+ Return major and minor version of the distribution
+ from a CentOS example, returns '7.6'
+ extracted from platform.linux_distribution()
+ """
+ dist_version=linux_distribution()[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):
+ '''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.
'''
if is_windows():
- return platform.release()
+ return 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=linux_distribution()[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():
'''
# The platform python module gives the answer
- return platform.python_version()
+ return python_version()
def get_nb_proc():
'''Gets the number of processors of the machine
import multiprocessing
nb_proc=multiprocessing.cpu_count()
except :
- nb_proc=int(os.sysconf('SC_NPROCESSORS_ONLN'))
+ if is_windows():
+ if os.environ.has_key("NUMBER_OF_PROCESSORS"):
+ nb_proc = int(os.environ["NUMBER_OF_PROCESSORS"])
+ else:
+ nb_proc = 1
+ else:
+ nb_proc=int(os.sysconf('SC_NPROCESSORS_ONLN'))
return nb_proc