summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2014-03-22 21:58:23 +0100
committerLudovic Courtès <ludo@gnu.org>2014-03-22 22:57:09 +0100
commit7a8024a33ad862fdbc8ae741f0a6f5338cb5b82b (patch)
treec2ab108d3f49f4ead6f964ae46e1c0c3678df66c
parent443eb4e9506026094f5e0dadc3e11d3cf7a86a24 (diff)
utils: Add 'decompressed-port' and 'compressed-port'.
* guix/utils.scm (decompressed-port, compressed-port): New procedures. * guix/scripts/substitute-binary.scm (decompressed-port): Remove. (guix-substitute-binary): Pass a symbol or #f as the first argument to 'decompress-port'. * tests/utils.scm ("compressed-port, decompressed-port, non-file"): New test.
-rwxr-xr-xguix/scripts/substitute-binary.scm13
-rw-r--r--guix/utils.scm25
-rw-r--r--tests/utils.scm11
3 files changed, 37 insertions, 12 deletions
diff --git a/guix/scripts/substitute-binary.scm b/guix/scripts/substitute-binary.scm
index 7ac12ddef2a..4e49b0c3ac7 100755
--- a/guix/scripts/substitute-binary.scm
+++ b/guix/scripts/substitute-binary.scm
@@ -400,16 +400,6 @@ indefinitely."
400 (call-with-output-file expiry-file 400 (call-with-output-file expiry-file
401 (cute write (time-second now) <>)))) 401 (cute write (time-second now) <>))))
402 402
403(define (decompressed-port compression input)
404 "Return an input port where INPUT is decompressed according to COMPRESSION,
405along with a list of PIDs to wait for."
406 (match compression
407 ("none" (values input '()))
408 ("bzip2" (filtered-port `(,%bzip2 "-dc") input))
409 ("xz" (filtered-port `(,%xz "-dc") input))
410 ("gzip" (filtered-port `(,%gzip "-dc") input))
411 (else (error "unsupported compression scheme" compression))))
412
413(define (progress-report-port report-progress port) 403(define (progress-report-port report-progress port)
414 "Return a port that calls REPORT-PROGRESS every time something is read from 404 "Return a port that calls REPORT-PROGRESS every time something is read from
415PORT. REPORT-PROGRESS is a two-argument procedure such as that returned by 405PORT. REPORT-PROGRESS is a two-argument procedure such as that returned by
@@ -598,7 +588,8 @@ substituter disabled~%")
598 (current-error-port)))) 588 (current-error-port))))
599 (progress-report-port progress raw))) 589 (progress-report-port progress raw)))
600 ((input pids) 590 ((input pids)
601 (decompressed-port (narinfo-compression narinfo) 591 (decompressed-port (and=> (narinfo-compression narinfo)
592 string->symbol)
602 progress))) 593 progress)))
603 ;; Unpack the Nar at INPUT into DESTINATION. 594 ;; Unpack the Nar at INPUT into DESTINATION.
604 (restore-file input destination) 595 (restore-file input destination)
diff --git a/guix/utils.scm b/guix/utils.scm
index 15a43900748..f786c83f471 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -70,7 +70,10 @@
70 call-with-temporary-output-file 70 call-with-temporary-output-file
71 with-atomic-file-output 71 with-atomic-file-output
72 fold2 72 fold2
73 filtered-port)) 73
74 filtered-port
75 compressed-port
76 decompressed-port))
74 77
75 78
76;;; 79;;;
@@ -200,6 +203,26 @@ buffered data is lost."
200 (close-port out) 203 (close-port out)
201 (loop in (cons child pids))))))))) 204 (loop in (cons child pids)))))))))
202 205
206(define (decompressed-port compression input)
207 "Return an input port where INPUT is decompressed according to COMPRESSION,
208a symbol such as 'xz."
209 (match compression
210 ((or #f 'none) (values input '()))
211 ('bzip2 (filtered-port `(,%bzip2 "-dc") input))
212 ('xz (filtered-port `(,%xz "-dc") input))
213 ('gzip (filtered-port `(,%gzip "-dc") input))
214 (else (error "unsupported compression scheme" compression))))
215
216(define (compressed-port compression input)
217 "Return an input port where INPUT is decompressed according to COMPRESSION,
218a symbol such as 'xz."
219 (match compression
220 ((or #f 'none) (values input '()))
221 ('bzip2 (filtered-port `(,%bzip2 "-c") input))
222 ('xz (filtered-port `(,%xz "-c") input))
223 ('gzip (filtered-port `(,%gzip "-c") input))
224 (else (error "unsupported compression scheme" compression))))
225
203 226
204;;; 227;;;
205;;; Nixpkgs. 228;;; Nixpkgs.
diff --git a/tests/utils.scm b/tests/utils.scm
index 85daa3db919..39cad701b89 100644
--- a/tests/utils.scm
+++ b/tests/utils.scm
@@ -150,6 +150,17 @@
150 (any (compose (negate zero?) cdr waitpid) 150 (any (compose (negate zero?) cdr waitpid)
151 pids)))) 151 pids))))
152 152
153(test-assert "compressed-port, decompressed-port, non-file"
154 (let ((data (call-with-input-file (search-path %load-path "guix.scm")
155 get-bytevector-all)))
156 (let*-values (((compressed pids1)
157 (compressed-port 'xz (open-bytevector-input-port data)))
158 ((decompressed pids2)
159 (decompressed-port 'xz compressed)))
160 (and (every (compose zero? cdr waitpid)
161 (append pids1 pids2))
162 (equal? (get-bytevector-all decompressed) data)))))
163
153(false-if-exception (delete-file temp-file)) 164(false-if-exception (delete-file temp-file))
154(test-equal "fcntl-flock wait" 165(test-equal "fcntl-flock wait"
155 42 ; the child's exit status 166 42 ; the child's exit status