| 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 |
"""Helpful common code for release management tasks that is shared across |
| 12 |
multiple projects. |
| 13 |
|
| 14 |
Note that most code in here is brittle and specific to our build and |
| 15 |
probably makes all sorts of undocumented assumptions, even as it looks |
| 16 |
like a nice tidy reusable set of functionality. |
| 17 |
""" |
| 18 |
|
| 19 |
import re |
| 20 |
from datetime import datetime, timezone |
| 21 |
|
| 22 |
import hypothesistooling as tools |
| 23 |
|
| 24 |
__RELEASE_DATE_STRING = None |
| 25 |
|
| 26 |
|
| 27 |
def release_date_string(): |
| 28 |
"""Returns a date string that represents what should be considered "today" |
| 29 |
for the purposes of releasing, and ensure that we don't change part way |
| 30 |
through a release.""" |
| 31 |
global __RELEASE_DATE_STRING |
| 32 |
if __RELEASE_DATE_STRING is None: |
| 33 |
__RELEASE_DATE_STRING = datetime.now(timezone.utc).strftime("%Y-%m-%d") |
| 34 |
return __RELEASE_DATE_STRING |
| 35 |
|
| 36 |
|
| 37 |
def assignment_matcher(name): |
| 38 |
""" |
| 39 |
Matches a single line of the form (some space)name = (some value). e.g. |
| 40 |
" foo = 1". |
| 41 |
The whole line up to the assigned value is the first matching group, |
| 42 |
the rest of the line is the second matching group. |
| 43 |
i.e. group 1 is the assignment, group 2 is the value. In the above |
| 44 |
example group 1 would be " foo = " and group 2 would be "1" |
| 45 |
""" |
| 46 |
return re.compile(rf"\A(\s*{re.escape(name)}\s*=\s*)(.+)\Z") |
| 47 |
|
| 48 |
|
| 49 |
def extract_assignment_from_string(contents, name): |
| 50 |
lines = contents.split("\n") |
| 51 |
|
| 52 |
matcher = assignment_matcher(name) |
| 53 |
|
| 54 |
for l in lines: |
| 55 |
match = matcher.match(l) |
| 56 |
if match is not None: |
| 57 |
return match[2].strip() |
| 58 |
|
| 59 |
raise ValueError(f"Key {name} not found in {contents}") |
| 60 |
|
| 61 |
|
| 62 |
def extract_assignment(filename, name): |
| 63 |
with open(filename, encoding="utf-8") as i: |
| 64 |
return extract_assignment_from_string(i.read(), name) |
| 65 |
|
| 66 |
|
| 67 |
def replace_assignment_in_string(contents, name, value): |
| 68 |
lines = contents.split("\n") |
| 69 |
|
| 70 |
matcher = assignment_matcher(name) |
| 71 |
|
| 72 |
count = 0 |
| 73 |
|
| 74 |
for i, l in enumerate(lines): |
| 75 |
match = matcher.match(l) |
| 76 |
if match is not None: |
| 77 |
count += 1 |
| 78 |
lines[i] = match[1] + value |
| 79 |
|
| 80 |
if count == 0: |
| 81 |
raise ValueError(f"Key {name} not found in {contents}") |
| 82 |
if count > 1: |
| 83 |
raise ValueError(f"Key {name} found {count} times in {contents}") |
| 84 |
|
| 85 |
return "\n".join(lines) |
| 86 |
|
| 87 |
|
| 88 |
def replace_assignment(filename, name, value): |
| 89 |
"""Replaces a single assignment of the form key = value in a file with a |
| 90 |
new value, attempting to preserve the existing format. |
| 91 |
|
| 92 |
This is fairly fragile - in particular it knows nothing about |
| 93 |
the file format. The existing value is simply the rest of the line after |
| 94 |
the last space after the equals. |
| 95 |
""" |
| 96 |
with open(filename, encoding="utf-8") as f: |
| 97 |
contents = f.read() |
| 98 |
result = replace_assignment_in_string(contents, name, value) |
| 99 |
with open(filename, "w", encoding="utf-8") as f: |
| 100 |
f.write(result) |
| 101 |
|
| 102 |
|
| 103 |
RELEASE_TYPE = re.compile(r"^RELEASE_TYPE: +(major|minor|patch)") |
| 104 |
|
| 105 |
|
| 106 |
MAJOR = "major" |
| 107 |
MINOR = "minor" |
| 108 |
PATCH = "patch" |
| 109 |
|
| 110 |
|
| 111 |
VALID_RELEASE_TYPES = (MAJOR, MINOR, PATCH) |
| 112 |
|
| 113 |
|
| 114 |
def parse_release_file(filename): |
| 115 |
with open(filename, encoding="utf-8") as i: |
| 116 |
return parse_release_file_contents(i.read(), filename) |
| 117 |
|
| 118 |
|
| 119 |
def parse_release_file_contents(release_contents, filename): |
| 120 |
release_lines = [l.rstrip() for l in release_contents.split("\n")] |
| 121 |
|
| 122 |
m = RELEASE_TYPE.match(release_lines[0]) |
| 123 |
if m is not None: |
| 124 |
release_type = m.group(1) |
| 125 |
if release_type not in VALID_RELEASE_TYPES: |
| 126 |
raise ValueError(f"Unrecognised release type {release_type!r}") |
| 127 |
del release_lines[0] |
| 128 |
release_contents = "\n".join(release_lines).strip() |
| 129 |
else: |
| 130 |
raise ValueError( |
| 131 |
f"{filename} does not start by specifying release type. The first " |
| 132 |
"line of the file should be RELEASE_TYPE: followed by one of " |
| 133 |
"major, minor, or patch, to specify the type of release that " |
| 134 |
"this is (i.e. which version number to increment). Instead the " |
| 135 |
f"first line was {release_lines[0]!r}" |
| 136 |
) |
| 137 |
|
| 138 |
return release_type, release_contents |
| 139 |
|
| 140 |
|
| 141 |
def bump_version_info(version_info, release_type): |
| 142 |
new_version = list(version_info) |
| 143 |
bump = VALID_RELEASE_TYPES.index(release_type) |
| 144 |
new_version[bump] += 1 |
| 145 |
for i in range(bump + 1, len(new_version)): |
| 146 |
new_version[i] = 0 |
| 147 |
new_version = tuple(new_version) |
| 148 |
new_version_string = ".".join(map(str, new_version)) |
| 149 |
return new_version_string, new_version |
| 150 |
|
| 151 |
|
| 152 |
def update_markdown_changelog(changelog, name, version, entry): |
| 153 |
with open(changelog, encoding="utf-8") as f: |
| 154 |
prev_contents = f.read() |
| 155 |
|
| 156 |
title = f"# {name} {version} ({release_date_string()})\n\n" |
| 157 |
|
| 158 |
with open(changelog, "w", encoding="utf-8") as f: |
| 159 |
f.write(title) |
| 160 |
f.write(entry.strip()) |
| 161 |
f.write("\n\n") |
| 162 |
f.write(prev_contents) |
| 163 |
|
| 164 |
|
| 165 |
def parse_version(version): |
| 166 |
return tuple(map(int, version.split("."))) |
| 167 |
|
| 168 |
|
| 169 |
def commit_pending_release(project): |
| 170 |
"""Create a commit with the new release.""" |
| 171 |
tools.git("rm", project.RELEASE_FILE) |
| 172 |
tools.git("add", "-u", project.BASE_DIR) |
| 173 |
|
| 174 |
tools.git( |
| 175 |
"commit", |
| 176 |
"-m", |
| 177 |
f"Bump {project.PACKAGE_NAME} version to {project.current_version()} " |
| 178 |
"and update changelog\n\n[skip ci]", |
| 179 |
) |