diff mbox series

[2/2] classes/cargo-update-recipe-crates: use tomllib directly

Message ID 20260909151906.3417094-2-ross.burton@arm.com
State Under Review
Headers show
Series [1/2] classes/setuptools: use tomllib instead of regex to parse pyproject.toml | expand

Commit Message

Ross Burton Sept. 9, 2026, 3:19 p.m. UTC
Now that BitBake vendors in a copy of tomli for when the host Python is
too old to support tomllib, we can do the crate update logic directly
in the task instead of passing an large inline script to python3-native.

The only changes are the removal of nativepython3 and not relying on
bitbake's shell expansion to expand variable names.

[1] bitbake 69810e0c0a ("lib/bb/_vendor: resync to add tomli")

Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 .../cargo-update-recipe-crates.bbclass        | 101 ++++++++++--------
 1 file changed, 54 insertions(+), 47 deletions(-)
diff mbox series

Patch

diff --git a/meta/classes-recipe/cargo-update-recipe-crates.bbclass b/meta/classes-recipe/cargo-update-recipe-crates.bbclass
index 47e845c8221..e62039208c1 100644
--- a/meta/classes-recipe/cargo-update-recipe-crates.bbclass
+++ b/meta/classes-recipe/cargo-update-recipe-crates.bbclass
@@ -14,7 +14,6 @@ 
 ## To perform the update: bitbake -c update_crates recipe-name
 
 addtask do_update_crates after do_patch
-do_update_crates[depends] = "python3-native:do_populate_sysroot"
 do_update_crates[nostamp] = "1"
 do_update_crates[doc] = "Update the recipe by reading Cargo.lock and write in ${THISDIR}/${BPN}-crates.inc"
 
@@ -23,58 +22,66 @@  RECIPE_UPGRADE_EXTRA_TASKS += "do_update_crates"
 # The directory where to search for Cargo.lock files
 CARGO_LOCK_SRC_DIR ??= "${S}"
 
-do_update_crates() {
-    TARGET_FILE="${THISDIR}/${BPN}-crates.inc"
+python do_update_crates() {
+    cargo_lock_src_dir = d.getVar("CARGO_LOCK_SRC_DIR")
 
-    nativepython3 - <<EOF
+    def get_crates(filename):
+        try:
+            import tomllib
+        except ImportError:
+            import bb._vendor.tomli as tomllib
 
-def get_crates(f):
-    import tomllib
-    c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}')
-    c_list += '\nSRC_URI += " \\\'
-    crates = tomllib.load(open(f, 'rb'))
+        c_list = '# from %s' % os.path.relpath(filename, cargo_lock_src_dir)
+        c_list += '\nSRC_URI += " \\'
+        with open(filename, 'rb') as fp:
+            crates = tomllib.load(fp)
 
-    # Build a list with crates info that have crates.io in the source
-    crates_candidates = list(filter(lambda c: 'crates.io' in c.get('source', ''), crates['package']))
+        # Build a list with crates info that have crates.io in the source
+        crates_candidates = list(filter(lambda c: 'crates.io' in c.get('source', ''), crates['package']))
 
-    if not crates_candidates:
-        print("WARNING: Unable to find any candidate crates that use crates.io")
-        return None
+        if not crates_candidates:
+            print("WARNING: Unable to find any candidate crates that use crates.io")
+            return None
 
-    # Update crates uri and their checksum, to avoid name clashing on the checksum
-    # we need to rename crates with name and version to have a unique key
-    cksum_list = ''
-    for c in crates_candidates:
-        rename = "%s-%s" % (c['name'], c['version'])
-        c_list += '\n    crate://crates.io/%s/%s \\\' % (c['name'], c['version'])
-        if 'checksum' in c:
-            cksum_list += '\nSRC_URI[%s.sha256sum] = "%s"' % (rename, c['checksum'])
+        # Update crates uri and their checksum, to avoid name clashing on the checksum
+        # we need to rename crates with name and version to have a unique key
+        cksum_list = ''
+        for c in crates_candidates:
+            rename = "%s-%s" % (c['name'], c['version'])
+            c_list += '\n    crate://crates.io/%s/%s \\' % (c['name'], c['version'])
+            if 'checksum' in c:
+                cksum_list += '\nSRC_URI[%s.sha256sum] = "%s"' % (rename, c['checksum'])
 
-    c_list += '\n"\n'
-    c_list += cksum_list
-    c_list += '\n'
-    return c_list
+        c_list += '\n"\n'
+        c_list += cksum_list
+        c_list += '\n'
+        return c_list
 
-import os
-crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n"
-found = False
-for root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'):
-    # ignore git and patches directories
-    if root.startswith(os.path.join('${CARGO_LOCK_SRC_DIR}', '.pc')):
-        continue
-    if root.startswith(os.path.join('${CARGO_LOCK_SRC_DIR}', '.git')):
-        continue
-    for file in files:
-        if file == 'Cargo.lock':
-            cargo_lock_path = os.path.join(root, file)
-            c = get_crates(cargo_lock_path)
-            if c is not None:
-                crates += c
-if crates is None:
-    raise ValueError("Unable to find any Cargo.lock in ${CARGO_LOCK_SRC_DIR}")
-with open("${TARGET_FILE}", 'w') as f:
-    f.write(crates)
-EOF
+    import os
 
-    bbnote "Successfully update crates inside '${TARGET_FILE}'"
+    pn = d.getVar("PN")
+    crates = f"# Autogenerated with 'bitbake -c update_crates {pn}'\n\n"
+
+    found = False
+    for root, dirs, files in os.walk(cargo_lock_src_dir):
+        # ignore git and patches directories
+        if root.startswith(os.path.join(cargo_lock_src_dir, '.pc')):
+            continue
+        if root.startswith(os.path.join(cargo_lock_src_dir, '.git')):
+            continue
+        for file in files:
+            if file == 'Cargo.lock':
+                cargo_lock_path = os.path.join(root, file)
+                c = get_crates(cargo_lock_path)
+                if c is not None:
+                    crates += c
+
+    if crates is None:
+        raise ValueError(f"Unable to find any Cargo.lock in {cargo_lock_src_dir}")
+
+    target_file = d.expand("${THISDIR}/${BPN}-crates.inc")
+    with open(target_file, 'w') as f:
+        f.write(crates)
+
+    bb.note(f"Successfully updated crates inside {target_file}")
 }