Re: python3: Allow to specify which pyc files to keep

Lukas Woodtli
2025-04-10T11:37:00Z

[-- Attachment #1: Type: text/plain, Size: 3618 bytes --]

>
> Should we rather just package the higher level .pyc into their own
> packages?
>

But this would include 3 additional packages. One for each optimization level.
Embedded devices might have limited storage resources.
Therefore, it makes sense to just include the .pyc files with the necessary optimization level to the image.
And not all of them.

Patch with (hopefully) proper formatting:

From d7159cd842fd2a10ba5aeff62877be2ebb2eab87 Mon Sep 17 00:00:00 2001
From: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
Date: Mon, 7 Apr 2025 11:55:33 +0200
Subject: [PATCH] python3: Allow to specify which pyc files to keep

The pyc files for a specific optimization level can be kept and are
installed in the final image.

Signed-off-by: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
---
.../recipes-devtools/python/python3_3.13.2.bb | 32 +++++++++++++++----
1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/meta/recipes-devtools/python/python3_3.13.2.bb b/meta/recipes-devtools/python/python3_3.13.2.bb
index 7c36fd92ed..ac74432a2a 100644
--- a/meta/recipes-devtools/python/python3_3.13.2.bb
+++ b/meta/recipes-devtools/python/python3_3.13.2.bb
@@ -217,6 +217,12 @@ do_install:append:class-native() {
mv ${D}/${bindir}/${PN}/python*config ${D}/${bindir}/
}

+# We want bytecode precompiled .py files (.pyc's) by default
+# but the user may set it on their own conf
+INCLUDE_PYCS ?= "1"
+
+PYCS_OPT_LEVEL ?= "0"
+
do_install:append() {
for c in ${D}/${libdir}/python${PYTHON_MAJMIN}/_sysconfigdata*.py; do
python3 ${UNPACKDIR}/reformat_sysconfig.py $c
@@ -249,10 +255,25 @@ do_install:append() {
# so remove it too
rm -f ${D}${libdir}/python${PYTHON_MAJMIN}/__pycache__/traceback.cpython*

-        # Remove the opt-1.pyc and opt-2.pyc files. They effectively waste space on embedded
-        # style targets as they're only used when python is called with the -O or -OO options
-        # which is rare.
-        find ${D} -name *opt-*.pyc -delete
+        if [ "${INCLUDE_PYCS}" -eq "1" ]; then
+             # Remove only the .pyc files of unused optimization level.
+            if [ "${PYCS_OPT_LEVEL}" -eq "0" ]; then
+                # keep only unoptimized .pyc files
+                find ${D} -name *.opt-1.pyc -exec rm -f {} \;
+                find ${D} -name *.opt-2.pyc -exec rm -f {} \;
+            elif [ "${PYCS_OPT_LEVEL}" -eq "1" ]; then
+                # keep only .pyc files with optimization level 1
+                find ${D} -name *.pyc -and -not -name *.opt-1.pyc -exec rm -f {} \;
+            elif [ "${PYCS_OPT_LEVEL}" -eq "2" ]; then
+                # keep only .pyc files with optimization level 2
+                find ${D} -name *.pyc -and -not -name *.opt-2.pyc -exec rm -f {} \;
+            else
+                bberror "Python optimization level ${PYCS_OPT_LEVEL} is not supported"
+            fi
+        else
+            # remove all .pyc files
+            find ${D} -name *.pyc -delete
+        fi
}

do_install:append:class-nativesdk () {
@@ -326,9 +347,6 @@ py_package_preprocess () {
rm -rf ${PKGD}/${libdir}/python-sysconfigdata
}

-# We want bytecode precompiled .py files (.pyc's) by default
-# but the user may set it on their own conf
-INCLUDE_PYCS ?= "1"

python(){
import collections, json
--
2.43.0

[-- Attachment #2: Type: text/html, Size: 5722 bytes --]