-
Notifications
You must be signed in to change notification settings - Fork 647
/
update-version.py
100 lines (82 loc) · 2.5 KB
/
update-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
#
# this updates the (tagged) version of the software
#
# it will only update the "minor" version (e.g. 0.12.* -> 0.13.0)
#
# Any "options" are hard-coded in here (e.g. the GnuPG key to use)
#
import sys
import time
from datetime import datetime
from dulwich.repo import Repo
from dulwich.porcelain import (
tag_list,
tag_create,
status,
)
from twisted.internet.task import (
react,
)
from twisted.internet.defer import (
ensureDeferred,
)
author = "meejah <[email protected]>"
def existing_tags(git):
versions = [
tuple(map(int, v.decode("utf8").split(".")))
for v in tag_list(git)
]
return versions
def create_new_version(git, only_patch):
versions = existing_tags(git)
major, minor, patch = sorted(versions)[-1]
if only_patch:
next_version = "{}.{}.{}".format(major, minor, patch + 1)
else:
next_version = "{}.{}.{}".format(major, minor + 1, 0)
return next_version
async def main(reactor):
git = Repo(".")
# including untracked files can be very slow (if there are lots,
# like in virtualenvs) and we don't care anyway
st = status(git, untracked_files="no")
if any(st.staged.values()) or st.unstaged:
print("unclean checkout; aborting")
raise SystemExit(1)
for arg in sys.argv[1:]:
if arg not in ("--no-tag", "--patch"):
print("unknown arg: {}".format(arg))
raise SystemExit(2)
v = create_new_version(git, "--patch" in sys.argv)
if "--no-tag" in sys.argv:
print(v)
return
print("Latest version: {}.{}.{}".format(*sorted(existing_tags(git))[-1]))
print("New tag will be {}".format(v))
# the "tag time" is seconds from the epoch .. we quantize these to
# the start of the day in question, in UTC.
now = datetime.now()
s = now.utctimetuple()
ts = int(
time.mktime(
time.struct_time((
s.tm_year, s.tm_mon, s.tm_mday, 0, 0, 0, 0, s.tm_yday, 0
))
)
)
tag_create(
repo=git,
tag=v.encode("utf8"),
author=author.encode("utf8"),
message="release magic-wormhole-{}".format(v).encode("utf8"),
annotated=True,
objectish=b"HEAD",
sign=author.encode("utf8"),
tag_time=ts,
tag_timezone=0,
)
print("Tag created locally, it is not pushed")
print("To push it run something like:")
print(" git push origin {}".format(v))
if __name__ == "__main__":
react(lambda r: ensureDeferred(main(r)))