From 7be30455673e1474e309439dd3608e2826f40c20 Mon Sep 17 00:00:00 2001 From: Liam Bigelow <40188355+bglw@users.noreply.github.com> Date: Wed, 2 Oct 2024 09:01:07 +1300 Subject: [PATCH] Fix version parsing for python binary packaging --- .github/workflows/release.yml | 2 +- .../scripts/build/all_binary_only_wheels.py | 7 +++- wrappers/python/scripts/build/api_package.py | 38 +------------------ wrappers/python/scripts/build/versioning.py | 36 ++++++++++++++++++ 4 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 wrappers/python/scripts/build/versioning.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0a13156f..643d9e31 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -132,7 +132,7 @@ jobs: run: | # should take ~30s; writes wheels to wrappers/python/dist export PAGEFIND_PYTHON_LOG_LEVEL=DEBUG python -m scripts.build.all_binary_only_wheels \ - --git-tag "${{ github.ref_name }}" \ + --tag "${{ github.ref_name }}" \ --bin-dir ./vendor - name: package python api working-directory: ./wrappers/python diff --git a/wrappers/python/scripts/build/all_binary_only_wheels.py b/wrappers/python/scripts/build/all_binary_only_wheels.py index e0d6a179..0f2f73be 100644 --- a/wrappers/python/scripts/build/all_binary_only_wheels.py +++ b/wrappers/python/scripts/build/all_binary_only_wheels.py @@ -14,6 +14,7 @@ write_pagefind_bin_only_wheel, ) from .get_pagefind_release import download, find_bins +from .versioning import process_tag __candidates = ( "pagefind", @@ -87,7 +88,7 @@ def parse_args() -> Args: if tag_name is None: raise ValueError("tag_name is None") assert re.match( - r"^v\d+\.\d+\.\d+(-\w+)?", tag_name + r"^v\d+\.\d+\.\d+(-\w+\.?\d*)?", tag_name ), f"Invalid tag_name: {tag_name}" check_platforms(certified) @@ -95,6 +96,8 @@ def parse_args() -> Args: dist_dir.rmdir() dist_dir.mkdir(exist_ok=True) + version = process_tag(tag_name) + for tar_gz in certified: log.info("Processing %s", tar_gz) llvm_triple = get_llvm_triple(tar_gz) @@ -112,6 +115,6 @@ def parse_args() -> Args: write_pagefind_bin_only_wheel( executable=find_bin(temp_dir), output_dir=dist_dir, - version=tag_name.removeprefix("v"), + version=version, platform=platform, ) diff --git a/wrappers/python/scripts/build/api_package.py b/wrappers/python/scripts/build/api_package.py index 99de3364..b9b22283 100644 --- a/wrappers/python/scripts/build/api_package.py +++ b/wrappers/python/scripts/build/api_package.py @@ -8,6 +8,7 @@ from argparse import ArgumentParser from . import python_root, setup_logging +from .versioning import process_tag pyproject_toml = python_root / "pyproject.toml" @@ -16,43 +17,6 @@ cli.add_argument("--tag", required=True, help="The version to build.") log = logging.getLogger(__name__) - -def process_tag(tag: str) -> str: - """Convert a git tag to a version string compliant with PEP 440. - See https://peps.python.org/pep-0440/#public-version-identifiers - """ - pattern = ( - # note that this pattern accepts a superset of the tagging pattern used - # in this repository. - r"^v(?P\d+)" - r"\.(?P\d+)" - r"\.(?P\d+)" - r"(-" - r"(?Palpha|beta|rc)" - r"\.?(?P\d+)" - ")?" - ) - parts = re.match(pattern, tag) - if parts is None: - raise ValueError(f"Invalid tag: `{tag}` does not match pattern `{pattern}`") - major = int(parts["major"]) - minor = int(parts["minor"]) - patch = int(parts["patch"]) - suffix = "" - - if (prerelease_kind := parts["prerelease_kind"]) is not None: - if prerelease_kind == "rc": - suffix = "rc" - elif prerelease_kind.startswith("alpha"): - suffix = "a" - elif prerelease_kind.startswith("beta"): - suffix = "b" - if (prerelease_number := parts["prerelease_number"]) is not None: - suffix += str(int(prerelease_number)) - - return f"{major}.{minor}.{patch}{suffix}" - - def main() -> None: setup_logging() args = cli.parse_args() diff --git a/wrappers/python/scripts/build/versioning.py b/wrappers/python/scripts/build/versioning.py new file mode 100644 index 00000000..efb19b63 --- /dev/null +++ b/wrappers/python/scripts/build/versioning.py @@ -0,0 +1,36 @@ +import re + +def process_tag(tag: str) -> str: + """Convert a git tag to a version string compliant with PEP 440. + See https://peps.python.org/pep-0440/#public-version-identifiers + """ + pattern = ( + # note that this pattern accepts a superset of the tagging pattern used + # in this repository. + r"^v(?P\d+)" + r"\.(?P\d+)" + r"\.(?P\d+)" + r"(-" + r"(?Palpha|beta|rc)" + r"\.?(?P\d+)" + ")?" + ) + parts = re.match(pattern, tag) + if parts is None: + raise ValueError(f"Invalid tag: `{tag}` does not match pattern `{pattern}`") + major = int(parts["major"]) + minor = int(parts["minor"]) + patch = int(parts["patch"]) + suffix = "" + + if (prerelease_kind := parts["prerelease_kind"]) is not None: + if prerelease_kind == "rc": + suffix = "rc" + elif prerelease_kind.startswith("alpha"): + suffix = "a" + elif prerelease_kind.startswith("beta"): + suffix = "b" + if (prerelease_number := parts["prerelease_number"]) is not None: + suffix += str(int(prerelease_number)) + + return f"{major}.{minor}.{patch}{suffix}"