forked from pyfidelity/setuptools-git-version
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetuptools_git_version_cc.py
More file actions
91 lines (65 loc) · 2.2 KB
/
setuptools_git_version_cc.py
File metadata and controls
91 lines (65 loc) · 2.2 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from pkg_resources import get_distribution
import subprocess
import sys
def get_git_version_cc(dist, attr, value):
try:
version = get_git_version()
except:
version = get_distribution(dist.get_name()).version
dist.metadata.version = version
def entry_point():
"""
"""
version = get_git_version();
sys.stdout.write("{}".format(version))
sys.exit(0)
def get_git_version():
"""
Computes the version of a git repository (in current path)
The version is computed assuming that the commit messages are formatted according
to SemVer and Conventional Commits. The version format is the following:
<major>.<minor>.<patch>-r<release>
The convention used here is the following
- 'breaking:' types increase major version (rather than 'refactor:')
- 'feat:' types increase minor version
- 'fix:' types increase patch version
- any other type that conforms to '<type>:' (where <type> can be chore, ci, test, ...)
:return: The version number as a string
"""
cmd = ['git', 'log', '--reverse','--pretty=format:%s']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
major = 1
minor = 0
patch = 0
release = 0
for line in p.stdout:
line = line.decode('utf-8')
if line.startswith("breaking"):
major += 1
minor = 0
patch = 0
release = 0
elif line.startswith("feat"):
minor += 1
patch = 0
release = 0
elif line.startswith("fix"):
patch += 1
release = 0
else:
release += 1
ver = '{}.{}.{}'.format(major, minor, patch)
if (release > 0):
ver = ver + '-r{}'.format(release)
return ver
if __name__ == "__main__":
# determine version from git
git_version = get_git_version()
# monkey-patch `setuptools.setup` to inject the git version
import setuptools
original_setup = setuptools.setup
def setup(version=None, *args, **kw):
return original_setup(version=git_version, *args, **kw)
setuptools.setup = setup
# import the packages's setup module
import setup