-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
76 lines (62 loc) · 2.24 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from distutils.core import setup
import os
import subprocess
class NotAGitRepo(Exception):
pass
class Git(object):
def __init__(self):
if not os.path.isdir(".git"):
raise NotAGitRepo()
def _exec(self, *args):
cmds = ["git"] + list(args)
output = subprocess.check_output(cmds)
return output
def latest_tag(self):
try:
return self._exec("describe", "--abbrev=0").decode('utf8').strip()
except subprocess.CalledProcessError:
return None
def branch_name(self):
return self._exec("rev-parse", "--abbrev-ref", "HEAD").decode('utf8').strip()
def commit_count_since(self, ref):
if ref:
return int(self._exec("rev-list", "--count", "HEAD", f"^{ref}").decode('utf8').strip())
else:
return int(self._exec("rev-list", "--count", "HEAD").decode('utf8').strip())
def _get_version_from_scm():
try:
git = Git()
tag = git.latest_tag()
if tag:
version = tag
commit_count = git.commit_count_since(tag)
else:
# no tags exist on the repo, so it's never been released
version = "0.0.0"
commit_count = git.commit_count_since(None)
if commit_count > 0:
# progress has been made since the last release
branch_name = git.branch_name()
if branch_name == "master" or branch_name.startswith("v"):
# master or maintenance branch
if tag:
# commit counts are added once tags exist
version += f".dev{commit_count}"
else:
# feature branch; all commits have the same package version
version += f"+{branch_name}"
with open("version.py", "wt") as f:
f.write(f"__version__='{version}'")
return version
except NotAGitRepo:
version = {}
with open("version.py") as f:
exec(f.read(), version)
return version["__version__"]
setup(
name='packageversiontest',
version=_get_version_from_scm(),
long_description=open('README.md').read(),
long_description_content_type="text/markdown",
py_modules=['packageversiontest'],
)