setup.py 2.54 KB
Newer Older
1 2
from __future__ import print_function

3
import os
4
import sys
5
from subprocess import check_output
6

7 8
from setuptools import find_packages, setup

9

10
def get_version():
11
    """Determine the most appropriate version number for this package."""
12 13

    # prefer explicit version provided by (docker) build environment
14
    if os.path.exists('version'):
15 16 17 18 19
        version = open('version').read().strip()
        # ignore empty version file
        if version:
            print('Found version in file', file=sys.stderr)
            return version
20

21
    # try to use git tag if building python package
22
    try:
23
        # get closest tag version
24 25
        tag_version = check_output(["git", "describe", "--tags", "--abbrev=0"]).rstrip().decode()
        # determine if there has been development beyond the latest tagged commit
26
        dirty = bool(check_output(["git", "status", "--porcelain"]).strip())
27
        unreleased = bool(check_output(["git", "rev-list", tag_version + ".."]).strip())
28

29 30
        # there are unsaved changes
        if dirty:
31
            print('Repo has unsaved changes, versioning as development', file=sys.stderr)
32
            return tag_version + '.dev0'
33

34 35
        # the verion is commits ahead of latest tagged release
        if unreleased:
36 37
            print('Found commits after last release, versioning with latest sha', file=sys.stderr)

38 39 40 41 42 43 44
            # append git sha to version
            return tag_version + '+' + check_output("git rev-parse --short HEAD".split()).strip().decode()

        return tag_version
    except Exception as e:
        print("Failed to acquire version info from git: {e}".format(e=e), file=sys.stderr)
        return '0.0.0'
45 46


47
def requirements(extra=None):
48 49
    """Return list of required package names from requirements.txt."""
    # strip trailing comments, and extract package name from git urls.
50 51 52 53
    if extra:
        filename = 'requirements.' + extra + '.txt'
    else:
        filename = 'requirements.txt'
54 55 56 57 58
    requirements = [r.strip().split(' ', 1)[0].split('egg=', 1)[-1]
                    for r in open(filename) if not r.startswith('#')]
    return requirements


59 60
setup(
    name='failmap-admin',
61
    version=get_version(),
Johan Bloemberg's avatar
Johan Bloemberg committed
62
    packages=find_packages(),
63 64 65
    install_requires=requirements(),
    # allow extra packages to be installed, eg: `pip install -e .[deploy]`
    extras_require={
66 67
        'development': requirements(extra='dev'),
        'deploy': requirements(extra='deploy'),
68
    },
Johan Bloemberg's avatar
Johan Bloemberg committed
69 70 71 72
    entry_points={
        'console_scripts': [
            'failmap-admin = failmap_admin.manage:main',
        ],
Johan Bloemberg's avatar
Johan Bloemberg committed
73 74
    },
    include_package_data=True,
75
)