Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH] pypi.eclass: A new eclass to aid creating PyPI SRC_URIs
Date: Wed, 01 Feb 2023 19:37:35
Message-Id: 20230201193727.24823-1-mgorny@gentoo.org
1 Signed-off-by: Michał Górny <mgorny@g.o>
2 ---
3 eclass/pypi.eclass | 117 +++++++++++++++++++++++++++++++++++++++++++++
4 1 file changed, 117 insertions(+)
5 create mode 100644 eclass/pypi.eclass
6
7
8 Example use on: https://github.com/gentoo/gentoo/pull/29361
9
10
11 diff --git a/eclass/pypi.eclass b/eclass/pypi.eclass
12 new file mode 100644
13 index 000000000000..12ce5d4ace15
14 --- /dev/null
15 +++ b/eclass/pypi.eclass
16 @@ -0,0 +1,117 @@
17 +# Copyright 2023 Gentoo Authors
18 +# Distributed under the terms of the GNU General Public License v2
19 +
20 +# @ECLASS: pypi.eclass
21 +# @MAINTAINER:
22 +# Michał Górny <mgorny@g.o>
23 +# @AUTHOR:
24 +# Michał Górny <mgorny@g.o>
25 +# @SUPPORTED_EAPIS: 8
26 +# @BLURB: A helper eclass to generate PyPI source URIs
27 +# @DESCRIPTION:
28 +# The pypi.eclass can be used to easily obtain URLs for artifacts
29 +# uploaded to PyPI.org. When inherited, the eclass defaults SRC_URI
30 +# to fetch ${P}.tar.gz sdist.
31 +#
32 +# If necessary, SRC_URI can be overriden by the ebuild. Two helper
33 +# functions, pypi_sdist_url and pypi_wheel_url are provided to generate
34 +# URLs to artifacts of specified type, with customizable project name.
35 +# Additionally, pypi_wheel_name can be used to generate wheel filename.
36 +#
37 +# @EXAMPLE:
38 +# @CODE@
39 +# inherit pypi
40 +#
41 +# SRC_URI="$(pypi_sdist_url "${PN^}" "${PV}")"
42 +# S=${WORKDIR}/${P^}
43 +# @CODE@
44 +
45 +case ${EAPI} in
46 + 8) ;;
47 + *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
48 +esac
49 +
50 +if [[ ! ${_PYPI_ECLASS} ]]; then
51 +_PYPI_ECLASS=1
52 +
53 +SRC_URI="
54 + https://files.pythonhosted.org/packages/source/${PN::1}/${PN}/${P}.tar.gz
55 +"
56 +
57 +# @FUNCTION: pypi_sdist_url
58 +# @USAGE: <project> [<version> [<suffix>]]
59 +# @DESCRIPTION:
60 +# Output the URL to PyPI sdist for specified project/version tuple.
61 +#
62 +# If <version> is unspecified, it defaults to ${PV}.
63 +#
64 +# If <format> is unspecified, it defaults to ".tar.gz". Another valid
65 +# value is ".zip" (please remember to add a BDEPEND on app-arch/unzip).
66 +pypi_sdist_url() {
67 + if ! has "${#}" {1..3}; then
68 + die "Usage: ${FUNCNAME} <project> [<version> [<suffix>]]"
69 + fi
70 +
71 + local project=${1}
72 + local version=${2-"${PV}"}
73 + local suffix=${3-.tar.gz}
74 + printf "https://files.pythonhosted.org/packages/source/%s" \
75 + "${project::1}/${project}/${project}-${version}${suffix}"
76 +}
77 +
78 +# @FUNCTION: pypi_wheel_name
79 +# @USAGE: <project> [<version> [<python-tag> [<abi-platform-tag>]]]
80 +# @DESCRIPTION:
81 +# Output the wheel filename for the specified project/version tuple.
82 +#
83 +# If <version> is unspecified, it defaults to ${PV}.
84 +#
85 +# If <python-tag> is unspecified, it defaults to "py3". It can also be
86 +# "py2.py3", or a specific version in case of non-pure wheels.
87 +#
88 +# If <abi-platform-tag> is unspecified, it defaults to "none-any".
89 +# You need to specify the correct value for non-pure wheels,
90 +# e.g. "abi3-linux_x86_64".
91 +pypi_wheel_name() {
92 + if ! has "${#}" {1..4}; then
93 + die "Usage: ${FUNCNAME} <project> [<version> [<python-tag> [<abi-platform-tag>]]]"
94 + fi
95 +
96 + local project=${1}
97 + local version=${2-"${PV}"}
98 + local pytag=${3-py3}
99 + local abitag=${4-none-any}
100 + echo "${project}-${version}-${pytag}-${abitag}.whl"
101 +}
102 +
103 +# @FUNCTION: pypi_wheel_url
104 +# @USAGE: <project> [<version> [<python-tag> [<abi-platform-tag>]]]
105 +# @DESCRIPTION:
106 +# Output the URL to PyPI wheel for specified project/version tuple.
107 +#
108 +# If <version> is unspecified, it defaults to ${PV}.
109 +#
110 +# If <python-tag> is unspecified, it defaults to "py3". It can also be
111 +# "py2.py3", or a specific version in case of non-pure wheels.
112 +#
113 +# If <abi-platform-tag> is unspecified, it defaults to "none-any".
114 +# You need to specify the correct value for non-pure wheels,
115 +# e.g. "abi3-linux_x86_64".
116 +#
117 +# Note that wheels are suffixed .whl by default and therefore are not
118 +# unpacked automatically. If you need automatic unpacking, use "->"
119 +# operator to rename it or call unzip directly. Remember to BDEPEND
120 +# on app-arch/unzip.
121 +pypi_wheel_url() {
122 + if ! has "${#}" {1..4}; then
123 + die "Usage: ${FUNCNAME} <project> [<version> [<python-tag> [<abi-platform-tag>]]]"
124 + fi
125 +
126 + local project=${1}
127 + local version=${2-"${PV}"}
128 + local pytag=${3-py3}
129 + printf "https://files.pythonhosted.org/packages/%s" \
130 + "${pytag}/${project::1}/${project}/$(pypi_wheel_name "${@}")"
131 +}
132 +
133 +fi
134 --
135 2.39.1

Replies

Subject Author
Re: [gentoo-dev] [PATCH] pypi.eclass: A new eclass to aid creating PyPI SRC_URIs Adrian Schollmeyer <nex@××××××.de>
Re: [gentoo-dev] [PATCH] pypi.eclass: A new eclass to aid creating PyPI SRC_URIs "Anna (cybertailor) Vyalkova" <cyber+gentoo@×××××.in>