-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
187 lines (147 loc) · 4.94 KB
/
noxfile.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
186
187
import argparse
import nox
@nox.session
def lint(session):
"""
Run the linters.
"""
session.install("pre-commit")
session.run("pre-commit", "run", "-a")
@nox.session(reuse_venv=True)
def docs(session):
session.install(".")
session.run("sphinx-build", "-b", "html", "docs/source", "build")
@nox.session(name="docs-live", reuse_venv=True)
def docs_live(session):
session.install(".")
session.run(
"sphinx-autobuild",
"-b",
"html",
"docs/source",
"build",
*session.posargs,
)
@nox.session
def release(session: nox.Session) -> None:
"""
Kicks off an automated release process by creating and pushing a new tag.
Invokes bump2version with the posarg setting the version.
Usage:
$ nox -s release -- new-version -- description
"""
parser = argparse.ArgumentParser(description="Release a semver version.")
parser.add_argument(
"version",
type=str,
nargs=1,
help="The version to release. Must be structured as [major.minor.patch-release.update].",
)
parser.add_argument(
"description",
type=str,
nargs=1,
help="Short description of the version to release.",
)
args: argparse.Namespace = parser.parse_args(args=session.posargs)
version: str = args.version.pop()
description: str = args.description.pop()
# If we get here, we should be good to go
# Let's do a final check for safety
confirm = input(
f"You are about to bump the {version!r} version, described as: {description!r}.\nAre you sure? [y/n]: "
)
# Abort on anything other than 'y'
if confirm.lower().strip() != "y":
session.error(
f"You said no when prompted to bump the {version!r} version."
)
enable_full_tests()
# run pre-commit to ensure all checks pass
session.install("pre-commit")
session.log(f"pre-commit")
session.run("pre-commit", "run", "-a")
# update changelogs
update_changelog(version, description)
# add changelog updates to git
session.run("git", "add", "-u", external=True)
# find current version from .bumpversion.cfg
with open(".bumpversion.cfg") as f:
text = f.readlines()
current_version = (
[l for l in text if "current_version" in l][0]
.replace("current_version = ", "")
.replace("\n", "")
)
# commit the changelog updates
session.install("bump2version")
session.log(f"Bumping {current_version!r} to {version!r} version")
session.run(
"bump2version",
"--current-version",
current_version,
"--new-version",
version,
"--allow-dirty",
"patch",
external=True,
)
# push the new tag
session.log("Pushing the new tag")
session.run("git", "push", external=True)
session.run("git", "push", "--tags", external=True)
def update_changelog(version, description):
"""This function updates the changelog file with the new version and description
Parameters
----------
version : str
version number
description : str
version description
"""
import pathlib
import os
import shutil
path = pathlib.Path(__file__).parent.absolute()
changelog_file = os.path.join(path, "CHANGELOG.rst")
docs_path = os.path.join(path, "docs/source")
# open current changelog
with open(changelog_file, "r+") as f:
text = f.readlines()
# prepare the new version title
versioning_format = [
"[{}_] - {}\n".format(version, description),
"=======================================================\n",
]
# add the new version title to the changelog
for i, line in enumerate(versioning_format):
text.insert(9 + i, line)
# find the last index of the keepachangelog link
last_index = [i for i, s in enumerate(text) if ".. _keepachangelog" in s][
0
]
# prepare the link to the new version
link = ".. _{}: https://github.com/arielmission-space/ExoSim2-public/releases/tag/v{}\n".format(
version, version
)
# add the link to the changelog
text.insert(last_index - 1, link)
# overwrite the changelog
with open(changelog_file, "w+") as file_obj:
file_obj.writelines(text)
# Copy changelog to docs
shutil.copy(changelog_file, docs_path)
def enable_full_tests():
"""this function enables the full test suite by changing the fast_test flag to False in the inputs.py file"""
import pathlib
import os
path = pathlib.Path(__file__).parent.absolute()
test_input_file = os.path.join(path, "tests/inputs.py")
# open current input file
with open(test_input_file, "r+") as f:
text = f.readlines()
# find the input keyword
last_index = [i for i, s in enumerate(text) if "fast_test" in s][0]
text[last_index] = "fast_test = False\n"
with open(test_input_file, "w+") as file_obj:
file_obj.writelines(text)