1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
33 fold-module-public-variables))
37 ;;; This module provides tools to discover Guile modules and the variables
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)
49 (stat:type (lstat name)))
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)
62 (let ((absolute (string-append directory "/" name)))
63 (case (entry-type absolute properties)
65 (append (scheme-files absolute) result))
67 (if (string-suffix? ".scm" name)
68 (cons absolute result)
71 (cond ((string-suffix? ".scm" name)
72 (cons absolute result))
77 ((= stat:type 'directory)
78 (append (scheme-files absolute)
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)))
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.
100 WARN is called when a module could not be loaded. It is passed the module
101 name and the exception key and arguments."
103 (string-length directory))
105 (filter-map (lambda (file)
106 (let* ((file (substring file prefix-len))
107 (module (file-name->module-name file)))
110 (resolve-interface module))
112 ;; Report the error, but keep going.
115 (scheme-files (if sub-directory
116 (string-append directory "/" sub-directory)
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)))
124 (file-name->module-name (string-drop file prefix)))
125 (scheme-files (if sub-directory
126 (string-append directory "/" sub-directory)
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)
134 ((? string? directory)
135 (fold proc result (scheme-modules directory #:warn warn)))
136 ((directory . sub-directory)
138 (scheme-modules directory sub-directory
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
148 (fold-modules cons '() path #:warn warn))
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)))
163 (module-map (lambda (sym var)
164 (false-if-exception (variable-ref var)))
170 ;;; discovery.scm ends here