Salome HOME
style: black format
[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
25 from platform import system, python_version, release
26
27 # linux_distribution is removed from platform module in python 3.8+
28 # we have to use distro module, which is not standard.
29 # write an error message if distro is not installed
30 try:
31     from platform import linux_distribution
32 except:
33     try:
34         from distro import linux_distribution
35     except:
36         print(
37             "\nError :\n"
38             "  linux_distribution was removed from platform module in Python 3.8+\n"
39             "  For python 3.8+ sat requires distro module to get information on linux distribution.\n"
40             "  Please install distro module with : pip install distro"
41         )
42         sys.exit(-1)
43
44
45 def is_windows():
46     """method that checks windows OS
47
48     :rtype: boolean
49     """
50     return system() == "Windows"
51
52
53 def get_user():
54     """method that gets the username that launched sat
55
56     :rtype: str
57     """
58     try:
59         if is_windows():
60             # In windows case, the USERNAME environment variable has to be set
61             user_name = os.environ["USERNAME"]
62         else:  # linux
63             import pwd
64
65             user_name = pwd.getpwuid(os.getuid())[0]
66     except:
67         user_name = "Unknown"
68     return user_name
69
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 "W"
81
82     # else get linux distribution description from platform, and encode it with code
83     lin_distrib = linux_distribution()[0].lower()
84     distrib = "not found"
85     for dist in codes:
86         if dist in lin_distrib:
87             distrib = codes[dist]
88             break
89     if distrib == "not found":
90         sys.stderr.write(_(u"Unknown distribution: '%s'\n") % distrib)
91         sys.stderr.write(
92             _(u"Please add your distribution to src/internal_config/distrib.pyconf\n")
93         )
94         sys.exit(-1)
95
96     return distrib
97
98
99 def get_version_XY():
100     """
101     Return major and minor version of the distribution
102     from a CentOS example, returns '7.6'
103     extracted from platform.linux_distribution()
104     """
105     dist_version = linux_distribution()[1].split(".")
106     if len(dist_version) == 1:
107         version = dist_version[0]
108     else:
109         version = dist_version[0] + "." + dist_version[1]
110     return version
111
112
113 def get_distrib_version(distrib):
114     """Return the sat encoded version of the distribution
115        This code is used in config to apend the name of the application directories
116        withdistribution info"
117
118     :param distrib str: The distribution on which the version will be found.
119     :return: The version of the distribution on which salomeTools is running,
120              regarding the distribution correlation table contained in codes
121              variable.
122     :rtype: str
123     """
124
125     if is_windows():
126         return release()
127
128     # get version from platform
129     dist_version = linux_distribution()[1].split(".")
130
131     # encode it (conform to src/internal_config/distrib.pyconf VERSIONS dist
132     if distrib == "CO":
133         version = dist_version[0]  # for centos, we only care for major version
134     elif distrib == "UB":
135         # for ubuntu, we care for major + minor version
136         version = dist_version[0] + "." + dist_version[1]
137     elif distrib == "DB":
138         if len(dist_version[0]) == 1:
139             version = "0" + dist_version[0]
140         else:
141             version = dist_version[0]  # unstable, and version >= 10
142     elif distrib == "MG":
143         version = "0" + dist_version[0]
144     else:
145         version = dist_version[0]
146
147     return version
148
149
150 def get_python_version():
151     """Gets the version of the running python.
152
153     :return: the version of the running python.
154     :rtype: str
155     """
156
157     # The platform python module gives the answer
158     return python_version()
159
160
161 def get_nb_proc():
162     """Gets the number of processors of the machine
163        on which salomeTools is running.
164
165     :return: the number of processors.
166     :rtype: str
167     """
168
169     try:
170         import multiprocessing
171
172         nb_proc = multiprocessing.cpu_count()
173     except:
174         if is_windows():
175             if os.environ.has_key("NUMBER_OF_PROCESSORS"):
176                 nb_proc = int(os.environ["NUMBER_OF_PROCESSORS"])
177             else:
178                 nb_proc = 1
179         else:
180             nb_proc = int(os.sysconf("SC_NPROCESSORS_ONLN"))
181     return nb_proc