-
Notifications
You must be signed in to change notification settings - Fork 33
/
noxfile.py
133 lines (108 loc) · 3.35 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
import argparse
import json
import pathlib
import nox
ROOT = pathlib.Path(__file__).resolve().parent
INIT_PY = ROOT.joinpath("src", "resolvelib", "__init__.py")
nox.options.sessions = ["lint", "tests"]
nox.options.reuse_existing_virtualenvs = True
@nox.session
def lint(session):
session.install(".[lint, test]")
session.run("ruff", "format", "--check", ".")
session.run("ruff", "check", ".")
session.run("mypy", "src", "tests")
@nox.session(python=["3.13", "3.12", "3.11", "3.10", "3.9", "3.8", "3.7"])
def tests(session):
session.install(".[test]")
files = session.posargs or ["tests"]
session.run("pytest", *files)
def _write_package_version(v):
lines = []
version_line = None
with INIT_PY.open() as f:
for line in f:
if line.startswith("__version__ = "):
line = version_line = f"__version__ = {json.dumps(str(v))}\n"
lines.append(line)
if not version_line:
raise ValueError("__version__ not found in __init__.py")
with INIT_PY.open("w", newline="\n") as f:
f.write("".join(lines))
SAFE_RMTREE = """
import os
import shutil
if os.path.isdir({path!r}):
shutil.rmtree({path!r})
"""
@nox.session
def release(session):
session.install(".[release]")
parser = argparse.ArgumentParser()
parser.add_argument(
"--version",
required=True,
help="Version to release. Empty value uses the value in __init__.py.",
)
parser.add_argument(
"--repo",
required=True,
help="Repository to upload to. Empty value disables publish.",
)
parser.add_argument(
"--prebump",
required=True,
help="Version to bump to after release. Empty value disables bump.",
)
options = parser.parse_args(session.posargs)
# Make sure the workspace is clean.
session.run("git", "diff", "--no-patch", "--exit-code", external=True)
if options.version:
_write_package_version(options.version)
session.run("towncrier", "build", "--version", options.version)
session.run(
"git",
"commit",
"--all",
"--message",
f"Release {options.version}",
external=True,
)
session.run(
"git",
"tag",
"--annotate",
"--message",
f"Version {options.version}",
options.version,
external=True,
)
else:
session.log("Skipping preprocessing since --version is empty")
session.log("Cleaning dist/ content...")
session.run("python", "-c", SAFE_RMTREE.format(path="dist"))
session.log("Building distributions...")
session.run("python", "-m", "build")
session.run("twine", "check", "dist/*")
if options.repo:
session.log(f"Releasing distributions to {options.repo}...")
else:
session.log("Storing distributions locally since --repo is empty")
if options.repo:
session.run(
"twine",
"upload",
"--repository-url",
options.repo,
"dist/*",
)
if options.prebump:
_write_package_version(options.prebump)
session.run(
"git",
"commit",
"--all",
"--message",
f"Prebump to {options.prebump}",
external=True,
)