Signed-off-by: Michał Górny <[email protected]>
---
 eclass/pypi.eclass   | 19 +++++++++++++++++++
 eclass/tests/pypi.sh |  7 +++++++
 2 files changed, 26 insertions(+)

diff --git a/eclass/pypi.eclass b/eclass/pypi.eclass
index dd24b8337e62..27bd17403100 100644
--- a/eclass/pypi.eclass
+++ b/eclass/pypi.eclass
@@ -54,6 +54,25 @@ pypi_normalize_name() {
        echo "${name,,}"
 }
 
+# @FUNCTION: pypi_translate_version
+# @USAGE: <version>
+# @DESCRIPTION:
+# Translate the specified Gentoo version into the usual Python
+# counterpart.  Assumes PEP 440 versions.
+#
+# Note that we do not have clear counterparts for the epoch segment,
+# nor for development release segment.
+pypi_translate_version() {
+       [[ ${#} -ne 1 ]] && die "Usage: ${FUNCNAME} <version>"
+
+       local version=${1}
+       version=${version/_alpha/a}
+       version=${version/_beta/b}
+       version=${version/_rc/rc}
+       version=${version/_p/.post}
+       echo "${version}"
+}
+
 # @FUNCTION: pypi_sdist_url
 # @USAGE: [--no-normalize] [<project> [<version> [<suffix>]]]
 # @DESCRIPTION:
diff --git a/eclass/tests/pypi.sh b/eclass/tests/pypi.sh
index 385b1c028bce..af5d714af748 100755
--- a/eclass/tests/pypi.sh
+++ b/eclass/tests/pypi.sh
@@ -32,6 +32,13 @@ test-eq "pypi_normalize_name foo___bar" foo_bar
 test-eq "pypi_normalize_name Flask-BabelEx" flask_babelex
 test-eq "pypi_normalize_name jaraco.context" jaraco_context
 
+test-eq "pypi_translate_version 1.2.3" 1.2.3
+test-eq "pypi_translate_version 1.2.3_p101" 1.2.3.post101
+test-eq "pypi_translate_version 1.2.3_alpha4" 1.2.3a4
+test-eq "pypi_translate_version 1.2.3_beta1" 1.2.3b1
+test-eq "pypi_translate_version 1.2.3_rc2" 1.2.3rc2
+test-eq "pypi_translate_version 1.2.3_rc2_p1" 1.2.3rc2.post1
+
 test-eq "pypi_wheel_name" foo_bar-1.2.3-py3-none-any.whl
 test-eq "pypi_wheel_name Flask-BabelEx" flask_babelex-1.2.3-py3-none-any.whl
 test-eq "pypi_wheel_name Flask-BabelEx 4" flask_babelex-4-py3-none-any.whl
-- 
2.39.1


Reply via email to