diff mbox series

[1/3] cargo-update-recipe-crates: generate checksum for each crates

Message ID 20230316121249.28209-1-frederic.martinsons@gmail.com
State New
Headers show
Series [1/3] cargo-update-recipe-crates: generate checksum for each crates | expand

Commit Message

Frédéric Martinsons March 16, 2023, 12:12 p.m. UTC
From: Frederic Martinsons <frederic.martinsons@gmail.com>

This is related to checksum verification introduction from
https://patchwork.yoctoproject.org/project/bitbake/patch/20230315131513.50635-1-frederic.martinsons@gmail.com/

I also choose to raise an exception if:
 - no crates can be found
 - no Cargo.lock file exist

Otherwise the generated inc file will silently be emptied.

Signed-off-by: Frederic Martinsons <frederic.martinsons@gmail.com>
---
 .../cargo-update-recipe-crates.bbclass        | 44 +++++++++++++++++--
 1 file changed, 40 insertions(+), 4 deletions(-)

Comments

Frédéric Martinsons March 16, 2023, 12:36 p.m. UTC | #1
Sorry, I messed up with the different mailing list, this patch series
should be for openembedded-core

Le jeu. 16 mars 2023, 13:12, <frederic.martinsons@gmail.com> a écrit :

> From: Frederic Martinsons <frederic.martinsons@gmail.com>
>
> This is related to checksum verification introduction from
>
> https://patchwork.yoctoproject.org/project/bitbake/patch/20230315131513.50635-1-frederic.martinsons@gmail.com/
>
> I also choose to raise an exception if:
>  - no crates can be found
>  - no Cargo.lock file exist
>
> Otherwise the generated inc file will silently be emptied.
>
> Signed-off-by: Frederic Martinsons <frederic.martinsons@gmail.com>
> ---
>  .../cargo-update-recipe-crates.bbclass        | 44 +++++++++++++++++--
>  1 file changed, 40 insertions(+), 4 deletions(-)
>
> diff --git a/meta/classes-recipe/cargo-update-recipe-crates.bbclass
> b/meta/classes-recipe/cargo-update-recipe-crates.bbclass
> index 697460d215..daa363b0dd 100644
> --- a/meta/classes-recipe/cargo-update-recipe-crates.bbclass
> +++ b/meta/classes-recipe/cargo-update-recipe-crates.bbclass
> @@ -16,11 +16,14 @@
>  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"
>
>  # The directory where to search for Cargo.lock files
>  CARGO_LOCK_SRC_DIR ??= "${S}"
>
>  do_update_crates() {
> +    TARGET_FILE="${THISDIR}/${BPN}-crates.inc"
> +
>      nativepython3 - <<EOF
>
>  def get_crates(f):
> @@ -28,19 +31,52 @@ 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'))
> -    for c in crates['package']:
> -        if 'source' in c and 'crates.io' in c['source']:
> +
> +    # 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")
> +
> +    # Build a list of crates name that have multiple version
> +    crates_multiple_vers = []
> +    tmp = []
> +    for c in crates_candidates:
> +        if c['name'] in tmp:
> +            crates_multiple_vers.append(c['name'])
> +        else:
> +            tmp.append(c['name'])
> +
> +    # Update crates uri and their checksum, to avoid name clashing on the
> checksum
> +    # we need to rename crates of the same name but different version
> +    cksum_list = ''
> +    for c in crates_candidates:
> +        if c['name'] in crates_multiple_vers:
> +            rename = "%s-%s" % (c['name'], c['version'])
> +            c_list += '\n    crate://crates.io/%s/%s;name=%s \\\' %
> (c['name'], c['version'], rename)
> +        else:
> +            rename = c['name']
>              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
>
>  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}'):
>      for file in files:
>          if file == 'Cargo.lock':
>              crates += get_crates(os.path.join(root, file))
> -open(os.path.join('${THISDIR}', '${BPN}'+"-crates.inc"),
> 'w').write(crates)
> -
> +            found = True
> +if not found:
> +    raise ValueError("Unable to find Cargo.lock in ${CARGO_LOCK_SRC_DIR}")
> +open("${TARGET_FILE}", 'w').write(crates)
>  EOF
> +
> +    bbnote "Successfully update crates inside '${TARGET_FILE}'"
>  }
> --
> 2.34.1
>
>
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 697460d215..daa363b0dd 100644
--- a/meta/classes-recipe/cargo-update-recipe-crates.bbclass
+++ b/meta/classes-recipe/cargo-update-recipe-crates.bbclass
@@ -16,11 +16,14 @@ 
 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"
 
 # The directory where to search for Cargo.lock files
 CARGO_LOCK_SRC_DIR ??= "${S}"
 
 do_update_crates() {
+    TARGET_FILE="${THISDIR}/${BPN}-crates.inc"
+
     nativepython3 - <<EOF
 
 def get_crates(f):
@@ -28,19 +31,52 @@  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'))
-    for c in crates['package']:
-        if 'source' in c and 'crates.io' in c['source']:
+
+    # 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")
+
+    # Build a list of crates name that have multiple version
+    crates_multiple_vers = []
+    tmp = []
+    for c in crates_candidates:
+        if c['name'] in tmp:
+            crates_multiple_vers.append(c['name'])
+        else:
+            tmp.append(c['name'])
+
+    # Update crates uri and their checksum, to avoid name clashing on the checksum
+    # we need to rename crates of the same name but different version
+    cksum_list = ''
+    for c in crates_candidates:
+        if c['name'] in crates_multiple_vers:
+            rename = "%s-%s" % (c['name'], c['version'])
+            c_list += '\n    crate://crates.io/%s/%s;name=%s \\\' % (c['name'], c['version'], rename)
+        else:
+            rename = c['name']
             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
 
 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}'):
     for file in files:
         if file == 'Cargo.lock':
             crates += get_crates(os.path.join(root, file))
-open(os.path.join('${THISDIR}', '${BPN}'+"-crates.inc"), 'w').write(crates)
-
+            found = True
+if not found:
+    raise ValueError("Unable to find Cargo.lock in ${CARGO_LOCK_SRC_DIR}")
+open("${TARGET_FILE}", 'w').write(crates)
 EOF
+
+    bbnote "Successfully update crates inside '${TARGET_FILE}'"
 }