version.py 4.47 KB
Newer Older
1
#!/usr/bin/env python
Damien Naudet's avatar
Damien Naudet committed
2
3
4
# coding: utf-8
# /*##########################################################################
#
5
# Copyright (c) 2015-2020 European Synchrotron Radiation Facility
Damien Naudet's avatar
Damien Naudet committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
26
"""Unique place where the version number is defined.
Damien Naudet's avatar
Damien Naudet committed
27

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
provides:
* version = "1.2.3" or "1.2.3-beta4"
* version_info = named tuple (1,2,3,"beta",4)
* hexversion: 0x010203B4
* strictversion = "1.2.3b4
* debianversion = "1.2.3~beta4"
* calc_hexversion: the function to transform a version_tuple into an integer

This is called hexversion since it only really looks meaningful when viewed as the
result of passing it to the built-in hex() function.
The version_info value may be used for a more human-friendly encoding of the same information.

The hexversion is a 32-bit number with the following layout:
Bits (big endian order)     Meaning
1-8     PY_MAJOR_VERSION (the 2 in 2.1.0a3)
9-16     PY_MINOR_VERSION (the 1 in 2.1.0a3)
17-24     PY_MICRO_VERSION (the 0 in 2.1.0a3)
25-28     PY_RELEASE_LEVEL (0xA for alpha, 0xB for beta, 0xC for release candidate and 0xF for final)
29-32     PY_RELEASE_SERIAL (the 3 in 2.1.0a3, zero for final releases)

Thus 2.1.0a3 is hexversion 0x020100a3.

"""

from __future__ import absolute_import, print_function, division
Damien Naudet's avatar
Damien Naudet committed
53
54
__authors__ = ["Jérôme Kieffer"]
__license__ = "MIT"
55
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
56
__date__ = "30/09/2020"
57
58
59
60
__status__ = "production"
__docformat__ = 'restructuredtext'
__all__ = ["date", "version_info", "strictversion", "hexversion", "debianversion",
           "calc_hexversion"]
Damien Naudet's avatar
Damien Naudet committed
61

62
63
64
RELEASE_LEVEL_VALUE = {"dev": 0,
                       "alpha": 10,
                       "beta": 11,
65
                       "candidate": 12,
66
                       "final": 15}
Damien Naudet's avatar
Damien Naudet committed
67

68
69
70
71
72
PRERELEASE_NORMALIZED_NAME = {"dev": "a",
                              "alpha": "a",
                              "beta": "b",
                              "candidate": "rc"}

Thomas Vincent's avatar
Thomas Vincent committed
73
MAJOR = 2020
74
MINOR = 11
Thomas Vincent's avatar
Thomas Vincent committed
75
76
MICRO = 1
RELEV = "dev"  # <16
Damien Naudet's avatar
Damien Naudet committed
77
78
SERIAL = 0  # <16

79
80
date = __date__

Damien Naudet's avatar
Damien Naudet committed
81
from collections import namedtuple
82
_version_info = namedtuple("version_info", ["major", "minor", "micro", "releaselevel", "serial"])
Damien Naudet's avatar
Damien Naudet committed
83
84
85

version_info = _version_info(MAJOR, MINOR, MICRO, RELEV, SERIAL)

86
strictversion = version = debianversion = "%d.%d.%d" % version_info[:3]
Damien Naudet's avatar
Damien Naudet committed
87
if version_info.releaselevel != "final":
88
89
90
91
    _prerelease = PRERELEASE_NORMALIZED_NAME[version_info[3]]
    version += "-%s%s" % (_prerelease, version_info[-1])
    debianversion += "~adev%i" % version_info[-1] if RELEV == "dev" else "~%s%i" % (_prerelease, version_info[-1])
    strictversion += _prerelease + str(version_info[-1])
Damien Naudet's avatar
Damien Naudet committed
92

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

def calc_hexversion(major=0, minor=0, micro=0, releaselevel="dev", serial=0):
    """Calculate the hexadecimal version number from the tuple version_info:

    :param major: integer
    :param minor: integer
    :param micro: integer
    :param relev: integer or string
    :param serial: integer
    :return: integer always increasing with revision numbers
    """
    try:
        releaselevel = int(releaselevel)
    except ValueError:
        releaselevel = RELEASE_LEVEL_VALUE.get(releaselevel, 0)

    hex_version = int(serial)
    hex_version |= releaselevel * 1 << 4
    hex_version |= int(micro) * 1 << 8
    hex_version |= int(minor) * 1 << 16
    hex_version |= int(major) * 1 << 24
    return hex_version


hexversion = calc_hexversion(*version_info)

if __name__ == "__main__":
    print(version)