]> SALOME platform Git repositories - modules/adao.git/blob - src/daComposant/daCore/PlatformInfo.py
Salome HOME
Minor corrections and improvement for SALOME support
[modules/adao.git] / src / daComposant / daCore / PlatformInfo.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2017 EDF R&D
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 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 """
24     Informations sur le code et la plateforme, et mise à jour des chemins
25
26     La classe "PlatformInfo" permet de récupérer les informations générales sur
27     le code et la plateforme sous forme de strings, ou d'afficher directement
28     les informations disponibles par les méthodes. L'impression directe d'un
29     objet de cette classe affiche les informations minimales. Par exemple :
30         print PlatformInfo()
31         print PlatformInfo().getVersion()
32         created = PlatformInfo().getDate()
33
34     La classe "PathManagement" permet de mettre à jour les chemins système pour
35     ajouter les outils numériques, matrices... On l'utilise en instanciant
36     simplement cette classe, sans meme récupérer d'objet :
37         PathManagement()
38 """
39 __author__ = "Jean-Philippe ARGAUD"
40 __all__ = []
41
42 import os, sys
43
44 # ==============================================================================
45 class PlatformInfo(object):
46     """
47     Rassemblement des informations sur le code et la plateforme
48     """
49     def __init__(self):
50         "Sans effet"
51         pass
52
53     def getName(self):
54         "Retourne le nom de l'application"
55         import daCore.version as dav
56         return dav.name
57
58     def getVersion(self):
59         "Retourne le numéro de la version"
60         import daCore.version as dav
61         return dav.version
62
63     def getDate(self):
64         "Retourne la date de création de la version"
65         import daCore.version as dav
66         return dav.date
67
68     def getPythonVersion(self):
69         "Retourne la version de python disponible"
70         return ".".join([str(x) for x in sys.version_info[0:3]]) # map(str,sys.version_info[0:3]))
71
72     def getNumpyVersion(self):
73         "Retourne la version de numpy disponible"
74         import numpy.version
75         return numpy.version.version
76
77     def getScipyVersion(self):
78         "Retourne la version de scipy disponible"
79         try:
80             import scipy.version
81             return scipy.version.version
82         except ImportError:
83             return "0.0.0"
84
85     def getMatplotlibVersion(self):
86         "Retourne la version de matplotlib disponible"
87         try:
88             import matplotlib
89             return matplotlib.__version__
90         except ImportError:
91             return "0.0.0"
92
93     def getGnuplotVersion(self):
94         "Retourne la version de gnuplotpy disponible"
95         try:
96             import Gnuplot
97             return Gnuplot.__version__
98         except ImportError:
99             return "0.0"
100
101     def getSphinxVersion(self):
102         "Retourne la version de sphinx disponible"
103         try:
104             import sphinx
105             return sphinx.__version__
106         except ImportError:
107             return "0.0.0"
108
109     def getNloptVersion(self):
110         "Retourne la version de nlopt disponible"
111         try:
112             import nlopt
113             return "%s.%s.%s"%(
114                 nlopt.version_major(),
115                 nlopt.version_minor(),
116                 nlopt.version_bugfix(),
117                 )
118         except ImportError:
119             return "0.0.0"
120
121     def getCurrentMemorySize(self):
122         "Retourne la taille mémoire courante utilisée"
123         return 1
124
125     def MaximumPrecision(self):
126         "Retourne la precision maximale flottante pour Numpy"
127         import numpy
128         try:
129             x = numpy.array([1.,], dtype='float128')
130             mfp = 'float128'
131         except:
132             mfp = 'float64'
133         return mfp
134
135     def MachinePrecision(self):
136         # Alternative sans module :
137         # eps = 2.38
138         # while eps > 0:
139         #     old_eps = eps
140         #     eps = (1.0 + eps/2) - 1.0
141         return sys.float_info.epsilon
142
143     def __str__(self):
144         import daCore.version as dav
145         return "%s %s (%s)"%(dav.name,dav.version,dav.date)
146
147 # ==============================================================================
148 try:
149     import scipy
150     import scipy.optimize
151     has_scipy = True
152 except ImportError:
153     has_scipy = False
154
155 try:
156     import matplotlib
157     has_matplotlib = True
158 except ImportError:
159     has_matplotlib = False
160
161 try:
162     import Gnuplot
163     has_gnuplot = True
164 except ImportError:
165     has_gnuplot = False
166
167 try:
168     import sphinx
169     has_sphinx = True
170 except ImportError:
171     has_sphinx = False
172
173 try:
174     import nlopt
175     has_nlopt = True
176 except ImportError:
177     has_nlopt = False
178
179 if "ROOT_SALOME" in os.environ:
180     has_salome = True
181 else:
182     has_salome = False
183
184 if "YACS_ROOT_DIR" in os.environ:
185     has_yacs = True
186 else:
187     has_yacs = False
188
189 if "ADAO_ROOT_DIR" in os.environ:
190     has_adao = True
191 else:
192     has_adao = False
193
194 # ==============================================================================
195 def uniq(sequence):
196     """
197     Fonction pour rendre unique chaque élément d'une liste, en préservant l'ordre
198     """
199     __seen = set()
200     return [x for x in sequence if x not in __seen and not __seen.add(x)]
201
202 # ==============================================================================
203 class PathManagement(object):
204     """
205     Mise à jour du path système pour les répertoires d'outils
206     """
207     def __init__(self):
208         "Déclaration des répertoires statiques"
209         parent = os.path.abspath(os.path.join(os.path.dirname(__file__),".."))
210         self.__paths = {}
211         self.__paths["daExternals"] = os.path.join(parent,"daExternals")
212         self.__paths["daMatrices"]  = os.path.join(parent,"daMatrices")
213         self.__paths["daNumerics"]  = os.path.join(parent,"daNumerics")
214         #
215         for v in self.__paths.values():
216             sys.path.insert(0, v )
217         #
218         # Conserve en unique exemplaire chaque chemin
219         sys.path = uniq( sys.path )
220         del parent
221
222     def getpaths(self):
223         """
224         Renvoie le dictionnaire des chemins ajoutés
225         """
226         return self.__paths
227
228 # ==============================================================================
229 class SystemUsage(object):
230     """
231     Permet de récupérer les différentes tailles mémoires du process courant
232     """
233     #
234     # Le module resource renvoie 0 pour les tailles mémoire. On utilise donc
235     # plutôt : http://code.activestate.com/recipes/286222/ et Wikipedia
236     #
237     _proc_status = '/proc/%d/status' % os.getpid()
238     _memo_status = '/proc/meminfo'
239     _scale = {
240         'o'  : 1.0,     # Multiples SI de l'octet
241         'ko' : 1.e3,
242         'Mo' : 1.e6,
243         'Go' : 1.e9,
244         'kio': 1024.0,  # Multiples binaires de l'octet
245         'Mio': 1024.0*1024.0,
246         'Gio': 1024.0*1024.0*1024.0,
247         'B':     1.0,   # Multiples binaires du byte=octet
248         'kB' : 1024.0,
249         'MB' : 1024.0*1024.0,
250         'GB' : 1024.0*1024.0*1024.0,
251         }
252     #
253     def __init__(self):
254         "Sans effet"
255         pass
256     #
257     def _VmA(self, VmKey, unit):
258         "Lecture des paramètres mémoire de la machine"
259         try:
260             t = open(self._memo_status)
261             v = t.read()
262             t.close()
263         except IOError:
264             return 0.0           # non-Linux?
265         i = v.index(VmKey)       # get VmKey line e.g. 'VmRSS:  9999  kB\n ...'
266         v = v[i:].split(None, 3) # whitespace
267         if len(v) < 3:
268             return 0.0           # invalid format?
269         # convert Vm value to bytes
270         mem = float(v[1]) * self._scale[v[2]]
271         return mem / self._scale[unit]
272     #
273     def getAvailablePhysicalMemory(self, unit="o"):
274         "Renvoie la mémoire physique utilisable en octets"
275         return self._VmA('MemTotal:', unit)
276     #
277     def getAvailableSwapMemory(self, unit="o"):
278         "Renvoie la mémoire swap utilisable en octets"
279         return self._VmA('SwapTotal:', unit)
280     #
281     def getAvailableMemory(self, unit="o"):
282         "Renvoie la mémoire totale (physique+swap) utilisable en octets"
283         return self._VmA('MemTotal:', unit) + self._VmA('SwapTotal:', unit)
284     #
285     def getUsableMemory(self, unit="o"):
286         """Renvoie la mémoire utilisable en octets
287         Rq : il n'est pas sûr que ce décompte soit juste...
288         """
289         return self._VmA('MemFree:', unit) + self._VmA('SwapFree:', unit) + \
290                self._VmA('Cached:', unit) + self._VmA('SwapCached:', unit)
291     #
292     def _VmB(self, VmKey, unit):
293         "Lecture des paramètres mémoire du processus"
294         try:
295             t = open(self._proc_status)
296             v = t.read()
297             t.close()
298         except IOError:
299             return 0.0           # non-Linux?
300         i = v.index(VmKey)       # get VmKey line e.g. 'VmRSS:  9999  kB\n ...'
301         v = v[i:].split(None, 3) # whitespace
302         if len(v) < 3:
303             return 0.0           # invalid format?
304         # convert Vm value to bytes
305         mem = float(v[1]) * self._scale[v[2]]
306         return mem / self._scale[unit]
307     #
308     def getUsedMemory(self, unit="o"):
309         "Renvoie la mémoire résidente utilisée en octets"
310         return self._VmB('VmRSS:', unit)
311     #
312     def getVirtualMemory(self, unit="o"):
313         "Renvoie la mémoire totale utilisée en octets"
314         return self._VmB('VmSize:', unit)
315     #
316     def getUsedStacksize(self, unit="o"):
317         "Renvoie la taille du stack utilisé en octets"
318         return self._VmB('VmStk:', unit)
319     #
320     def getMaxUsedMemory(self, unit="o"):
321         "Renvoie la mémoire résidente maximale mesurée"
322         return self._VmB('VmHWM:', unit)
323     #
324     def getMaxVirtualMemory(self, unit="o"):
325         "Renvoie la mémoire totale maximale mesurée"
326         return self._VmB('VmPeak:', unit)
327
328 # ==============================================================================
329 if __name__ == "__main__":
330     print('\n AUTODIAGNOSTIC \n')