-
Notifications
You must be signed in to change notification settings - Fork 51
/
new_version.py
executable file
·185 lines (153 loc) · 5.93 KB
/
new_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python3
#
# Copyright 2021-2023 Axel Huebl
#
# This file is part of openPMD-api.
#
# This file is a maintainer tool to bump the versions inside openPMD-api's
# source directory at all places where necessary.
#
from pathlib import Path
import re
import sys
# Maintainer Inputs ###########################################################
print("""Hi there, this is an openPMD maintainer tool to update the source
code of openPMD-api to a new version.
For it to work, you need write access on the source directory and
you should be working in a clean git branch without ongoing
rebase/merge/conflict resolves and without unstaged changes.""")
# check source dir
# REPO_DIR = Path(__file__).parent.parent.parent.absolute()
REPO_DIR = Path(__file__).parent.absolute()
print(f"\nYour current source directory is: {REPO_DIR}")
REPLY = input("Are you sure you want to continue? [Y/n] ")
print()
if REPLY not in ["Y", "y", ""]:
print("You did not confirm with 'y', aborting.")
sys.exit(1)
MAJOR = input("MAJOR version? (e.g., 1) ")
MINOR = input("MINOR version? (e.g., 0) ")
PATCH = input("PATCH version? (e.g., 0) ")
SUFFIX = input("SUFFIX? (e.g., dev) ")
VERSION_STR = f"{MAJOR}.{MINOR}.{PATCH}"
VERSION_STR_SUFFIX = VERSION_STR + (f"-{SUFFIX}" if SUFFIX else "")
VERSION_STR_SUFFIX_WITH_DOT = VERSION_STR + (f".{SUFFIX}" if SUFFIX else "")
print()
print(f"Your new version is: {VERSION_STR_SUFFIX}")
# Recover the old version from the Readme.
# Do not use CMakeLists.txt as it might already contain the upcoming version
# code.
with open(str(REPO_DIR.joinpath("README.md")), encoding="utf-8") as f:
for line in f:
match = re.search(r"find_package.*openPMD *([^ ]*) *CONFIG\).*", line)
if match:
OLD_VERSION_STR_README = match.group(1)
break
with open(str(REPO_DIR.joinpath("CMakeLists.txt")), encoding="utf-8") as f:
for line in f:
match = re.search(r"project\(openPMD *VERSION *(.*)\)", line)
if match:
OLD_VERSION_STR_CMAKE = match.group(1)
break
OLD_VERSION_TAG = ""
with open(str(REPO_DIR.joinpath("include/openPMD/version.hpp")),
encoding="utf-8") as f:
for line in f:
match = re.search(r'#define OPENPMDAPI_VERSION_LABEL "([^"]+)"', line)
if match:
OLD_VERSION_TAG = match.group(1)
break
OLD_VERSION_SUFFIX = f"(-{OLD_VERSION_TAG})?" if OLD_VERSION_TAG else ""
# The order of the alternatives is important, since the Regex parser
# should greedily include the old version suffix
OLD_VERSION_STR = \
f"({re.escape(OLD_VERSION_STR_CMAKE)}{OLD_VERSION_SUFFIX})" + \
f"|({re.escape(OLD_VERSION_STR_README)})"
print(f"The old version is: {OLD_VERSION_STR}")
print()
REPLY = input("Is this information correct? Will now start updating! [y/N] ")
print()
if REPLY not in ["Y", "y", ""]:
print("You did not confirm with 'y', aborting.")
sys.exit(1)
# Ask for new #################################################################
print("""We will now run a few sed commands on your source directory.\n""")
# Updates #####################################################################
# run_test.sh (used also for Azure Pipelines)
cmakelists_path = str(REPO_DIR.joinpath("CMakeLists.txt"))
with open(cmakelists_path, encoding="utf-8") as f:
cmakelists_content = f.read()
cmakelists_content = re.sub(
r"^(project.*openPMD.*VERSION *)(.*)(\).*)$",
r"\g<1>{}\g<3>".format(VERSION_STR),
cmakelists_content,
flags=re.MULTILINE,
)
with open(cmakelists_path, "w", encoding="utf-8") as f:
f.write(cmakelists_content)
def generic_replace(filename, previous, after):
filename = str(REPO_DIR.joinpath(filename))
with open(filename, encoding="utf-8") as f:
content = f.read()
content = re.sub(previous, after, content, flags=re.MULTILINE)
with open(filename, "w", encoding="utf-8") as f:
f.write(content)
for file in [
"docs/source/dev/linking.rst",
"README.md",
]:
generic_replace(file, previous=OLD_VERSION_STR, after=VERSION_STR)
for file in ["CITATION.cff", "test/SerialIOTest.cpp"]:
generic_replace(file, previous=OLD_VERSION_STR, after=VERSION_STR_SUFFIX)
generic_replace(
"docs/source/index.rst",
previous=r"``0.13.1-[^`]*``",
after=f"``0.13.1-{VERSION_STR}``",
)
setup_py_path = str(REPO_DIR.joinpath("setup.py"))
with open(setup_py_path, encoding="utf-8") as f:
for line in f:
match = re.search(r"version='([^']+)',", line)
if match:
PREVIOUS_PIP_VERSION = match.group(1)
break
generic_replace("setup.py",
previous=PREVIOUS_PIP_VERSION,
after=VERSION_STR_SUFFIX_WITH_DOT)
generic_replace(
".github/workflows/windows.yml",
previous=f"{PREVIOUS_PIP_VERSION}0?",
after=(f"{VERSION_STR_SUFFIX_WITH_DOT}0"
if SUFFIX else VERSION_STR_SUFFIX_WITH_DOT),
)
generic_replace(
"docs/source/conf.py",
previous=r"^version.*=.*",
after=f"version = u'{VERSION_STR}'",
)
generic_replace(
"docs/source/conf.py",
previous=r"^release.*=.*",
after=f"release = u'{VERSION_STR_SUFFIX}'",
)
version_hpp_path = str(REPO_DIR.joinpath("include/openPMD/version.hpp"))
with open(version_hpp_path, encoding="utf-8") as f:
version_hpp_content = f.read()
def replace(key, value):
global version_hpp_content
version_hpp_content = re.sub(
r"^(#define OPENPMDAPI_VERSION_{}) .*$".format(re.escape(key)),
r"\1 {}".format(value),
version_hpp_content,
flags=re.MULTILINE,
)
replace("MAJOR", MAJOR)
replace("MINOR", MINOR)
replace("PATCH", PATCH)
replace("LABEL", '"{}"'.format(SUFFIX))
with open(version_hpp_path, "w", encoding="utf-8") as f:
f.write(version_hpp_content)
# Epilogue ####################################################################
print("""Done. Please check your source, e.g. via
git diff
now and commit the changes if no errors occurred.""")