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