-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnoxfile.py
142 lines (105 loc) · 4.24 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
import re
import subprocess
from glob import glob
from pathlib import Path
from time import time
from typing import List, Optional
import nox
nox.options.sessions = ["lint", "test"]
@nox.session(python="3.8")
def lint(session: nox.Session) -> None:
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", *args)
@nox.session(python="3.8")
def test(session: nox.Session) -> None:
session.install(".[test]")
session.run("pytest", *session.posargs)
def get_version_from_arguments(arguments: List[str]) -> Optional[str]:
"""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: nox.Session, version_tag: str) -> None:
# 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: nox.Session, *, version: str, file: Path, kind: str) -> None:
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}"])
@nox.session
def release(session: nox.Session) -> None:
package_name = "vendoring"
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", 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)