Skip to content

Commit

Permalink
Merge branch 'morosi-version' into 'master'
Browse files Browse the repository at this point in the history
Add back and improve build_wheel.py

See merge request it/e3-aws!28
  • Loading branch information
adanaja committed Dec 9, 2024
2 parents 64f381b + c7e56bf commit 3b81ff2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 49 deletions.
9 changes: 3 additions & 6 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22.3
22.4.dev1
98 changes: 56 additions & 42 deletions build_wheel.py
Original file line number Diff line number Diff line change
@@ -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<major>.<minor>.0 is automatically added to the commit if the major
or minor version changes.
For that, a tag v<major>.<minor>.0 must be manually added when a major or
minor version change occurs.
"""
from __future__ import annotations
import sys
Expand Down Expand Up @@ -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",
Expand All @@ -70,64 +65,83 @@ def main() -> None:
with open(version_path) as f:
version_content = f.read()

# Extract the <major>.<minor> part.
# We will replace the dev1 part by the number of commits since the most
# Extract the <major>.<minor>.<patch> part.
# We will replace the patch version by the number of commits since the most
# recent tagged version
match = re.match(r"(?P<version>\d+\.\d+)\.dev1", version_content)
match = re.search(
r"(?P<major>\d+)\.(?P<minor>\d+)(\.(?P<patch>\w+))?", version_content
)
if not match:
logger.error(f"No <major>.<minor>.dev1 version found in {version_path.name}")
logger.error(
f"No <major>.<minor>(.<patch>)? 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<major>.<minor>.<patch>(-<commits>)? with commits omitted if
# the current commit is also the one tagged
match = re.match(
r"v(?P<version>\d+\.\d+)\.\d+(\-(?P<commits>\d+))?", tagged_version
r"v(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\w+)(\-(?P<commits>\d+))?",
last_tag,
)
if not match:
logger.error(
"Expected v<major>.<minor>.<patch>(-<commits>)? "
f"format for tag {tagged_version}"
f"Expected v<major>.<minor>.<patch>(-<commits>)? 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
# <major>.<minor> would mess up with the versioning system and then only
# <major>.<minor>.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 <major>.<minor>.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
Expand Down

0 comments on commit 3b81ff2

Please sign in to comment.