poky/meta/classes-recipe/python_pep517.bbclass
Ross Burton 9866412c42 classes-recipe/python_pep517: remove all RECORD files
Python modules install metadata into a .dist-info directory, one of which
is RECORD, which contains the files that were installed and their
checksum[1].  This is typically used by pip to validate the install, or
to know what files to remove when the module is uninstalled.

This is slightly problematic when we need to do patching of installed
.py files in do_install(), as the RECORD file has already been written
at that point.

However, the RECORD files only really have a use outside of a system-
managed environment, which our python packages are.  We already have
commands to verify and remove modules (opkg, dpkg, rpm) and the RECORD
file existing simply allows people to 'sudo pip' and alter the package-
managed directories outside of the package manager.

This is not a good idea, and some other distros remove the RECORD file
to stop this possibility:
- Debian[2]
- Fedora[3]
- Gentoo[4]

We can follow for all packages which inherit python_pep517, which is the
majority of the Python packages now.

[1] https://peps.python.org/pep-0491/#the-dist-info-directory
[2] https://salsa.debian.org/python-team/tools/dh-python/-/blob/master/dhpython/fs.py?ref_type=heads#L185
[3] https://src.fedoraproject.org/rpms/pyproject-rpm-macros/blob/rawhide/f/macros.pyproject#_105
[4] https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=73c49f3c00415dee99407dabba8d3b22895c9d25

(From OE-Core rev: 917df5ed022f9512473fe0971db48b5253c97b85)

Signed-off-by: Ross Burton <ross.burton@arm.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2024-11-21 12:16:28 +00:00

64 lines
2.1 KiB
Plaintext

#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#
# Common infrastructure for Python packages that use PEP-517 compliant packaging.
# https://www.python.org/dev/peps/pep-0517/
#
# This class will build a wheel in do_compile, and use pypa/installer to install
# it in do_install.
DEPENDS:append = " python3-build-native python3-installer-native"
# Where to execute the build process from
PEP517_SOURCE_PATH ?= "${S}"
# The directory where wheels will be written
PEP517_WHEEL_PATH ?= "${WORKDIR}/dist"
# Other options to pass to build
PEP517_BUILD_OPTS ?= ""
# The interpreter to use for installed scripts
PEP517_INSTALL_PYTHON = "python3"
PEP517_INSTALL_PYTHON:class-native = "nativepython3"
# pypa/installer option to control the bytecode compilation
INSTALL_WHEEL_COMPILE_BYTECODE ?= "--compile-bytecode=0"
# PEP517 doesn't have a specific configure step, so set an empty do_configure to avoid
# running base_do_configure.
python_pep517_do_configure () {
:
}
# When we have Python 3.11 we can parse pyproject.toml to determine the build
# API entry point directly
python_pep517_do_compile () {
nativepython3 -m build --no-isolation --wheel --outdir ${PEP517_WHEEL_PATH} ${PEP517_SOURCE_PATH} ${PEP517_BUILD_OPTS}
}
do_compile[cleandirs] += "${PEP517_WHEEL_PATH}"
python_pep517_do_install () {
COUNT=$(find ${PEP517_WHEEL_PATH} -name '*.whl' -maxdepth 1 | wc -l)
if test $COUNT -eq 0; then
bbfatal No wheels found in ${PEP517_WHEEL_PATH}
elif test $COUNT -gt 1; then
bbfatal More than one wheel found in ${PEP517_WHEEL_PATH}, this should not happen
fi
nativepython3 -m installer ${INSTALL_WHEEL_COMPILE_BYTECODE} --interpreter "${USRBINPATH}/env ${PEP517_INSTALL_PYTHON}" --destdir=${D} ${PEP517_WHEEL_PATH}/*.whl
find ${D} -path *.dist-info/RECORD -delete
}
# A manual do_install that just uses unzip for bootstrapping purposes. Callers should DEPEND on unzip-native.
python_pep517_do_bootstrap_install () {
install -d ${D}${PYTHON_SITEPACKAGES_DIR}
unzip -d ${D}${PYTHON_SITEPACKAGES_DIR} ${PEP517_WHEEL_PATH}/*.whl
}
EXPORT_FUNCTIONS do_configure do_compile do_install