From patchwork Sun Mar 23 07:28:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hilton Chain X-Patchwork-Id: 40709 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id AF01327BBE2; Sun, 23 Mar 2025 07:30:58 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-5.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,FROM_SUSPICIOUS_NTLD,MAILING_LIST_MULTI, PDS_OTHER_BAD_TLD,RCVD_IN_DNSWL_BLOCKED,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE,SPF_HELO_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id BF5C527BBEA for ; Sun, 23 Mar 2025 07:30:57 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1twFn2-0001X7-HS; Sun, 23 Mar 2025 03:30:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1twFmu-0001UL-M5 for guix-patches@gnu.org; Sun, 23 Mar 2025 03:30:29 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1twFmu-00006Z-9p for guix-patches@gnu.org; Sun, 23 Mar 2025 03:30:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:References:In-Reply-To:Date:From:To:Subject; bh=ouv9vwLl4lUf0H6sCyzs76iAhwKUiPvE3S8S+A1iQBM=; b=AjjRfC7D0jdBmw5IpASM2I2Cgu+aTY5syaO0DkZg20/zbu79YweKcI3Vajkojq9dHEywkLjWH1JEsee+rWM2C6ZOlQ69/7dIXaVPxbrEilZHV+Qy/PdqfKEOWIecZRMpGd6tUhyVD9cdIoMebgfSYDM++GiI74mr3ltjnw1+V5yPHv4gT2d6nww5w98c+7rZvMLhsc2KySa/C7b2qwJPb0PWZmlA1+t53j55IYrsgXi5x9tYfyI8YPXKuQ/A5gbj/gWqPV8Tor9t0h4GXHwgwO8kBsrEgQIWadYrgUrd14vAPdRWB8EdSlCjIO09/QzyOYk99sdNa2tI9lRDJz2Fdw==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1twFmu-0004SU-3b; Sun, 23 Mar 2025 03:30:28 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#77093] [PATCH rust-team v3 15/17] import: crate: Add Cargo.lock parser. Resent-From: Hilton Chain Original-Sender: "Debbugs-submit" Resent-CC: divya@subvertising.org, efraim@flashner.co.il, steve@futurile.net, guix-patches@gnu.org Resent-Date: Sun, 23 Mar 2025 07:30:28 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 77093 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 77093@debbugs.gnu.org Cc: Murilo , Hilton Chain , Luis Guilherme Coelho , Divya Ranjan Pattanaik , Efraim Flashner , Steve George X-Debbugs-Original-Xcc: Divya Ranjan Pattanaik , Efraim Flashner , Steve George Received: via spool by 77093-submit@debbugs.gnu.org id=B77093.174271500816613 (code B ref 77093); Sun, 23 Mar 2025 07:30:28 +0000 Received: (at 77093) by debbugs.gnu.org; 23 Mar 2025 07:30:08 +0000 Received: from localhost ([127.0.0.1]:47361 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1twFmW-0004I0-Oh for submit@debbugs.gnu.org; Sun, 23 Mar 2025 03:30:08 -0400 Received: from mx.boiledscript.com ([88.99.243.112]:40058) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1twFmG-0004Du-8c for 77093@debbugs.gnu.org; Sun, 23 Mar 2025 03:29:49 -0400 From: Hilton Chain DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ultrarare.space; s=mail; t=1742714987; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aVHLOZmV4I/qcACy1bMCS4p3weOyVcxLn/v5Effmdss=; b=Cz814f2WlHfmTMNma8tZqVkAG5bA+4Sb7HAqCbVLABqt5M0ltYta9/Hv6LcR7ui1FhyUO7 DeD26FQaRyStjOVtAedQ4IEtdsT3M7M++4zWLvtbNK9S+hd9xp0KACFzdrgRrwDb9bc40D Qx/yXIjrpL0/zckEyHEN1K5hjRtH65Fzi2xA5uSuY6Xpk3gCC3/XVmU7IiA+57YyIZbahi cKf+zCbaMPZTg+q3+Sp1hgFbc5fntgZgutwNvIIK+P6FMGWG/L5hYDM/rDRdZZ1xn0LqeE rAXmYc6w4us/vkBzncQVPbfjWOoKzwlgsAA1/nc8Tqevz9wUQZB8tkKdkcD3Yg== Date: Sun, 23 Mar 2025 15:28:37 +0800 Message-ID: <52f74fa07c2b1671769f33aadd28037732062d79.1742713356.git.hako@ultrarare.space> In-Reply-To: References: MIME-Version: 1.0 X-MS-Reactions: disallow X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * guix/import/crate/cargo-lock.scm: New file. * Makefile.am (MODULES): Regisiter it. * etc/teams.scm (rust)[#:scope]: Add it. * guix/import/crate.scm (cargo-lock->definitions): New procedure. Co-authored-by: Murilo Co-authored-by: Luis Guilherme Coelho Change-Id: I95421e9e2ba11a671b4bc4e1323c6d31a1b012c5 --- Makefile.am | 1 + etc/teams.scm | 1 + guix/import/crate.scm | 78 +++++++++++++++++++++++ guix/import/crate/cargo-lock.scm | 105 +++++++++++++++++++++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 guix/import/crate/cargo-lock.scm diff --git a/Makefile.am b/Makefile.am index 4091a52387..a04e461ea8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -295,6 +295,7 @@ MODULES = \ guix/import/cpan.scm \ guix/import/cran.scm \ guix/import/crate.scm \ + guix/import/crate/cargo-lock.scm \ guix/import/egg.scm \ guix/import/elm.scm \ guix/import/elpa.scm \ diff --git a/etc/teams.scm b/etc/teams.scm index 6bddbd91fa..c4bee8d3d9 100755 --- a/etc/teams.scm +++ b/etc/teams.scm @@ -389,6 +389,7 @@ (define-team rust "guix/build/cargo-utils.scm" "guix/build-system/cargo.scm" "guix/import/crate.scm" + "guix/import/crate/cargo-lock.scm" "guix/scripts/import/crate.scm" "tests/crate.scm"))) diff --git a/guix/import/crate.scm b/guix/import/crate.scm index a6f247bbae..ef3a6d0b4b 100644 --- a/guix/import/crate.scm +++ b/guix/import/crate.scm @@ -9,6 +9,9 @@ ;;; Copyright © 2023, 2024 David Elsing ;;; Copyright © 2024 Maxim Cournoyer ;;; Copyright © 2025 Herman Rimm +;;; Copyright © 2024 Murilo +;;; Copyright © 2024-2025 Luis Guilherme Coelho +;;; Copyright © 2025 Hilton Chain ;;; ;;; This file is part of GNU Guix. ;;; @@ -26,12 +29,14 @@ ;;; along with GNU Guix. If not, see . (define-module (guix import crate) + #:use-module (guix base16) #:use-module (guix base32) #:use-module ((guix build-system cargo) #:hide (crate-source)) #:use-module (guix diagnostics) #:use-module (gcrypt hash) #:use-module (guix http-client) #:use-module (guix i18n) + #:use-module (guix import crate cargo-lock) #:use-module (guix import json) #:use-module (guix import utils) #:use-module (guix memoization) @@ -39,9 +44,11 @@ (define-module (guix import crate) #:use-module (guix read-print) #:use-module (guix upstream) #:use-module (guix utils) + #:use-module (guix scripts download) #:use-module (gnu packages) #:use-module (ice-9 match) #:use-module (ice-9 regex) + #:use-module (ice-9 textual-ports) #:use-module (json) #:use-module (srfi srfi-1) #:use-module (srfi srfi-2) @@ -52,6 +59,7 @@ (define-module (guix import crate) guix-package->crate-name string->license crate-recursive-import + cargo-lock->definitions %crate-updater)) @@ -481,6 +489,76 @@ (define (guix-package->crate-name package) (match parts ((name _ ...) name)))) + +;;; +;;; Convert ‘Cargo.lock’ to Guix sources. +;;; + +(define (cargo-lock->definitions lockfile package-name) + "Given LOCKFILE, a 'Cargo.lock' file, convert its content into source +definitions. Return a list of sources and 'PACKAGE-NAME-cargo-inputs', an input +list referencing all imported sources." + (define (crate->definition crate) + (match crate + (('crate + ('crate-name name) + ('crate-version version) + ('crate-source _) + ('crate-checksum checksum)) + `(define + ,(string->symbol + (string-append (crate-name->package-name name) "-" version)) + ,@(if (or (string-suffix? "src" name) + (string-suffix? "sys" name)) + (list (comment ";; TODO: Check bundled sources.\n" #f)) + '()) + (crate-source ,name ,version + ,(bytevector->nix-base32-string + (base16-string->bytevector checksum))))) + ;; Git snapshot. + (('crate + ('crate-name name) + ('crate-version version) + ('crate-source source)) + (begin + (let* ((src (string-split source (char-set #\+ #\? #\#))) + (url (second src)) + (commit (last src)) + (version (string-append version "." (string-take commit 7))) + (checksum + (second + (string-split + (with-output-to-string + (lambda _ + (guix-download "-g" url + (string-append "--commit=" commit)))) + #\newline)))) + `(define + ,(string->symbol + (string-append (crate-name->package-name name) "-" version)) + ,(comment + ";; TODO: Define standalone package if this is a workspace.\n" + #f) + (origin + (method git-fetch) + (uri (git-reference + (url ,url) + (commit ,commit))) + (file-name + (git-file-name ,(crate-name->package-name name) ,version)) + (sha256 (base32 ,checksum))))))) + ;; Workspace member. + (else #f))) + + (let ((definitions + (filter-map crate->definition + (cargo-lock-string->scm + (call-with-input-file lockfile get-string-all))))) + (values definitions + `(define-public + ,(string->symbol (string-append package-name "-cargo-inputs")) + (list ,@(map second definitions)))))) + ;;; ;;; Updater diff --git a/guix/import/crate/cargo-lock.scm b/guix/import/crate/cargo-lock.scm new file mode 100644 index 0000000000..b8a6ab20c0 --- /dev/null +++ b/guix/import/crate/cargo-lock.scm @@ -0,0 +1,105 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2024 Murilo +;;; Copyright © 2024 Luis Guilherme Coelho +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (guix import crate cargo-lock) + #:use-module (ice-9 peg) + #:export (cargo-lock-string->scm + + crate-name + crate-version + crate-source + crate-checksum + crate-dependencies + cargo-lock)) + +;;; +;;; PEG parser for ‘Cargo.lock’. +;;; + +(define (cargo-lock-string->scm str) + (peg:tree (search-for-pattern cargo-lock str))) + +;; Auxiliar peg patterns +(define-peg-pattern numeric-char body + (range #\0 #\9)) + +(define-peg-pattern lowercase-char body + (range #\a #\z)) + +(define-peg-pattern uppercase-char body + (range #\A #\Z)) + +(define-peg-pattern alphabetic-char body + (or lowercase-char uppercase-char)) + +(define-peg-pattern alphanumeric-char body + (or alphabetic-char numeric-char)) + +;; name +(define-peg-pattern crate-name all + (+ (or "-" alphabetic-char + "_" numeric-char))) + +;; version +(define-peg-pattern non-negative-integer body + (+ numeric-char)) + +(define-peg-pattern crate-version all + (and non-negative-integer "." + non-negative-integer "." + non-negative-integer + (? (+ (or "-" lowercase-char + "." uppercase-char + "+" numeric-char "_"))))) + +;; source +(define-peg-pattern crate-source all + (and (or "registry" "git") + "+https://" + (+ (or "/" "." "?" "=" "-" "#" "_" + alphanumeric-char)))) + +;; checksum +(define-peg-pattern crate-checksum all + (+ (or lowercase-char numeric-char))) + +;; dependency specification +(define-peg-pattern dependency-specification all + (and crate-name (? (and (ignore " ") crate-version)))) + +;; dependencies +(define-peg-pattern crate-dependencies all + (and (ignore "[\n") + (+ (and (ignore " \"") + (capture dependency-specification) + (ignore "\",\n"))) + (ignore "]"))) + +;; crates +(define-peg-pattern crate all + (and (ignore "[[package]]\n") + (ignore "name = \"") (capture crate-name) (ignore "\"\n") + (ignore "version = \"") (capture crate-version) (ignore "\"\n") + (? (and (ignore "source = \"") (capture crate-source) (ignore "\"\n"))) + (? (and (ignore "checksum = \"") (capture crate-checksum) (ignore "\"\n"))) + (? (ignore (and "dependencies = " crate-dependencies "\n"))))) + +;; Cargo.lock +(define-peg-pattern cargo-lock all + (+ (and (ignore "\n") crate)))