diff --git a/.gitignore b/.gitignore index 861fc73..052defc 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ build dist htmlcov __pycache__ +_version.py diff --git a/setup.py b/setup.py index 91d53db..19c9adb 100755 --- a/setup.py +++ b/setup.py @@ -1,14 +1,30 @@ #!/usr/bin/env python -import os from io import open from setuptools import find_packages, setup +import os +import re +import subprocess def get_version(prefix): - import re - with open(os.path.join(prefix, '__init__.py')) as fd: - metadata = dict(re.findall("__([a-z]+)__ = '([^']+)'", fd.read())) + if os.path.exists('.git'): + parts = subprocess.check_output(['git', 'describe', '--tags']).decode().strip().split('-') + version = '{}.{}+{}'.format(*parts) + version_py = "__version__ = '{}'".format(version) + _version = os.path.join(prefix, '_version.py') + if not os.path.exists(_version) or open(_version).read().strip() != version_py: + with open(_version, 'w') as fd: + fd.write(version_py) + return version + else: + for f in ('_version.py', '__init__.py'): + f = os.path.join(prefix, f) + if os.path.exists(f): + with open(f) as fd: + metadata = dict(re.findall("__([a-z]+)__ = '([^']+)'", fd.read())) + if 'version' in metadata: + break return metadata['version'] diff --git a/shared/people/__init__.py b/shared/people/__init__.py index cac05ea..2f83f35 100644 --- a/shared/people/__init__.py +++ b/shared/people/__init__.py @@ -1,32 +1,11 @@ -import subprocess - +__version__ = '0.0' try: - git_describe = subprocess.check_output(['git', 'describe', '--tags'], universal_newlines=True, stderr=subprocess.DEVNULL).strip() - # v0.2-69-g181f854 - if git_describe[0] in "vr": - git_describe = git_describe[1:] - parts = git_describe.split("-") - VERSION = parts[0].split(".")[:3] - VERSION += [0] * (3 - len(VERSION)) - git_version = parts[1:] - -except subprocess.CalledProcessError: - # Not yet tagged - VERSION = [0, 0, 0] - git_version = [] - try: - git_version = ( - subprocess.check_output(['git', 'rev-list', '--count', 'HEAD'], universal_newlines=True).strip(), - subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], universal_newlines=True).strip() - ) - except subprocess.CalledProcessError: - git_version = [] - - -# 1.3.0-64-g1f9a30 -__version__ = '-'.join(map(str, ['.'.join(map(str, VERSION)), *git_version])) -VERSION.extend(git_version) + from ._version import __version__ +except ImportError: + pass +VERSION = __version__.split('+') +VERSION = tuple(list(map(int, VERSION[0].split('.'))) + VERSION[1:]) default_app_config = 'people.apps.PeopleConfig'