diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2014-03-24 22:15:29 +0100 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2014-03-24 22:15:29 +0100 |
| commit | 01ac19dca4318d577cf3bef53cfe6af590f0e5f8 (patch) | |
| tree | 413b19db595ab5138a74a635419bd82aac5d3c24 | |
| parent | 80dea563a3dad98bda60385188509ca79a3651f8 (diff) | |
utils: Add 'call-with-decompressed-port' and 'call-with-compressed-output-port'.
* guix/utils.scm (call-with-decompressed-port,
call-with-compressed-output-port): New procedures.
* tests/utils.scm ("compressed-output-port + decompressed-port"):
Rewrite to use them.
| -rw-r--r-- | .dir-locals.el | 2 | ||||
| -rw-r--r-- | guix/utils.scm | 37 | ||||
| -rw-r--r-- | tests/utils.scm | 27 |
3 files changed, 48 insertions, 18 deletions
diff --git a/.dir-locals.el b/.dir-locals.el index 03d9a4ec8db..22ade9f8a5d 100644 --- a/.dir-locals.el +++ b/.dir-locals.el | |||
| @@ -22,6 +22,8 @@ | |||
| 22 | (eval . (put 'with-error-handling 'scheme-indent-function 0)) | 22 | (eval . (put 'with-error-handling 'scheme-indent-function 0)) |
| 23 | (eval . (put 'with-mutex 'scheme-indent-function 1)) | 23 | (eval . (put 'with-mutex 'scheme-indent-function 1)) |
| 24 | (eval . (put 'with-atomic-file-output 'scheme-indent-function 1)) | 24 | (eval . (put 'with-atomic-file-output 'scheme-indent-function 1)) |
| 25 | (eval . (put 'call-with-compressed-output-port 'scheme-indent-function 2)) | ||
| 26 | (eval . (put 'call-with-decompressed-port 'scheme-indent-function 2)) | ||
| 25 | 27 | ||
| 26 | (eval . (put 'syntax-parameterize 'scheme-indent-function 1)) | 28 | (eval . (put 'syntax-parameterize 'scheme-indent-function 1)) |
| 27 | (eval . (put 'with-monad 'scheme-indent-function 1)) | 29 | (eval . (put 'with-monad 'scheme-indent-function 1)) |
diff --git a/guix/utils.scm b/guix/utils.scm index 44060c46b59..7306c6011d2 100644 --- a/guix/utils.scm +++ b/guix/utils.scm | |||
| @@ -21,6 +21,7 @@ | |||
| 21 | #:use-module (guix config) | 21 | #:use-module (guix config) |
| 22 | #:use-module (srfi srfi-1) | 22 | #:use-module (srfi srfi-1) |
| 23 | #:use-module (srfi srfi-9) | 23 | #:use-module (srfi srfi-9) |
| 24 | #:use-module (srfi srfi-11) | ||
| 24 | #:use-module (srfi srfi-26) | 25 | #:use-module (srfi srfi-26) |
| 25 | #:use-module (srfi srfi-39) | 26 | #:use-module (srfi srfi-39) |
| 26 | #:use-module (srfi srfi-60) | 27 | #:use-module (srfi srfi-60) |
| @@ -74,7 +75,9 @@ | |||
| 74 | filtered-port | 75 | filtered-port |
| 75 | compressed-port | 76 | compressed-port |
| 76 | decompressed-port | 77 | decompressed-port |
| 77 | compressed-output-port)) | 78 | call-with-decompressed-port |
| 79 | compressed-output-port | ||
| 80 | call-with-compressed-output-port)) | ||
| 78 | 81 | ||
| 79 | 82 | ||
| 80 | ;;; | 83 | ;;; |
| @@ -224,6 +227,22 @@ a symbol such as 'xz." | |||
| 224 | ('gzip (filtered-port `(,%gzip "-c") input)) | 227 | ('gzip (filtered-port `(,%gzip "-c") input)) |
| 225 | (else (error "unsupported compression scheme" compression)))) | 228 | (else (error "unsupported compression scheme" compression)))) |
| 226 | 229 | ||
| 230 | (define (call-with-decompressed-port compression port proc) | ||
| 231 | "Call PROC with a wrapper around PORT, a file port, that decompresses data | ||
| 232 | read from PORT according to COMPRESSION, a symbol such as 'xz. PORT is closed | ||
| 233 | as soon as PROC's dynamic extent is entered." | ||
| 234 | (let-values (((decompressed pids) | ||
| 235 | (decompressed-port compression port))) | ||
| 236 | (dynamic-wind | ||
| 237 | (const #f) | ||
| 238 | (lambda () | ||
| 239 | (close-port port) | ||
| 240 | (proc decompressed)) | ||
| 241 | (lambda () | ||
| 242 | (close-port decompressed) | ||
| 243 | (unless (every (compose zero? cdr waitpid) pids) | ||
| 244 | (error "decompressed-port failure" pids)))))) | ||
| 245 | |||
| 227 | (define (filtered-output-port command output) | 246 | (define (filtered-output-port command output) |
| 228 | "Return an output port. Data written to that port is filtered through | 247 | "Return an output port. Data written to that port is filtered through |
| 229 | COMMAND and written to OUTPUT, an output file port. In addition, return a | 248 | COMMAND and written to OUTPUT, an output file port. In addition, return a |
| @@ -265,6 +284,22 @@ of PIDs to wait for." | |||
| 265 | ('gzip (filtered-output-port `(,%gzip "-c") output)) | 284 | ('gzip (filtered-output-port `(,%gzip "-c") output)) |
| 266 | (else (error "unsupported compression scheme" compression)))) | 285 | (else (error "unsupported compression scheme" compression)))) |
| 267 | 286 | ||
| 287 | (define (call-with-compressed-output-port compression port proc) | ||
| 288 | "Call PROC with a wrapper around PORT, a file port, that compresses data | ||
| 289 | that goes to PORT according to COMPRESSION, a symbol such as 'xz. PORT is | ||
| 290 | closed as soon as PROC's dynamic extent is entered." | ||
| 291 | (let-values (((compressed pids) | ||
| 292 | (compressed-output-port compression port))) | ||
| 293 | (dynamic-wind | ||
| 294 | (const #f) | ||
| 295 | (lambda () | ||
| 296 | (close-port port) | ||
| 297 | (proc compressed)) | ||
| 298 | (lambda () | ||
| 299 | (close-port compressed) | ||
| 300 | (unless (every (compose zero? cdr waitpid) pids) | ||
| 301 | (error "compressed-output-port failure" pids)))))) | ||
| 302 | |||
| 268 | 303 | ||
| 269 | ;;; | 304 | ;;; |
| 270 | ;;; Nixpkgs. | 305 | ;;; Nixpkgs. |
diff --git a/tests/utils.scm b/tests/utils.scm index adbfdf55baa..1da847689c6 100644 --- a/tests/utils.scm +++ b/tests/utils.scm | |||
| @@ -162,23 +162,16 @@ | |||
| 162 | (equal? (get-bytevector-all decompressed) data))))) | 162 | (equal? (get-bytevector-all decompressed) data))))) |
| 163 | 163 | ||
| 164 | (false-if-exception (delete-file temp-file)) | 164 | (false-if-exception (delete-file temp-file)) |
| 165 | (test-equal "compressed-output-port + decompressed-port" | 165 | (test-assert "compressed-output-port + decompressed-port" |
| 166 | '((0) "Hello, compressed port!") | 166 | (let* ((file (search-path %load-path "guix/derivations.scm")) |
| 167 | (let ((text "Hello, compressed port!") | 167 | (data (call-with-input-file file get-bytevector-all))) |
| 168 | (output (open-file temp-file "w0b"))) | 168 | (call-with-compressed-output-port 'xz (open-file temp-file "w0b") |
| 169 | (let-values (((compressed pids) | 169 | (lambda (compressed) |
| 170 | (compressed-output-port 'xz output))) | 170 | (put-bytevector compressed data))) |
| 171 | (display text compressed) | 171 | |
| 172 | (close-port compressed) | 172 | (bytevector=? data |
| 173 | (close-port output) | 173 | (call-with-decompressed-port 'xz (open-file temp-file "r0b") |
| 174 | (and (every (compose zero? cdr waitpid) pids) | 174 | get-bytevector-all)))) |
| 175 | (let*-values (((input) | ||
| 176 | (open-file temp-file "r0b")) | ||
| 177 | ((decompressed pids) | ||
| 178 | (decompressed-port 'xz input))) | ||
| 179 | (let ((str (get-string-all decompressed))) | ||
| 180 | (list (map (compose cdr waitpid) pids) | ||
| 181 | str))))))) | ||
| 182 | 175 | ||
| 183 | (false-if-exception (delete-file temp-file)) | 176 | (false-if-exception (delete-file temp-file)) |
| 184 | (test-equal "fcntl-flock wait" | 177 | (test-equal "fcntl-flock wait" |
