From c7e56bf1c47799088055cb9c57fc771a6aa94b5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Morosi?= Date: Fri, 6 Dec 2024 16:51:50 +0100 Subject: [PATCH] Add back and improve build_wheel.py The call to build_wheel.py is added back after it was removed in it/e3-aws!27 because of an issue with pushing the new tag. Now the tag will not be created automatically. Instead, the script will check that VERSION matches the major/minor version in the most recent tag that we will have to manually create. Ref it/org/operation_support/iaas/issues#214 --- .gitlab-ci.yml | 9 ++--- VERSION | 2 +- build_wheel.py | 98 ++++++++++++++++++++++++++++---------------------- 3 files changed, 60 insertions(+), 49 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 092fc53..971a745 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -217,21 +217,18 @@ run windows tests: # This is only useful if the previous stages have failed and you still want to run the tests. upload-python-registry: - extends: .linux-image stage: upload + services: + - image:e3 before_script: - - !reference [.linux-image, before_script] - python -m pip install twine script: - - CURRENT_DATE=$(date +"%Y%m%d%H%M") - - sed -i "s|[0-9][0-9.]*|&.${CURRENT_DATE}|" VERSION - - python -m pip wheel . -q --no-deps -C--python-tag=py3 -w build + - python build_wheel.py - python -m twine upload --skip-existing --repository-url https://${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/projects/202/packages/pypi build/*.whl rules: - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH variables: - PYTHON_VERSION: "3.11" TWINE_PASSWORD: $CI_JOB_TOKEN TWINE_USERNAME: gitlab-ci-token diff --git a/VERSION b/VERSION index 937387f..2c34300 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -22.3 +22.4.dev1 diff --git a/build_wheel.py b/build_wheel.py index ed7c8c3..1310499 100644 --- a/build_wheel.py +++ b/build_wheel.py @@ -1,11 +1,11 @@ #!/usr/bin/env python """Build the wheel. -dev1 in the version of the package is automatically replaced by the number -of commits since the last tagged version. +Patch version in the version of the package is automatically replaced by +the number of commits since the last tagged version. -A tag v..0 is automatically added to the commit if the major -or minor version changes. +For that, a tag v..0 must be manually added when a major or +minor version change occurs. """ from __future__ import annotations import sys @@ -45,11 +45,6 @@ def main() -> None: parser = main.argument_parser parser.description = "Build the wheel" - parser.add_argument( - "--update", - action="store_true", - help="Tag the commit in case of version change", - ) parser.add_argument( "--last-tag", help="Provide the last tagged version", @@ -70,64 +65,83 @@ def main() -> None: with open(version_path) as f: version_content = f.read() - # Extract the . part. - # We will replace the dev1 part by the number of commits since the most + # Extract the .. part. + # We will replace the patch version by the number of commits since the most # recent tagged version - match = re.match(r"(?P\d+\.\d+)\.dev1", version_content) + match = re.search( + r"(?P\d+)\.(?P\d+)(\.(?P\w+))?", version_content + ) if not match: - logger.error(f"No ..dev1 version found in {version_path.name}") + logger.error( + f"No .(.)? version found in {version_path.name}" + ) sys.exit(1) - logger.info("Version is {}.dev1".format(version := match.group("version"))) + version_major = match.group("major") + version_minor = match.group("minor") + version_patch = match.group("patch") + version = f"{version_major}.{version_minor}.{version_patch}" + logger.info(f"Version is {version}") # Find previous version from the most recent tag - tagged_version = main.args.last_tag - if not tagged_version: + last_tag = main.args.last_tag + if not last_tag: # Need to unshallow the clone so we get the list of tags. # That command can fail for an already complete clone run(["git", "fetch", "--unshallow", "--tags"], fail_ok=True) # Describe the most recent tag p = run(["git", "describe", "--tags"]) - tagged_version = p.out + last_tag = p.out.strip() # Format is v..(-)? with commits omitted if # the current commit is also the one tagged match = re.match( - r"v(?P\d+\.\d+)\.\d+(\-(?P\d+))?", tagged_version + r"v(?P\d+)\.(?P\d+)\.(?P\w+)(\-(?P\d+))?", + last_tag, ) if not match: logger.error( - "Expected v..(-)? " - f"format for tag {tagged_version}" + f"Expected v..(-)? format for tag {last_tag}" ) sys.exit(1) - # tagged_version_commits is None only if the current commit is also the one tagged - # so there is 0 commits since that tag - tagged_version_commits = match.group("commits") - version_patch = tagged_version_commits if tagged_version_commits is not None else 0 - logger.info( - "Tagged version {} commit(s) ago is {}".format( - version_patch, tagged_version := match.group("version") + # Ensure the major and minor versions match. + # Also ensure the patch version is 0 because multiple tags for the same + # . would mess up with the versioning system and then only + # ..0 should exist + tagged_version_major = match.group("major") + tagged_version_minor = match.group("minor") + tagged_version_patch = match.group("patch") + if (version_major, version_minor, "0") != ( + tagged_version_major, + tagged_version_minor, + tagged_version_patch, + ): + logger.error( + ( + "Found tag v{major}.{minor}.{patch} but was expecting " + "v{major}.{minor}.0. Please manually create the tag if not done yet " + "or make sure this is the most recent tag" + ).format( + major=tagged_version_major, + minor=tagged_version_minor, + patch=tagged_version_patch, + ) ) - ) - - # Set patch version to 0 and tag the commit with ..0 in case of - # version change. If tagged_version_commits is None then the current commit - # is already tagged and the patch version must be set to 0 too - if tagged_version_commits is None or version != tagged_version: - version_patch = 0 + sys.exit(1) - # Don't recreate the tag if tagged_version_commits is None - if tagged_version_commits is not None and main.args.update: - tag = f"v{version}.0" - run(["git", "tag", tag]) - run(["git", "push", "origin", tag]) + # match.group("commits") is None only if the current commit is also + # the one tagged so there is 0 commits since that tag + new_version = "{}.{}.{}".format( + version_major, + version_minor, + match.group("commits") or "0", + ) - # Replace dev1 in the version file. - logger.info(f"Set version to {version}.{version_patch}") + # Replace the version in the file + logger.info(f"Set version to {new_version}") with open(version_path, "w") as f: - f.write(version_content.replace("dev1", str(version_patch))) + f.write(version_content.replace(version, new_version)) try: # Build the wheel