summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2016-05-20 22:14:46 +0200
committerLudovic Courtès <ludo@gnu.org>2016-05-21 01:35:14 +0200
commitece6864bd04fc2f9ff86fd4ac9cb0712dd71c094 (patch)
tree53334de02ef208350cdb7d9d05306e729ed2633f
parentcf8b312d1872aec1f38a179eeb981d79bf7faa03 (diff)
grafts: Rename files whose name matches a graft.
Fixes <http://bugs.gnu.org/23132>. Reported by Mark H Weaver <mhw@netris.org>. * guix/build/graft.scm (rename-matching-files): New procedure. (rewrite-directory): Use it. * tests/grafts.scm ("graft-derivation, renaming"): New test.
-rw-r--r--guix/build/graft.scm25
-rw-r--r--tests/grafts.scm17
2 files changed, 41 insertions, 1 deletions
diff --git a/guix/build/graft.scm b/guix/build/graft.scm
index e9fce031810..b61982dd644 100644
--- a/guix/build/graft.scm
+++ b/guix/build/graft.scm
@@ -83,6 +83,28 @@ writing the result to OUTPUT."
83 (put-u8 output (char->integer char)) 83 (put-u8 output (char->integer char))
84 result))))) 84 result)))))
85 85
86(define (rename-matching-files directory mapping)
87 "Apply MAPPING to the names of all the files in DIRECTORY, where MAPPING is
88a list of store file name pairs."
89 (let* ((mapping (map (match-lambda
90 ((source . target)
91 (cons (basename source) (basename target))))
92 mapping))
93 (matches (find-files directory
94 (lambda (file stat)
95 (assoc-ref mapping (basename file)))
96 #:directories? #t)))
97
98 ;; XXX: This is not quite correct: if MAPPING contains "foo", and
99 ;; DIRECTORY contains "bar/foo/foo", we first rename "bar/foo" and then
100 ;; "bar/foo/foo" no longer exists so we fail. Oh well, surely that's good
101 ;; enough!
102 (for-each (lambda (file)
103 (let ((target (assoc-ref mapping (basename file))))
104 (rename-file file
105 (string-append (dirname file) "/" target))))
106 matches)))
107
86(define* (rewrite-directory directory output mapping 108(define* (rewrite-directory directory output mapping
87 #:optional (store (%store-directory))) 109 #:optional (store (%store-directory)))
88 "Copy DIRECTORY to OUTPUT, replacing strings according to MAPPING, a list of 110 "Copy DIRECTORY to OUTPUT, replacing strings according to MAPPING, a list of
@@ -127,6 +149,7 @@ file name pairs."
127 149
128 (n-par-for-each (parallel-job-count) 150 (n-par-for-each (parallel-job-count)
129 rewrite-leaf (find-files directory (const #t) 151 rewrite-leaf (find-files directory (const #t)
130 #:directories? #t))) 152 #:directories? #t))
153 (rename-matching-files output mapping))
131 154
132;;; graft.scm ends here 155;;; graft.scm ends here
diff --git a/tests/grafts.scm b/tests/grafts.scm
index f8c9eced1df..8cd048552c7 100644
--- a/tests/grafts.scm
+++ b/tests/grafts.scm
@@ -182,4 +182,21 @@
182 (and (string=? (readlink one) repl) 182 (and (string=? (readlink one) repl)
183 (string=? (readlink two) one)))))) 183 (string=? (readlink two) one))))))
184 184
185(test-assert "graft-derivation, renaming" ;<http://bugs.gnu.org/23132>
186 (let* ((build `(begin
187 (use-modules (guix build utils))
188 (mkdir-p (string-append (assoc-ref %outputs "out") "/"
189 (assoc-ref %build-inputs "in")))))
190 (orig (build-expression->derivation %store "thing-to-graft" build
191 #:modules '((guix build utils))
192 #:inputs `(("in" ,%bash))))
193 (repl (add-text-to-store %store "bash" "fake bash"))
194 (grafted (graft-derivation %store orig
195 (list (graft
196 (origin %bash)
197 (replacement repl))))))
198 (and (build-derivations %store (list grafted))
199 (let ((out (derivation->output-path grafted)))
200 (file-is-directory? (string-append out "/" repl))))))
201
185(test-end) 202(test-end)