From patchwork Mon Apr 28 16:23:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hilton Chain X-Patchwork-Id: 42125 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 BB2CA27BC49; Mon, 28 Apr 2025 17:35:23 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,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=unavailable 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 B3C0E27BC4A for ; Mon, 28 Apr 2025 17:35:21 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u9RRo-0000Ww-Dr; Mon, 28 Apr 2025 12:35:15 -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 1u9RPk-0007sx-NH for guix-patches@gnu.org; Mon, 28 Apr 2025 12:33:05 -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 1u9RPk-0004Xh-AS; Mon, 28 Apr 2025 12:33:04 -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=F+FOZr7P7tJgPbvzsGVz2VGpAY/OjCoUWwyYTpRthRw=; b=RtO6XlYaMsFAq6QdAO1SQTEwq+XAoAIDhmT60MWm3LXQwGFwuYM6tZ2VkaXtigJybz3UBbzJ7WfBBXsAVJLydBw3Z4Dtjek+CrlQmQj6YoEFNr7XNNKLrLEtUHF6P5zD7kMaEz8ir70bGvchejx5/TONicWfs7ABoqXY++gLS2GJNkkltQCPhcP3ad7cEFu8I5OvRQs8wvFrECHt4J37bzHZQr2R4zGYWb5SHZWqwHpxUjb3UENAWJAjrW38JOasdw2v3fSoK+d85pbdf1QkrQJFJmibsss5BWgB+9BOQ22AX0pUJnO69cBauPIVp1rFu58IewWnV5Ye4RX+kiYVgw==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1u9RPi-0003Re-VR; Mon, 28 Apr 2025 12:33:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#77093] [PATCH v4 rust-team 22/22] import: crate: Add =?utf-8?b?4oCYLS1sb2NrZmlsZeKAmQ==?= option. Resent-From: Hilton Chain Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, divya@subvertising.org, efraim@flashner.co.il, hako@ultrarare.space, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, maxim.cournoyer@gmail.com, zimon.toutoune@gmail.com, steve@futurile.net, me@tobias.gr, guix-patches@gnu.org Resent-Date: Mon, 28 Apr 2025 16:33:02 +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: Hilton Chain , Christopher Baines , Divya Ranjan Pattanaik , Efraim Flashner , Hilton Chain , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Maxim Cournoyer , Simon Tournier , Steve George , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Christopher Baines , Divya Ranjan Pattanaik , Efraim Flashner , Hilton Chain , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Maxim Cournoyer , Simon Tournier , Steve George , Tobias Geerinckx-Rice Received: via spool by 77093-submit@debbugs.gnu.org id=B77093.174585795013009 (code B ref 77093); Mon, 28 Apr 2025 16:33:02 +0000 Received: (at 77093) by debbugs.gnu.org; 28 Apr 2025 16:32:30 +0000 Received: from localhost ([127.0.0.1]:33006 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u9RPB-0003Nc-TQ for submit@debbugs.gnu.org; Mon, 28 Apr 2025 12:32:30 -0400 Received: from mail.boiledscript.com ([49.12.134.184]:51894) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u9ROh-0003Eb-F1 for 77093@debbugs.gnu.org; Mon, 28 Apr 2025 12:32:00 -0400 From: Hilton Chain DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ultrarare.space; s=mail; t=1745857918; 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=DnAX0EtTZ6atX3aIfwBUNdiGnLbnTspSvjuHy+IeeKw=; b=GD5npVRD/nkRGvudANlW1Ixv5T6Y0n36IOrUJqaHh3o4D8oRBuW7bjv9DgrRETQJuPzjNy CjiKPkhDKaYf1E4LUYc3QJRaSBdqmWy9muzotv10pXw33N7vpOd6pUsNCbJadBAG/bdav9 y9AJtxujwPJZi3q/SvSJRjMe9m6V4YFRiKv7XvlZwMXVNdaKQImN2pfRZOebdL1P9c3SS1 zWEb5VO1NLOrTXt9P0Qu9l9hLbAbz8cxYmAPXt1calPU+eLik5nw+q7l7E4RN2NbBE/BGu dv6fghbMk58ZKZr6UC5onibWxJcL8A8JVRsnWRy+0sruTKUQmH9sGMAE0ak0Gg== Date: Tue, 29 Apr 2025 00:23:19 +0800 Message-ID: <0d02c65ab3209a1a8c4ba0f754937d4f4cba54ae.1745855744.git.hako@ultrarare.space> In-Reply-To: References: MIME-Version: 1.0 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.scm (cargo-inputs-from-lockfile) find-cargo-inputs-location, extract-cargo-inputs): New procedures. * guix/scripts/import/crate.scm (%options): Add ‘--lockfile’ option. (show-help): Add it. (guix-import-crate): Use it. * doc/guix.texi (Invoking guix import): Document it. Change-Id: I291478e04adf9f2df0bf216425a5e8aeba0bedd9 --- doc/guix.texi | 5 +++ guix/import/crate.scm | 47 +++++++++++++++++++++++++++++ guix/scripts/import/crate.scm | 57 +++++++++++++++++++++++++++++------ 3 files changed, 99 insertions(+), 10 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 6388f7b28f..7aa36b347a 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -14833,6 +14833,11 @@ Invoking guix import If a crate dependency is not (yet) packaged, make the corresponding input in @code{#:cargo-inputs} or @code{#:cargo-development-inputs} into a comment. +@item --lockfile=@var{file} +@itemx -f @var{file} +When @option{--lockfile} is specified, the importer will ignore other options +and won't output package expressions, instead importing source expressions +from @var{file}, a @file{Cargo.lock} file. @end table @item elm diff --git a/guix/import/crate.scm b/guix/import/crate.scm index 39da867805..d425e07b48 100644 --- a/guix/import/crate.scm +++ b/guix/import/crate.scm @@ -52,6 +52,7 @@ (define-module (guix import crate) #:use-module (json) #:use-module (srfi srfi-1) #:use-module (srfi srfi-2) + #:use-module (srfi srfi-11) #:use-module (srfi srfi-26) #:use-module (srfi srfi-69) #:use-module (srfi srfi-71) @@ -60,6 +61,9 @@ (define-module (guix import crate) string->license crate-recursive-import cargo-lock->expressions + cargo-inputs-from-lockfile + find-cargo-inputs-location + extract-cargo-inputs %crate-updater)) @@ -559,6 +563,49 @@ (define (cargo-lock->expressions lockfile package-name) (list ,@(map second source-expressions))))) (values source-expressions cargo-inputs-entry))) +(define* (cargo-inputs-from-lockfile #:optional (lockfile "Cargo.lock")) + "Given LOCKFILE (default to \"Cargo.lock\" in current directory), return a +source list imported from it, to be used as package inputs. This procedure +can be used for adding a manifest file within the source tree of a Rust +application." + (let-values + (((source-expressions cargo-inputs-entry) + (cargo-lock->expressions lockfile "cargo-inputs-temporary"))) + (eval-string + (call-with-output-string + (lambda (port) + (for-each + (cut pretty-print-with-comments port <>) + `((use-modules (guix build-system cargo)) + ,@source-expressions + (define-cargo-inputs lookup-cargo-inputs ,cargo-inputs-entry) + (lookup-cargo-inputs 'cargo-inputs-temporary)))))))) + +(define (find-cargo-inputs-location file) + "Search in FILE for a top-level definition of Cargo inputs. Return the +location if found, or #f otherwise." + (find-definition-location file 'lookup-cargo-inputs + #:define-prefix 'define-cargo-inputs)) + +(define* (extract-cargo-inputs file #:key exclude) + "Search in FILE for a top-level definition of Cargo inputs. If found, +return its entries excluding EXCLUDE, or an empty list otherwise." + (call-with-input-file file + (lambda (port) + (do ((syntax (read-syntax port) + (read-syntax port))) + ((match (syntax->datum syntax) + (('define-cargo-inputs 'lookup-cargo-inputs _ ...) #t) + ((? eof-object?) #t) + (_ #f)) + (or (and (not (eof-object? syntax)) + (match (syntax->datum syntax) + (('define-cargo-inputs 'lookup-cargo-inputs inputs ...) + (remove (lambda (cargo-input-entry) + (eq? exclude (first cargo-input-entry))) + inputs)))) + '())))))) + ;;; ;;; Updater diff --git a/guix/scripts/import/crate.scm b/guix/scripts/import/crate.scm index 723cbb3665..a251cd9538 100644 --- a/guix/scripts/import/crate.scm +++ b/guix/scripts/import/crate.scm @@ -25,11 +25,13 @@ (define-module (guix scripts import crate) #:use-module (guix ui) #:use-module (guix utils) + #:use-module (guix read-print) #:use-module (guix scripts) #:use-module (guix import crate) #:use-module (guix scripts import) #:use-module (srfi srfi-1) #:use-module (srfi srfi-11) + #:use-module (srfi srfi-26) #:use-module (srfi srfi-37) #:use-module (ice-9 match) #:use-module (ice-9 format) @@ -60,6 +62,9 @@ (define (show-help) sufficient package exists for it")) (newline) (display (G_ " + -f, --lockfile=FILE import dependencies from FILE, a 'Cargo.lock' file")) + (newline) + (display (G_ " -h, --help display this help and exit")) (display (G_ " -V, --version display version information and exit")) @@ -87,6 +92,9 @@ (define %options (option '("mark-missing") #f #f (lambda (opt name arg result) (alist-cons 'mark-missing #t result))) + (option '(#\f "lockfile") #f #t + (lambda (opt name arg result) + (alist-cons 'lockfile arg result))) %standard-import-options)) @@ -101,6 +109,8 @@ (define (guix-import-crate . args) #:build-options? #f)) (let* ((opts (parse-options)) + (lockfile (assoc-ref opts 'lockfile)) + (file-to-insert (assoc-ref opts 'file-to-insert)) (args (filter-map (match-lambda (('argument . value) value) @@ -111,16 +121,43 @@ (define (guix-import-crate . args) (define-values (name version) (package-name->name+version spec)) - (match (if (assoc-ref opts 'recursive) - (crate-recursive-import - name #:version version - #:recursive-dev-dependencies? - (assoc-ref opts 'recursive-dev-dependencies) - #:allow-yanked? (assoc-ref opts 'allow-yanked)) - (crate->guix-package - name #:version version #:include-dev-deps? #t - #:allow-yanked? (assoc-ref opts 'allow-yanked) - #:mark-missing? (assoc-ref opts 'mark-missing))) + (match (cond + ((and=> lockfile + (lambda (file) + (or (file-exists? file) + (leave (G_ "file '~a' does not exist~%") file)))) + (let-values (((source-expressions cargo-inputs-entry) + (cargo-lock->expressions lockfile name))) + (when file-to-insert + (let* ((term (first cargo-inputs-entry)) + (cargo-inputs + `(define-cargo-inputs lookup-cargo-inputs + ,@(sort + (cons cargo-inputs-entry + (extract-cargo-inputs + file-to-insert #:exclude term)) + (lambda (a b) + (string< (symbol->string (first a)) + (symbol->string (first b))))))) + (_ + (and=> (find-cargo-inputs-location file-to-insert) + delete-expression)) + (port (open-file file-to-insert "a"))) + (pretty-print-with-comments port cargo-inputs) + (newline port) + (close-port port))) + source-expressions)) + ((assoc-ref opts 'recursive) + (crate-recursive-import + name #:version version + #:recursive-dev-dependencies? + (assoc-ref opts 'recursive-dev-dependencies) + #:allow-yanked? (assoc-ref opts 'allow-yanked))) + (else + (crate->guix-package + name #:version version #:include-dev-deps? #t + #:allow-yanked? (assoc-ref opts 'allow-yanked) + #:mark-missing? (assoc-ref opts 'mark-missing)))) ((or #f '()) (leave (G_ "failed to download meta-data for package '~a'~%") (if version