"""Version helpers shared by the release scripts. Standard library only.""" import re # A version is always three numbers: 1.4.0. The tag has a v in front (v1.4.0), the branch is release/v1.4.0. VERSION = re.compile(r"(\d+)\.(\d+)\.(\d+)") RELEASE_BRANCH = re.compile(r"release/v(\d+)\.(\d+)\.(\d+)") TAG = re.compile(r"v(\d+)\.(\d+)\.(\d+)") def release_version(branch): """The version of a `release/vX.Y.Z` branch as a tuple, or None when it is not one.""" match = RELEASE_BRANCH.fullmatch(branch or "") return tuple(int(part) for part in match.groups()) if match else None def tag_version(tag): match = TAG.fullmatch(tag or "") return tuple(int(part) for part in match.groups()) if match else None def latest(tags): """The highest version among the tags that are versions, or None.""" versions = [version for version in (tag_version(tag) for tag in tags) if version] return max(versions) if versions else None def text(version): return ".".join(str(part) for part in version) def changelog_section(changelog, version): """The body under `## vX.Y.Z` of the changelog, up to the next `## `; None when there is no such heading.""" heading = re.compile(rf"^##\s+v?{re.escape(text(version))}(\s|$)") lines = changelog.splitlines() for index, line in enumerate(lines): if heading.match(line): body = [] for following in lines[index + 1:]: if following.startswith("## "): break body.append(following) return "\n".join(body).strip() return None