2017-05-03 23:03:20 +02:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2019-01-12 22:26:01 +01:00
|
|
|
;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
|
2017-05-03 23:03:20 +02:00
|
|
|
;;;
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (guix discovery)
|
2018-03-26 23:42:59 +02:00
|
|
|
#:use-module (guix i18n)
|
2017-10-13 22:16:46 +02:00
|
|
|
#:use-module (guix modules)
|
2017-05-03 23:03:20 +02:00
|
|
|
#:use-module (guix combinators)
|
2017-06-16 12:07:26 +02:00
|
|
|
#:use-module (guix build syscalls)
|
2017-05-03 23:03:20 +02:00
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
#:use-module (ice-9 vlist)
|
|
|
|
#:use-module (ice-9 ftw)
|
2018-03-23 15:36:12 +01:00
|
|
|
#:export (scheme-files
|
|
|
|
scheme-modules
|
2018-08-24 15:51:31 +02:00
|
|
|
scheme-modules*
|
2017-05-03 23:03:20 +02:00
|
|
|
fold-modules
|
|
|
|
all-modules
|
2019-01-12 22:26:01 +01:00
|
|
|
fold-module-public-variables
|
|
|
|
fold-module-public-variables*))
|
2017-05-03 23:03:20 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; This module provides tools to discover Guile modules and the variables
|
|
|
|
;;; they export.
|
|
|
|
;;;
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(define* (scheme-files directory)
|
|
|
|
"Return the list of Scheme files found under DIRECTORY, recursively. The
|
2017-06-18 00:02:56 +02:00
|
|
|
returned list is sorted in alphabetical order. Return the empty list if
|
|
|
|
DIRECTORY is not accessible."
|
2017-06-16 12:07:26 +02:00
|
|
|
(define (entry-type name properties)
|
|
|
|
(match (assoc-ref properties 'type)
|
|
|
|
('unknown
|
|
|
|
(stat:type (lstat name)))
|
|
|
|
((? symbol? type)
|
|
|
|
type)))
|
2017-05-03 23:03:20 +02:00
|
|
|
|
2019-07-26 21:49:25 +02:00
|
|
|
(define (dot-prefixed? file)
|
|
|
|
(string-prefix? "." file))
|
|
|
|
|
2017-06-16 12:07:26 +02:00
|
|
|
;; Use 'scandir*' so we can avoid an extra 'lstat' for each entry, as
|
|
|
|
;; opposed to Guile's 'scandir' or 'file-system-fold'.
|
|
|
|
(fold-right (lambda (entry result)
|
|
|
|
(match entry
|
2019-07-26 21:49:25 +02:00
|
|
|
(((? dot-prefixed?) . _)
|
|
|
|
;; Exclude ".", "..", and hidden files such as backups.
|
2017-06-16 12:07:26 +02:00
|
|
|
result)
|
|
|
|
((name . properties)
|
|
|
|
(let ((absolute (string-append directory "/" name)))
|
|
|
|
(case (entry-type absolute properties)
|
|
|
|
((directory)
|
|
|
|
(append (scheme-files absolute) result))
|
2017-07-03 23:35:56 +02:00
|
|
|
((regular)
|
2017-06-16 12:07:26 +02:00
|
|
|
(if (string-suffix? ".scm" name)
|
|
|
|
(cons absolute result)
|
|
|
|
result))
|
2017-07-03 23:35:56 +02:00
|
|
|
((symlink)
|
|
|
|
(cond ((string-suffix? ".scm" name)
|
|
|
|
(cons absolute result))
|
|
|
|
((stat absolute #f)
|
|
|
|
=>
|
|
|
|
(match-lambda
|
|
|
|
(#f result)
|
|
|
|
((= stat:type 'directory)
|
|
|
|
(append (scheme-files absolute)
|
|
|
|
result))
|
2019-07-07 13:14:58 +02:00
|
|
|
(_ result)))
|
|
|
|
(else
|
|
|
|
result)))
|
2017-06-16 12:07:26 +02:00
|
|
|
(else
|
|
|
|
result))))))
|
|
|
|
'()
|
2017-06-18 00:02:56 +02:00
|
|
|
(catch 'system-error
|
|
|
|
(lambda ()
|
|
|
|
(scandir* directory))
|
|
|
|
(lambda args
|
|
|
|
(let ((errno (system-error-errno args)))
|
|
|
|
(unless (= errno ENOENT)
|
2018-03-26 23:42:59 +02:00
|
|
|
(format (current-error-port) ;XXX
|
|
|
|
(G_ "cannot access `~a': ~a~%")
|
|
|
|
directory (strerror errno)))
|
2017-06-18 00:02:56 +02:00
|
|
|
'())))))
|
2017-05-03 23:03:20 +02:00
|
|
|
|
2018-03-26 23:42:59 +02:00
|
|
|
(define* (scheme-modules directory #:optional sub-directory
|
|
|
|
#:key (warn (const #f)))
|
2017-05-03 23:03:20 +02:00
|
|
|
"Return the list of Scheme modules available under DIRECTORY.
|
2018-03-26 23:42:59 +02:00
|
|
|
Optionally, narrow the search to SUB-DIRECTORY.
|
|
|
|
|
|
|
|
WARN is called when a module could not be loaded. It is passed the module
|
|
|
|
name and the exception key and arguments."
|
2017-05-03 23:03:20 +02:00
|
|
|
(define prefix-len
|
|
|
|
(string-length directory))
|
|
|
|
|
2021-09-30 10:16:02 +02:00
|
|
|
;; Hide Guile warnings such as "source file [...] newer than compiled" when
|
|
|
|
;; loading user code, unless we're hacking on Guix proper. See
|
|
|
|
;; <https://issues.guix.gnu.org/43747>.
|
|
|
|
(parameterize ((current-warning-port (if (getenv "GUIX_UNINSTALLED")
|
|
|
|
(current-warning-port)
|
|
|
|
(%make-void-port "w"))))
|
|
|
|
(filter-map (lambda (file)
|
|
|
|
(let* ((relative (string-drop file prefix-len))
|
|
|
|
(module (file-name->module-name relative)))
|
|
|
|
(catch #t
|
|
|
|
(lambda ()
|
|
|
|
(resolve-interface module))
|
|
|
|
(lambda args
|
|
|
|
;; Report the error, but keep going.
|
|
|
|
(warn file module args)
|
|
|
|
#f))))
|
|
|
|
(scheme-files (if sub-directory
|
|
|
|
(string-append directory "/" sub-directory)
|
|
|
|
directory)))))
|
2017-05-03 23:03:20 +02:00
|
|
|
|
2018-08-24 15:51:31 +02:00
|
|
|
(define* (scheme-modules* directory #:optional sub-directory)
|
|
|
|
"Return the list of module names found under SUB-DIRECTORY in DIRECTORY.
|
|
|
|
This is a source-only variant that does not try to load files."
|
|
|
|
(let ((prefix (string-length directory)))
|
|
|
|
(map (lambda (file)
|
|
|
|
(file-name->module-name (string-drop file prefix)))
|
|
|
|
(scheme-files (if sub-directory
|
|
|
|
(string-append directory "/" sub-directory)
|
|
|
|
directory)))))
|
|
|
|
|
2018-03-26 23:42:59 +02:00
|
|
|
(define* (fold-modules proc init path #:key (warn (const #f)))
|
2017-05-03 23:03:20 +02:00
|
|
|
"Fold over all the Scheme modules present in PATH, a list of directories.
|
|
|
|
Call (PROC MODULE RESULT) for each module that is found."
|
|
|
|
(fold (lambda (spec result)
|
|
|
|
(match spec
|
|
|
|
((? string? directory)
|
2018-03-26 23:42:59 +02:00
|
|
|
(fold proc result (scheme-modules directory #:warn warn)))
|
2017-05-03 23:03:20 +02:00
|
|
|
((directory . sub-directory)
|
|
|
|
(fold proc result
|
2018-03-26 23:42:59 +02:00
|
|
|
(scheme-modules directory sub-directory
|
|
|
|
#:warn warn)))))
|
2017-05-03 23:03:20 +02:00
|
|
|
'()
|
|
|
|
path))
|
|
|
|
|
2018-03-26 23:42:59 +02:00
|
|
|
(define* (all-modules path #:key (warn (const #f)))
|
2017-05-03 23:03:20 +02:00
|
|
|
"Return the list of package modules found in PATH, a list of directories to
|
|
|
|
search. Entries in PATH can be directory names (strings) or (DIRECTORY
|
|
|
|
. SUB-DIRECTORY) pairs, in which case modules are searched for beneath
|
2019-05-25 08:40:38 +02:00
|
|
|
SUB-DIRECTORY. Modules are listed in the order they appear on the path."
|
|
|
|
(reverse (fold-modules cons '() path #:warn warn)))
|
2017-05-03 23:03:20 +02:00
|
|
|
|
2019-01-12 22:26:01 +01:00
|
|
|
(define (fold-module-public-variables* proc init modules)
|
2020-07-05 00:14:05 +02:00
|
|
|
"Call (PROC MODULE SYMBOL VARIABLE RESULT) for each variable exported by one
|
|
|
|
of MODULES, using INIT as the initial value of RESULT. It is guaranteed to
|
|
|
|
never traverse the same object twice."
|
2019-01-12 22:26:01 +01:00
|
|
|
;; Here SEEN is populated by variables; if two different variables refer to
|
|
|
|
;; the same object, we still let them through.
|
|
|
|
(identity ;discard second return value
|
|
|
|
(fold2 (lambda (module result seen)
|
|
|
|
(fold2 (lambda (sym+var result seen)
|
|
|
|
(match sym+var
|
|
|
|
((sym . var)
|
|
|
|
(if (not (vhash-assq var seen))
|
|
|
|
(values (proc module sym var result)
|
|
|
|
(vhash-consq var #t seen))
|
|
|
|
(values result seen)))))
|
|
|
|
result
|
|
|
|
seen
|
|
|
|
(module-map cons module)))
|
|
|
|
init
|
|
|
|
vlist-null
|
|
|
|
modules)))
|
|
|
|
|
2017-05-03 23:03:20 +02:00
|
|
|
(define (fold-module-public-variables proc init modules)
|
|
|
|
"Call (PROC OBJECT RESULT) for each variable exported by one of MODULES,
|
|
|
|
using INIT as the initial value of RESULT. It is guaranteed to never traverse
|
|
|
|
the same object twice."
|
2019-01-12 22:26:01 +01:00
|
|
|
;; Note: here SEEN is populated by objects, not by variables.
|
2017-05-03 23:03:20 +02:00
|
|
|
(identity ; discard second return value
|
|
|
|
(fold2 (lambda (module result seen)
|
|
|
|
(fold2 (lambda (var result seen)
|
|
|
|
(if (not (vhash-assq var seen))
|
|
|
|
(values (proc var result)
|
|
|
|
(vhash-consq var #t seen))
|
|
|
|
(values result seen)))
|
|
|
|
result
|
|
|
|
seen
|
|
|
|
(module-map (lambda (sym var)
|
|
|
|
(false-if-exception (variable-ref var)))
|
|
|
|
module)))
|
|
|
|
init
|
|
|
|
vlist-null
|
|
|
|
modules)))
|
|
|
|
|
|
|
|
;;; discovery.scm ends here
|