APKBUILD examples:Python: Difference between revisions

From Alpine Linux
(Add py-setuptools to makedepends)
(gpep517 only)
 
(32 intermediate revisions by 6 users not shown)
Line 1: Line 1:
A lot of Python packages use the ''setuptools'' or ''distutils'' framework. This mean that the build() and the package() section looks a bit different compared to an application which uses ''make''.
A lot of Python packages use the ''setuptools'' or ''distutils'' framework. This mean that the build() and the package() section looks a bit different compared to an application which uses ''make''.
== Considerations ==
=== pkgname ===
Package name for a Python ''library'' must be prefixed with ''py3-''. 
For an 'executable' (for example, <code>black</code>, <code>binwalk</code>), you generally don't need to prefix it. 
There’s no exact rule if the prefix should be used for tools and applications written in Python, it varies.
=== arch ===
; noarch : Use for pure Python packages (i.e. without compiled code).
; all (and others) :  Use for packages with native extensions (i.e. with compiled code). '''Do not''' add python3 to <tt>depends=</tt> (it's auto-detected via dynamic linking to python library).
=== source ===
Most Python packages are published in [https://pypi.python.org/pypi PyPI](the Python Package Index).
If the package has a source tarball available in PyPI (that’s true for most packages), and it contains tests (some explicitly remove them from PyPI), you should reference it in <tt>source=</tt> as:


<pre>
<pre>
pkgname="py3-foo"
https://files.pythonhosted.org/packages/source/${_pyname%${_pyname#?}}/$_pyname/$_pyname-$pkgver.tar.gz
...
</pre>
depends="python3"
 
makedepends="python3-dev"
where <tt>_pyname</tt> is the real name of the Python package.
...


build() {
Otherwise, use the normal upstream git tarballs.
cd "$builddir"
python3 setup.py build
}


package() {
== Examples ==
cd "$builddir"
python3 setup.py install --prefix=/usr --root="$pkgdir"
}
</pre>


If Python package support both Python 2 and Python 3, then the Alpine package should provide both variants as ''py2-'' and ''py3-'' subpackages.
=== pep517 invocation ===


<pre>
<pre>
pkgname="py-foo"
pkgname="py3-foo"
_pkgname="PyFoo"
...
...
arch="noarch"
depends="py3-bar py3-baz"
depends="py-bar"
makedepends="py3-gpep517 py3-setuptools py3-wheel python3-dev"
makedepends="python2-dev python3-dev py-setuptools"
checkdepends="py3-pytest"
subpackages="py2-${pkgname#py-}:_py2 py3-${pkgname#py-}:_py3"
subpackages="$pkgname-pyc"
...
...


build() {
build() {
cd "$builddir"
gpep517 build-wheel \
python2 setup.py build || return 1
--wheel-dir dist \
python3 setup.py build || return 1
--output-fd 3 3>&1 >&2
}
}


package() {
check() {
mkdir -p "$pkgdir"
python3 -m venv --clear --system-site-packages testenv
testenv/bin/python3 -m installer dist/*.whl
testenv/bin/python3 -m pytest
}
}


_py2() {
package() {
replaces="$pkgname"
python3 -m installer -d "$pkgdir" \
depends="${depends//py-/py2-}"
dist/*.whl
_py python2
}
 
_py3() {
depends="${depends//py-/py3-}"
_py python3
}
 
_py() {
local python="$1"
pkgdesc="$pkgdesc (for $python)"
depends="$depends $python"
install_if="$pkgname=$pkgver-r$pkgrel $python"
 
cd "$builddir"
$python setup.py install --prefix=/usr --root="$subpkgdir"
}
}
</pre>
</pre>


* If the package doesn’t have any dependencies, then omit <tt>depends=""</tt> in the <tt>_py2()</tt> and <tt>_py3()</tt> functions.
Depending on the <code>build-backend</code> in the pyproject.toml, you might need py3-setuptools or py3-flit-core or py3-poetry-core or py3-hatchling at build time. If a project specifies literally <code>flit</code> or <code>poetry</code>, patch it to use the <code>-core</code> variant.
* If the package contains some compiled code (native extensions), then replace the <tt>subpackages=</tt> line with: <pre>subpackages="py2-${pkgname#py-}:_py2:all py3-${pkgname#py-}:_py3:all"</pre>
 
Source located at [http://pypi.python.org PyPi]
<pre>
_pkgname=ShortName
...
source="https://files.pythonhosted.org/packages/source/${_pkgname:0:1}/$_pkgname/$_pkgname-$pkgver.tar.gz"
</pre>


[[Category:Development]] [[Category:Python]]
[[Category:Development]] [[Category:Python]]

Latest revision as of 07:06, 11 May 2023

A lot of Python packages use the setuptools or distutils framework. This mean that the build() and the package() section looks a bit different compared to an application which uses make.

Considerations

pkgname

Package name for a Python library must be prefixed with py3-.

For an 'executable' (for example, black, binwalk), you generally don't need to prefix it.

There’s no exact rule if the prefix should be used for tools and applications written in Python, it varies.

arch

noarch
Use for pure Python packages (i.e. without compiled code).
all (and others)
Use for packages with native extensions (i.e. with compiled code). Do not add python3 to depends= (it's auto-detected via dynamic linking to python library).

source

Most Python packages are published in PyPI(the Python Package Index). If the package has a source tarball available in PyPI (that’s true for most packages), and it contains tests (some explicitly remove them from PyPI), you should reference it in source= as:

https://files.pythonhosted.org/packages/source/${_pyname%${_pyname#?}}/$_pyname/$_pyname-$pkgver.tar.gz

where _pyname is the real name of the Python package.

Otherwise, use the normal upstream git tarballs.

Examples

pep517 invocation

pkgname="py3-foo"
...
depends="py3-bar py3-baz"
makedepends="py3-gpep517 py3-setuptools py3-wheel python3-dev"
checkdepends="py3-pytest"
subpackages="$pkgname-pyc"
...

build() {
	gpep517 build-wheel \
		--wheel-dir dist \
		--output-fd 3 3>&1 >&2
}

check() {
	python3 -m venv --clear --system-site-packages testenv
	testenv/bin/python3 -m installer dist/*.whl
	testenv/bin/python3 -m pytest
}

package() {
	python3 -m installer -d "$pkgdir" \
		dist/*.whl
}

Depending on the build-backend in the pyproject.toml, you might need py3-setuptools or py3-flit-core or py3-poetry-core or py3-hatchling at build time. If a project specifies literally flit or poetry, patch it to use the -core variant.