Salome HOME
fix for windows: import pwd does not work in win
[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 os.environ.has_key('USERNAME'):
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},
62                      stdout=PIPE).communicate()[0][:-1]
63         # in case of python3, convert byte to str
64         if isinstance(res, bytes):
65             res = res.decode()
66         return res
67     except OSError:
68         sys.stderr.write(_(u"lsb_release not installed\n"))
69         sys.stderr.write(_(u"You can define $LSB_PATH to give"
70                            " the path to lsb_release\n"))
71         sys.exit(-1)
72
73 def get_distribution(codes):
74     '''Gets the code for the distribution
75     
76     :param codes L{Mapping}: The map containing distribution correlation table.
77     :return: The distribution on which salomeTools is running, regarding the 
78              distribution correlation table contained in codes variable.
79     :rtype: str
80     '''
81     if is_windows():
82         return "Win"
83
84     # Call to lsb_release
85     distrib = _lsb_release('-si')
86     if codes is not None and distrib in codes:
87         distrib = codes[distrib]
88     else:
89         sys.stderr.write(_(u"Unknown distribution: '%s'\n") % distrib)
90         sys.stderr.write(_(u"Please add your distribution to"
91                            " data/distrib.pyconf\n"))
92         sys.exit(-1)
93
94     return distrib
95
96
97 def get_distrib_version(distrib, codes):
98     '''Gets the version of the distribution
99     
100     :param distrib str: The distribution on which the version will be found.
101     :param codes L{Mapping}: The map containing distribution correlation table.
102     :return: The version of the distribution on which salomeTools is running, 
103              regarding the distribution correlation table contained in codes 
104              variable.
105     :rtype: str
106     '''
107
108     if is_windows():
109         return platform.release()
110
111     # Call to lsb_release
112     version = _lsb_release('-sr')
113     if distrib in codes:
114         if version in codes[distrib]:
115             version = codes[distrib][version]
116
117     return version
118
119 def get_python_version():
120     '''Gets the version of the running python.
121     
122     :return: the version of the running python.
123     :rtype: str
124     '''
125     
126     # The platform python module gives the answer
127     return platform.python_version()
128
129 def get_nb_proc():
130     '''Gets the number of processors of the machine 
131        on which salomeTools is running.
132     
133     :return: the number of processors.
134     :rtype: str
135     '''
136     
137     try :
138         import multiprocessing
139         nb_proc=multiprocessing.cpu_count()
140     except :
141         nb_proc=int(os.sysconf('SC_NPROCESSORS_ONLN'))
142     return nb_proc