| 1 | #!/usr/bin/env -S gawk -f |
| 2 | # GNU Guix --- Functional package management for GNU |
| 3 | # Copyright © 2025, 2026 Efraim Flashner <[email protected]> |
| 4 | # |
| 5 | # This file is part of GNU Guix. |
| 6 | # |
| 7 | # GNU Guix is free software; you can redistribute it and/or modify it |
| 8 | # under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 3 of the License, or (at |
| 10 | # your option) any later version. |
| 11 | # |
| 12 | # GNU Guix is distributed in the hope that it will be useful, but |
| 13 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | # To run: |
| 21 | # ./etc/teams/rust/audit-rust-crates ./path/to/file.scm |
| 22 | # Prints the output of cargo-audit to the shell. |
| 23 | |
| 24 | BEGIN { |
| 25 | "which cargo-audit" | getline cargoAudit |
| 26 | close("which cargo-audit") |
| 27 | cargoAudit = cargoAudit " audit --file -" |
| 28 | |
| 29 | # Parse a record at a time. |
| 30 | RS = "\n\n" |
| 31 | } |
| 32 | |
| 33 | # Check the crate-source origin-only inputs, like in rust-crates.scm |
| 34 | /crate-source/ { |
| 35 | for(i=3; i <= NF-2; i++) { |
| 36 | if($i == "(crate-source") { |
| 37 | crateName = $(i+1) |
| 38 | crateVersion = $(i+2) |
| 39 | cargoLock = cargoLock "[[package]]\nname = " crateName "\nversion = " crateVersion "\n" |
| 40 | next |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | # Check the crates packaged from crates.io tarballs |
| 46 | /crate-uri/ { |
| 47 | for(i=3; i <= NF; i++) { |
| 48 | if($i == "(version") |
| 49 | crateVersion = $(i+1) |
| 50 | if($i == "(crate-uri") |
| 51 | crateName = $(i+1) |
| 52 | } |
| 53 | gsub(/)/, "", crateVersion) |
| 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 |
| 98 | } |
| 99 | |
| 100 | # Note those which we were unable to parse |
| 101 | /define rust/ { |
| 102 | if($3 == "#f)") |
| 103 | next |
| 104 | print("Unable to parse " $0) |
| 105 | } |
| 106 | |
| 107 | { untested++ } |
| 108 | |
| 109 | END { print("Number of crates untested: " untested); print cargoLock | cargoAudit } |