-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathupdate_version.py
More file actions
149 lines (127 loc) · 4.94 KB
/
update_version.py
File metadata and controls
149 lines (127 loc) · 4.94 KB
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
"""Utility used to bump the version of the package."""
import argparse
import re
import sys
from pathlib import Path
REPLACE_CURR_VERSION = {}
REPLACE_NEXT_VERSION = {
"framework/pyproject.toml": ['version = "{version}"'],
"framework/docs/source/conf.py": [
'release = "{version}"',
".. |stable_flwr_version| replace:: {version}",
],
"examples/docs/source/conf.py": ['release = "{version}"'],
"baselines/docs/source/conf.py": ['release = "{version}"'],
"framework/docker/complete/compose.yml": ["FLWR_VERSION:-{version}"],
"framework/docker/distributed/client/compose.yml": ["FLWR_VERSION:-{version}"],
"framework/docker/distributed/server/compose.yml": ["FLWR_VERSION:-{version}"],
"framework/py/flwr/cli/new/templates/app/pyproject.*.toml.tpl": [
"flwr[simulation]>={version}",
],
}
EXAMPLES = {
"hub/apps/**/pyproject.toml": [
"flwr[simulation]>={version}",
"flwr[simulation]=={version}",
"flwr>={version}",
"flwr=={version}",
],
}
ROOT_DIR = Path(__file__).parents[2]
def _get_next_version(curr_version, increment):
"""Calculate the next version based on the type of release."""
major, minor, patch_version = map(int, curr_version.split("."))
if increment == "patch":
patch_version += 1
elif increment == "minor":
minor += 1
patch_version = 0
elif increment == "major":
major += 1
minor = 0
patch_version = 0
else:
raise ValueError(
"Invalid increment type. Must be 'major', 'minor', or 'patch'."
)
return f"{major}.{minor}.{patch_version}"
def _update_versions(file_patterns, replace_strings, new_version, check):
"""Update the version strings in the specified files."""
wrong = False
for pattern in file_patterns:
files = list(ROOT_DIR.glob(pattern))
for file_path in files:
if not file_path.is_file():
continue
content = file_path.read_text()
original_content = content
for s in replace_strings:
# Construct regex pattern to match any version number in the string
escaped_s = re.escape(s).replace(r"\{version\}", r"(\d+\.\d+\.\d+)")
regex_pattern = re.compile(escaped_s)
content = regex_pattern.sub(s.format(version=new_version), content)
if content != original_content:
wrong = True
if check:
print(f"{file_path} would be updated")
else:
file_path.write_text(content)
print(f"Updated {file_path}")
return wrong
if __name__ == "__main__":
# Search for the latest stable release version in the CHANGELOG
changelog_path = ROOT_DIR / "framework/docs/source/ref-changelog.md"
with changelog_path.open("r") as f:
for line in f:
if match := re.match(r"^## v(\d+\.\d+\.\d+).+", line):
break
parser = argparse.ArgumentParser(
description="Utility used to bump the version of the package."
)
parser.add_argument(
"--old_version",
help="Current (non-updated) version of the package, soon to be the old version.",
default=match.group(1) if match else None,
)
parser.add_argument(
"--check", action="store_true", help="Fails if any file would be modified."
)
parser.add_argument(
"--no_examples",
action="store_true",
help="Also modify flwr version in examples.",
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--patch", action="store_true", help="Increment the patch version."
)
group.add_argument(
"--major", action="store_true", help="Increment the major version."
)
args = parser.parse_args()
if not args.old_version:
raise ValueError("Version not found in conf.py, please provide current version")
# Determine the type of version increment
if args.major:
increment = "major"
elif args.patch:
increment = "patch"
else:
increment = "minor"
curr_version = _get_next_version(args.old_version, increment)
next_version = _get_next_version(curr_version, "minor")
wrong = False
# Update files with next version
for file_pattern, strings in REPLACE_NEXT_VERSION.items():
if not _update_versions([file_pattern], strings, next_version, args.check):
wrong = True
# Update files with current version
for file_pattern, strings in REPLACE_CURR_VERSION.items():
if not _update_versions([file_pattern], strings, curr_version, args.check):
wrong = True
if not args.no_examples:
for file_pattern, strings in EXAMPLES.items():
if not _update_versions([file_pattern], strings, curr_version, args.check):
wrong = True
if wrong and args.check:
sys.exit("Some version haven't been updated.")