Signed-off-by: Michał Górny <[email protected]>
---
 eclass/pypi.eclass   | 20 ++++++++++++++++++++
 eclass/tests/pypi.sh | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
 create mode 100755 eclass/tests/pypi.sh

diff --git a/eclass/pypi.eclass b/eclass/pypi.eclass
index d00b1171fd16..3a37214f8977 100644
--- a/eclass/pypi.eclass
+++ b/eclass/pypi.eclass
@@ -34,6 +34,26 @@ esac
 if [[ ! ${_PYPI_ECLASS} ]]; then
 _PYPI_ECLASS=1
 
+# @FUNCTION: pypi_normalize_name
+# @USAGE: <name>
+# @DESCRIPTION:
+# Normalize the project name according to sdist/wheel normalization
+# rules.  That is, convert to lowercase and replace runs of [._-]
+# with a single underscore.
+#
+# Based on the spec, as of 2023-02-10:
+# 
https://packaging.python.org/en/latest/specifications/#package-distribution-file-formats
+pypi_normalize_name() {
+       [[ ${#} -ne 1 ]] && die "Usage: ${FUNCNAME} <name>"
+
+       local name=${1}
+       local shopt_save=$(shopt -p extglob)
+       shopt -s extglob
+       name=${name//+([._-])/_}
+       ${shopt_save}
+       echo "${name,,}"
+}
+
 # @FUNCTION: pypi_sdist_url
 # @USAGE: [<project> [<version> [<suffix>]]]
 # @DESCRIPTION:
diff --git a/eclass/tests/pypi.sh b/eclass/tests/pypi.sh
new file mode 100755
index 000000000000..67b2c3c481fb
--- /dev/null
+++ b/eclass/tests/pypi.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+# Copyright 2023 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=8
+source tests-common.sh || exit
+
+inherit pypi
+
+test-eq() {
+       local call=${1}
+       local exp=${2}
+
+       tbegin "${call} -> ${exp}"
+       local ret=0
+       local have=$(${call})
+       if [[ ${have} != ${exp} ]]; then
+               eindent
+               eerror "incorrect result: ${have}"
+               eoutdent
+               ret=1
+       fi
+       tend "${ret}"
+}
+
+test-eq "pypi_normalize_name foo" foo
+test-eq "pypi_normalize_name foo_bar" foo_bar
+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
+
+texit
-- 
2.39.1


Reply via email to