-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_version_py.py
59 lines (46 loc) · 1.42 KB
/
create_version_py.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
#!/usr/bin/env python
"""
Create version.py for a package
Example:
$ create_version_py.py "# mcvine version" ~/dv/mcvine/mcvine ~/dv/mcvine/mcvine/version.py
"""
template = """
%(banner)s
version = %(version)r
git_revision = %(git_revision)r
"""
import subprocess as sp, os, sys
def get_version_from_git():
if not os.path.isdir(".git"):
raise IOError("not a git repo")
args = ["git", "describe", "--tags", "--dirty", "--always"]
p = sp.Popen(args, stdout=sp.PIPE)
stdout = p.communicate()[0]
if p.returncode != 0:
raise RuntimeError("cmd %r failed" % ' '.join(args))
# output is like 1.0-31-ge63953d
if sys.version_info>=(3,0):
stdout = stdout.decode()
ver = stdout.strip().split('-')[0]
return ver
def get_git_revision():
if not os.path.isdir(".git"):
raise IOError("not a git repo")
args = ["git", "rev-parse", "HEAD"]
p = sp.Popen(args, stdout=sp.PIPE)
stdout = p.communicate()[0]
if p.returncode != 0:
raise RuntimeError("cmd %r failed" % ' '.join(args))
s = stdout.strip()
if sys.version_info >= (3,0) and isinstance(s, bytes):
s = s.decode()
return s
def main():
banner, srcdir, outpath = sys.argv[1:]
os.chdir(srcdir)
version = get_version_from_git()
git_revision = get_git_revision()
content = template % locals()
open(outpath, 'wt').write(content)
return
if __name__ == '__main__': main()