Salome HOME
merge with master
[tools/sat.git] / src / architecture.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2013  CEA/DEN
4 #
5 #  This library is free software; you can redistribute it and/or
6 #  modify it under the terms of the GNU Lesser General Public
7 #  License as published by the Free Software Foundation; either
8 #  version 2.1 of the License.
9 #
10 #  This library is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public
16 #  License along with this library; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18
19 '''
20 In this file : all the stuff that can change with the architecture 
21 on which SAT is running
22 '''
23
24 import os, sys, platform
25
26 def is_windows():
27     '''method that checks windows OS
28       
29     :rtype: boolean
30     '''
31     return platform.system() == 'Windows'
32
33 def get_user():
34     '''method that gets the username that launched sat  
35     
36     :rtype: str
37     '''
38     # In windows case, the USERNAME environment variable has to be set
39     if is_windows():
40         if not 'USERNAME' in os.environ:
41             raise Exception('USERNAME environment variable not set')
42         return os.environ['USERNAME']
43     else: # linux
44         import pwd
45         return pwd.getpwuid(os.getuid())[0]
46
47 def _lsb_release(args):
48     '''Get system information with lsb_release.
49     
50     :param args str: The arguments to give to lsb_release.
51     :return: The distribution.
52     :rtype: str
53     '''
54     try:
55         path = '/usr/local/bin:/usr/bin:/bin'
56         lsb_path = os.getenv("LSB_PATH")
57         if lsb_path is not None:
58             path = lsb_path + ":" + path
59         
60         from subprocess import Popen, PIPE
61         res = Popen(['lsb_release', args], env={'PATH': path}, stdout=PIPE).communicate()[0][:-1]
62         # in case of python3, convert byte to str
63         if isinstance(res, bytes):
64             res = res.decode()
65         return res
66     except OSError:
67         sys.stderr.write(_(u"lsb_release not installed\n"))
68         sys.stderr.write(_(u"You can define $LSB_PATH to give the path to lsb_release\n"))
69         sys.exit(-1)
70
71 def get_distribution(codes):
72     '''Gets the code for the distribution
73     
74     :param codes L{Mapping}: The map containing distribution correlation table.
75     :return: The distribution on which salomeTools is running, regarding the 
76              distribution correlation table contained in codes variable.
77     :rtype: str
78     '''
79     if is_windows():
80         return "Win"
81
82     # Call to lsb_release
83     distrib = _lsb_release('-si')
84     if codes is not None and distrib in codes:
85         distrib = codes[distrib]
86     else:
87         sys.stderr.write(_(u"Unknown distribution: '%s'\n") % distrib)
88         sys.stderr.write(_(u"Please add your distribution to src/internal_config/distrib.pyconf\n"))
89         sys.exit(-1)
90
91     return distrib
92
93 def get_infosys():
94     """
95     from a CentOS example,
96     returns '7.6'  from  command 'lsb_release -ds'
97     extracted from 'CentOS Linux release 7.6.1810 (Core)'
98     and also for RedHat fedora etc.
99     """
100     import re
101     osys = ""
102     version = ""
103     architecture = ""
104     osys_value = "Unknown"
105     os_dict = {"mandrivalinux":"MD",
106                "centos":"CO",
107                "RedHatEnterpriseServer":"CO",
108                "RedHatEnterpriseWorkstation":"CO",
109                "fedora":"FD",
110                "ubuntu":"UB",
111                "debian":"DB",
112                "mageia":"MG",}
113     # lsb_cmd = "lsb_release -ds"
114     args = "-ds"
115     output = _lsb_release(args)
116     # print("lsb_release output %s" % output)
117     regexp = r"(^[0-9]+([.]?[0-9]+)+)"
118     for an_item in output.replace('"','').split():
119         if re.match(regexp, an_item) is not None and not version:
120             version = ".".join(an_item.split(".")[:2])
121         else:
122             for sub_item in os_dict.keys():
123                 if sub_item == an_item.lower():
124                     osys = an_item
125                     osys_value = os_dict[sub_item]
126         if version and osys: break
127     return version
128
129 def get_distrib_version(distrib, codes):
130     '''Gets the version of the distribution
131     
132     :param distrib str: The distribution on which the version will be found.
133     :param codes L{Mapping}: The map containing distribution correlation table.
134     :return: The version of the distribution on which salomeTools is running, 
135              regarding the distribution correlation table contained in codes 
136              variable.
137     :rtype: str
138     '''
139
140     if is_windows():
141         return platform.release()
142
143     # Call to lsb_release
144     version = _lsb_release('-sr')
145     if distrib in codes:
146         if version in codes[distrib]:
147             version = codes[distrib][version]
148
149     if distrib == "CO":
150         version=version[0]  #for centos, we only care for major version
151     return version
152
153 def get_python_version():
154     '''Gets the version of the running python.
155     
156     :return: the version of the running python.
157     :rtype: str
158     '''
159     
160     # The platform python module gives the answer
161     return platform.python_version()
162
163 def get_nb_proc():
164     '''Gets the number of processors of the machine 
165        on which salomeTools is running.
166     
167     :return: the number of processors.
168     :rtype: str
169     '''
170     
171     try :
172         import multiprocessing
173         nb_proc=multiprocessing.cpu_count()
174     except :
175         nb_proc=int(os.sysconf('SC_NPROCESSORS_ONLN'))
176     return nb_proc