[v3,05/32] pip_install_wheel.bbclass: add helper class

Message ID 7ded178838deb30461a06ebf7cfef04f0a08af74.1645557032.git.tim.orling@konsulko.com
State Accepted, archived
Commit 32a61afde0e7d8df6634b88525d8c3e8c6c3516e
Headers show
Series [v3,01/32] python3-wheel: move 0.37.1 from meta-python | expand

Commit Message

Tim Orling Feb. 22, 2022, 7:16 p.m. UTC
Provide a helper class to use pip to install wheels built by either
bdist_wheel or a PEP-517 backend.

Set pip install arguments via PIP_INSTALL_ARGS, which can be overriden
by recipes.

Pass --root and --prefix to ensure that pip installs things into the
proper place in sysroot.

By passing --no-deps and --no-index we avoid finicky dependency
checking (pip expects wheels in its cache) and avoid trying to fetch
wheels from pypi.org. This is basically the same behavior we have now,
the dependencies should be declared in the recipe.

Also pass --force-reinstall to make sure built wheels are always installed
so that FILES gets properly populated.

Pass --no-cache to avoid a (harmless) warning about the pip cache in
$HOME be avoiding use of cache. We do not likely want wheels cached
anyway,

pip install changes the python interpreter in scripts installed in
${bindir}, e.g. to #!/usr/bin/nativepython3, correct the behavior after
install to #!/usr/bin/env python3.

[YOCTO #14638]

Signed-off-by: Tim Orling <tim.orling@konsulko.com>
---
 meta/classes/pip_install_wheel.bbclass | 39 ++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 meta/classes/pip_install_wheel.bbclass

Patch

diff --git a/meta/classes/pip_install_wheel.bbclass b/meta/classes/pip_install_wheel.bbclass
new file mode 100644
index 00000000000..70f47d6f79c
--- /dev/null
+++ b/meta/classes/pip_install_wheel.bbclass
@@ -0,0 +1,39 @@ 
+DEPENDS:append = " python3-pip-native"
+
+PIP_INSTALL_PACKAGE ?= "${PYPI_PACKAGE}"
+PIP_INSTALL_DIST_PATH ?= "${B}/dist"
+PYPA_WHEEL ??= "${PIP_INSTALL_DIST_PATH}/${PIP_INSTALL_PACKAGE}-${PV}-*.whl"
+
+PIP_INSTALL_ARGS ?= "\
+    -vvvv \
+    --force-reinstall \
+    --no-cache \
+    --no-deps \
+    --no-index \
+    --root=${D} \
+    --prefix=${prefix} \
+"
+
+pip_install_wheel_do_install:prepend () {
+    install -d ${D}${PYTHON_SITEPACKAGES_DIR}
+}
+
+export PYPA_WHEEL
+
+PIP_INSTALL_PYTHON = "python3"
+PIP_INSTALL_PYTHON:class-native = "nativepython3"
+
+pip_install_wheel_do_install () {
+    nativepython3 -m pip install ${PIP_INSTALL_ARGS} ${PYPA_WHEEL} ||
+    bbfatal_log "Failed to pip install wheel. Check the logs."
+
+    for i in ${D}${bindir}/* ${D}${sbindir}/*; do
+        if [ -f "$i" ]; then
+            sed -i -e "1s,#!.*nativepython3,#!${USRBINPATH}/env ${PIP_INSTALL_PYTHON}," $i
+            sed -i -e "s:${PYTHON}:${USRBINPATH}/env\ ${PIP_INSTALL_PYTHON}:g" $i
+            sed -i -e "s:${STAGING_BINDIR_NATIVE}:${bindir}:g" $i
+        fi
+    done
+}
+
+EXPORT_FUNCTIONS do_install