Salome HOME
Copyright update 2022
[modules/kernel.git] / src / KERNEL_PY / salome_version.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 #  File   : salome_version.py
25 #  Author : Vadim SANDLER
26 #  Module : SALOME
27 #
28 __ALL__ = [
29     "getVersion",
30     "getVersionMajor",
31     "getVersionMinor",
32     "getVersionRelease",
33     "getVersions",
34     "getXVersion",
35     "isDevelopmentVersion",
36     ]
37
38 _salome_versions = {}
39
40 def getVersion( mod = "KERNEL", full = False ):
41     """
42     Get SALOME module version number
43     Parameters:
44     - mod  : SALOME module name, default is 'KERNEL'
45     - full : take into account development marker (in this case 'dev' is added
46              to the end of version value for development version), False by default
47     Returns: version number string or None if VERSION file is not found
48     """
49     global _salome_versions
50     mod = mod.upper()
51     dev_flag = { True : "dev", False : "" }
52     if mod not in _salome_versions:
53         _salome_versions[ mod ] = [ None, "" ]
54         import os
55         root_dir = os.getenv( "%s_ROOT_DIR" % mod )
56         if root_dir:
57             try:
58                 filename = root_dir + "/bin/salome/VERSION"
59                 if not os.path.exists( filename ):
60                     filename = root_dir + "/bin/VERSION"
61                 f = open( filename )
62                 data = f.readlines()
63                 f.close()
64                 for l in data:
65                     if l.strip().startswith("#") or ":" not in l: continue
66                     key = ":".join( l.split( ":" )[ :-1 ] ).strip()
67                     val = l.split( ":" )[ -1 ].strip()
68                     if "development" in key.lower():
69                         _salome_versions[ mod ][ 1 ] = dev_flag[ val == "1" ]
70                     elif "version" in key.lower() or mod in key:
71                         _salome_versions[ mod ][ 0 ] = val
72                     pass
73             except Exception:
74                 pass
75     v = _salome_versions[ mod ][ 0 ]
76     if full and v is not None:
77         v += _salome_versions[ mod ][ 1 ]
78     return v is not None and v or ""
79
80 def getVersionMajor( mod = "KERNEL" ):
81     """
82     Get SALOME module major version number
83     Parameters:
84     - mod  : SALOME module name, default is 'KERNEL'
85     Returns: version major number string or None if VERSION file is not found
86     """
87     ver = getVersion( mod )
88     try:
89         return ver.split( "." )[ 0 ]
90     except Exception:
91         pass
92     return None
93
94 def getVersionMinor( mod = "KERNEL" ):
95     """
96     Get SALOME module minor version number
97     Parameters:
98     - mod  : SALOME module name, default is 'KERNEL'
99     Returns: version minor number string or None if VERSION file is not found
100     """
101     ver = getVersion( mod )
102     try:
103         return ver.split( "." )[ 1 ]
104     except Exception:
105         pass
106     return None
107
108 def getVersionRelease( mod = "KERNEL" ):
109     """
110     Get SALOME module release version number
111     Parameters:
112     - mod  : SALOME module name, default is 'KERNEL'
113     Returns: version release number string or None if VERSION file is not found
114     """
115     ver = getVersion( mod )
116     try:
117         return ver.split( "." )[ 2 ]
118     except Exception:
119         pass
120     return None
121
122 def getVersions( mod = "KERNEL" ):
123     """
124     Get SALOME module version as list of [major, minor, release] numbers
125     Parameters:
126     - mod  : SALOME module name, default is 'KERNEL'
127     Returns: version numbers list
128     """
129     try:
130         major = int( getVersionMajor( mod ) )
131     except Exception:
132         major = 0
133         pass
134     try:
135         minor = int( getVersionMinor( mod ) )
136     except Exception:
137         minor = 0
138         pass
139     try:
140         rel = int( getVersionRelease( mod ) )
141     except Exception:
142         rel = 0
143         pass
144     return [ major, minor, rel ]
145     
146 def getXVersion( mod = "KERNEL" ):
147     """
148     Get SALOME module version as list of [major, minor, release] numbers
149     Parameters:
150     - mod  : SALOME module name, default is 'KERNEL'
151     Returns: version numbers list
152     """
153     major, minor, rel = getVersions( mod )
154     return hex( (major<<16) + (minor<<8) + rel )
155
156 def isDevelopmentVersion( mod = "KERNEL" ):
157     """
158     Checks if the version of SALOME module is marked as development one
159     Parameters:
160     - mod  : SALOME module name, default is 'KERNEL'
161     Returns: Return True for delopment version of module or False otherwise
162     """
163     ver = getVersion( mod, True )
164     return ver.endswith( "dev" )