1 | n/a | # -*- coding: utf-8 -*- |
---|
2 | n/a | """ |
---|
3 | n/a | patchlevel.py |
---|
4 | n/a | ~~~~~~~~~~~~~ |
---|
5 | n/a | |
---|
6 | n/a | Extract version info from Include/patchlevel.h. |
---|
7 | n/a | Adapted from Doc/tools/getversioninfo. |
---|
8 | n/a | |
---|
9 | n/a | :copyright: 2007-2008 by Georg Brandl. |
---|
10 | n/a | :license: Python license. |
---|
11 | n/a | """ |
---|
12 | n/a | |
---|
13 | n/a | from __future__ import print_function |
---|
14 | n/a | |
---|
15 | n/a | import os |
---|
16 | n/a | import re |
---|
17 | n/a | import sys |
---|
18 | n/a | |
---|
19 | n/a | def get_header_version_info(srcdir): |
---|
20 | n/a | patchlevel_h = os.path.join(srcdir, '..', 'Include', 'patchlevel.h') |
---|
21 | n/a | |
---|
22 | n/a | # This won't pick out all #defines, but it will pick up the ones we |
---|
23 | n/a | # care about. |
---|
24 | n/a | rx = re.compile(r'\s*#define\s+([a-zA-Z][a-zA-Z_0-9]*)\s+([a-zA-Z_0-9]+)') |
---|
25 | n/a | |
---|
26 | n/a | d = {} |
---|
27 | n/a | with open(patchlevel_h) as f: |
---|
28 | n/a | for line in f: |
---|
29 | n/a | m = rx.match(line) |
---|
30 | n/a | if m is not None: |
---|
31 | n/a | name, value = m.group(1, 2) |
---|
32 | n/a | d[name] = value |
---|
33 | n/a | |
---|
34 | n/a | release = version = '%s.%s' % (d['PY_MAJOR_VERSION'], d['PY_MINOR_VERSION']) |
---|
35 | n/a | micro = int(d['PY_MICRO_VERSION']) |
---|
36 | n/a | release += '.' + str(micro) |
---|
37 | n/a | |
---|
38 | n/a | level = d['PY_RELEASE_LEVEL'] |
---|
39 | n/a | suffixes = { |
---|
40 | n/a | 'PY_RELEASE_LEVEL_ALPHA': 'a', |
---|
41 | n/a | 'PY_RELEASE_LEVEL_BETA': 'b', |
---|
42 | n/a | 'PY_RELEASE_LEVEL_GAMMA': 'rc', |
---|
43 | n/a | } |
---|
44 | n/a | if level != 'PY_RELEASE_LEVEL_FINAL': |
---|
45 | n/a | release += suffixes[level] + str(int(d['PY_RELEASE_SERIAL'])) |
---|
46 | n/a | return version, release |
---|
47 | n/a | |
---|
48 | n/a | |
---|
49 | n/a | def get_sys_version_info(): |
---|
50 | n/a | major, minor, micro, level, serial = sys.version_info |
---|
51 | n/a | release = version = '%s.%s' % (major, minor) |
---|
52 | n/a | release += '.%s' % micro |
---|
53 | n/a | if level != 'final': |
---|
54 | n/a | release += '%s%s' % (level[0], serial) |
---|
55 | n/a | return version, release |
---|
56 | n/a | |
---|
57 | n/a | |
---|
58 | n/a | def get_version_info(): |
---|
59 | n/a | try: |
---|
60 | n/a | return get_header_version_info('.') |
---|
61 | n/a | except (IOError, OSError): |
---|
62 | n/a | version, release = get_sys_version_info() |
---|
63 | n/a | print('Can\'t get version info from Include/patchlevel.h, ' \ |
---|
64 | n/a | 'using version of this interpreter (%s).' % release, file=sys.stderr) |
---|
65 | n/a | return version, release |
---|
66 | n/a | |
---|
67 | n/a | if __name__ == '__main__': |
---|
68 | n/a | print(get_header_version_info('.')[1]) |
---|