Port the do_update_crates() from Shell to a real Python function. This improves readability by directly executing Python code, instead of redirecting it through a string.
In the process, two new variables "target_file" and "cargo_lock_src_dir" are introduced, which just contain the previously, similarly named Bitbake variables in the Python function. No functional changes are made. Signed-off-by: Martin Schwan <[email protected]> --- Changes in v2: - Split changes into multiple patches, for improved readability --- .../cargo-update-recipe-crates.bbclass | 93 +++++++++---------- 1 file changed, 46 insertions(+), 47 deletions(-) diff --git a/meta/classes-recipe/cargo-update-recipe-crates.bbclass b/meta/classes-recipe/cargo-update-recipe-crates.bbclass index 3251d5ef2e..2ccaf80e40 100644 --- a/meta/classes-recipe/cargo-update-recipe-crates.bbclass +++ b/meta/classes-recipe/cargo-update-recipe-crates.bbclass @@ -23,59 +23,58 @@ 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() { + import tomllib - nativepython3 - <<EOF + target_file = d.expand('${THISDIR}/${BPN}-crates.inc') + cargo_lock_src_dir = d.getVar('CARGO_LOCK_SRC_DIR') -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')) + def get_crates(f): + c_list = '# from %s' % os.path.relpath(f, cargo_lock_src_dir) + c_list += '\nSRC_URI += " \\' + crates = tomllib.load(open(f, 'rb')) - # 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: - raise ValueError("Unable to find any candidate crates that use crates.io") + if not crates_candidates: + raise ValueError("Unable to find any candidate crates that use crates.io") - # 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': - try: - cargo_lock_path = os.path.join(root, file) - crates += get_crates(os.path.join(root, file)) - except Exception as e: - raise ValueError("Cannot parse '%s'" % cargo_lock_path) from e - else: - found = True -if not found: - raise ValueError("Unable to find any Cargo.lock in ${CARGO_LOCK_SRC_DIR}") -open("${TARGET_FILE}", 'w').write(crates) -EOF + import os + crates = d.expand("# 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': + try: + cargo_lock_path = os.path.join(root, file) + crates += get_crates(os.path.join(root, file)) + except Exception as e: + raise ValueError("Cannot parse '%s'" % cargo_lock_path) from e + else: + found = True + if not found: + raise ValueError(f"Unable to find any Cargo.lock in {cargo_lock_src_dir}") + open(target_file, 'w').write(crates) - bbnote "Successfully update crates inside '${TARGET_FILE}'" + bb.note(f"Successfully update crates inside '{target_file}'") } -- 2.43.0
-=-=-=-=-=-=-=-=-=-=-=- Links: You receive all messages sent to this group. View/Reply Online (#227574): https://lists.openembedded.org/g/openembedded-core/message/227574 Mute This Topic: https://lists.openembedded.org/mt/116743596/21656 Group Owner: [email protected] Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub [[email protected]] -=-=-=-=-=-=-=-=-=-=-=-
