guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

01/02: guix download: Add '-o' option.


From: Ludovic Courtès
Subject: 01/02: guix download: Add '-o' option.
Date: Fri, 28 Oct 2016 23:20:13 +0000 (UTC)

civodul pushed a commit to branch wip-oob-download
in repository guix.

commit d18bdeda898fce6d99373c80c199735969f70cfd
Author: Ludovic Courtès <address@hidden>
Date:   Sat Oct 29 01:16:24 2016 +0200

    guix download: Add '-o' option.
    
    * guix/scripts/download.scm (download-to-file, download-to-store*): New
    procedures.
    (%default-options): Add 'download-proc'.
    (show-help): Adjust description and document '-o'.
    (%options): Add '-o'.
    (guix-download): Remove 'store' variable.  Add 'fetch' and define 'path'
    to as its result.
    * tests/guix-download.sh: Add test.
---
 doc/guix.texi             |    7 ++++++-
 guix/scripts/download.scm |   51 +++++++++++++++++++++++++++++++--------------
 tests/guix-download.sh    |    9 +++++++-
 3 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 22d353c..25cdb4b 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -4768,13 +4768,18 @@ they are not available, an error is raised.  
@xref{Guile Preparations,
 how to install the GnuTLS bindings for Guile,, gnutls-guile,
 GnuTLS-Guile}, for more information.
 
-The following option is available:
+The following options are available:
 
 @table @code
 @item address@hidden
 @itemx -f @var{fmt}
 Write the hash in the format specified by @var{fmt}.  For more
 information on the valid values for @var{fmt}, @pxref{Invoking guix hash}.
+
address@hidden address@hidden
address@hidden -o @var{file}
+Save the downloaded file to @var{file} instead of adding it to the
+store.
 @end table
 
 @node Invoking guix hash
diff --git a/guix/scripts/download.scm b/guix/scripts/download.scm
index bcb4eaa..422a807 100644
--- a/guix/scripts/download.scm
+++ b/guix/scripts/download.scm
@@ -23,12 +23,15 @@
   #:use-module (guix hash)
   #:use-module (guix utils)
   #:use-module (guix base32)
-  #:use-module (guix download)
-  #:use-module ((guix build download) #:select (current-terminal-columns))
-  #:use-module ((guix build syscalls) #:select (terminal-columns))
+  #:use-module ((guix download) #:hide (url-fetch))
+  #:use-module ((guix build download)
+                #:select (url-fetch current-terminal-columns))
+  #:use-module ((guix build syscalls)
+                #:select (terminal-columns))
   #:use-module (web uri)
   #:use-module (ice-9 match)
   #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-26)
   #:use-module (srfi srfi-37)
   #:use-module (rnrs bytevectors)
   #:use-module (ice-9 binary-ports)
@@ -39,19 +42,36 @@
 ;;; Command-line options.
 ;;;
 
+(define (download-to-file url file)
+  "Download the file at URI to FILE.  Return FILE."
+  (let ((uri (string->uri url)))
+    (match (uri-scheme uri)
+      ((or 'file #f)
+       (copy-file (uri-path uri) file))
+      (_
+       (url-fetch url file)))
+    file))
+
+(define (download-to-store* url)
+  (with-store store
+    (download-to-store store url)))
+
 (define %default-options
   ;; Alist of default option values.
-  `((format . ,bytevector->nix-base32-string)))
+  `((format . ,bytevector->nix-base32-string)
+    (download-proc . ,download-to-store*)))
 
 (define (show-help)
   (display (_ "Usage: guix download [OPTION] URL
-Download the file at URL, add it to the store, and print its store path
-and the hash of its contents.
+Download the file at URL to the store or to the given file, and print its
+file name and the hash of its contents.
 
 Supported formats: 'nix-base32' (default), 'base32', and 'base16'
 ('hex' and 'hexadecimal' can be used as well).\n"))
   (format #t (_ "
   -f, --format=FMT       write the hash in the given format"))
+  (format #f (_ "
+  -o, --output=FILE      download to FILE"))
   (newline)
   (display (_ "
   -h, --help             display this help and exit"))
@@ -77,6 +97,11 @@ Supported formats: 'nix-base32' (default), 'base32', and 
'base16'
 
                   (alist-cons 'format fmt-proc
                               (alist-delete 'format result))))
+        (option '(#\o "output") #t #f
+                (lambda (opt name arg result)
+                  (alist-cons 'download-proc
+                              (cut download-to-file <> arg)
+                              (alist-delete 'download result))))
 
         (option '(#\h "help") #f #f
                 (lambda args
@@ -106,21 +131,15 @@ Supported formats: 'nix-base32' (default), 'base32', and 
'base16'
 
   (with-error-handling
     (let* ((opts  (parse-options))
-           (store (open-connection))
            (arg   (or (assq-ref opts 'argument)
                       (leave (_ "no download URI was specified~%"))))
            (uri   (or (string->uri arg)
                       (leave (_ "~a: failed to parse URI~%")
                              arg)))
-           (path  (case (uri-scheme uri)
-                    ((file)
-                     (add-to-store store (basename (uri-path uri))
-                                   #f "sha256" (uri-path uri)))
-                    (else
-                     (parameterize ((current-terminal-columns
-                                     (terminal-columns)))
-                       (download-to-store store (uri->string uri)
-                                          (basename (uri-path uri)))))))
+           (fetch (assq-ref opts 'download-proc))
+           (path  (parameterize ((current-terminal-columns
+                                  (terminal-columns)))
+                    (fetch arg)))
            (hash  (call-with-input-file
                       (or path
                           (leave (_ "~a: download failed~%")
diff --git a/tests/guix-download.sh b/tests/guix-download.sh
index 6283772..ebc853c 100644
--- a/tests/guix-download.sh
+++ b/tests/guix-download.sh
@@ -1,5 +1,5 @@
 # GNU Guix --- Functional package management for GNU
-# Copyright © 2012, 2015 Ludovic Courtès <address@hidden>
+# Copyright © 2012, 2015, 2016 Ludovic Courtès <address@hidden>
 #
 # This file is part of GNU Guix.
 #
@@ -35,6 +35,13 @@ then false; else true; fi
 # This one should succeed.
 guix download "file://$abs_top_srcdir/README"
 
+# This one too, even if it cannot talk to the daemon.
+output="t-download-$$"
+trap 'rm -f "$output"' EXIT
+GUIX_DAEMON_SOCKET="/nowhere" guix download -o "$output" \
+                 "file://$abs_top_srcdir/README"
+cmp "$output" "$abs_top_srcdir/README"
+
 # This one should fail.
 if guix download "file:///does-not-exist" "file://$abs_top_srcdir/README"
 then false; else true; fi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]