summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2015-05-04 21:44:52 +0200
committerLudovic Courtès <ludo@gnu.org>2015-05-04 23:30:51 +0200
commit6568d2bd6e4e047dd95b00a7a6e7501a16491eb5 (patch)
treef5f16ed73cbb0486fd00a8147707ce75b2132df4
parente89431bf016830a919ec2430889f6c2679aab408 (diff)
search-paths: Add 'evaluate-search-paths', from (guix scripts package).
* guix/scripts/package.scm (with-null-error-port, evaluate-search-paths): Move to... * guix/search-paths.scm: ... here. * guix/utils.scm (string-tokenize*): Move to... * guix/search-paths.scm: ... here. * tests/utils.scm ("string-tokenize*"): Adjust accordingly.
-rw-r--r--guix/scripts/package.scm36
-rw-r--r--guix/search-paths.scm72
-rw-r--r--guix/utils.scm28
-rw-r--r--tests/utils.scm11
4 files changed, 77 insertions, 70 deletions
diff --git a/guix/scripts/package.scm b/guix/scripts/package.scm
index 44cacdca8b9..933f7d8ee57 100644
--- a/guix/scripts/package.scm
+++ b/guix/scripts/package.scm
@@ -375,42 +375,6 @@ an output path different than CURRENT-PATH."
375;;; Search paths. 375;;; Search paths.
376;;; 376;;;
377 377
378(define-syntax-rule (with-null-error-port exp)
379 "Evaluate EXP with the error port pointing to the bit bucket."
380 (with-error-to-port (%make-void-port "w")
381 (lambda () exp)))
382
383(define* (evaluate-search-paths search-paths directory
384 #:optional (getenv (const #f)))
385 "Evaluate SEARCH-PATHS, a list of search-path specifications, for DIRECTORY,
386and return a list of variable/value pairs. Use GETENV to determine the
387current settings and report only settings not already effective."
388 (define search-path-definition
389 (match-lambda
390 (($ <search-path-specification> variable files separator
391 type pattern)
392 (let* ((values (or (and=> (getenv variable)
393 (cut string-tokenize* <> separator))
394 '()))
395 ;; Add a trailing slash to force symlinks to be treated as
396 ;; directories when 'find-files' traverses them.
397 (files (if pattern
398 (map (cut string-append <> "/") files)
399 files))
400
401 ;; XXX: Silence 'find-files' when it stumbles upon non-existent
402 ;; directories (see
403 ;; <http://lists.gnu.org/archive/html/guix-devel/2015-01/msg00269.html>.)
404 (path (with-null-error-port
405 (search-path-as-list files (list directory)
406 #:type type
407 #:pattern pattern))))
408 (if (every (cut member <> values) path)
409 #f ;VARIABLE is already set appropriately
410 (cons variable (string-join path separator)))))))
411
412 (filter-map search-path-definition search-paths))
413
414(define* (search-path-environment-variables entries profile 378(define* (search-path-environment-variables entries profile
415 #:optional (getenv getenv)) 379 #:optional (getenv getenv))
416 "Return environment variable definitions that may be needed for the use of 380 "Return environment variable definitions that may be needed for the use of
diff --git a/guix/search-paths.scm b/guix/search-paths.scm
index 147bfcae8c3..b17f5acd5d2 100644
--- a/guix/search-paths.scm
+++ b/guix/search-paths.scm
@@ -18,6 +18,9 @@
18 18
19(define-module (guix search-paths) 19(define-module (guix search-paths)
20 #:use-module (guix records) 20 #:use-module (guix records)
21 #:use-module (guix build utils)
22 #:use-module (srfi srfi-1)
23 #:use-module (srfi srfi-26)
21 #:use-module (ice-9 match) 24 #:use-module (ice-9 match)
22 #:export (<search-path-specification> 25 #:export (<search-path-specification>
23 search-path-specification 26 search-path-specification
@@ -29,7 +32,8 @@
29 search-path-specification-file-pattern 32 search-path-specification-file-pattern
30 33
31 search-path-specification->sexp 34 search-path-specification->sexp
32 sexp->search-path-specification)) 35 sexp->search-path-specification
36 evaluate-search-paths))
33 37
34;;; Commentary: 38;;; Commentary:
35;;; 39;;;
@@ -74,4 +78,70 @@ a <search-path-specification> object."
74 (file-type type) 78 (file-type type)
75 (file-pattern pattern))))) 79 (file-pattern pattern)))))
76 80
81(define-syntax-rule (with-null-error-port exp)
82 "Evaluate EXP with the error port pointing to the bit bucket."
83 (with-error-to-port (%make-void-port "w")
84 (lambda () exp)))
85
86;; XXX: This procedure used to be in (guix utils) but since we want to be able
87;; to use (guix search-paths) on the build side, we want to avoid the
88;; dependency on (guix utils), and so this procedure is back here for now.
89(define (string-tokenize* string separator)
90 "Return the list of substrings of STRING separated by SEPARATOR. This is
91like `string-tokenize', but SEPARATOR is a string."
92 (define (index string what)
93 (let loop ((string string)
94 (offset 0))
95 (cond ((string-null? string)
96 #f)
97 ((string-prefix? what string)
98 offset)
99 (else
100 (loop (string-drop string 1) (+ 1 offset))))))
101
102 (define len
103 (string-length separator))
104
105 (let loop ((string string)
106 (result '()))
107 (cond ((index string separator)
108 =>
109 (lambda (offset)
110 (loop (string-drop string (+ offset len))
111 (cons (substring string 0 offset)
112 result))))
113 (else
114 (reverse (cons string result))))))
115
116(define* (evaluate-search-paths search-paths directory
117 #:optional (getenv (const #f)))
118 "Evaluate SEARCH-PATHS, a list of search-path specifications, for DIRECTORY,
119and return a list of variable/value pairs. Use GETENV to determine the
120current settings and report only settings not already effective."
121 (define search-path-definition
122 (match-lambda
123 (($ <search-path-specification> variable files separator
124 type pattern)
125 (let* ((values (or (and=> (getenv variable)
126 (cut string-tokenize* <> separator))
127 '()))
128 ;; Add a trailing slash to force symlinks to be treated as
129 ;; directories when 'find-files' traverses them.
130 (files (if pattern
131 (map (cut string-append <> "/") files)
132 files))
133
134 ;; XXX: Silence 'find-files' when it stumbles upon non-existent
135 ;; directories (see
136 ;; <http://lists.gnu.org/archive/html/guix-devel/2015-01/msg00269.html>.)
137 (path (with-null-error-port
138 (search-path-as-list files (list directory)
139 #:type type
140 #:pattern pattern))))
141 (if (every (cut member <> values) path)
142 #f ;VARIABLE is already set appropriately
143 (cons variable (string-join path separator)))))))
144
145 (filter-map search-path-definition search-paths))
146
77;;; search-paths.scm ends here 147;;; search-paths.scm ends here
diff --git a/guix/utils.scm b/guix/utils.scm
index 3d38ba12235..a2ade2bf970 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -72,7 +72,6 @@
72 version-major+minor 72 version-major+minor
73 guile-version>? 73 guile-version>?
74 package-name->name+version 74 package-name->name+version
75 string-tokenize*
76 string-replace-substring 75 string-replace-substring
77 arguments-from-environment-variable 76 arguments-from-environment-variable
78 file-extension 77 file-extension
@@ -606,33 +605,6 @@ introduce the version part."
606 (substring file 0 dot) 605 (substring file 0 dot)
607 file))) 606 file)))
608 607
609(define (string-tokenize* string separator)
610 "Return the list of substrings of STRING separated by SEPARATOR. This is
611like `string-tokenize', but SEPARATOR is a string."
612 (define (index string what)
613 (let loop ((string string)
614 (offset 0))
615 (cond ((string-null? string)
616 #f)
617 ((string-prefix? what string)
618 offset)
619 (else
620 (loop (string-drop string 1) (+ 1 offset))))))
621
622 (define len
623 (string-length separator))
624
625 (let loop ((string string)
626 (result '()))
627 (cond ((index string separator)
628 =>
629 (lambda (offset)
630 (loop (string-drop string (+ offset len))
631 (cons (substring string 0 offset)
632 result))))
633 (else
634 (reverse (cons string result))))))
635
636(define* (string-replace-substring str substr replacement 608(define* (string-replace-substring str substr replacement
637 #:optional 609 #:optional
638 (start 0) 610 (start 0)
diff --git a/tests/utils.scm b/tests/utils.scm
index a662c9a8d38..e03a07b2f58 100644
--- a/tests/utils.scm
+++ b/tests/utils.scm
@@ -1,5 +1,5 @@
1;;; GNU Guix --- Functional package management for GNU 1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org> 2;;; Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org> 3;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
4;;; 4;;;
5;;; This file is part of GNU Guix. 5;;; This file is part of GNU Guix.
@@ -82,10 +82,11 @@
82 ("foo" "bar" "baz") 82 ("foo" "bar" "baz")
83 ("foo" "bar" "") 83 ("foo" "bar" "")
84 ("foo" "bar" "baz")) 84 ("foo" "bar" "baz"))
85 (list (string-tokenize* "foo" ":") 85 (let ((string-tokenize* (@@ (guix search-paths) string-tokenize*)))
86 (string-tokenize* "foo;bar;baz" ";") 86 (list (string-tokenize* "foo" ":")
87 (string-tokenize* "foo!bar!" "!") 87 (string-tokenize* "foo;bar;baz" ";")
88 (string-tokenize* "foo+-+bar+-+baz" "+-+"))) 88 (string-tokenize* "foo!bar!" "!")
89 (string-tokenize* "foo+-+bar+-+baz" "+-+"))))
89 90
90(test-equal "string-replace-substring" 91(test-equal "string-replace-substring"
91 '("foo BAR! baz" 92 '("foo BAR! baz"