-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.py
75 lines (63 loc) · 2.34 KB
/
deploy.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
#!/usr/bin/env python3
import sys
import shlex
import argparse
from pathlib import Path
import subprocess as sp
def main(args):
args.debug and print(args)
args.dry_run and print("Dry run active!")
folder = Path(__file__).parent
version_filename = folder / "VERSION"
version = open(version_filename, encoding="utf-8").readline().strip()
if args.show:
print(version)
return
major, minor, patch, *_ = version.split(".")
if "major" in args.field:
major = int(major) + 1
minor = 0
patch = 0
if "minor" in args.field:
minor = int(minor) + 1
patch = 0
if "patch" in args.field or not args.field:
patch = int(patch) + 1
new_version = f"{major}.{minor}.{patch}"
if args.dry_run:
print(f"The new version that would be written: {new_version}")
return
git_status_output = sp.run("git status".split(), capture_output=True).stdout
if b"Changes not staged for commit:" in git_status_output:
print("Repo has uncommitted changes. Cannot continue")
sys.exit(1)
if b"Untracked files:" in git_status_output:
print("Repo has untracked files. Cannot continue")
sys.exit(1)
with open(version_filename, "w", encoding="utf-8") as f:
f.write(new_version)
sp.run(f"git add {version_filename}".split(), cwd=folder)
sp.run(shlex.split(f"git commit -m 'Bump version to {new_version}'"), cwd=folder)
sp.run(f"git tag v{new_version}".split(), cwd=folder)
if args.push_git:
sp.run("git push".split(), cwd=folder)
sp.run("git push --tags".split(), cwd=folder)
sp.run(f"{sys.executable} setup.py bdist_wheel sdist".split(), cwd=folder)
if args.push_pypi:
sp.run(f"twine upload --skip-existing dist/*{new_version}*", cwd=folder)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--debug", action="store_true")
parser.add_argument("--show", action="store_true")
parser.add_argument("--dry-run", action="store_true")
parser.add_argument("--push-git", action="store_true")
parser.add_argument("--push-pypi", action="store_true")
parser.add_argument(
"field",
choices=["major", "minor", "patch"],
default="patch",
const="patch",
nargs="?",
)
args = parser.parse_args()
main(args)