Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
| 7 | ## |
| 8 | ## Purpose: |
| 9 | ## This class is used to update the list of crates in SRC_URI |
| 10 | ## by reading Cargo.lock in the source tree. |
| 11 | ## |
| 12 | ## See meta/recipes-devtools/python/python3-bcrypt_*.bb for an example |
| 13 | ## |
| 14 | ## To perform the update: bitbake -c update_crates recipe-name |
| 15 | |
| 16 | addtask do_update_crates after do_patch |
| 17 | do_update_crates[depends] = "python3-native:do_populate_sysroot" |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 18 | do_update_crates[nostamp] = "1" |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 19 | do_update_crates[doc] = "Update the recipe by reading Cargo.lock and write in ${THISDIR}/${BPN}-crates.inc" |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 20 | |
| 21 | # The directory where to search for Cargo.lock files |
| 22 | CARGO_LOCK_SRC_DIR ??= "${S}" |
| 23 | |
| 24 | do_update_crates() { |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 25 | TARGET_FILE="${THISDIR}/${BPN}-crates.inc" |
| 26 | |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 27 | nativepython3 - <<EOF |
| 28 | |
| 29 | def get_crates(f): |
| 30 | import tomllib |
| 31 | c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}') |
| 32 | c_list += '\nSRC_URI += " \\\' |
| 33 | crates = tomllib.load(open(f, 'rb')) |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 34 | |
| 35 | # Build a list with crates info that have crates.io in the source |
| 36 | crates_candidates = list(filter(lambda c: 'crates.io' in c.get('source', ''), crates['package'])) |
| 37 | |
| 38 | if not crates_candidates: |
| 39 | raise ValueError("Unable to find any candidate crates that use crates.io") |
| 40 | |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 41 | # Update crates uri and their checksum, to avoid name clashing on the checksum |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 42 | # we need to rename crates with name and version to have a unique key |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 43 | cksum_list = '' |
| 44 | for c in crates_candidates: |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 45 | rename = "%s-%s" % (c['name'], c['version']) |
| 46 | c_list += '\n crate://crates.io/%s/%s \\\' % (c['name'], c['version']) |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 47 | if 'checksum' in c: |
| 48 | cksum_list += '\nSRC_URI[%s.sha256sum] = "%s"' % (rename, c['checksum']) |
| 49 | |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 50 | c_list += '\n"\n' |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 51 | c_list += cksum_list |
| 52 | c_list += '\n' |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 53 | return c_list |
| 54 | |
| 55 | import os |
| 56 | crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n" |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 57 | found = False |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 58 | for root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'): |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 59 | # ignore git and patches directories |
| 60 | if root.startswith(os.path.join('${CARGO_LOCK_SRC_DIR}', '.pc')): |
| 61 | continue |
| 62 | if root.startswith(os.path.join('${CARGO_LOCK_SRC_DIR}', '.git')): |
| 63 | continue |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 64 | for file in files: |
| 65 | if file == 'Cargo.lock': |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 66 | try: |
| 67 | cargo_lock_path = os.path.join(root, file) |
| 68 | crates += get_crates(os.path.join(root, file)) |
| 69 | except Exception as e: |
| 70 | raise ValueError("Cannot parse '%s'" % cargo_lock_path) from e |
| 71 | else: |
| 72 | found = True |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 73 | if not found: |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 74 | raise ValueError("Unable to find any Cargo.lock in ${CARGO_LOCK_SRC_DIR}") |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 75 | open("${TARGET_FILE}", 'w').write(crates) |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 76 | EOF |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 77 | |
| 78 | bbnote "Successfully update crates inside '${TARGET_FILE}'" |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 79 | } |