From: Ross Burton <ross.burton@arm.com>
To: openembedded-core@lists.openembedded.org
Subject: [PATCH 2/2] classes/cargo-update-recipe-crates: use tomllib directly
Date: Wed, 9 Sep 2026 16:19:06 +0100 [thread overview]
Message-ID: <20260909151906.3417094-2-ross.burton@arm.com> (raw)
In-Reply-To: <20260909151906.3417094-1-ross.burton@arm.com>
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 --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}")
}
--
2.43.0
next prev parent reply other threads:[~2026-09-09 15:19 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-09 15:19 [PATCH 1/2] classes/setuptools: use tomllib instead of regex to parse pyproject.toml Ross Burton
2026-09-09 15:19 ` Ross Burton [this message]
2026-09-09 17:37 ` [OE-core] " Mathieu Dubois-Briand
2026-09-09 21:45 ` Ross Burton
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=20260909151906.3417094-2-ross.burton@arm.com \
--to=ross.burton@arm.com \
--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