public inbox for openembedded-core@lists.openembedded.org
 help / color / mirror / Atom feed
From: "Lukas Woodtli" <lw@hqv.ch>
To: openembedded-core@lists.openembedded.org
Subject: Re: python3: Allow to specify which pyc files to keep
Date: Thu, 10 Apr 2025 04:36:50 -0700	[thread overview]
Message-ID: <13400.1744285010599994886@lists.openembedded.org> (raw)
In-Reply-To: <CANNYZj9qgX5=4Jym0Uq2u5VJcZ89c-MRSU9oiTkUbBvNvfSFtQ@mail.gmail.com>

[-- 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 --]

  reply	other threads:[~2025-04-10 11:37 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-10  6:37 python3: Allow to specify which pyc files to keep Lukas Woodtli
2025-04-10  8:02 ` [OE-core] " Alexander Kanavin
2025-04-10 11:36   ` Lukas Woodtli [this message]
2025-04-10 11:58     ` Richard Purdie
2025-04-10 12:11       ` Alexander Kanavin
2025-04-10 13:05         ` Lukas Woodtli
2025-04-10 14:31           ` [OE-core] " Alexander Kanavin
2025-04-11  8:55             ` Mike Looijmans
2025-04-17  9:19               ` Lukas Woodtli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=13400.1744285010599994886@lists.openembedded.org \
    --to=lw@hqv.ch \
    --cc=openembedded-core@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox