discovery: Add 'scheme-modules*'.
[jackhill/guix/guix.git] / guix / discovery.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
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)
20 #:use-module (guix i18n)
21 #:use-module (guix modules)
22 #:use-module (guix combinators)
23 #:use-module (guix build syscalls)
24 #:use-module (srfi srfi-1)
25 #:use-module (ice-9 match)
26 #:use-module (ice-9 vlist)
27 #:use-module (ice-9 ftw)
28 #:export (scheme-files
29 scheme-modules
30 scheme-modules*
31 fold-modules
32 all-modules
33 fold-module-public-variables))
34
35 ;;; Commentary:
36 ;;;
37 ;;; This module provides tools to discover Guile modules and the variables
38 ;;; they export.
39 ;;;
40 ;;; Code:
41
42 (define* (scheme-files directory)
43 "Return the list of Scheme files found under DIRECTORY, recursively. The
44 returned list is sorted in alphabetical order. Return the empty list if
45 DIRECTORY is not accessible."
46 (define (entry-type name properties)
47 (match (assoc-ref properties 'type)
48 ('unknown
49 (stat:type (lstat name)))
50 ((? symbol? type)
51 type)))
52
53 ;; Use 'scandir*' so we can avoid an extra 'lstat' for each entry, as
54 ;; opposed to Guile's 'scandir' or 'file-system-fold'.
55 (fold-right (lambda (entry result)
56 (match entry
57 (("." . _)
58 result)
59 ((".." . _)
60 result)
61 ((name . properties)
62 (let ((absolute (string-append directory "/" name)))
63 (case (entry-type absolute properties)
64 ((directory)
65 (append (scheme-files absolute) result))
66 ((regular)
67 (if (string-suffix? ".scm" name)
68 (cons absolute result)
69 result))
70 ((symlink)
71 (cond ((string-suffix? ".scm" name)
72 (cons absolute result))
73 ((stat absolute #f)
74 =>
75 (match-lambda
76 (#f result)
77 ((= stat:type 'directory)
78 (append (scheme-files absolute)
79 result))
80 (_ result)))))
81 (else
82 result))))))
83 '()
84 (catch 'system-error
85 (lambda ()
86 (scandir* directory))
87 (lambda args
88 (let ((errno (system-error-errno args)))
89 (unless (= errno ENOENT)
90 (format (current-error-port) ;XXX
91 (G_ "cannot access `~a': ~a~%")
92 directory (strerror errno)))
93 '())))))
94
95 (define* (scheme-modules directory #:optional sub-directory
96 #:key (warn (const #f)))
97 "Return the list of Scheme modules available under DIRECTORY.
98 Optionally, narrow the search to SUB-DIRECTORY.
99
100 WARN is called when a module could not be loaded. It is passed the module
101 name and the exception key and arguments."
102 (define prefix-len
103 (string-length directory))
104
105 (filter-map (lambda (file)
106 (let* ((file (substring file prefix-len))
107 (module (file-name->module-name file)))
108 (catch #t
109 (lambda ()
110 (resolve-interface module))
111 (lambda args
112 ;; Report the error, but keep going.
113 (warn module args)
114 #f))))
115 (scheme-files (if sub-directory
116 (string-append directory "/" sub-directory)
117 directory))))
118
119 (define* (scheme-modules* directory #:optional sub-directory)
120 "Return the list of module names found under SUB-DIRECTORY in DIRECTORY.
121 This is a source-only variant that does not try to load files."
122 (let ((prefix (string-length directory)))
123 (map (lambda (file)
124 (file-name->module-name (string-drop file prefix)))
125 (scheme-files (if sub-directory
126 (string-append directory "/" sub-directory)
127 directory)))))
128
129 (define* (fold-modules proc init path #:key (warn (const #f)))
130 "Fold over all the Scheme modules present in PATH, a list of directories.
131 Call (PROC MODULE RESULT) for each module that is found."
132 (fold (lambda (spec result)
133 (match spec
134 ((? string? directory)
135 (fold proc result (scheme-modules directory #:warn warn)))
136 ((directory . sub-directory)
137 (fold proc result
138 (scheme-modules directory sub-directory
139 #:warn warn)))))
140 '()
141 path))
142
143 (define* (all-modules path #:key (warn (const #f)))
144 "Return the list of package modules found in PATH, a list of directories to
145 search. Entries in PATH can be directory names (strings) or (DIRECTORY
146 . SUB-DIRECTORY) pairs, in which case modules are searched for beneath
147 SUB-DIRECTORY."
148 (fold-modules cons '() path #:warn warn))
149
150 (define (fold-module-public-variables proc init modules)
151 "Call (PROC OBJECT RESULT) for each variable exported by one of MODULES,
152 using INIT as the initial value of RESULT. It is guaranteed to never traverse
153 the same object twice."
154 (identity ; discard second return value
155 (fold2 (lambda (module result seen)
156 (fold2 (lambda (var result seen)
157 (if (not (vhash-assq var seen))
158 (values (proc var result)
159 (vhash-consq var #t seen))
160 (values result seen)))
161 result
162 seen
163 (module-map (lambda (sym var)
164 (false-if-exception (variable-ref var)))
165 module)))
166 init
167 vlist-null
168 modules)))
169
170 ;;; discovery.scm ends here