blob: 919ba4a12e99899a3498ab9dde7cc7cfebd90d04 [file] [log] [blame]
Georg Brandl86def6c2008-01-21 20:36:10 +00001# -*- coding: utf-8 -*-
2"""
3 patchlevel.py
4 ~~~~~~~~~~~~~
5
6 Extract version info from Include/patchlevel.h.
7 Adapted from Doc/tools/getversioninfo.
8
9 :copyright: 2007-2008 by Georg Brandl.
10 :license: Python license.
11"""
12
Martin Panter9a454022017-01-29 23:33:13 +000013from __future__ import print_function
14
Georg Brandl86def6c2008-01-21 20:36:10 +000015import os
16import re
17import sys
18
19def get_header_version_info(srcdir):
20 patchlevel_h = os.path.join(srcdir, '..', 'Include', 'patchlevel.h')
21
22 # This won't pick out all #defines, but it will pick up the ones we
23 # care about.
24 rx = re.compile(r'\s*#define\s+([a-zA-Z][a-zA-Z_0-9]*)\s+([a-zA-Z_0-9]+)')
25
26 d = {}
27 f = open(patchlevel_h)
28 try:
29 for line in f:
30 m = rx.match(line)
31 if m is not None:
32 name, value = m.group(1, 2)
33 d[name] = value
34 finally:
35 f.close()
36
37 release = version = '%s.%s' % (d['PY_MAJOR_VERSION'], d['PY_MINOR_VERSION'])
38 micro = int(d['PY_MICRO_VERSION'])
Martin v. Löwis24a05f72012-05-01 16:27:55 +020039 release += '.' + str(micro)
Georg Brandl86def6c2008-01-21 20:36:10 +000040
41 level = d['PY_RELEASE_LEVEL']
42 suffixes = {
43 'PY_RELEASE_LEVEL_ALPHA': 'a',
44 'PY_RELEASE_LEVEL_BETA': 'b',
Benjamin Petersonb48f6342009-06-22 19:36:31 +000045 'PY_RELEASE_LEVEL_GAMMA': 'rc',
Georg Brandl86def6c2008-01-21 20:36:10 +000046 }
47 if level != 'PY_RELEASE_LEVEL_FINAL':
48 release += suffixes[level] + str(int(d['PY_RELEASE_SERIAL']))
49 return version, release
50
51
52def get_sys_version_info():
53 major, minor, micro, level, serial = sys.version_info
54 release = version = '%s.%s' % (major, minor)
Martin v. Löwis24a05f72012-05-01 16:27:55 +020055 release += '.%s' % micro
Georg Brandl86def6c2008-01-21 20:36:10 +000056 if level != 'final':
57 release += '%s%s' % (level[0], serial)
58 return version, release
59
60
61def get_version_info():
62 try:
63 return get_header_version_info('.')
64 except (IOError, OSError):
65 version, release = get_sys_version_info()
Martin Panter8f3fb722017-01-29 10:05:02 +000066 print('Can\'t get version info from Include/patchlevel.h, ' \
67 'using version of this interpreter (%s).' % release, file=sys.stderr)
Georg Brandl86def6c2008-01-21 20:36:10 +000068 return version, release
Christian Heimesb558a2e2008-03-02 22:46:37 +000069
70if __name__ == '__main__':
Georg Brandla17fd1f2010-10-29 05:30:17 +000071 print(get_header_version_info('.')[1])