forked from hohlraum/gdsCAD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
git_version.py
107 lines (89 loc) · 3.04 KB
/
git_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
100
101
102
103
104
105
106
107
# -*- coding: utf-8 -*-
# Author: Douglas Creager <dcreager@dcreager.net>
# Modified: Andrew Mark
#
# Updates the package version number to reflect the git tag of the current commit.
#
# There are two normal use cases:
# python setup.py sdist: Building a source distribution. This is expected to
# take place in a folder tracked by git. The return value of "git --tags"
# is taken as the version number, and this is stored in version_file
# as part of the distribution.
# python setup.py install: Here the version number is taken from the value
# stored in version_file
#
# The file version_file should not be tracked. Doing so will lead to version
# recursion, since it is updated only after the latest version has been
# committed and tagged.
#
# To use this script, simply import it your setup.py file, and use the
# results of get_version() as your package version, and add the locally defined
# command 'sdist' as a cmdclass:
#
# from git_version import get_version, sdist
#
# setup(
# version=get_git_version(),
# cmdclass={"sdist": sdist },
# .
# .
# )
__all__ = ("get_version",)
from distutils.command.sdist import sdist as _sdist
from subprocess import Popen, PIPE
import re
version_file = 'gdsCAD/_version.py'
def git_version():
"""Return the git tag of the current commit"""
try:
p = Popen(['git', 'describe', '--tags'],
stdout=PIPE, stderr=PIPE)
p.stderr.close()
line = p.stdout.readlines()[0]
return line.strip()
except:
return None
def file_version():
"""Return the version number stored in _version.py"""
try:
f = open(version_file)
except EnvironmentError:
return None
for line in f.readlines():
mo = re.match("__version__ = '([^']+)'", line)
if mo:
ver = mo.group(1)
return ver
return None
VERSION_PY = """
# This version number is untracked and may not be up to date.
# It is updated during source builds.
__version__ = '%s'
"""
def update_f_version(new_ver):
"""Change the version number stored in _version.py"""
with open(version_file, "w") as f:
f.write(VERSION_PY % new_ver)
class sdist(_sdist):
"""New sdist command that updates the version number stored in _version.py"""
def run(self):
version = get_version()
update_f_version(version)
self.distribution.metadata.version = version
return _sdist.run(self)
def get_version():
# Read in the version that's currently in RELEASE-VERSION.
f_version = file_version()
# First try to get the current version using “git describe”.
version = git_version()
# If that doesn't work, fall back on the value that's in
# RELEASE-VERSION.
if version is None:
version = f_version
# If we still don't have anything, that's an error.
if version is None:
raise ValueError("Cannot find the version number!")
# Finally, return the current version.
return version
if __name__ == "__main__":
print get_version()