-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathversioning.py
70 lines (53 loc) · 1.95 KB
/
versioning.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
#!/usr/bin/env python
""" Git Versioning Script
Will transform stdin to expand some keywords with git version/author/date information.
Specify --clean to remove this information before commit.
Setup:
1. Copy versioning.py into your git repository
2. Run:
git config filter.versioning.smudge 'python versioning.py'
git config filter.versioning.clean 'python versioning.py --clean'
echo 'version.py filter=versioning' >> .gitattributes
git add versioning.py
3. add a version.py file with this contents:
__version__ = ""
__author__ = ""
__email__ = ""
__date__ = ""
"""
import sys
import subprocess
import re
def main():
clean = False
if len(sys.argv) > 1:
if sys.argv[1] == '--clean':
clean = True
# initialise empty here. Otherwise: forkbomb through the git calls.
subst_list = {
"version": "",
"date": "",
"author": "",
"email": ""
}
for line in sys.stdin:
if not clean:
subst_list = {
# '--dirty' could be added to the following, too, but is not supported everywhere
"version": subprocess.check_output(['git', 'describe', '--always', '--tags', '--long']),
"date": subprocess.check_output(['git', 'log', '--pretty=format:"%ad"', '-1']),
"author": subprocess.check_output(['git', 'log', '--pretty=format:"%an"', '-1']),
"email": subprocess.check_output(['git', 'log', '--pretty=format:"%ae"', '-1'])
}
for k, v in iter(subst_list.items()):
v = re.sub(r'[\n\r\t"\']', "", v)
rexp = "__%s__\s*=[\s'\"]+" % k
line = re.sub(rexp, "__%s__ = \"%s\"\n" % (k, v), line)
sys.stdout.write(line)
else:
for k in subst_list:
rexp = "__%s__\s*=.*" % k
line = re.sub(rexp, "__%s__ = \"\"" % k, line)
sys.stdout.write(line)
if __name__ == "__main__":
main()