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 13import os 14import re 15import sys 16 17def 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']) 37 if micro != 0: 38 release += '.' + str(micro) 39 40 level = d['PY_RELEASE_LEVEL'] 41 suffixes = { 42 'PY_RELEASE_LEVEL_ALPHA': 'a', 43 'PY_RELEASE_LEVEL_BETA': 'b', 44 'PY_RELEASE_LEVEL_GAMMA': 'rc', 45 } 46 if level != 'PY_RELEASE_LEVEL_FINAL': 47 release += suffixes[level] + str(int(d['PY_RELEASE_SERIAL'])) 48 return version, release 49 50 51def get_sys_version_info(): 52 major, minor, micro, level, serial = sys.version_info 53 release = version = '%s.%s' % (major, minor) 54 if micro: 55 release += '.%s' % micro 56 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() 66 print >>sys.stderr, 'Can\'t get version info from Include/patchlevel.h, ' \ 67 'using version of this interpreter (%s).' % release 68 return version, release 69 70if __name__ == '__main__': 71 print(get_header_version_info('.')[1]) 72