From patchwork Wed Oct 20 16:54:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Simon Tournier X-Patchwork-Id: 33964 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 AB9F927BBE3; Wed, 20 Oct 2021 17:55:39 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,T_DKIM_INVALID,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 29D1527BBE1 for ; Wed, 20 Oct 2021 17:55:39 +0100 (BST) Received: from localhost ([::1]:47114 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mdEsM-000194-7E for patchwork@mira.cbaines.net; Wed, 20 Oct 2021 12:55:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55976) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mdErn-00017o-Lr for guix-patches@gnu.org; Wed, 20 Oct 2021 12:55:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:43813) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mdErm-0000rL-Ax for guix-patches@gnu.org; Wed, 20 Oct 2021 12:55:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mdErm-0007Sv-99 for guix-patches@gnu.org; Wed, 20 Oct 2021 12:55:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#51307] [PATCH 1/2] scripts: hash: Improve error handling. References: <20211020165020.3358311-1-zimon.toutoune@gmail.com> In-Reply-To: <20211020165020.3358311-1-zimon.toutoune@gmail.com> Resent-From: zimoun Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 20 Oct 2021 16:55:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51307 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 51307@debbugs.gnu.org Cc: zimoun Received: via spool by 51307-submit@debbugs.gnu.org id=B51307.163474889128672 (code B ref 51307); Wed, 20 Oct 2021 16:55:02 +0000 Received: (at 51307) by debbugs.gnu.org; 20 Oct 2021 16:54:51 +0000 Received: from localhost ([127.0.0.1]:55357 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mdErb-0007SN-0p for submit@debbugs.gnu.org; Wed, 20 Oct 2021 12:54:51 -0400 Received: from mail-wr1-f50.google.com ([209.85.221.50]:45632) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mdErY-0007S2-MJ for 51307@debbugs.gnu.org; Wed, 20 Oct 2021 12:54:49 -0400 Received: by mail-wr1-f50.google.com with SMTP id r10so48265346wra.12 for <51307@debbugs.gnu.org>; Wed, 20 Oct 2021 09:54:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=OCiq+B8eY3dy24gREyN0p0VuTvCUjlqt45JBpT64X1g=; b=iiZDbFfXtEJuoUFA5jn2VuLREdMKQv5qEclE5TvRv9yA4v/EH/O+Yy5kbpQzIFyyjh /jsSEJQsNCGVFop2/SNYOajwd0sMEiynuJNeKaTWo6RaHS3+ecHAAV4xNG5GTOPJkc+A 0zD6EKwLAy2TcMXyOgrjLVRCyoovOxRdciPWWEFlBez2AN+Cz3uBOSA9T/vQ4FvpD+PZ 22ZF/v/BmkuMzkpApf/aV9C5YgFbSOZmeKWDab+XD2XQiVtF9ttX/QHu3gejsVllZmtA ECsmDcIO4tlmBzLCe2jHMY6kaWkQxtHqamulAaWw/hIBcpbVRKqvZBhDCW2hQIExopNK baSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=OCiq+B8eY3dy24gREyN0p0VuTvCUjlqt45JBpT64X1g=; b=h3kCHEqOxYE7b9hnQkD4kizwqxyshmH3okR5KbhHr5hKYTPw0AZDEDf8qFyM/S0/Nn 0OzOE80Yevyrw64d0dYdvGiIN8HTgCbo+20mN/NHQXazSfEARQIJqiZEcz6IQNNpq2vV NRnCYv0xRedUh4EAuVbkk9PKHOCJUIpeN8HUIy2kijvLW/JRY5w3hA+3GikzwCpWr9Vb ITWi7dhOFJkgy8DpErRFOENvhQs1ttkUxU4KQLrBhFGcnVE+4FrbvP6+/hDt5euNTUZW C+woVyPk1KOXj5xcYE8/cAEpA8nFj3PbCeBfPjzc6cKEFVdC9eTnai9P6eHAHO6U/j52 iRPg== X-Gm-Message-State: AOAM531SqonA7N09H4ur0xz72sU//zfbh5UiMk+ttKLEBWacQC4TBww8 BOQllvtUbDhZwkhROnmPt+bT43APUh0= X-Google-Smtp-Source: ABdhPJzEeEPHMwjPY8fy3pK763EZ5pD3BaG0siPnvRRUbHiZzSgP3tRkL/PeaAjbwMWmA1JH8nWnKg== X-Received: by 2002:a5d:47cb:: with SMTP id o11mr487718wrc.184.1634748882763; Wed, 20 Oct 2021 09:54:42 -0700 (PDT) Received: from localhost.localdomain ([193.48.40.117]) by smtp.gmail.com with ESMTPSA id o11sm3639977wry.0.2021.10.20.09.54.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Oct 2021 09:54:42 -0700 (PDT) From: zimoun Date: Wed, 20 Oct 2021 18:54:34 +0200 Message-Id: <20211020165435.3358398-1-zimon.toutoune@gmail.com> X-Mailer: git-send-email 2.32.0 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" X-getmail-retrieved-from-mailbox: Patches * guix/scripts/hash.scm (guix-hash): Allow several files. [directory?]: New procedure. [file-hash]: Catch system-error. [hash-to-display]: New procedure. --- guix/scripts/hash.scm | 56 +++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm index b8622373cc..f3363549d3 100644 --- a/guix/scripts/hash.scm +++ b/guix/scripts/hash.scm @@ -3,6 +3,7 @@ ;;; Copyright © 2013 Nikita Karetnikov ;;; Copyright © 2016 Jan Nieuwenhuizen ;;; Copyright © 2018 Tim Gesthuizen +;;; Copyright © 2021 Simon Tournier ;;; ;;; This file is part of GNU Guix. ;;; @@ -135,6 +136,11 @@ (define (vcs-file? file stat) (else #f))) + (define (directory? file) + (case (stat:type (stat file)) + ((directory) #t) + (else #f))) + (let* ((opts (parse-options)) (args (filter-map (match-lambda (('argument . value) @@ -149,27 +155,37 @@ (define (vcs-file? file stat) (define (file-hash file) ;; Compute the hash of FILE. ;; Catch and gracefully report possible '&nar-error' conditions. - (with-error-handling - (if (assoc-ref opts 'recursive?) + (if (and (assoc-ref opts 'recursive?) + (directory? file)) + (with-error-handling (let-values (((port get-hash) (open-hash-port (assoc-ref opts 'hash-algorithm)))) (write-file file port #:select? select?) (force-output port) - (get-hash)) - (match file - ("-" (port-hash (assoc-ref opts 'hash-algorithm) - (current-input-port))) - (_ (call-with-input-file file - (cute port-hash (assoc-ref opts 'hash-algorithm) - <>))))))) - - (match args - ((file) - (catch 'system-error - (lambda () - (format #t "~a~%" (fmt (file-hash file)))) - (lambda args - (leave (G_ "~a~%") - (strerror (system-error-errno args)))))) - (x - (leave (G_ "wrong number of arguments~%")))))) + (get-hash))) + (catch 'system-error + (lambda _ + (call-with-input-file file + (cute port-hash (assoc-ref opts 'hash-algorithm) + <>))) + (lambda args + (when (directory? file) + (display-hint (G_ "Try @option{--recursive}."))) + (leave (G_ "~a ~a~%") + file + (strerror (system-error-errno args))))))) + + (define (hash-to-display thing) + (match thing + ((? file-exists? file) + (fmt (file-hash file))) + ("-" (with-error-handling + (fmt (port-hash (assoc-ref opts 'hash-algorithm) + (current-input-port))))) + (x + (leave (G_ "wrong argument~%"))))) + + (for-each + (lambda (arg) + (format #t "~a~%" (hash-to-display arg))) + args))) From patchwork Wed Oct 20 16:54:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Simon Tournier X-Patchwork-Id: 33963 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 F15C927BBE3; Wed, 20 Oct 2021 17:55:38 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,T_DKIM_INVALID,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 867BA27BBE1 for ; Wed, 20 Oct 2021 17:55:38 +0100 (BST) Received: from localhost ([::1]:47102 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mdEsL-00018J-Mg for patchwork@mira.cbaines.net; Wed, 20 Oct 2021 12:55:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55980) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mdErn-00017p-O2 for guix-patches@gnu.org; Wed, 20 Oct 2021 12:55:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:43814) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mdErm-0000rv-ND for guix-patches@gnu.org; Wed, 20 Oct 2021 12:55:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mdErm-0007T2-N5 for guix-patches@gnu.org; Wed, 20 Oct 2021 12:55:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#51307] [PATCH 2/2] scripts: hash: Support file or package. Resent-From: zimoun Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 20 Oct 2021 16:55:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51307 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 51307@debbugs.gnu.org Cc: zimoun Received: via spool by 51307-submit@debbugs.gnu.org id=B51307.163474889128678 (code B ref 51307); Wed, 20 Oct 2021 16:55:02 +0000 Received: (at 51307) by debbugs.gnu.org; 20 Oct 2021 16:54:51 +0000 Received: from localhost ([127.0.0.1]:55359 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mdErb-0007SP-DL for submit@debbugs.gnu.org; Wed, 20 Oct 2021 12:54:51 -0400 Received: from mail-wr1-f45.google.com ([209.85.221.45]:36707) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mdErY-0007S3-Tg for 51307@debbugs.gnu.org; Wed, 20 Oct 2021 12:54:49 -0400 Received: by mail-wr1-f45.google.com with SMTP id o20so48413176wro.3 for <51307@debbugs.gnu.org>; Wed, 20 Oct 2021 09:54:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WydHqYw6Dw85SFFZAywbmj0Idl7DO7jpycThEmU3aGM=; b=O6+QbwDZYqGJ4O1mCFRV9O5wyyHPBWqI6CByRPvtWQuNZB+Uib0GYzy0/WVmQkkbYO BMWGA+0bxztDy7u+flXoe8gak5KLZ452vuth1wOcvQFQqa3Tbf+haj88Am3+d191oF8m Zhx7WpItGqRRO5g7v0aSR1iIkldx9MD1VOGVvP+nO2wOWj3Dhq55ug1RX1ToNPYwavQs lsmg3/6Y+qp51bW4xLIJ3ndiH5ueW8HBMfbTcy1w7T7OArbKLCeF5WzQuGA8MxcXN9rx 7JJmJScYoPKlS9uxeizLLo0D3XFvUjjWfYxC6WJCGq0fLDf9ertg6ZYtDriZubw+1q3n bVCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WydHqYw6Dw85SFFZAywbmj0Idl7DO7jpycThEmU3aGM=; b=ER7MT/IwBmkPvguUjrFFnyy9AU3/dsH3Jqzxs8saOLYkKJmwc7RsQRJJrkFvs91kXu je+xR/PpXsSl2E8/0VvbatzlvsDznDjr3j82KKykFSOQfruJpvXpCKn7SdNsIt/PzZci 8kQfCL+cHMb49oMAZa77paegAhJRDhO1Cv5Tj/48oCHkcPu0Jcgw91hsXnVRAG2zNchn DReam0T67X2GiiNE3vcDv1JipRivzkIGqx2PbPwd6mvTRTYbZjvA9cY/QMhCuMcAfimN Hvyl/OzNLrFMLAWd+J3mDFyzgwAUsEi/7GKTLVhh8BJMSHnBSA+6nA4d12lr/s2Bu8/M ZOmA== X-Gm-Message-State: AOAM533vMx9o9BWqbjeK0t6tWRUeYbwj/dKCzJ9GYReqR/XSPuZdttlr NxM44/TokgFMPT1cfLhqnGfk7VI9rQU= X-Google-Smtp-Source: ABdhPJyuyhStVb83QEVr+jaXqkC7mmOW2Gmm1E13hqScvRhWKaJD0l6VNLt0a5DN0Aya5cnknexhqQ== X-Received: by 2002:adf:a54f:: with SMTP id j15mr485809wrb.218.1634748883217; Wed, 20 Oct 2021 09:54:43 -0700 (PDT) Received: from localhost.localdomain ([193.48.40.117]) by smtp.gmail.com with ESMTPSA id o11sm3639977wry.0.2021.10.20.09.54.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Oct 2021 09:54:42 -0700 (PDT) From: zimoun Date: Wed, 20 Oct 2021 18:54:35 +0200 Message-Id: <20211020165435.3358398-2-zimon.toutoune@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211020165435.3358398-1-zimon.toutoune@gmail.com> References: <20211020165435.3358398-1-zimon.toutoune@gmail.com> 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" X-getmail-retrieved-from-mailbox: Patches * guix/scripts/hash.scm (guix-hash)[package?]: New procedure. [hash-to-display]: Use it. * tests/guix-hash.scm: New test. --- guix/scripts/hash.scm | 19 +++++++++++++++++-- tests/guix-hash.sh | 10 ++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm index f3363549d3..4f0d41629f 100644 --- a/guix/scripts/hash.scm +++ b/guix/scripts/hash.scm @@ -22,6 +22,9 @@ (define-module (guix scripts hash) #:use-module (gcrypt hash) + #:use-module ((gnu packages) #:select (find-best-packages-by-name)) + #:use-module (guix packages) + #:use-module ((guix utils) #:select (package-name->name+version)) #:use-module (guix serialization) #:use-module (guix ui) #:use-module (guix scripts) @@ -48,8 +51,8 @@ (define %default-options (hash-algorithm . ,(hash-algorithm sha256)))) (define (show-help) - (display (G_ "Usage: guix hash [OPTION] FILE -Return the cryptographic hash of FILE.\n")) + (display (G_ "Usage: guix hash [OPTION] FILE-OR-PACKAGE +Return the cryptographic hash of FILE-OR-PACKAGE.\n")) (newline) (display (G_ "\ Supported formats: 'base64', 'nix-base32' (default), 'base32', @@ -141,6 +144,12 @@ (define (directory? file) ((directory) #t) (else #f))) + (define (package? spec) + (let-values (((name version) (package-name->name+version spec))) + (match (find-best-packages-by-name name version) + ((package) package) + (_ #f)))) + (let* ((opts (parse-options)) (args (filter-map (match-lambda (('argument . value) @@ -182,6 +191,12 @@ (define (hash-to-display thing) ("-" (with-error-handling (fmt (port-hash (assoc-ref opts 'hash-algorithm) (current-input-port))))) + ((? package? spec) + (let* ((package (package? spec)) + (origin (package-source package)) + (content-hash (origin-hash origin)) + (hash (content-hash-value content-hash))) + (fmt hash))) (x (leave (G_ "wrong argument~%"))))) diff --git a/tests/guix-hash.sh b/tests/guix-hash.sh index c4461fa955..41bd2b1588 100644 --- a/tests/guix-hash.sh +++ b/tests/guix-hash.sh @@ -1,6 +1,7 @@ # GNU Guix --- Functional package management for GNU # Copyright © 2013, 2014, 2016, 2020, 2021 Ludovic Courtès # Copyright © 2016 Jan Nieuwenhuizen +# Copyright © 2021 Simon Tournier # # This file is part of GNU Guix. # @@ -65,3 +66,12 @@ test `guix hash -r $tmpdir -x` = 10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g # Without '-r', this should fail. ! guix hash "$tmpdir" +cat > "$tmpdir/foo.scm"<base16-string + (content-hash-value (origin-hash (package-source hello))))) +EOF +test `guix hash hello -f base16` = `guix repl -- $tmpdir/foo.scm`