Skip to content

Commit 8af3884

Browse files
committed
semver convertor to pep440 added
1 parent a3c8557 commit 8af3884

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

.github/workflows/on_release.yml

+16-16
Original file line numberDiff line numberDiff line change
@@ -240,23 +240,23 @@ jobs:
240240
# This is the root file representing the package for all the sub-packages.
241241
- name: Bump version - packaging__.py
242242
run: |
243+
# Install required packages
244+
pip install semver
245+
246+
# Get version tag and remove 'v' prefix
243247
version_tag=${{ github.event.release.tag_name }}
244-
version_tag=${version_tag#v} # Remove the leading 'v'
245-
246-
# Handle RC versions
247-
if [[ $version_tag =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)[-.]?rc([0-9]+)$ ]]; then
248-
# For RC versions (PEP 440 format)
249-
major="${BASH_REMATCH[1]}"
250-
minor="${BASH_REMATCH[2]}"
251-
patch="${BASH_REMATCH[3]}"
252-
rc_num="${BASH_REMATCH[4]}"
253-
version_tuple="$major, $minor, $patch, 'rc', $rc_num" # Will become "1.2.3rc1"
254-
else
255-
# Regular versions
256-
version_tuple=$(echo $version_tag | sed 's/\./, /g')
257-
fi
258-
259-
sed -i "s/VERSION = (.*/VERSION = (${version_tuple})/" packages/__packaging__.py
248+
version_tag=${version_tag#v}
249+
250+
# Convert semver to PyPI version using the script
251+
pypi_version=$(python smver2pypi.py $version_tag)
252+
253+
# Update only the __version__ in __packaging__.py
254+
sed -i "s/__version__ = VERSION_STRING/__version__ = \"$pypi_version\"/" packages/__packaging__.py
255+
256+
# Print the result for verification
257+
echo "Original version tag: $version_tag"
258+
echo "PyPI version: $pypi_version"
259+
cat packages/__packaging__.py
260260
261261
- name: Cleanup setup.py and Build every sub-packages
262262
run: |

smver2pypi.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Converts a semver version into a version for PyPI package
2+
https://peps.python.org/pep-0440/
3+
4+
A semver prerelease will be converted into prerelease of PyPI.
5+
A semver build will be converted into a development part of PyPI
6+
7+
Usage:
8+
python smver2pypi.py 0.1.0-rc1
9+
0.1.0rc1
10+
python smver2pypi.py 0.1.0-dev1
11+
0.1.0.dev1
12+
python smver2pypi.py 0.1.0
13+
0.1.0
14+
"""
15+
16+
import sys
17+
18+
from semver import Version as SemVerVersion
19+
from packaging.version import Version as PyPIVersion
20+
21+
semver_version = SemVerVersion.parse(sys.argv[1])
22+
finalized_version = semver_version.finalize_version()
23+
prerelease = semver_version.prerelease or ""
24+
build = semver_version.build or ""
25+
pypi_version = PyPIVersion(f"{finalized_version}{prerelease}{build}")
26+
print(pypi_version)

0 commit comments

Comments
 (0)