-
Notifications
You must be signed in to change notification settings - Fork 1
/
do_release.py
106 lines (86 loc) · 2.83 KB
/
do_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
import argparse
import subprocess
import re
import os
import sys
from typing import Tuple
from pathlib import Path
from setuptools_scm import get_version
def get_active_branch_name():
head_dir = Path(".") / ".git" / "HEAD"
with head_dir.open("r") as f:
content = f.read().splitlines()
for line in content:
if line[0:4] == "ref:":
return line.partition("refs/heads/")[2]
def define_parser() -> argparse.ArgumentParser:
p = argparse.ArgumentParser(
description="Automatically create and push tag so CI/CD pipeline publishes a release"
)
choices = ("major", "minor", "patch")
p.add_argument(
"type",
type=str,
help="Type of release to perform",
choices=choices,
)
p.add_argument(
"-s",
"--skip-increment",
action="store_true",
help="Flag indicating if incrementing the version should be skipped. "
+ "If this is passed then the type is irrelevant.",
)
default = "Automatic release"
p.add_argument(
"-m",
"--message",
type=str,
default=default,
help="Message for the release (Doesn't seem to be used in CI/CD). The default is: {:s}".format(
default
),
)
return p
if __name__ == "__main__":
args = define_parser().parse_args()
version_parts = get_version(
root=".", relative_to=__file__, version_scheme="no-guess-dev"
).split(".")
major = int(version_parts[0])
minor = int(version_parts[1])
patch = int(version_parts[2])
print("Current version: {:d}.{:d}.{:d}".format(major, minor, patch))
cur_branch = get_active_branch_name()
if cur_branch.lower() != "master":
print(
"WARN: Not on master branch ({:s}), checkout to master branch for release".format(
cur_branch
)
)
sys.exit(-1)
if not args.skip_increment:
if args.type == "major":
major += 1
minor = 0
patch = 0
elif args.type == "minor":
minor += 1
patch = 0
elif args.type == "patch":
patch += 1
else:
raise RuntimeError("Invalid type: {} should not be here".format(args.type))
version_str = "v{:d}.{:d}.{:d}".format(major, minor, patch)
if args.skip_increment:
print("Skipping incrementing of version number!")
print("Removing old version tag")
cmd_str = f"git tag -d {version_str}"
print(cmd_str)
subprocess.run(cmd_str, shell=True)
print("Releasing version: {:s}".format(version_str[1:]))
cmd_str = "git tag -a {:s} -m '{:s}'".format(version_str, args.message)
print(cmd_str)
subprocess.run(cmd_str, shell=True)
cmd_str = "git push origin {:s}".format(version_str)
subprocess.run(cmd_str, shell=True)