| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import json |
| 12 |
import os |
| 13 |
import re |
| 14 |
import shutil |
| 15 |
import subprocess |
| 16 |
import sys |
| 17 |
|
| 18 |
import requests |
| 19 |
import tomli |
| 20 |
|
| 21 |
import hypothesistooling as tools |
| 22 |
from hypothesistooling import releasemanagement as rm |
| 23 |
|
| 24 |
PACKAGE_NAME = "hypothesis-python" |
| 25 |
|
| 26 |
HYPOTHESIS_PYTHON = tools.ROOT / PACKAGE_NAME |
| 27 |
PYTHON_TAG_PREFIX = "hypothesis-python-" |
| 28 |
|
| 29 |
|
| 30 |
BASE_DIR = HYPOTHESIS_PYTHON |
| 31 |
|
| 32 |
PYTHON_SRC = HYPOTHESIS_PYTHON / "src" |
| 33 |
PYTHON_TESTS = HYPOTHESIS_PYTHON / "tests" |
| 34 |
DOMAINS_LIST = PYTHON_SRC / "hypothesis" / "vendor" / "tlds-alpha-by-domain.txt" |
| 35 |
|
| 36 |
RELEASE_FILE = HYPOTHESIS_PYTHON / "RELEASE.rst" |
| 37 |
RELEASE_SAMPLE_FILE = HYPOTHESIS_PYTHON / "RELEASE-sample.rst" |
| 38 |
|
| 39 |
assert PYTHON_SRC.exists() |
| 40 |
|
| 41 |
|
| 42 |
__version__ = None |
| 43 |
__version_info__ = None |
| 44 |
|
| 45 |
VERSION_FILE = os.path.join(PYTHON_SRC, "hypothesis/version.py") |
| 46 |
|
| 47 |
with open(VERSION_FILE, encoding="utf-8") as fp: |
| 48 |
exec(fp.read()) |
| 49 |
|
| 50 |
assert __version__ is not None |
| 51 |
assert __version_info__ is not None |
| 52 |
|
| 53 |
|
| 54 |
def has_release(): |
| 55 |
return RELEASE_FILE.exists() |
| 56 |
|
| 57 |
|
| 58 |
def has_release_sample(): |
| 59 |
return RELEASE_SAMPLE_FILE.exists() |
| 60 |
|
| 61 |
|
| 62 |
def parse_release_file(): |
| 63 |
return rm.parse_release_file(RELEASE_FILE) |
| 64 |
|
| 65 |
|
| 66 |
def has_source_changes(): |
| 67 |
return tools.has_changes([PYTHON_SRC]) |
| 68 |
|
| 69 |
|
| 70 |
def build_docs(*, builder="html", only=(), to=None): |
| 71 |
# See https://www.sphinx-doc.org/en/stable/man/sphinx-build.html |
| 72 |
tools.scripts.pip_tool( |
| 73 |
"sphinx-build", |
| 74 |
"--fail-on-warning", |
| 75 |
"--show-traceback", |
| 76 |
"--fresh-env", |
| 77 |
"--builder", |
| 78 |
builder, |
| 79 |
"docs", |
| 80 |
"docs/_build/" + (builder if to is None else to), |
| 81 |
*only, |
| 82 |
cwd=HYPOTHESIS_PYTHON, |
| 83 |
) |
| 84 |
|
| 85 |
|
| 86 |
CHANGELOG_ANCHOR = re.compile(r"^\.\. _v\d+\.\d+\.\d+:$", flags=re.MULTILINE) |
| 87 |
CHANGELOG_BORDER = re.compile(r"^-+$", flags=re.MULTILINE) |
| 88 |
CHANGELOG_HEADER = re.compile( |
| 89 |
r"^\d+\.\d+\.\d+ - \d\d\d\d-\d\d-\d\d$", flags=re.MULTILINE |
| 90 |
) |
| 91 |
|
| 92 |
|
| 93 |
def update_changelog_and_version(): |
| 94 |
global __version_info__ |
| 95 |
global __version__ |
| 96 |
|
| 97 |
contents = changelog() |
| 98 |
assert "\r" not in contents |
| 99 |
lines = contents.split("\n") |
| 100 |
for i, l in enumerate(lines): |
| 101 |
if CHANGELOG_ANCHOR.match(l): |
| 102 |
assert CHANGELOG_BORDER.match(lines[i + 2]), repr(lines[i + 2]) |
| 103 |
assert CHANGELOG_HEADER.match(lines[i + 3]), repr(lines[i + 3]) |
| 104 |
assert CHANGELOG_BORDER.match(lines[i + 4]), repr(lines[i + 4]) |
| 105 |
assert lines[i + 3].startswith( |
| 106 |
__version__ |
| 107 |
), f"{__version__=} {lines[i + 3]=}" |
| 108 |
beginning = "\n".join(lines[:i]) |
| 109 |
rest = "\n".join(lines[i:]) |
| 110 |
assert f"{beginning}\n{rest}" == contents |
| 111 |
break |
| 112 |
|
| 113 |
release_type, release_contents = parse_release_file() |
| 114 |
|
| 115 |
new_version_string, new_version_info = rm.bump_version_info( |
| 116 |
__version_info__, release_type |
| 117 |
) |
| 118 |
|
| 119 |
__version_info__ = new_version_info |
| 120 |
__version__ = new_version_string |
| 121 |
|
| 122 |
if release_type == "major": |
| 123 |
major, _, _ = __version_info__ |
| 124 |
old = f"Hypothesis {major - 1}.x" |
| 125 |
beginning = beginning.replace(old, f"Hypothesis {major}.x") |
| 126 |
rest = "\n".join([old, len(old) * "=", "", rest]) |
| 127 |
|
| 128 |
rm.replace_assignment(VERSION_FILE, "__version_info__", repr(new_version_info)) |
| 129 |
|
| 130 |
heading_for_new_version = f"{new_version_string} - {rm.release_date_string()}" |
| 131 |
border_for_new_version = "-" * len(heading_for_new_version) |
| 132 |
|
| 133 |
new_changelog_parts = [ |
| 134 |
beginning.strip(), |
| 135 |
"", |
| 136 |
f".. _v{new_version_string}:", |
| 137 |
"", |
| 138 |
border_for_new_version, |
| 139 |
heading_for_new_version, |
| 140 |
border_for_new_version, |
| 141 |
"", |
| 142 |
release_contents, |
| 143 |
"", |
| 144 |
rest, |
| 145 |
] |
| 146 |
|
| 147 |
CHANGELOG_FILE.write_text("\n".join(new_changelog_parts), encoding="utf-8") |
| 148 |
|
| 149 |
# Replace the `since="RELEASEDAY"` argument to `note_deprecation` |
| 150 |
# with today's date, to record it for future reference. |
| 151 |
before = 'since="RELEASEDAY"' |
| 152 |
after = before.replace("RELEASEDAY", rm.release_date_string()) |
| 153 |
for root, _, files in os.walk(PYTHON_SRC): |
| 154 |
for fname in (os.path.join(root, f) for f in files if f.endswith(".py")): |
| 155 |
with open(fname, encoding="utf-8") as f: |
| 156 |
contents = f.read() |
| 157 |
if before in contents: |
| 158 |
with open(fname, "w", encoding="utf-8") as f: |
| 159 |
f.write(contents.replace(before, after)) |
| 160 |
|
| 161 |
update_pyproject_toml() |
| 162 |
|
| 163 |
|
| 164 |
def update_pyproject_toml(): |
| 165 |
# manually write back these changes using regex instead of pulling in a |
| 166 |
# toml dependency for writing. tomli doesn't support writing, and |
| 167 |
# tomli-w doesn't support writing with comments. |
| 168 |
toml_p = HYPOTHESIS_PYTHON / "pyproject.toml" |
| 169 |
toml_data = tomli.loads(toml_p.read_text()) |
| 170 |
extras = toml_data["project"]["optional-dependencies"] |
| 171 |
extras.pop("all") |
| 172 |
readme = (tools.ROOT / "README.md").read_text() |
| 173 |
content = toml_p.read_text() |
| 174 |
content = re.sub( |
| 175 |
r'\[project\.readme\].*content-type = "text/markdown"', |
| 176 |
f'[project.readme]\ntext = """{readme}"""\ncontent-type = "text/markdown"', |
| 177 |
content, |
| 178 |
flags=re.DOTALL, |
| 179 |
) |
| 180 |
|
| 181 |
all_extras = sorted(set(sum(extras.values(), []))) |
| 182 |
all_extras = json.dumps(all_extras).replace("\n", "\\n") |
| 183 |
content = re.sub( |
| 184 |
r"^all = \[.*\]$", |
| 185 |
f"all = {all_extras}", |
| 186 |
content, |
| 187 |
flags=re.MULTILINE, |
| 188 |
) |
| 189 |
toml_p.write_text(content) |
| 190 |
|
| 191 |
|
| 192 |
CHANGELOG_FILE = HYPOTHESIS_PYTHON / "docs" / "changelog.rst" |
| 193 |
DIST = HYPOTHESIS_PYTHON / "dist" |
| 194 |
|
| 195 |
|
| 196 |
def changelog(): |
| 197 |
return CHANGELOG_FILE.read_text(encoding="utf-8") |
| 198 |
|
| 199 |
|
| 200 |
def build_distribution(): |
| 201 |
if os.path.exists(DIST): |
| 202 |
shutil.rmtree(DIST) |
| 203 |
subprocess.check_output([sys.executable, "-m", "build", "--outdir", DIST]) |
| 204 |
|
| 205 |
|
| 206 |
def upload_distribution(): |
| 207 |
tools.assert_can_release() |
| 208 |
|
| 209 |
subprocess.check_call( |
| 210 |
[ |
| 211 |
sys.executable, |
| 212 |
"-m", |
| 213 |
"twine", |
| 214 |
"upload", |
| 215 |
"--skip-existing", |
| 216 |
"--username=__token__", |
| 217 |
os.path.join(DIST, "*"), |
| 218 |
] |
| 219 |
) |
| 220 |
|
| 221 |
# Construct plain-text + markdown version of this changelog entry, |
| 222 |
# with link to canonical source. |
| 223 |
build_docs(builder="text", only=["docs/changelog.rst"]) |
| 224 |
textfile = os.path.join( |
| 225 |
HYPOTHESIS_PYTHON, "docs", "_build", "text", "changelog.txt" |
| 226 |
) |
| 227 |
with open(textfile, encoding="utf-8") as f: |
| 228 |
lines = f.readlines() |
| 229 |
entries = [i for i, l in enumerate(lines) if CHANGELOG_HEADER.match(l)] |
| 230 |
anchor = current_version().replace(".", "-") |
| 231 |
changelog_body = ( |
| 232 |
"".join(lines[entries[0] + 2 : entries[1]]).strip() |
| 233 |
+ "\n\n*[The canonical version of these notes (with links) is on readthedocs.]" |
| 234 |
f"(https://hypothesis.readthedocs.io/en/latest/changelog.html#v{anchor})*" |
| 235 |
) |
| 236 |
|
| 237 |
# Create a GitHub release, to trigger Zenodo DOI minting. See |
| 238 |
# https://developer.github.com/v3/repos/releases/#create-a-release |
| 239 |
resp = requests.post( |
| 240 |
"https://api.github.com/repos/HypothesisWorks/hypothesis/releases", |
| 241 |
headers={ |
| 242 |
"Accept": "application/vnd.github+json", |
| 243 |
"Authorization": f"Bearer {os.environ['GH_TOKEN']}", |
| 244 |
"X-GitHub-Api-Version": "2022-11-28", |
| 245 |
}, |
| 246 |
json={ |
| 247 |
"tag_name": tag_name(), |
| 248 |
"name": "Hypothesis for Python - version " + current_version(), |
| 249 |
"body": changelog_body, |
| 250 |
}, |
| 251 |
timeout=120, # seconds |
| 252 |
) |
| 253 |
|
| 254 |
# TODO: work out why this is 404'ing despite success (?!?) and fix it |
| 255 |
try: |
| 256 |
resp.raise_for_status() |
| 257 |
except Exception: |
| 258 |
import traceback |
| 259 |
|
| 260 |
traceback.print_exc() |
| 261 |
|
| 262 |
|
| 263 |
def current_version(): |
| 264 |
return __version__ |
| 265 |
|
| 266 |
|
| 267 |
def latest_version(): |
| 268 |
versions = [] |
| 269 |
|
| 270 |
for t in tools.tags(): |
| 271 |
if t.startswith(PYTHON_TAG_PREFIX): |
| 272 |
t = t.removeprefix(PYTHON_TAG_PREFIX) |
| 273 |
else: |
| 274 |
continue |
| 275 |
assert t == t.strip() |
| 276 |
parts = t.split(".") |
| 277 |
assert len(parts) == 3 |
| 278 |
v = tuple(map(int, parts)) |
| 279 |
versions.append((v, t)) |
| 280 |
|
| 281 |
_, latest = max(versions) |
| 282 |
|
| 283 |
return latest |
| 284 |
|
| 285 |
|
| 286 |
def tag_name(): |
| 287 |
return PYTHON_TAG_PREFIX + __version__ |
| 288 |
|
| 289 |
|
| 290 |
def get_autoupdate_message(domainlist_changed): |
| 291 |
if domainlist_changed: |
| 292 |
return ( |
| 293 |
"This patch updates our vendored `list of top-level domains " |
| 294 |
"<https://www.iana.org/domains/root/db>`__,\nwhich is used by the " |
| 295 |
"provisional :func:`~hypothesis.provisional.domains` strategy.\n" |
| 296 |
) |
| 297 |
return ( |
| 298 |
"This patch updates our autoformatting tools, " |
| 299 |
"improving our code style without any API changes." |
| 300 |
) |