-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.py
99 lines (72 loc) · 2.68 KB
/
version.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Gets the current version number.
If in a git repository, it is the current git tag.
Otherwise it is the one contained in the PKG-INFO file.
To use this script, simply import it in your setup.py file
and use the results of get_version() as your package version:
from version import *
setup(
...
version=get_version(),
...
)
"""
__all__ = ('get_version')
import os.path
import re
from subprocess import Popen, PIPE
VERSION_RE = re.compile('^Version: (.+)$', re.M)
def call_git_describe():
"""Determine package version from Git describe command"""
cmd = 'git describe --abbrev --tags --match v[0-9]*'.split()
print(' '.join(cmd))
git = Popen(cmd, stdout=PIPE, stderr=PIPE)
(stdout, _) = git.communicate()
return stdout.strip()
def write_pkg_info():
"""Write package metadata PKG-INFO file"""
if os.path.isfile('PKG-INFO'):
return
project_path = os.path.abspath(os.path.dirname(__file__))
try:
version = re.match(
r".*-v([\d\.]+-[^-]+-g[^/]+)",
project_path).group(1)
except AttributeError:
version = '0.0'
print("{}: Writing version info to: {}".format(
__file__, os.path.abspath('PKG-INFO')))
with open(os.path.join(project_path, 'PKG-INFO'), 'w') as pkginfo:
pkginfo.write("Metadata-Version: 1.0\n")
pkginfo.write("Name: information-package-tools\n")
pkginfo.write("Version: %s\n" % version)
pkginfo.write("Summary: UNKNOWN\n")
pkginfo.write("Home-page: UNKNOWN\n")
pkginfo.write("Author: UNKNOWN\n")
pkginfo.write("Author-email: UNKNOWN\n")
pkginfo.write("License: UNKNOWN\n")
pkginfo.write("Description: UNKNOWN\n")
pkginfo.write("Platform: UNKNOWN\n")
def get_version():
"""Determine version number for the project"""
project_path = os.path.dirname(__file__)
git_repo_path = os.path.join(project_path, '../../.git')
if os.path.isdir(git_repo_path):
# Get the version using "git describe".
version_git = call_git_describe()
# PEP 386 compatibility
if version_git:
version = "{}-{}".format(
'.post'.join(version_git.split('-')[:2]),
'-'.join(version_git.split('-')[2:])
)
print("Version number from GIT repository:", version)
else:
write_pkg_info()
# Extract the version from the PKG-INFO file.
with open(os.path.join(project_path, 'PKG-INFO')) as pkginfo:
version = VERSION_RE.search(pkginfo.read()).group(1)
print("Version number from PKG-INFO:", version)
return version
if __name__ == '__main__':
print(get_version())