forked from cutechess/cutechess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release-tool
executable file
·93 lines (62 loc) · 2.56 KB
/
release-tool
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
#!/usr/bin/env python3
# tool to help make consistent Cute Chess releases
import subprocess
import sys
import tempfile
import os
import re
def usage() -> None:
print("usage: release-tool <version>")
sys.exit(1)
def error(msg: str) -> None:
print("error:", msg, file=sys.stderr)
sys.exit(1)
def validate_version(ver: str) -> bool:
return re.match('^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)', ver)
def git_dir_exists() -> bool:
return os.path.isdir(f"{os.getcwd()}/.git")
def current_branch() -> str:
return subprocess.run(["git", "branch", "--show-current"],
stdout=subprocess.PIPE).stdout.decode().strip()
def release_exists(rel: str) -> bool:
return len(subprocess.run(["git", "tag", "-l", f"v{rel}"],
stdout=subprocess.PIPE).stdout.decode().strip()) != 0
def working_tree_clean() -> bool:
return subprocess.run(["git", "diff-index", "--quiet", "HEAD", "--"]).returncode == 0
def pull_remote_changes() -> bool:
return subprocess.run(["git", "pull", "--rebase", "--ff-only"]).returncode == 0
def update_version_file(new_version: str) -> bool:
try:
with open(".version", "w") as f:
f.write(new_version)
except (OSError, IOError):
return False
# before entering to this function, working tree is clean
# and ".version" should be already in the repo so
# "commit -a" should be safe
return subprocess.run(["git", "commit", "-a", "-m",
f"version {new_version}"]).returncode == 0
if __name__ == "__main__":
if len(sys.argv) != 2:
usage()
new_version = sys.argv[1]
if not validate_version(new_version):
error(f"{new_version} does not conform to the semver standard MAJOR.MINOR.PATCH")
if not git_dir_exists():
error("'.git' does not exist; move to the source root")
if current_branch() != "master":
error("you're not in the 'master' branch")
if not working_tree_clean():
error("your working tree is not clean; commit or unstage changes")
if release_exists(new_version):
error(f"{new_version} already exists")
if not pull_remote_changes():
error("unable to pull remote changes")
if not update_version_file(new_version):
error("unable to update '.version' file")
with tempfile.NamedTemporaryFile() as f:
basename = os.path.basename(os.getcwd())
f.write(f"{basename} {new_version}".encode())
f.flush()
subprocess.run(["git", "tag", "-e", "-F", f.name, "-a", f"v{new_version}"])
print(new_version)