From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id ALASBgxKcGFAvwAAgWs5BA (envelope-from ) for ; Wed, 20 Oct 2021 18:55:40 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id mAbTAQxKcGGVMgAA1q6Kng (envelope-from ) for ; Wed, 20 Oct 2021 16:55:40 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 5F80F14667 for ; Wed, 20 Oct 2021 18:55:39 +0200 (CEST) Received: from localhost ([::1]:47102 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mdEsM-00018J-I8 for larch@yhetil.org; Wed, 20 Oct 2021 12:55:38 -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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1634748939; h=from:from:sender:sender: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:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=WydHqYw6Dw85SFFZAywbmj0Idl7DO7jpycThEmU3aGM=; b=IpjcfKd8fsGtzBszEQ/ILwkqbkXQL+t4IHimwkw0gEWU/07XCoIOUUHg/cYmdXdihieg+5 qIrUDq0uLYq7zBcO1kSCG1/wJRZ/mwADoFkmh5hirk1UuEOSkkXJI+U3Q5ayeQZI6WQLyY QgbAXaHAYohit8Vqs0SybFpeA+zQQGInLLann+P8kN2cGQgegTB7kKv4vrTLkuuTSj7G1f 0NkTI9egTIe/2v8wDpqt2kBXaWLWiyRGTxIzv35LByxi6Z9i3DgKdl7idHP4aO1lI16Syi 5GdOWcnePjga+XKQWPah8p9i+gUY/pEU65ute9lsFqHGYVBP/iExbk2Bb80G8Q== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1634748939; a=rsa-sha256; cv=none; b=U4zqxbuqognz0a5KVSaeu5X068CU5s+Py57Rk4P2piK7QnMrWru66HU+Q1/oxf8Z+mNnmo LtCsrNi4emI8NJVMcnMfIuXSJmp8gVo6d49/Ultbg4/gCBXZaFEnckLHB5QXw7fK4sUzCQ 5q70ryjuKNLxHpxLrKD7b38Zdk/JOVUV7J0lKK4GkpV6os9Jkyv0aYam0/baU8DgxboHi4 MG3M35uJhSn3DIxERti0AUWa+whCy5rfEcZUV12Ak17/+i8Xker7U8DxYY+jFzZWjDwZAY AHSq/Mf3jwFrwMdkAyfqEfpkwMmrd+QfwS5KUdnxmR88XXXoTD0d2ie4M3zQ1g== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=O6+QbwDZ; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Spam-Score: -1.33 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=O6+QbwDZ; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: 5F80F14667 X-Spam-Score: -1.33 X-Migadu-Scanner: scn0.migadu.com X-TUID: 8rAV2hw7ZaC4 * 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` -- 2.32.0