diff mbox series

[bug#51307,2/2] scripts: hash: Support file or package.

Message ID 20211020165435.3358398-2-zimon.toutoune@gmail.com
State New
Headers show
Series guix hash: eases conversion | expand

Checks

Context Check Description
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/applying patch success View Laminar job
cbaines/issue success View issue

Commit Message

zimoun Oct. 20, 2021, 4:54 p.m. UTC
* 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(-)

Comments

Ludovic Courtès Oct. 30, 2021, 2:48 p.m. UTC | #1
zimoun <zimon.toutoune@gmail.com> skribis:

> * 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))

I think I would prefer to keep (guix scripts hash) bare-bones, not
depending on the package machinery.

Most of the time one can run:

  guix hash $(guix build -S PACKAGE)

It’s not quite what you want if the package has patches or a snippet,
but that’s okay IMO.

WDYT?

Thanks,
Ludo’.
zimoun Oct. 30, 2021, 3:34 p.m. UTC | #2
On Sat, 30 Oct 2021 at 16:48, Ludovic Courtès <ludo@gnu.org> wrote:
> zimoun <zimon.toutoune@gmail.com> skribis:
>
>> * 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))
>
> I think I would prefer to keep (guix scripts hash) bare-bones, not
> depending on the package machinery.

I understand but I do not have better to propose. :-)


> Most of the time one can run:
>
>   guix hash $(guix build -S PACKAGE)

First, it is not true.  For instance,

        $ guix hash $(guix build -S graphviz)
        00skvq94xanwmprz5073mhmssz953dwf7h23p5czrpgd5s7hy444

and this hash does not correspond to the hash used by
Disarchive. Because “guix build -S” does not return what Guix downloads
but what Guix builds.  The cover letter provides another example for the
package ’ceph’.

Each time a patch or a snippet is added to origin, then it is not true.

Second, it requires to download for hashing.  When the hash is already
in the source.  I would like to avoid unnecessary downloads.  I mean, it
is ok to download for a couple of packages.  But it becomes impractical
for batch of 1200 (or more).


> It’s not quite what you want if the package has patches or a snippet,
> but that’s okay IMO.

No, that’s not OK. :-)  Because, it becomes really annoying.  I have to
open gnu/packages, then recompose URL, then download and hash with the
right format.  Too many manual and boring steps when all is there.  Just
require CLI to be displayed.

Cheers,
simon

PS:
That’s what the cover letter was explaining.  Sorry if it was badly
worded or unclear.
diff mbox series

Patch

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 <ludo@gnu.org>
 # Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
+# Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
 #
 # 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"<<EOF
+(use-modules (guix packages)
+             (gnu packages base)
+             (guix base16))
+(format #t "~a~%"
+        (bytevector->base16-string
+         (content-hash-value (origin-hash (package-source hello)))))
+EOF
+test `guix hash hello -f base16` = `guix repl -- $tmpdir/foo.scm`