blob: 617f28c2527ddff98eb308addf2addf17779be23 [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 = {}
Martin Panterc654f0a2017-01-29 10:17:17 +000027 with open(patchlevel_h) as f:
Georg Brandl86def6c2008-01-21 20:36:10 +000028 for line in f:
29 m = rx.match(line)
30 if m is not None:
31 name, value = m.group(1, 2)
32 d[name] = value
Georg Brandl86def6c2008-01-21 20:36:10 +000033
34 release = version = '%s.%s' % (d['PY_MAJOR_VERSION'], d['PY_MINOR_VERSION'])
35 micro = int(d['PY_MICRO_VERSION'])
Martin v. Löwis24a05f72012-05-01 16:27:55 +020036 release += '.' + str(micro)
Georg Brandl86def6c2008-01-21 20:36:10 +000037
38 level = d['PY_RELEASE_LEVEL']
39 suffixes = {
40 'PY_RELEASE_LEVEL_ALPHA': 'a',
41 'PY_RELEASE_LEVEL_BETA': 'b',
Benjamin Petersonb48f6342009-06-22 19:36:31 +000042 'PY_RELEASE_LEVEL_GAMMA': 'rc',
Georg Brandl86def6c2008-01-21 20:36:10 +000043 }
44 if level != 'PY_RELEASE_LEVEL_FINAL':
45 release += suffixes[level] + str(int(d['PY_RELEASE_SERIAL']))
46 return version, release
47
48
49def get_sys_version_info():
50 major, minor, micro, level, serial = sys.version_info
51 release = version = '%s.%s' % (major, minor)
Martin v. Löwis24a05f72012-05-01 16:27:55 +020052 release += '.%s' % micro
Georg Brandl86def6c2008-01-21 20:36:10 +000053 if level != 'final':
54 release += '%s%s' % (level[0], serial)
55 return version, release
56
57
58def get_version_info():
59 try:
60 return get_header_version_info('.')
61 except (IOError, OSError):
62 version, release = get_sys_version_info()
Martin Panter8f3fb722017-01-29 10:05:02 +000063 print('Can\'t get version info from Include/patchlevel.h, ' \
64 'using version of this interpreter (%s).' % release, file=sys.stderr)
Georg Brandl86def6c2008-01-21 20:36:10 +000065 return version, release
Christian Heimesb558a2e2008-03-02 22:46:37 +000066
67if __name__ == '__main__':
Georg Brandla17fd1f2010-10-29 05:30:17 +000068 print(get_header_version_info('.')[1])