-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_version.py
executable file
·162 lines (133 loc) · 4.22 KB
/
change_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
"""
Custom script for changing the version of the current package
"""
import argparse
import shutil
import subprocess
import sys
import warnings
from pathlib import Path
from typing import Union
from packaging.version import Version
def change_version(version_file: Union[str, Path]):
"""
Function used to change the version number of the current package
Parameters
----------
version_file : str or Path
relative path to the file containing the package version
"""
current_version = Version(Path(version_file).read_text(encoding="utf-8").strip())
parser = argparse.ArgumentParser(
description="Change the version of a package",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
examples:
To change the version to X.Y.Z while automatically commiting the changes,
without prompts, run:
./change_version.py --git --yes X.Y.Z""",
)
parser.add_argument(
"version",
nargs="?",
default=None,
help="The version we wish to change the package to",
)
parser.add_argument(
"--git",
action="store_true",
help="Automatically commit and tag changes to the version",
)
parser.add_argument(
"--yes",
action="store_true",
help="Do not prompt anything, assume the answer is always positive",
)
parsed_args = parser.parse_args()
if not parsed_args.version:
print(f"Current version: {current_version}")
new_version_input = input("Enter the new version:")
else:
new_version_input = str(parsed_args.version)
new_version = Version(new_version_input)
print(f"Proposed change: {current_version} -> {new_version}")
if new_version < current_version:
warnings.warn(
"The new version appears to be lower than the current one "
"(for details of the comparison, see PEP 440)"
)
if not parsed_args.yes:
response = input("Do you want to proceed? [Y/n]")
else:
response = "y"
valid_response_yes = ["y", "yes"]
valid_response_no = ["n", "no"]
if response.lower() not in valid_response_yes + valid_response_no:
print("Unable to understand input, exiting...", file=sys.stderr)
return 1
if response.lower() in valid_response_yes:
Path(version_file).write_text(f"{new_version}\n", encoding="utf-8")
subprocess.run(
[
sys.executable,
"-m",
"poetry",
"version",
str(new_version),
],
check=True,
)
if parsed_args.git:
git_location = shutil.which("git")
git_location = (
git_location if git_location is not None else "/usr/bin/python3"
)
# the script should fail if the working tree is not clean
is_clean = subprocess.run(
[
git_location,
"diff",
"--staged",
],
check=True,
capture_output=True,
)
if is_clean.stdout:
raise Exception(
"The current git staging area is not empty, "
"please unstage any changes using `git reset .` before continuing"
)
subprocess.run(
[
git_location,
"add",
"--update",
"--",
"pyproject.toml",
str(version_file),
],
check=True,
)
subprocess.run(
[
git_location,
"commit",
"--message",
"Bumped version",
],
check=True,
)
subprocess.run(
[
git_location,
"tag",
str(new_version),
],
check=True,
)
else:
print("Nothing to do")
return 0
if __name__ == "__main__":
sys.exit(change_version("fitk/VERSION.txt"))