forked from LedFx/LedFx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.py
105 lines (85 loc) · 3.03 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
#!/usr/bin/env python3
import argparse
import re
import shutil
import subprocess
from ledfx.consts import MAJOR_VERSION, MICRO_VERSION, MINOR_VERSION
def write_version(major, minor, micro):
with open("ledfx/consts.py") as fil:
content = fil.read()
content = re.sub(
"MAJOR_VERSION = .*\n", "MAJOR_VERSION = {}\n".format(major), content
)
content = re.sub(
"MINOR_VERSION = .*\n", "MINOR_VERSION = {}\n".format(minor), content
)
content = re.sub(
"MICRO_VERSION = .*\n", "MICRO_VERSION = {}\n".format(micro), content
)
with open("ledfx/consts.py", "wt") as fil:
content = fil.write(content)
def execute_command(command):
return subprocess.check_output(command.split(" ")).decode("UTF-8").rstrip()
def main():
parser = argparse.ArgumentParser(
description="Release a new version of LedFx"
)
parser.add_argument(
"type", help="The type of release", choices=["major", "minor", "micro"]
)
parser.add_argument("branch", help="Branch", choices=["dev"])
parser.add_argument(
"--no-bump", action="store_true", help="Create a version bump commit."
)
arguments = parser.parse_args()
branch = arguments.branch
current_branch = execute_command("git rev-parse --abbrev-ref HEAD")
if current_branch != "dev": # Temporary sanity check
print("Releases may only be pushed from the dev branch at this time.")
if current_branch != "master":
print("Releases must be pushed from the master branch.")
return
current_commit = execute_command("git rev-parse HEAD")
# Push to dev branch only for now.
branch_commit = execute_command("git rev-parse dev@{upstream}")
if current_commit != branch_commit:
print("Release must be pushed when up-to-date with origin.")
return
git_diff = execute_command("git diff HEAD")
if git_diff:
print("Release must be pushed without any staged changes.")
return
if not arguments.no_bump:
# Bump the version based on the release type
major = MAJOR_VERSION
minor = MINOR_VERSION
micro = MICRO_VERSION
if arguments.type == "major":
major += 1
minor = 0
micro = 0
elif arguments.type == "minor":
minor += 1
micro = 0
elif arguments.type == "micro":
micro += 1
# Write the new version to consts.py
write_version(major, minor, micro)
subprocess.run(
[
"git",
"commit",
"-am",
"Version Bump for Release {}.{}.{}".format(
major, minor, micro
),
]
)
subprocess.run(["git", "push", "origin", branch])
shutil.rmtree("dist", ignore_errors=True)
subprocess.run(["python", "setup.py", "sdist", "bdist_wheel"])
subprocess.run(
["python", "-m", "twine", "upload", "dist/*", "--skip-existing"]
)
if __name__ == "__main__":
main()