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