Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 1 | # -*- 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 | |
| 13 | import os |
| 14 | import re |
| 15 | import sys |
| 16 | |
| 17 | def get_header_version_info(srcdir): |
| 18 | patchlevel_h = os.path.join(srcdir, '..', 'Include', 'patchlevel.h') |
| 19 | |
| 20 | # This won't pick out all #defines, but it will pick up the ones we |
| 21 | # care about. |
| 22 | rx = re.compile(r'\s*#define\s+([a-zA-Z][a-zA-Z_0-9]*)\s+([a-zA-Z_0-9]+)') |
| 23 | |
| 24 | d = {} |
| 25 | f = open(patchlevel_h) |
| 26 | try: |
| 27 | for line in f: |
| 28 | m = rx.match(line) |
| 29 | if m is not None: |
| 30 | name, value = m.group(1, 2) |
| 31 | d[name] = value |
| 32 | finally: |
| 33 | f.close() |
| 34 | |
| 35 | release = version = '%s.%s' % (d['PY_MAJOR_VERSION'], d['PY_MINOR_VERSION']) |
| 36 | micro = int(d['PY_MICRO_VERSION']) |
Martin v. Löwis | 24a05f7 | 2012-05-01 16:27:55 +0200 | [diff] [blame] | 37 | release += '.' + str(micro) |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 38 | |
| 39 | level = d['PY_RELEASE_LEVEL'] |
| 40 | suffixes = { |
| 41 | 'PY_RELEASE_LEVEL_ALPHA': 'a', |
| 42 | 'PY_RELEASE_LEVEL_BETA': 'b', |
Benjamin Peterson | b48f634 | 2009-06-22 19:36:31 +0000 | [diff] [blame] | 43 | 'PY_RELEASE_LEVEL_GAMMA': 'rc', |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 44 | } |
| 45 | if level != 'PY_RELEASE_LEVEL_FINAL': |
| 46 | release += suffixes[level] + str(int(d['PY_RELEASE_SERIAL'])) |
| 47 | return version, release |
| 48 | |
| 49 | |
| 50 | def get_sys_version_info(): |
| 51 | major, minor, micro, level, serial = sys.version_info |
| 52 | release = version = '%s.%s' % (major, minor) |
Martin v. Löwis | 24a05f7 | 2012-05-01 16:27:55 +0200 | [diff] [blame] | 53 | release += '.%s' % micro |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 54 | if level != 'final': |
| 55 | release += '%s%s' % (level[0], serial) |
| 56 | return version, release |
| 57 | |
| 58 | |
| 59 | def get_version_info(): |
| 60 | try: |
| 61 | return get_header_version_info('.') |
| 62 | except (IOError, OSError): |
| 63 | version, release = get_sys_version_info() |
Benjamin Peterson | 62ed91d | 2008-05-13 12:56:25 +0000 | [diff] [blame] | 64 | print >>sys.stderr, 'Can\'t get version info from Include/patchlevel.h, ' \ |
| 65 | 'using version of this interpreter (%s).' % release |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 66 | return version, release |
Christian Heimes | b558a2e | 2008-03-02 22:46:37 +0000 | [diff] [blame] | 67 | |
| 68 | if __name__ == '__main__': |
Georg Brandl | a17fd1f | 2010-10-29 05:30:17 +0000 | [diff] [blame] | 69 | print(get_header_version_info('.')[1]) |