This repository has been archived by the owner on Jul 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
188 lines (144 loc) · 5.08 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
188
"""Development automation
"""
import os
import re
import subprocess
from contextlib import contextmanager
from glob import glob
from pathlib import Path
from time import sleep, time
import nox
PACKAGE_NAME = "sphinx-mkdocs-theme"
nox.options.sessions = ["lint", "test"]
nox.options.reuse_existing_virtualenvs = True
#
# Helpers
#
def _install_this_project_with_flit(session, *, extras=None, editable=False):
session.install("flit")
args = []
if extras:
args.append("--extras")
args.append(",".join(extras))
if editable:
args.append("--pth-file" if os.name == "nt" else "--symlink")
session.run("flit", "install", "--deps=production", *args, silent=True)
#
# Development Sessions
#
@nox.session(python="3.8")
def docs(session):
_install_this_project_with_flit(session, extras=["doc"])
# Generate documentation into `build/docs`
session.run("sphinx-build", "-W", "-b", "html", "-v", "docs/", "build/docs")
@nox.session(python="3.8")
def lint(session):
session.install("pre-commit")
if session.posargs:
args = session.posargs + ["--all-files"]
else:
args = ["--all-files", "--show-diff-on-failure"]
session.run("pre-commit", "run", "--all-files", *args)
@nox.session(python="3.8")
def test(session):
_install_this_project_with_flit(session, extras=["test"])
args = session.posargs or ["-n", "auto", "--cov", PACKAGE_NAME]
session.run("pytest", *args)
#
# Helpers (Release Automation)
#
def get_version_from_arguments(arguments):
"""Checks the arguments passed to `nox -s release`.
If there is only 1 argument that looks like a version, returns the argument.
Otherwise, returns None.
"""
if len(arguments) != 1:
return None
version = arguments[0]
parts = version.split(".")
if len(parts) != 3:
# Not of the form: MAJOR.MINOR.PATCH
return None
if not all(part.isdigit() for part in parts):
# Not all segments are integers.
return None
# All is good.
return version
def perform_git_checks(session, version_tag):
# Ensure we're on master branch for cutting a release.
result = subprocess.run(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
capture_output=True,
encoding="utf-8",
)
if result.stdout != "master\n":
session.error(f"Not on master branch: {result.stdout!r}")
# Ensure there are no uncommitted changes.
result = subprocess.run(
["git", "status", "--porcelain"], capture_output=True, encoding="utf-8"
)
if result.stdout:
print(result.stdout)
session.error("The working tree has uncommitted changes")
# Ensure this tag doesn't exist already.
result = subprocess.run(
["git", "rev-parse", version_tag], capture_output=True, encoding="utf-8"
)
if not result.returncode:
session.error(f"Tag already exists! {version_tag} -- {result.stdout!r}")
# Back up the current git reference, in a tag that's easy to clean up.
_release_backup_tag = "auto/release-start-" + str(int(time()))
session.run("git", "tag", _release_backup_tag, external=True)
def bump(session, *, version, file, kind):
session.log(f"Bump version to {version!r}")
contents = file.read_text()
new_contents = re.sub(
'__version__ = "(.+)"', f'__version__ = "{version}"', contents
)
file.write_text(new_contents)
session.log("git commit")
subprocess.run(["git", "add", str(file)])
subprocess.run(["git", "commit", "-m", f"Bump for {kind}"])
#
# Release Automation
#
@nox.session
def release(session):
release_version = get_version_from_arguments(session.posargs)
if not release_version:
session.error("Usage: nox -s release -- MAJOR.MINOR.PATCH")
# Do sanity check about the state of the git repository
perform_git_checks(session, release_version)
# Install release dependencies
session.install("twine", "flit")
version_file = Path(f"src/{PACKAGE_NAME}/__init__.py")
# Bump for release
bump(session, version=release_version, file=version_file, kind="release")
# Tag the release commit
session.run(
"git",
"tag",
"-s",
"-m",
f"Release {release_version}",
release_version,
external=True,
)
# Bump for development
major, minor, patch = map(int, release_version.split("."))
next_version = f"{major}.{minor}.{patch + 1}.dev0"
bump(session, version=next_version, file=version_file, kind="development")
# Checkout the git tag
session.run("git", "checkout", "-q", release_version, external=True)
# Build the distribution
session.run("flit", "build")
files = glob(f"dist/{PACKAGE_NAME}-{release_version}*")
assert len(files) == 2
# Get back out into master
session.run("git", "checkout", "-q", "master", external=True)
# Check and upload distribution files
session.run("twine", "check", *files)
# Upload the distribution
session.run("twine", "upload", *files)
# Push the commits and tag
session.run("git", "push", "origin", "master", release_version, external=True)