APKBUILD examples:Python: Difference between revisions

From Alpine Linux
m (fd1. one day i'll get it right)
Tag: Manual revert
(modernise)
Line 18: Line 18:
=== source ===
=== source ===


Most Python packages are published in [https://pypi.python.org/pypi PyPI] (the Python Package Index).
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:
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:


Line 31: Line 31:
== Examples ==
== Examples ==


=== Package for setuptools/setup.py build ===
=== setup.py invocation ===


If the project has a setup.py, then the APKBUILD is very simple:
If the project has a setup.py, then the APKBUILD goes as follows:


<pre>
<pre>
Line 57: Line 57:
</pre>
</pre>


Note that PyPI tarballs can contain a generated setup.py that does not exist in the upstream repo - this means you must use the pyproject.toml method if you change sources.
However, even with a setup.py, you can use the pep517 build process. If a project does not have a setup.py and only has a pyproject.toml, then you can only use the pep517 build process below.


=== Package for pyproject.toml build ===
=== pep517 invocation ===


<pre>
<pre>
pkgname="py-foo"
pkgname="py3-foo"
_pyname="PyFoo"
...
...
depends="python3 py3-bar py3-baz"
depends="python3 py3-bar py3-baz"
makedepends="py3-gpep517 py3-installer python3-dev py3-wheel"
makedepends="py3-gpep517 py3-setuptools py3-wheel python3-dev"
checkdepends="py3-pytest"
...
...


Line 72: Line 72:
gpep517 build-wheel \
gpep517 build-wheel \
--wheel-dir dist \
--wheel-dir dist \
--output-fd 1
--output-fd 3 3>&1 >&2
}
}


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


package() {
package() {
python3 -m installer -d "$pkgdir" \
python3 -m installer -d "$pkgdir" \
dist/PyFoo-$pkgver-py3-none-any.whl
dist/*.whl
}
}
</pre>
</pre>


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.
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.
 
Sometimes you might have to pass the path to the built package to run tests: 
 
<pre>
check() {
python3 -m venv --system-site-packages testenv
testenv/bin/python3 -m installer \
dist/PyFoo-$pkgver-py3-none-any.whl
testenv/bin/python3 -m pytest
}
</pre>


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

Revision as of 14:07, 30 January 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). Also add python3 to depends=.
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

setup.py invocation

If the project has a setup.py, then the APKBUILD goes as follows:

pkgname="py3-foo"
_pyname="foo"
...
depends="python3"
makedepends="python3-dev"
checkdepends="py3-pytest"
...

build() {
	python3 setup.py build
}

check() {
	pytest
}

package() {
	python3 setup.py install --skip-build --root="$pkgdir"
}

However, even with a setup.py, you can use the pep517 build process. If a project does not have a setup.py and only has a pyproject.toml, then you can only use the pep517 build process below.

pep517 invocation

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

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

check() {
	python3 -m venv --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.