-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathupdate_version.py
More file actions
226 lines (188 loc) · 7.21 KB
/
update_version.py
File metadata and controls
226 lines (188 loc) · 7.21 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import argparse
import re
import textwrap
from datetime import datetime
from pathlib import Path
import yaml
from filelock import FileLock
from packaging.version import Version
_epilog = """\
Update version information stored in version.txt in the project root,
as well as several other files in the repository. If --version is not
provided, the version number will not be changed. A file lock is held
to synchronize file access. The version tag must comply with standard
'<major>.<minor>.<patch>' format conventions for semantic versioning.
To show the version without changing anything, use --get (short -g).
"""
_project_name = "flopy"
_project_root_path = Path(__file__).parent.parent
_version_txt_path = _project_root_path / "version.txt"
_version_py_path = _project_root_path / "flopy" / "version.py"
# file names and the path to the file relative to the repo root directory
file_paths_list = [
_project_root_path / "CITATION.cff",
_project_root_path / "README.md",
_project_root_path / "docs" / "PyPI_release.md",
_project_root_path / "flopy" / "version.py",
]
file_paths = {pth.name: pth for pth in file_paths_list} # keys for each file
def split_nonnumeric(s):
match = re.compile(r"[^0-9]").search(s)
return [s[: match.start()], s[match.start() :]] if match else s
_current_version = Version(_version_txt_path.read_text().strip())
def update_version_txt(version: Version):
with open(_version_txt_path, "w") as f:
f.write(str(version))
print(f"Updated {_version_txt_path} to version {version}")
def update_version_py(timestamp: datetime, version: Version):
with open(_version_py_path, "w") as f:
f.write(
f"# {_project_name} version file automatically created using\n"
f"# {Path(__file__).name} on {timestamp:%B %d, %Y %H:%M:%S}\n\n"
)
f.write(f'__version__ = "{version}"\n')
f.close()
print(f"Updated {_version_py_path} to version {version}")
def get_software_citation(timestamp: datetime, version: Version):
# get data Software/Code citation for FloPy
citation = yaml.safe_load(file_paths["CITATION.cff"].read_text())
# format author names
authors = []
for author in citation["authors"]:
tauthor = author["family-names"] + ", "
gnames = author["given-names"].split()
if len(gnames) > 1:
for gname in gnames:
tauthor += gname[0]
if len(gname) > 1:
tauthor += "."
tauthor += " "
else:
tauthor += author["given-names"]
authors.append(tauthor.rstrip())
line = "["
for ipos, tauthor in enumerate(authors):
if ipos > 0:
line += ", "
if ipos == len(authors) - 1:
line += "and "
# add formatted author name to line
line += tauthor
# add the rest of the citation
line += (
f", {timestamp.year}, FloPy v{version}: "
f"U.S. Geological Survey Software Release, {timestamp:%d %B %Y}, "
"https://doi.org/10.5066/F7BK19FH]"
"(https://doi.org/10.5066/F7BK19FH)"
)
return line
def update_readme_markdown(timestamp: datetime, version: Version):
# read README.md into memory
fpth = file_paths["README.md"]
lines = fpth.read_text().rstrip().split("\n")
# rewrite README.md
with open(fpth, "w") as f:
for line in lines:
if "### Version " in line:
line = f"### Version {version}"
elif "[flopy continuous integration]" in line:
line = (
"[](https://github.com/modflowpy/flopy/actions/"
"workflows/commit.yml)"
)
elif "[Read the Docs]" in line:
line = (
"[]"
"(https://github.com/modflowpy/flopy/actions/"
"workflows/rtd.yml)"
)
elif "[Coverage Status]" in line:
line = (
"[]"
"(https://coveralls.io/github/modflowpy/"
"flopy?branch=develop)"
)
elif "doi.org/10.5066/F7BK19FH" in line:
line = get_software_citation(timestamp, version)
f.write(f"{line}\n")
print(f"Updated {fpth} to version {version}")
def update_citation_cff(timestamp: datetime, version: Version):
# read CITATION.cff to modify
fpth = file_paths["CITATION.cff"]
citation = yaml.safe_load(fpth.read_text())
# update version and date-released
citation["version"] = str(version)
citation["date-released"] = timestamp.strftime("%Y-%m-%d")
# write CITATION.cff
with open(fpth, "w") as f:
yaml.safe_dump(
citation, f, allow_unicode=True, default_flow_style=False, sort_keys=False
)
print(f"Updated {fpth} to version {version}")
def update_pypi_release(timestamp: datetime, version: Version):
# read PyPI_release.md into memory
fpth = file_paths["PyPI_release.md"]
lines = fpth.read_text().rstrip().split("\n")
# rewrite PyPI_release.md
f = open(fpth, "w")
for line in lines:
if "doi.org/10.5066/F7BK19FH" in line:
line = get_software_citation(timestamp, version)
f.write(f"{line}\n")
f.close()
print(f"Updated {fpth} to version {version}")
def update_version(
timestamp: datetime = datetime.now(),
version: Version = None,
):
lock_path = Path(_version_txt_path.name + ".lock")
try:
lock = FileLock(lock_path)
previous = Version(_version_txt_path.read_text().strip())
version = (
version
if version
else Version(previous.major, previous.minor, previous.micro)
)
with lock:
update_version_txt(version)
update_version_py(timestamp, version)
update_readme_markdown(timestamp, version)
update_citation_cff(timestamp, version)
update_pypi_release(timestamp, version)
finally:
try:
lock_path.unlink()
except:
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog=f"Update {_project_name} version",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent(_epilog),
)
parser.add_argument(
"-v",
"--version",
required=False,
help="Specify the release version",
)
parser.add_argument(
"-g",
"--get",
required=False,
action="store_true",
help="Just get the current version number, no updates (defaults false)",
)
args = parser.parse_args()
if args.get:
print(_current_version)
else:
update_version(
timestamp=datetime.now(),
version=(Version(args.version) if args.version else _current_version),
)