-
Notifications
You must be signed in to change notification settings - Fork 200
/
release.py
143 lines (126 loc) · 4.34 KB
/
release.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
136
137
138
139
140
141
142
143
import argparse
import json
import os
import subprocess
import sys
def createRelease():
if not inBikeshedRoot():
print("Run this command from inside the bikeshed root folder.")
sys.exit(1)
treeDirty = subprocess.check_output("git diff --stat", shell=True).decode(encoding="utf-8") != ""
if treeDirty:
print("Working tree is dirty. Finish committing files or stash, then try again.")
sys.exit(1)
with open("bikeshed/semver.txt", encoding="utf-8") as fh:
currentVersion = fh.read().strip()
semver = parseSemver(currentVersion)
try:
with open("secrets.json", encoding="utf-8") as fh:
secrets = json.load(fh)
except OSError:
print("Error trying to load the secrets.json file.")
raise
args = argparse.ArgumentParser(description="Releases a new Bikeshed version to pypi.org.")
args.add_argument(
"bump",
choices=["break", "feature", "bugfix"],
help="Which type of release is this?",
)
args.add_argument(
"--test",
dest="test",
action="store_true",
help="Upload to test.pypi.org instead.",
)
args.add_argument(
"--version",
dest="version",
nargs=3,
type=int,
)
options, _ = args.parse_known_args()
# Bump the semver
if options.version:
semver = options.version
else:
if options.bump == "break":
semver[0] += 1
semver[1] = 0
semver[2] = 0
elif options.bump == "feature":
semver[1] += 1
semver[2] = 0
elif options.bump == "bugfix":
semver[2] += 1
newVersion = ".".join(str(x) for x in semver)
print(f"Bumping to {newVersion}...")
# Update the hash-version
bikeshedVersion = (
subprocess.check_output(r"git log -1 --format='Bikeshed version %h, updated %cd'", shell=True)
.decode(encoding="utf-8")
.strip()
)
with open("bikeshed/spec-data/readonly/bikeshed-version.txt", "w", encoding="utf-8") as fh:
fh.write(bikeshedVersion)
# Update the semver
with open("bikeshed/semver.txt", "w", encoding="utf-8") as fh:
fh.write(newVersion)
try:
# Clear out the build artifacts, build it, upload, and clean up again.
subprocess.call("rm -r build dist", shell=True)
subprocess.check_call("python setup.py sdist", shell=True)
if options.test:
subprocess.check_call(
" ".join(
[
"twine upload",
"--repository-url https://test.pypi.org/legacy/",
"--username __token__",
"--password",
secrets["test.pypi.org release key"],
"dist/*",
]
),
shell=True,
)
else:
subprocess.check_call(
" ".join(
[
"twine upload",
"--username __token__",
"--password",
secrets["pypi.org release key"],
"dist/*",
]
),
shell=True,
)
subprocess.call("rm -r build dist", shell=True)
except:
# roll back the semver
with open("bikeshed/semver.txt", "w", encoding="utf-8") as fh:
fh.write(currentVersion)
raise
# Clean up with a final commit of the changed version files
subprocess.check_call(
"git add bikeshed/semver.txt bikeshed/spec-data/readonly/bikeshed-version.txt",
shell=True,
)
subprocess.check_call(f"git commit -m 'Bump semver to {newVersion}'", shell=True)
subprocess.check_call("git push", shell=True)
def inBikeshedRoot():
# Checks whether the cwd is in the Bikeshed root
try:
remotes = subprocess.check_output("git remote -v", stderr=subprocess.DEVNULL, shell=True).decode("utf-8")
if "bikeshed" in remotes:
return os.path.isdir(".git")
else:
return False
except subprocess.CalledProcessError:
return False
def parseSemver(s):
# TODO: replace with the semver module
return [int(x) for x in s.strip().split(".")]
if __name__ == "__main__":
createRelease()