extracting-download.scm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017 Mathieu Lirzin <mthl@gnu.org>
  4. ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
  5. ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
  6. ;;; Copyright © 2020 Hartmut Goebel <h.goebel@crazy-compilers.com>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (guix extracting-download)
  23. #:use-module (ice-9 match)
  24. #:use-module (ice-9 popen)
  25. #:use-module ((guix build download) #:prefix build:)
  26. #:use-module ((guix build utils) #:hide (delete))
  27. #:use-module (guix gexp)
  28. #:use-module (guix modules)
  29. #:use-module (guix monads)
  30. #:use-module (guix packages) ;; for %current-system
  31. #:use-module (guix store)
  32. #:use-module (guix utils)
  33. #:use-module (srfi srfi-26)
  34. #:export (http-fetch/extract
  35. download-to-store/extract))
  36. ;;;
  37. ;;; Produce fixed-output derivations with data extracted from n archive
  38. ;;; fetched over HTTP or FTP.
  39. ;;;
  40. ;;; This is meant to be used for package repositories where the actual source
  41. ;;; archive is packed into another archive, eventually carrying meta-data.
  42. ;;; Using this derivation saves both storing the outer archive and extracting
  43. ;;; the actual one at build time. The hash is calculated on the actual
  44. ;;; archive to ease validating the stored file.
  45. ;;;
  46. (define* (http-fetch/extract url filename-to-extract hash-algo hash
  47. #:optional name
  48. #:key (system (%current-system)) (guile (default-guile)))
  49. "Return a fixed-output derivation that fetches an archive at URL, and
  50. extracts FILE_TO_EXTRACT from the archive. The FILE_TO_EXTRACT is expected to
  51. have hash HASH of type HASH-ALGO (a symbol). By default, the file name is the
  52. base name of URL; optionally, NAME can specify a different file name."
  53. (define file-name
  54. (match url
  55. ((head _ ...)
  56. (basename head))
  57. (_
  58. (basename url))))
  59. (define guile-zlib
  60. (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
  61. (define guile-json
  62. (module-ref (resolve-interface '(gnu packages guile)) 'guile-json-4))
  63. (define gnutls
  64. (module-ref (resolve-interface '(gnu packages tls)) 'gnutls))
  65. (define inputs
  66. `(("tar" ,(module-ref (resolve-interface '(gnu packages base))
  67. 'tar))))
  68. (define config.scm
  69. (scheme-file "config.scm"
  70. #~(begin
  71. (define-module (guix config)
  72. #:export (%system))
  73. (define %system
  74. #$(%current-system)))))
  75. (define modules
  76. (cons `((guix config) => ,config.scm)
  77. (delete '(guix config)
  78. (source-module-closure '((guix build download)
  79. (guix build utils)
  80. (guix utils)
  81. (web uri))))))
  82. (define build
  83. (with-imported-modules modules
  84. (with-extensions (list guile-json gnutls ;for (guix swh)
  85. guile-zlib)
  86. #~(begin
  87. (use-modules (guix build download)
  88. (guix build utils)
  89. (guix utils)
  90. (web uri)
  91. (ice-9 match)
  92. (ice-9 popen))
  93. ;; The code below expects tar to be in $PATH.
  94. (set-path-environment-variable "PATH" '("bin")
  95. (match '#+inputs
  96. (((names dirs outputs ...) ...)
  97. dirs)))
  98. (setvbuf (current-output-port) 'line)
  99. (setvbuf (current-error-port) 'line)
  100. (call-with-temporary-directory
  101. (lambda (directory)
  102. ;; TODO: Support different archive types, based on content-type
  103. ;; or archive name extention.
  104. (let* ((file-to-extract (getenv "extract filename"))
  105. (port (http-fetch (string->uri (getenv "download url"))
  106. #:verify-certificate? #f))
  107. (tar (open-pipe* OPEN_WRITE "tar" "-C" directory
  108. "-xf" "-" file-to-extract)))
  109. (dump-port port tar)
  110. (close-port port)
  111. (let ((status (close-pipe tar)))
  112. (unless (zero? status)
  113. (error "tar extraction failure" status)))
  114. (copy-file (string-append directory "/"
  115. (getenv "extract filename"))
  116. #$output))))))))
  117. (mlet %store-monad ((guile (package->derivation guile system)))
  118. (gexp->derivation (or name file-name) build
  119. ;; Use environment variables and a fixed script name so
  120. ;; there's only one script in store for all the
  121. ;; downloads.
  122. #:script-name "extract-download"
  123. #:env-vars
  124. `(("download url" . ,url)
  125. ("extract filename" . ,filename-to-extract))
  126. #:leaked-env-vars '("http_proxy" "https_proxy"
  127. "LC_ALL" "LC_MESSAGES" "LANG"
  128. "COLUMNS")
  129. #:system system
  130. #:local-build? #t ; don't offload download
  131. #:hash-algo hash-algo
  132. #:hash hash
  133. #:guile-for-build guile)))
  134. (define* (download-to-store/extract store url filename-to-extract
  135. #:optional (name (basename url))
  136. #:key (log (current-error-port))
  137. (verify-certificate? #t))
  138. "Download an archive from URL, and extracts FILE_TO_EXTRACT from the archive
  139. to STORE, either under NAME or URL's basename if omitted. Write progress
  140. reports to LOG. VERIFY-CERTIFICATE? determines whether or not to validate
  141. HTTPS server certificates."
  142. (call-with-temporary-output-file
  143. (lambda (temp port)
  144. (let ((result
  145. (parameterize ((current-output-port log))
  146. (build:url-fetch url temp
  147. ;;#:mirrors %mirrors
  148. #:verify-certificate?
  149. verify-certificate?))))
  150. (close port)
  151. (and result
  152. (call-with-temporary-output-file
  153. (lambda (contents port)
  154. (let ((tar (open-pipe* OPEN_READ
  155. "tar" ;"--auto-compress"
  156. "-xf" temp "--to-stdout" filename-to-extract)))
  157. (dump-port tar port)
  158. (close-port port)
  159. (let ((status (close-pipe tar)))
  160. (unless (zero? status)
  161. (error "tar extraction failure" status)))
  162. (add-to-store store name #f "sha256" contents)))))))))