public inbox for openembedded-core@lists.openembedded.org
 help / color / mirror / Atom feed
* python3: Allow to specify which pyc files to keep
@ 2025-04-10  6:37 Lukas Woodtli
  2025-04-10  8:02 ` [OE-core] " Alexander Kanavin
  0 siblings, 1 reply; 9+ messages in thread
From: Lukas Woodtli @ 2025-04-10  6:37 UTC (permalink / raw)
  To: openembedded-core

 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




^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-04-17  9:19 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2025-04-10 11:58     ` [OE-core] " 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox