From 89f7f12f9226a702a52a04a6e79053b8b7a29050 Mon Sep 17 00:00:00 2001 From: kevgliss Date: Tue, 1 Dec 2015 08:33:37 -0800 Subject: [PATCH] adding version.py --- docs/version.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 docs/version.py diff --git a/docs/version.py b/docs/version.py new file mode 100644 index 00000000..9e02db92 --- /dev/null +++ b/docs/version.py @@ -0,0 +1,45 @@ +# Source: https://github.com/Changaco/version.py + +from os.path import dirname, isdir, join +import re +from subprocess import CalledProcessError, check_output + + +PREFIX = '' + +tag_re = re.compile(r'\btag: %s([0-9][^,]*)\b' % PREFIX) +version_re = re.compile('^Version: (.+)$', re.M) + + +def get_version(): + # Return the version if it has been injected into the file by git-archive + version = tag_re.search('$Format:%D$') + if version: + return version.group(1) + + d = dirname(__file__) + + if isdir(join(d, '.git')): + # Get the version using "git describe". + cmd = 'git describe --tags --match %s[0-9]* --dirty' % PREFIX + try: + version = check_output(cmd.split()).decode().strip()[len(PREFIX):] + except CalledProcessError: + raise RuntimeError('Unable to get version number from git tags') + + # PEP 440 compatibility + if '-' in version: + if version.endswith('-dirty'): + raise RuntimeError('The working tree is dirty') + version = '.post'.join(version.split('-')[:2]) + + else: + # Extract the version from the PKG-INFO file. + with open(join(d, 'PKG-INFO')) as f: + version = version_re.search(f.read()).group(1) + + return version + + +if __name__ == '__main__': + print(get_version())