Salome HOME
RE-organization of directories, according to CEA request
[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 on which SAT is running
21 '''
22
23 import os, sys, platform, pwd
24
25 def is_windows():
26     '''method that checks windows OS
27       
28     :rtype: boolean
29     '''
30     return platform.system() == 'Windows'
31
32 def get_user():
33     '''method that gets the username that launched sat  
34     
35     :rtype: str
36     '''
37     # In windows case, the USERNAME environment variable has to be set
38     if is_windows():
39         if not os.environ.has_key('USERNAME'):
40             raise Exception('USERNAME environment variable not set')
41         return os.environ['USERNAME']
42     else: # linux
43         return pwd.getpwuid(os.getuid())[0]
44
45 def _lsb_release(args):
46     '''Get system information with lsb_release.
47     
48     :param args str: The arguments to give to lsb_release.
49     :return: The distribution.
50     :rtype: str
51     '''
52     try:
53         path = '/usr/local/bin:/usr/bin:/bin'
54         lsb_path = os.getenv("LSB_PATH")
55         if lsb_path is not None:
56             path = lsb_path + ":" + path
57         
58         from subprocess import Popen, PIPE
59         res = Popen(['lsb_release', args], env={'PATH': path}, stdout=PIPE).communicate()[0][:-1]
60         # in case of python3, convert byte to str
61         if isinstance(res, bytes):
62             res = res.decode()
63         return res
64     except OSError:
65         sys.stderr.write(_(u"lsb_release not installed\n"))
66         sys.stderr.write(_(u"You can define $LSB_PATH to give the path to lsb_release\n"))
67         sys.exit(-1)
68
69 def get_distribution(codes):
70     '''Gets the code for the distribution
71     
72     :param codes L{Mapping}: The map containing distribution correlation table.
73     :return: The distribution on which salomeTools is running, regarding the distribution correlation table contained in codes variable.
74     :rtype: str
75     '''
76     if is_windows():
77         return "Win"
78
79     # Call to lsb_release
80     distrib = _lsb_release('-si')
81     if codes is not None and distrib in codes:
82         distrib = codes[distrib]
83     else:
84         sys.stderr.write(_(u"Unknown distribution: '%s'\n") % distrib)
85         sys.stderr.write(_(u"Please add your distribution to data/distrib.pyconf\n"))
86         sys.exit(-1)
87
88     return distrib
89
90
91 def get_distrib_version(distrib, codes):
92     '''Gets the version of the distribution
93     
94     :param distrib str: The distribution on which the version will be found.
95     :param codes L{Mapping}: The map containing distribution correlation table.
96     :return: The version of the distribution on which salomeTools is running, regarding the distribution correlation table contained in codes variable.
97     :rtype: str
98     '''
99
100     if is_windows():
101         return platform.release()
102
103     # Call to lsb_release
104     version = _lsb_release('-sr')
105     if distrib in codes:
106         if version in codes[distrib]:
107             version = codes[distrib][version]
108
109     return version
110
111 def get_python_version():
112     '''Gets the version of the running python.
113     
114     :return: the version of the running python.
115     :rtype: str
116     '''
117     
118     # The platform python module gives the answer
119     return platform.python_version()
120
121 def get_nb_proc():
122     '''Gets the number of processors of the machine on which salomeTools is running.
123     
124     :return: the number of processors.
125     :rtype: str
126     '''
127     
128     try :
129         import multiprocessing
130         nb_proc=multiprocessing.cpu_count()
131     except :
132         nb_proc=int(os.sysconf('SC_NPROCESSORS_ONLN'))
133     return nb_proc