-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
bump_version.py
113 lines (84 loc) · 3.01 KB
/
bump_version.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
import argparse
import io
import pathlib
import parver # type: ignore
REPO_ROOT = pathlib.Path(__file__).parent
VERSION = REPO_ROOT / pathlib.Path("VERSION")
def get_current_version() -> parver.Version:
with open(VERSION, "rt") as f:
return parver.Version.parse(f.read().strip())
def write_version(version: parver.Version):
with open(VERSION, "wt") as f:
f.write(str(version))
def update_package_version(version: parver.Version):
new_init = io.StringIO()
version_set = False
init = REPO_ROOT / "src" / "tzdata" / "__init__.py"
with open(init, "rt") as f:
for line in f:
if not version_set and line.startswith("__version__"):
line = f'__version__ = "{version}"\n'
version_set = True
new_init.write(line)
if not version_set:
raise ValueError("Version not found in __init__.py!")
new_init.seek(0)
with open(init, "wt") as f:
f.write(new_init.read())
def bump_version(version: parver.Version, args) -> parver.Version:
if args.release:
return version.base_version()
if args.dev:
if args.to is not None:
return version.replace(dev=args.to)
else:
return version.bump_dev()
version = version.replace(dev=None)
if args.post:
if args.to is not None:
return version.replace(post=args.to)
else:
return version.bump_post()
if args.rc:
if version.is_postrelease:
version = version.replace(post=None)
if args.to is not None:
return version.replace(pre_tag="rc", pre=args.to)
else:
return version.bump_pre("rc")
return version
def main(args):
original_version = get_current_version()
bumped_version = bump_version(original_version, args)
print(f"{original_version} → {bumped_version}")
if not args.dry_run:
write_version(bumped_version)
update_package_version(bumped_version)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Manipulate the package version")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--rc", action="store_true", help="Bump the release candidate")
group.add_argument("--dev", action="store_true", help="Bump the dev version")
group.add_argument(
"--release",
action="store_true",
help="Bump from release candidate / dev to release",
)
group.add_argument(
"--post", action="store_true", help="Bump the post release version"
)
parser.add_argument(
"--to",
type=int,
default=None,
help="Set the specified component to a specific number",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Preview what the new version will be without writing any files.",
)
args = parser.parse_args()
if args.to is not None and args.release:
raise ValueError("Cannot combine --to and --release")
main(args)