diff options
| author | Efraim Flashner <efraim@flashner.co.il> | 2026-04-21 12:33:54 +0300 |
|---|---|---|
| committer | Efraim Flashner <efraim@flashner.co.il> | 2026-04-21 17:05:52 +0300 |
| commit | 6a2c7213665a34ff09b0a0fa95d52f1f8708d63f (patch) | |
| tree | 712981cd079def6fe516ac89d86e5f24941b9aa1 | |
| parent | 7a3739951208b91fa144c4835ee9006198ff0fe4 (diff) | |
teams: rust: Improve audit-rust-crates script.
* etc/teams/rust/audit-rust-crates: Count the number of untested crates
and print them at the end of running the script.
[Begin]: Close open file descriptor.
[crate-source]: Use variables.
[package:rust, git-reference]: New matches.
Change-Id: If3d9dec79175dfa521a4dfa54d2fedf69712d96e
| -rwxr-xr-x | etc/teams/rust/audit-rust-crates | 87 |
1 files changed, 63 insertions, 24 deletions
diff --git a/etc/teams/rust/audit-rust-crates b/etc/teams/rust/audit-rust-crates index d5546fd1e18..bf0479a7c40 100755 --- a/etc/teams/rust/audit-rust-crates +++ b/etc/teams/rust/audit-rust-crates | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | #!/usr/bin/env -S gawk -f | 1 | #!/usr/bin/env -S gawk -f |
| 2 | # GNU Guix --- Functional package management for GNU | 2 | # GNU Guix --- Functional package management for GNU |
| 3 | # Copyright © 2025 Efraim Flashner <efraim@flashner.co.il> | 3 | # Copyright © 2025, 2026 Efraim Flashner <efraim@flashner.co.il> |
| 4 | # | 4 | # |
| 5 | # This file is part of GNU Guix. | 5 | # This file is part of GNU Guix. |
| 6 | # | 6 | # |
| @@ -21,20 +21,22 @@ | |||
| 21 | # ./etc/teams/rust/audit-rust-crates ./path/to/file.scm | 21 | # ./etc/teams/rust/audit-rust-crates ./path/to/file.scm |
| 22 | # Prints the output of cargo-audit to the shell. | 22 | # Prints the output of cargo-audit to the shell. |
| 23 | 23 | ||
| 24 | # Make sure we have cargo-audit in our PATH | ||
| 25 | BEGIN { | 24 | BEGIN { |
| 26 | if (system("which cargo-audit 1> /dev/null")) | 25 | "which cargo-audit" | getline cargoAudit |
| 27 | exit 1; | 26 | close("which cargo-audit") |
| 27 | cargoAudit = cargoAudit " audit --file -" | ||
| 28 | |||
| 28 | # Parse a record at a time. | 29 | # Parse a record at a time. |
| 29 | RS = "\n\n" | 30 | RS = "\n\n" |
| 30 | cargoAudit = "cargo-audit audit --file -" | ||
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | # Check the crate-source origin-only inputs | 33 | # Check the crate-source origin-only inputs, like in rust-crates.scm |
| 34 | /crate-source/ { | 34 | /crate-source/ { |
| 35 | for(i=3; i <= NF-2; i++) { | 35 | for(i=3; i <= NF-2; i++) { |
| 36 | if($i == "(crate-source") { | 36 | if($i == "(crate-source") { |
| 37 | cargoLock = cargoLock "[[package]]\nname = " $(i+1) "\nversion = " $(i+2) "\n" | 37 | crateName = $(i+1) |
| 38 | crateVersion = $(i+2) | ||
| 39 | cargoLock = cargoLock "[[package]]\nname = " crateName "\nversion = " crateVersion "\n" | ||
| 38 | next | 40 | next |
| 39 | } | 41 | } |
| 40 | } | 42 | } |
| @@ -44,27 +46,64 @@ BEGIN { | |||
| 44 | /crate-uri/ { | 46 | /crate-uri/ { |
| 45 | for(i=3; i <= NF; i++) { | 47 | for(i=3; i <= NF; i++) { |
| 46 | if($i == "(version") | 48 | if($i == "(version") |
| 47 | crateVersion = $(i+1) | 49 | crateVersion = $(i+1) |
| 48 | if($i == "(crate-uri") | 50 | if($i == "(crate-uri") |
| 49 | crateName = $(i+1) | 51 | crateName = $(i+1) |
| 50 | } | 52 | } |
| 51 | gsub(/)/, "", crateVersion) | 53 | gsub(/)/, "", crateVersion) |
| 52 | cargoLock = cargoLock "[[package]]\nname = " crateName "\nversion = " crateVersion "\n" | 54 | cargoLock = cargoLock "[[package]]\nname = " crateName "\nversion = " crateVersion "\n" |
| 55 | next | ||
| 56 | } | ||
| 57 | |||
| 58 | # Parse the crates created from packages using 'cargo package' | ||
| 59 | /package:rust/ { | ||
| 60 | pkg = $2 | ||
| 61 | split(pkg, name_version, "-") | ||
| 62 | crateVersion = name_version[length(name_version)] | ||
| 63 | crateName = substr(pkg, 6, (length(pkg) - length(crateVersion) - 6)) | ||
| 64 | split(crateVersion, versionDots, ".") | ||
| 65 | if(crateVersion && (crateVersion != "(git-version") && (length(versionDots) == 3) && crateName) { | ||
| 66 | cargoLock = cargoLock "[[package]]\nname = \"" crateName "\"\nversion = \"" crateVersion "\"\n" | ||
| 67 | } else { | ||
| 68 | untested++ | ||
| 69 | #print("Unable to test " $0) | ||
| 70 | } | ||
| 71 | next | ||
| 72 | } | ||
| 73 | |||
| 74 | # We make an attempt to create the crate name from the package name otherwise | ||
| 75 | /git-reference/ { | ||
| 76 | for(i=3; i <= NF; i++) { | ||
| 77 | if($i == "(version") | ||
| 78 | crateVersion = $(i+1) | ||
| 79 | if($i == "(name") | ||
| 80 | crateName = $(i+1) | ||
| 81 | if($i == "(git-file-name") { | ||
| 82 | crateName = $(i+1) | ||
| 83 | crateVersion = $(i+2) | ||
| 84 | } | ||
| 85 | } | ||
| 86 | gsub(/)/, "", crateVersion) | ||
| 87 | gsub(/)/, "", crateName) | ||
| 88 | sub(/rust-/, "", crateName) | ||
| 89 | # The crate version MUST be major.minor.patch | ||
| 90 | split(crateVersion, versionParts, ".") | ||
| 91 | if(crateVersion && (crateVersion != "(git-version") && (length(versionParts) == 3) && crateName) { | ||
| 92 | cargoLock = cargoLock "[[package]]\nname = " crateName "\nversion = " crateVersion "\n" | ||
| 93 | } else { | ||
| 94 | untested++ | ||
| 95 | #print("Unable to test " $0) | ||
| 96 | } | ||
| 97 | next | ||
| 53 | } | 98 | } |
| 54 | 99 | ||
| 55 | # The xxxx-cargo-input variables have a set style | 100 | # Note those which we were unable to parse |
| 56 | # TODO: Replace the last dash between the name and the version with a space! | 101 | /define rust/ { |
| 57 | # This doesn't take into account swapping between "-" and "_" so we skip it. | 102 | if($3 == "#f)") |
| 58 | #( $2 ~ /-cargo-inputs/ ) { | 103 | next |
| 59 | # sub(/-cargo-inputs/, "", $2) | 104 | print("Unable to parse " $0) |
| 60 | # gsub(/)/, "", $0) | 105 | } |
| 61 | # gsub(/rust-/, "", $0) | 106 | |
| 62 | # #gensub(/([[:alpha:]])-([[:digit:]]+)/, "\\1 \\2", "g", $i) | 107 | { untested++ } |
| 63 | # print "[[package]]\nname = \"" $2 "\"\nversion = \"1.0.0\"\ndependencies = [" | ||
| 64 | # for (i = 4; i <= NF; i++) { | ||
| 65 | # print "\"" $i "\"," | ||
| 66 | # } | ||
| 67 | # print "]" | ||
| 68 | #} | ||
| 69 | 108 | ||
| 70 | END { print cargoLock | cargoAudit } | 109 | END { print("Number of crates untested: " untested); print cargoLock | cargoAudit } |
