-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathrev_info.py
executable file
·72 lines (62 loc) · 2.31 KB
/
rev_info.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
#!/usr/bin/env python3
import argparse
import datetime
import subprocess
def git_short_rev():
try:
return subprocess.check_output([
'git',
'rev-parse',
'--short',
'HEAD',
]).decode('utf-8').strip()
except Exception:
raise RuntimeError("Could not read git revision. Make sure you have git installed and you're working with a git clone of the repository.")
def current_date():
return datetime.date.today().strftime('%Y-%m-%d')
def git_date(short=True):
try:
iso = subprocess.check_output([
'git',
'log',
'-1',
'--format=%ci',
'HEAD',
]).decode('utf-8').strip()
if short:
return iso.split(' ')[0]
else:
return iso
except Exception:
raise RuntimeError("Could not read git commit date. Make sure you have git installed and you're working with a git clone of the repository.")
def git_release_version(search_prefix, fallback=None):
try:
tags = subprocess.check_output([
'git',
'tag',
'--points-at',
'HEAD',
]).decode('utf-8').splitlines()
for tag in tags:
if tag.startswith(search_prefix):
return tag[len(search_prefix):]
return fallback
except Exception:
raise RuntimeError("Could not read git release tags. Make sure you have git installed and you're working with a git clone of the repository.")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(required=True, dest='option')
parser_git_short_rev = subparsers.add_parser('git_short_rev')
parser_git_date = subparsers.add_parser('git_date')
parser_git_date.add_argument('--short', action='store_true')
parser_git_release_version = subparsers.add_parser('git_release_version')
parser_git_release_version.add_argument('search_prefix')
args = parser.parse_args()
if args.option == 'git_short_rev':
print(git_short_rev())
elif args.option == 'git_date':
print(git_date(short=args.short))
elif args.option == 'git_release_version':
print(git_release_version(args.search_prefix, fallback='v#.#'))
else:
raise RuntimeError('Unexpected option')