services: gdm: Allow for custom X session scripts.
[guix.git] / guix / discovery.scm
blobef5ae73973c069f667f646b2bbc8834c1e3b26b3
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 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/>.
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             fold-module-public-variables*))
36 ;;; Commentary:
37 ;;;
38 ;;; This module provides tools to discover Guile modules and the variables
39 ;;; they export.
40 ;;;
41 ;;; Code:
43 (define* (scheme-files directory)
44   "Return the list of Scheme files found under DIRECTORY, recursively.  The
45 returned list is sorted in alphabetical order.  Return the empty list if
46 DIRECTORY is not accessible."
47   (define (entry-type name properties)
48     (match (assoc-ref properties 'type)
49       ('unknown
50        (stat:type (lstat name)))
51       ((? symbol? type)
52        type)))
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))
67                        ((regular)
68                         (if (string-suffix? ".scm" name)
69                             (cons absolute result)
70                             result))
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))
81                                  (_ result)))))
82                        (else
83                         result))))))
84               '()
85               (catch 'system-error
86                 (lambda ()
87                   (scandir* directory))
88                 (lambda args
89                   (let ((errno (system-error-errno args)))
90                     (unless (= errno ENOENT)
91                       (format (current-error-port) ;XXX
92                               (G_ "cannot access `~a': ~a~%")
93                               directory (strerror errno)))
94                     '())))))
96 (define* (scheme-modules directory #:optional sub-directory
97                          #:key (warn (const #f)))
98   "Return the list of Scheme modules available under DIRECTORY.
99 Optionally, narrow the search to SUB-DIRECTORY.
101 WARN is called when a module could not be loaded.  It is passed the module
102 name and the exception key and arguments."
103   (define prefix-len
104     (string-length directory))
106   (filter-map (lambda (file)
107                 (let* ((file   (substring file prefix-len))
108                        (module (file-name->module-name file)))
109                   (catch #t
110                     (lambda ()
111                       (resolve-interface module))
112                     (lambda args
113                       ;; Report the error, but keep going.
114                       (warn module args)
115                       #f))))
116               (scheme-files (if sub-directory
117                                 (string-append directory "/" sub-directory)
118                                 directory))))
120 (define* (scheme-modules* directory #:optional sub-directory)
121   "Return the list of module names found under SUB-DIRECTORY in DIRECTORY.
122 This is a source-only variant that does not try to load files."
123   (let ((prefix (string-length directory)))
124     (map (lambda (file)
125            (file-name->module-name (string-drop file prefix)))
126          (scheme-files (if sub-directory
127                            (string-append directory "/" sub-directory)
128                            directory)))))
130 (define* (fold-modules proc init path #:key (warn (const #f)))
131   "Fold over all the Scheme modules present in PATH, a list of directories.
132 Call (PROC MODULE RESULT) for each module that is found."
133   (fold (lambda (spec result)
134           (match spec
135             ((? string? directory)
136              (fold proc result (scheme-modules directory #:warn warn)))
137             ((directory . sub-directory)
138              (fold proc result
139                    (scheme-modules directory sub-directory
140                                    #:warn warn)))))
141         '()
142         path))
144 (define* (all-modules path #:key (warn (const #f)))
145   "Return the list of package modules found in PATH, a list of directories to
146 search.  Entries in PATH can be directory names (strings) or (DIRECTORY
147 . SUB-DIRECTORY) pairs, in which case modules are searched for beneath
148 SUB-DIRECTORY."
149   (fold-modules cons '() path #:warn warn))
151 (define (fold-module-public-variables* proc init modules)
152   "Call (PROC MODULE SYMBOL VARIABLE) for each variable exported by one of MODULES,
153 using INIT as the initial value of RESULT.  It is guaranteed to never traverse
154 the same object twice."
155   ;; Here SEEN is populated by variables; if two different variables refer to
156   ;; the same object, we still let them through.
157   (identity                                       ;discard second return value
158    (fold2 (lambda (module result seen)
159             (fold2 (lambda (sym+var result seen)
160                      (match sym+var
161                        ((sym . var)
162                         (if (not (vhash-assq var seen))
163                             (values (proc module sym var result)
164                                     (vhash-consq var #t seen))
165                             (values result seen)))))
166                    result
167                    seen
168                    (module-map cons module)))
169           init
170           vlist-null
171           modules)))
173 (define (fold-module-public-variables proc init modules)
174   "Call (PROC OBJECT RESULT) for each variable exported by one of MODULES,
175 using INIT as the initial value of RESULT.  It is guaranteed to never traverse
176 the same object twice."
177   ;; Note: here SEEN is populated by objects, not by variables.
178   (identity   ; discard second return value
179    (fold2 (lambda (module result seen)
180             (fold2 (lambda (var result seen)
181                      (if (not (vhash-assq var seen))
182                          (values (proc var result)
183                                  (vhash-consq var #t seen))
184                          (values result seen)))
185                    result
186                    seen
187                    (module-map (lambda (sym var)
188                                  (false-if-exception (variable-ref var)))
189                                module)))
190           init
191           vlist-null
192           modules)))
194 ;;; discovery.scm ends here