gnu: Add r-flowsom.
[jackhill/guix/guix.git] / guix / discovery.scm
CommitLineData
cd903ef7 1;;; GNU Guix --- Functional package management for GNU
1d90e9d7 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
cd903ef7
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (guix discovery)
3c0128b0 20 #:use-module (guix i18n)
7a51c78c 21 #:use-module (guix modules)
cd903ef7 22 #:use-module (guix combinators)
d27cc3bf 23 #:use-module (guix build syscalls)
cd903ef7
LC
24 #:use-module (srfi srfi-1)
25 #:use-module (ice-9 match)
26 #:use-module (ice-9 vlist)
27 #:use-module (ice-9 ftw)
eaae07ec
LC
28 #:export (scheme-files
29 scheme-modules
02fa1d25 30 scheme-modules*
cd903ef7
LC
31 fold-modules
32 all-modules
1d90e9d7
LC
33 fold-module-public-variables
34 fold-module-public-variables*))
cd903ef7
LC
35
36;;; Commentary:
37;;;
38;;; This module provides tools to discover Guile modules and the variables
39;;; they export.
40;;;
41;;; Code:
42
43(define* (scheme-files directory)
44 "Return the list of Scheme files found under DIRECTORY, recursively. The
d46c4423
LC
45returned list is sorted in alphabetical order. Return the empty list if
46DIRECTORY is not accessible."
d27cc3bf
LC
47 (define (entry-type name properties)
48 (match (assoc-ref properties 'type)
49 ('unknown
50 (stat:type (lstat name)))
51 ((? symbol? type)
52 type)))
cd903ef7 53
d27cc3bf
LC
54 ;; Use 'scandir*' so we can avoid an extra 'lstat' for each entry, as
55 ;; opposed to Guile's 'scandir' or 'file-system-fold'.
56 (fold-right (lambda (entry result)
57 (match entry
58 (("." . _)
59 result)
60 ((".." . _)
61 result)
62 ((name . properties)
63 (let ((absolute (string-append directory "/" name)))
64 (case (entry-type absolute properties)
65 ((directory)
66 (append (scheme-files absolute) result))
960c6ce9 67 ((regular)
d27cc3bf
LC
68 (if (string-suffix? ".scm" name)
69 (cons absolute result)
70 result))
960c6ce9
LC
71 ((symlink)
72 (cond ((string-suffix? ".scm" name)
73 (cons absolute result))
74 ((stat absolute #f)
75 =>
76 (match-lambda
77 (#f result)
78 ((= stat:type 'directory)
79 (append (scheme-files absolute)
80 result))
1d0bde2e
CB
81 (_ result)))
82 (else
83 result)))
d27cc3bf
LC
84 (else
85 result))))))
86 '()
d46c4423
LC
87 (catch 'system-error
88 (lambda ()
89 (scandir* directory))
90 (lambda args
91 (let ((errno (system-error-errno args)))
92 (unless (= errno ENOENT)
3c0128b0
LC
93 (format (current-error-port) ;XXX
94 (G_ "cannot access `~a': ~a~%")
95 directory (strerror errno)))
d46c4423 96 '())))))
cd903ef7 97
3c0128b0
LC
98(define* (scheme-modules directory #:optional sub-directory
99 #:key (warn (const #f)))
cd903ef7 100 "Return the list of Scheme modules available under DIRECTORY.
3c0128b0
LC
101Optionally, narrow the search to SUB-DIRECTORY.
102
103WARN is called when a module could not be loaded. It is passed the module
104name and the exception key and arguments."
cd903ef7
LC
105 (define prefix-len
106 (string-length directory))
107
108 (filter-map (lambda (file)
a2a94b6e
LC
109 (let* ((relative (string-drop file prefix-len))
110 (module (file-name->module-name relative)))
cd903ef7
LC
111 (catch #t
112 (lambda ()
113 (resolve-interface module))
114 (lambda args
115 ;; Report the error, but keep going.
a2a94b6e 116 (warn file module args)
cd903ef7
LC
117 #f))))
118 (scheme-files (if sub-directory
119 (string-append directory "/" sub-directory)
120 directory))))
121
02fa1d25
LC
122(define* (scheme-modules* directory #:optional sub-directory)
123 "Return the list of module names found under SUB-DIRECTORY in DIRECTORY.
124This is a source-only variant that does not try to load files."
125 (let ((prefix (string-length directory)))
126 (map (lambda (file)
127 (file-name->module-name (string-drop file prefix)))
128 (scheme-files (if sub-directory
129 (string-append directory "/" sub-directory)
130 directory)))))
131
3c0128b0 132(define* (fold-modules proc init path #:key (warn (const #f)))
cd903ef7
LC
133 "Fold over all the Scheme modules present in PATH, a list of directories.
134Call (PROC MODULE RESULT) for each module that is found."
135 (fold (lambda (spec result)
136 (match spec
137 ((? string? directory)
3c0128b0 138 (fold proc result (scheme-modules directory #:warn warn)))
cd903ef7
LC
139 ((directory . sub-directory)
140 (fold proc result
3c0128b0
LC
141 (scheme-modules directory sub-directory
142 #:warn warn)))))
cd903ef7
LC
143 '()
144 path))
145
3c0128b0 146(define* (all-modules path #:key (warn (const #f)))
cd903ef7
LC
147 "Return the list of package modules found in PATH, a list of directories to
148search. Entries in PATH can be directory names (strings) or (DIRECTORY
149. SUB-DIRECTORY) pairs, in which case modules are searched for beneath
002d17dc
RV
150SUB-DIRECTORY. Modules are listed in the order they appear on the path."
151 (reverse (fold-modules cons '() path #:warn warn)))
cd903ef7 152
1d90e9d7
LC
153(define (fold-module-public-variables* proc init modules)
154 "Call (PROC MODULE SYMBOL VARIABLE) for each variable exported by one of MODULES,
155using INIT as the initial value of RESULT. It is guaranteed to never traverse
156the same object twice."
157 ;; Here SEEN is populated by variables; if two different variables refer to
158 ;; the same object, we still let them through.
159 (identity ;discard second return value
160 (fold2 (lambda (module result seen)
161 (fold2 (lambda (sym+var result seen)
162 (match sym+var
163 ((sym . var)
164 (if (not (vhash-assq var seen))
165 (values (proc module sym var result)
166 (vhash-consq var #t seen))
167 (values result seen)))))
168 result
169 seen
170 (module-map cons module)))
171 init
172 vlist-null
173 modules)))
174
cd903ef7
LC
175(define (fold-module-public-variables proc init modules)
176 "Call (PROC OBJECT RESULT) for each variable exported by one of MODULES,
177using INIT as the initial value of RESULT. It is guaranteed to never traverse
178the same object twice."
1d90e9d7 179 ;; Note: here SEEN is populated by objects, not by variables.
cd903ef7
LC
180 (identity ; discard second return value
181 (fold2 (lambda (module result seen)
182 (fold2 (lambda (var result seen)
183 (if (not (vhash-assq var seen))
184 (values (proc var result)
185 (vhash-consq var #t seen))
186 (values result seen)))
187 result
188 seen
189 (module-map (lambda (sym var)
190 (false-if-exception (variable-ref var)))
191 module)))
192 init
193 vlist-null
194 modules)))
195
196;;; discovery.scm ends here