summaryrefslogtreecommitdiff
path: root/eclass
diff options
context:
space:
mode:
authorMichał Górny <mgorny@gentoo.org>2023-02-10 14:18:54 +0100
committerMichał Górny <mgorny@gentoo.org>2023-02-12 20:00:42 +0100
commitbe152335ee2cf11dc33378c2903873ad742e8116 (patch)
tree6b0702b5a776c440f837e8bd0663bfff1f4f17f9 /eclass
parentpypi.eclass: Use pypi_sdist_url to generate the default SRC_URI (diff)
downloadgentoo-be152335ee2cf11dc33378c2903873ad742e8116.tar.gz
gentoo-be152335ee2cf11dc33378c2903873ad742e8116.tar.bz2
gentoo-be152335ee2cf11dc33378c2903873ad742e8116.zip
pypi.eclass: Add a name normalization function
Signed-off-by: Michał Górny <mgorny@gentoo.org>
Diffstat (limited to 'eclass')
-rw-r--r--eclass/pypi.eclass20
-rwxr-xr-xeclass/tests/pypi.sh32
2 files changed, 52 insertions, 0 deletions
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