download.scm 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014, 2015 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2017 Efraim Flashner <efraim@flashner.co.il>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;
  21. ;;; Download a binary file from an external source.
  22. ;;;
  23. (use-modules (ice-9 match)
  24. (web uri)
  25. (web client)
  26. (rnrs io ports)
  27. (srfi srfi-11)
  28. (guix base16)
  29. (guix hash))
  30. (define %url-base
  31. "http://alpha.gnu.org/gnu/guix/bootstrap"
  32. ;; Alternately:
  33. ;;"http://www.fdn.fr/~lcourtes/software/guix/packages"
  34. )
  35. (define (file-name->uri file)
  36. "Return the URI for FILE."
  37. (match (string-tokenize file (char-set-complement (char-set #\/)))
  38. ((_ ... system basename)
  39. (string->uri
  40. (string-append %url-base "/" system
  41. (match system
  42. ("aarch64-linux"
  43. "/20170217/")
  44. ("armhf-linux"
  45. "/20150101/")
  46. (_
  47. "/20131110/"))
  48. basename)))))
  49. (match (command-line)
  50. ((_ file expected-hash)
  51. (let ((uri (file-name->uri file)))
  52. (format #t "downloading file `~a'~%from `~a'...~%"
  53. file (uri->string uri))
  54. (let*-values (((resp data) (http-get uri #:decode-body? #f))
  55. ((hash) (bytevector->base16-string (sha256 data)))
  56. ((part) (string-append file ".part")))
  57. (if (string=? expected-hash hash)
  58. (begin
  59. (call-with-output-file part
  60. (lambda (port)
  61. (put-bytevector port data)))
  62. (rename-file part file))
  63. (begin
  64. (format (current-error-port)
  65. "file at `~a' has SHA256 ~a; expected ~a~%"
  66. (uri->string uri) hash expected-hash)
  67. (exit 1)))))))