1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2018 EDF R&D
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.
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.
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
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
24 Informations sur le code et la plateforme, et mise à jour des chemins
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 :
31 print PlatformInfo().getVersion()
32 created = PlatformInfo().getDate()
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 :
39 La classe "SystemUsage" permet de sous Unix les différentes tailles
40 mémoires du process courant. Ces tailles peuvent être assez variables et
41 dépendent de la fiabilité des informations du système dans le suivi des
44 __author__ = "Jean-Philippe ARGAUD"
50 # ==============================================================================
51 class PlatformInfo(object):
53 Rassemblement des informations sur le code et la plateforme
60 "Retourne le nom de l'application"
61 import daCore.version as dav
65 "Retourne le numéro de la version"
66 import daCore.version as dav
70 "Retourne la date de création de la version"
71 import daCore.version as dav
74 def getPythonVersion(self):
75 "Retourne la version de python disponible"
76 return ".".join([str(x) for x in sys.version_info[0:3]]) # map(str,sys.version_info[0:3]))
78 def getNumpyVersion(self):
79 "Retourne la version de numpy disponible"
81 return numpy.version.version
83 def getScipyVersion(self):
84 "Retourne la version de scipy disponible"
86 __version = scipy.version.version
91 def getMatplotlibVersion(self):
92 "Retourne la version de matplotlib disponible"
94 __version = matplotlib.__version__
99 def getGnuplotVersion(self):
100 "Retourne la version de gnuplotpy disponible"
102 __version = Gnuplot.__version__
107 def getSphinxVersion(self):
108 "Retourne la version de sphinx disponible"
110 __version = sphinx.__version__
115 def getNloptVersion(self):
116 "Retourne la version de nlopt disponible"
118 __version = "%s.%s.%s"%(
119 nlopt.version_major(),
120 nlopt.version_minor(),
121 nlopt.version_bugfix(),
127 def getCurrentMemorySize(self):
128 "Retourne la taille mémoire courante utilisée"
131 def MaximumPrecision(self):
132 "Retourne la precision maximale flottante pour Numpy"
135 numpy.array([1.,], dtype='float128')
141 def MachinePrecision(self):
142 # Alternative sans module :
146 # eps = (1.0 + eps/2) - 1.0
147 return sys.float_info.epsilon
150 import daCore.version as dav
151 return "%s %s (%s)"%(dav.name,dav.version,dav.date)
153 # ==============================================================================
157 import scipy.optimize
164 has_matplotlib = True
166 has_matplotlib = False
186 has_salome = bool( "ROOT_SALOME" in os.environ )
187 has_yacs = bool( "YACS_ROOT_DIR" in os.environ )
188 has_adao = bool( "ADAO_ROOT_DIR" in os.environ )
190 # ==============================================================================
193 Fonction pour rendre unique chaque élément d'une liste, en préservant l'ordre
196 return [x for x in sequence if x not in __seen and not __seen.add(x)]
198 # ==============================================================================
199 class PathManagement(object):
201 Mise à jour du path système pour les répertoires d'outils
204 "Déclaration des répertoires statiques"
205 parent = os.path.abspath(os.path.join(os.path.dirname(__file__),".."))
207 self.__paths["daNumerics"] = os.path.join(parent,"daNumerics")
209 for v in self.__paths.values():
210 sys.path.insert(0, v )
212 # Conserve en unique exemplaire chaque chemin
213 sys.path = uniq( sys.path )
218 Renvoie le dictionnaire des chemins ajoutés
222 # ==============================================================================
223 class SystemUsage(object):
225 Permet de récupérer les différentes tailles mémoires du process courant
228 # Le module resource renvoie 0 pour les tailles mémoire. On utilise donc
229 # plutôt : http://code.activestate.com/recipes/286222/ et Wikipedia
231 _proc_status = '/proc/%d/status' % os.getpid()
232 _memo_status = '/proc/meminfo'
234 'o' : 1.0, # Multiples SI de l'octet
238 'kio': 1024.0, # Multiples binaires de l'octet
239 'Mio': 1024.0*1024.0,
240 'Gio': 1024.0*1024.0*1024.0,
241 'B': 1.0, # Multiples binaires du byte=octet
243 'MB' : 1024.0*1024.0,
244 'GB' : 1024.0*1024.0*1024.0,
251 def _VmA(self, VmKey, unit):
252 "Lecture des paramètres mémoire de la machine"
254 t = open(self._memo_status)
258 return 0.0 # non-Linux?
259 i = v.index(VmKey) # get VmKey line e.g. 'VmRSS: 9999 kB\n ...'
260 v = v[i:].split(None, 3) # whitespace
262 return 0.0 # invalid format?
263 # convert Vm value to bytes
264 mem = float(v[1]) * self._scale[v[2]]
265 return mem / self._scale[unit]
267 def getAvailablePhysicalMemory(self, unit="o"):
268 "Renvoie la mémoire physique utilisable en octets"
269 return self._VmA('MemTotal:', unit)
271 def getAvailableSwapMemory(self, unit="o"):
272 "Renvoie la mémoire swap utilisable en octets"
273 return self._VmA('SwapTotal:', unit)
275 def getAvailableMemory(self, unit="o"):
276 "Renvoie la mémoire totale (physique+swap) utilisable en octets"
277 return self._VmA('MemTotal:', unit) + self._VmA('SwapTotal:', unit)
279 def getUsableMemory(self, unit="o"):
280 """Renvoie la mémoire utilisable en octets
281 Rq : il n'est pas sûr que ce décompte soit juste...
283 return self._VmA('MemFree:', unit) + self._VmA('SwapFree:', unit) + \
284 self._VmA('Cached:', unit) + self._VmA('SwapCached:', unit)
286 def _VmB(self, VmKey, unit):
287 "Lecture des paramètres mémoire du processus"
289 t = open(self._proc_status)
293 return 0.0 # non-Linux?
294 i = v.index(VmKey) # get VmKey line e.g. 'VmRSS: 9999 kB\n ...'
295 v = v[i:].split(None, 3) # whitespace
297 return 0.0 # invalid format?
298 # convert Vm value to bytes
299 mem = float(v[1]) * self._scale[v[2]]
300 return mem / self._scale[unit]
302 def getUsedMemory(self, unit="o"):
303 "Renvoie la mémoire résidente utilisée en octets"
304 return self._VmB('VmRSS:', unit)
306 def getVirtualMemory(self, unit="o"):
307 "Renvoie la mémoire totale utilisée en octets"
308 return self._VmB('VmSize:', unit)
310 def getUsedStacksize(self, unit="o"):
311 "Renvoie la taille du stack utilisé en octets"
312 return self._VmB('VmStk:', unit)
314 def getMaxUsedMemory(self, unit="o"):
315 "Renvoie la mémoire résidente maximale mesurée"
316 return self._VmB('VmHWM:', unit)
318 def getMaxVirtualMemory(self, unit="o"):
319 "Renvoie la mémoire totale maximale mesurée"
320 return self._VmB('VmPeak:', unit)
322 # ==============================================================================
323 if __name__ == "__main__":
324 print('\n AUTODIAGNOSTIC \n')