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