-
Notifications
You must be signed in to change notification settings - Fork 94
/
make-tag.py
executable file
·135 lines (111 loc) · 3.5 KB
/
make-tag.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
'''
Make a new release tag, performing a few checks.
Usage: make-tag.py <tag>
'''
import os
import subprocess
import re
import sys
import collections
import treehash512
GIT = os.getenv("GIT", "git")
# Full version specification
VersionSpec = collections.namedtuple('VersionSpec', ['major', 'minor', 'build', 'rc'])
def version_name(spec):
'''
Short version name for comparison.
'''
if not spec.build:
version = f"{spec.major}.{spec.minor}"
else:
version = f"{spec.major}.{spec.minor}.{spec.build}"
if spec.rc:
version += f"rc{spec.rc}"
return version
def parse_tag(tag):
'''
Parse a version tag. Valid version tags are
- v1.2
- v1.2.3
- v1.2rc3
- v1.2.3rc4
'''
m = re.match("^v([0-9]+)\.([0-9]+)(?:\.([0-9]+))?(?:rc([0-9])+)?$", tag)
if m is None:
print(f"Invalid tag {tag}", file=sys.stderr)
sys.exit(1)
major = m.group(1)
minor = m.group(2)
build = m.group(3)
rc = m.group(4)
# Check for x.y.z.0 or x.y.zrc0
if build == '0' or rc == '0':
print('rc or build cannot be specified as 0 (leave them out instead)', file=sys.stderr)
sys.exit(1)
# Implicitly, treat no rc as rc0 and no build as build 0
if build is None:
build = 0
if rc is None:
rc = 0
return VersionSpec(int(major), int(minor), int(build), int(rc))
def check_configure_ac(spec):
'''
Parse configure.ac and return
(major, minor, build, rc)
'''
info = {}
filename = 'configure.ac'
with open(filename) as f:
for line in f:
m = re.match("define\(_CLIENT_VERSION_([A-Z_]+), ([0-9a-z]+)\)", line)
if m:
info[m.group(1)] = m.group(2)
# check if IS_RELEASE is set
if info["IS_RELEASE"] != "true":
print(f'{filename}: IS_RELEASE is not set to true', file=sys.stderr)
sys.exit(1)
cfg_spec = VersionSpec(
int(info['MAJOR']),
int(info['MINOR']),
int(info['BUILD']),
int(info['RC']),
)
if cfg_spec != spec:
print(f"{filename}: Version from tag {version_name(spec)} doesn't match specified version {version_name(cfg_spec)}", file=sys.stderr)
sys.exit(1)
def main():
try:
tag = sys.argv[1]
except IndexError:
print("Usage: make-tag.py <tag>, e.g. v0.19.0 or v0.19.0rc3", file=sys.stderr)
sys.exit(1)
spec = parse_tag(tag)
# Check that the script is called from repo root
if not os.path.exists('.git'):
print('Execute this script at the root of the repository', file=sys.stderr)
sys.exit(1)
# Check if working directory clean
if subprocess.call([GIT, 'diff-index', '--quiet', 'HEAD']):
print('Git working directory is not clean. Commit changes first.', file=sys.stderr)
sys.exit(1)
# Check version components against configure.ac in git tree
check_configure_ac(spec)
# Generate base message
if not spec.build:
version = f"{spec.major}.{spec.minor}"
else:
version = f"{spec.major}.{spec.minor}.{spec.build}"
if spec.rc:
version += f" release candidate {spec.rc}"
else:
version += " final"
msg = 'Bitcoin Core ' + version + '\n'
# Add treehash header
msg += "\n"
msg += 'Tree-SHA512: ' + treehash512.tree_sha512sum() + '\n'
# Finally, make the tag
print(msg)
return subprocess.call([GIT, "tag", "-s", tag, "-m", msg])
if __name__ == '__main__':
sys.exit(main())