discovery.scm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/>.
  18. (define-module (guix discovery)
  19. #:use-module (guix i18n)
  20. #:use-module (guix modules)
  21. #:use-module (guix combinators)
  22. #:use-module (guix build syscalls)
  23. #:use-module (srfi srfi-1)
  24. #:use-module (ice-9 match)
  25. #:use-module (ice-9 vlist)
  26. #:use-module (ice-9 ftw)
  27. #:export (scheme-files
  28. scheme-modules
  29. scheme-modules*
  30. fold-modules
  31. all-modules
  32. fold-module-public-variables
  33. fold-module-public-variables*))
  34. ;;; Commentary:
  35. ;;;
  36. ;;; This module provides tools to discover Guile modules and the variables
  37. ;;; they export.
  38. ;;;
  39. ;;; Code:
  40. (define* (scheme-files directory)
  41. "Return the list of Scheme files found under DIRECTORY, recursively. The
  42. returned list is sorted in alphabetical order. Return the empty list if
  43. DIRECTORY is not accessible."
  44. (define (entry-type name properties)
  45. (match (assoc-ref properties 'type)
  46. ('unknown
  47. (stat:type (lstat name)))
  48. ((? symbol? type)
  49. type)))
  50. (define (dot-prefixed? file)
  51. (string-prefix? "." file))
  52. ;; Use 'scandir*' so we can avoid an extra 'lstat' for each entry, as
  53. ;; opposed to Guile's 'scandir' or 'file-system-fold'.
  54. (fold-right (lambda (entry result)
  55. (match entry
  56. (((? dot-prefixed?) . _)
  57. ;; Exclude ".", "..", and hidden files such as backups.
  58. result)
  59. ((name . properties)
  60. (let ((absolute (string-append directory "/" name)))
  61. (case (entry-type absolute properties)
  62. ((directory)
  63. (append (scheme-files absolute) result))
  64. ((regular)
  65. (if (string-suffix? ".scm" name)
  66. (cons absolute result)
  67. result))
  68. ((symlink)
  69. (cond ((string-suffix? ".scm" name)
  70. (cons absolute result))
  71. ((stat absolute #f)
  72. =>
  73. (match-lambda
  74. (#f result)
  75. ((= stat:type 'directory)
  76. (append (scheme-files absolute)
  77. result))
  78. (_ result)))
  79. (else
  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. (define* (scheme-modules directory #:optional sub-directory
  95. #:key (warn (const #f)))
  96. "Return the list of Scheme modules available under DIRECTORY.
  97. Optionally, narrow the search to SUB-DIRECTORY.
  98. WARN is called when a module could not be loaded. It is passed the module
  99. name and the exception key and arguments."
  100. (define prefix-len
  101. (string-length directory))
  102. (filter-map (lambda (file)
  103. (let* ((relative (string-drop file prefix-len))
  104. (module (file-name->module-name relative)))
  105. (catch #t
  106. (lambda ()
  107. (resolve-interface module))
  108. (lambda args
  109. ;; Report the error, but keep going.
  110. (warn file module args)
  111. #f))))
  112. (scheme-files (if sub-directory
  113. (string-append directory "/" sub-directory)
  114. directory))))
  115. (define* (scheme-modules* directory #:optional sub-directory)
  116. "Return the list of module names found under SUB-DIRECTORY in DIRECTORY.
  117. This is a source-only variant that does not try to load files."
  118. (let ((prefix (string-length directory)))
  119. (map (lambda (file)
  120. (file-name->module-name (string-drop file prefix)))
  121. (scheme-files (if sub-directory
  122. (string-append directory "/" sub-directory)
  123. directory)))))
  124. (define* (fold-modules proc init path #:key (warn (const #f)))
  125. "Fold over all the Scheme modules present in PATH, a list of directories.
  126. Call (PROC MODULE RESULT) for each module that is found."
  127. (fold (lambda (spec result)
  128. (match spec
  129. ((? string? directory)
  130. (fold proc result (scheme-modules directory #:warn warn)))
  131. ((directory . sub-directory)
  132. (fold proc result
  133. (scheme-modules directory sub-directory
  134. #:warn warn)))))
  135. '()
  136. path))
  137. (define* (all-modules path #:key (warn (const #f)))
  138. "Return the list of package modules found in PATH, a list of directories to
  139. search. Entries in PATH can be directory names (strings) or (DIRECTORY
  140. . SUB-DIRECTORY) pairs, in which case modules are searched for beneath
  141. SUB-DIRECTORY. Modules are listed in the order they appear on the path."
  142. (reverse (fold-modules cons '() path #:warn warn)))
  143. (define (fold-module-public-variables* proc init modules)
  144. "Call (PROC MODULE SYMBOL VARIABLE RESULT) for each variable exported by one
  145. of MODULES, using INIT as the initial value of RESULT. It is guaranteed to
  146. never traverse the same object twice."
  147. ;; Here SEEN is populated by variables; if two different variables refer to
  148. ;; the same object, we still let them through.
  149. (identity ;discard second return value
  150. (fold2 (lambda (module result seen)
  151. (fold2 (lambda (sym+var result seen)
  152. (match sym+var
  153. ((sym . var)
  154. (if (not (vhash-assq var seen))
  155. (values (proc module sym var result)
  156. (vhash-consq var #t seen))
  157. (values result seen)))))
  158. result
  159. seen
  160. (module-map cons module)))
  161. init
  162. vlist-null
  163. modules)))
  164. (define (fold-module-public-variables proc init modules)
  165. "Call (PROC OBJECT RESULT) for each variable exported by one of MODULES,
  166. using INIT as the initial value of RESULT. It is guaranteed to never traverse
  167. the same object twice."
  168. ;; Note: here SEEN is populated by objects, not by variables.
  169. (identity ; discard second return value
  170. (fold2 (lambda (module result seen)
  171. (fold2 (lambda (var result seen)
  172. (if (not (vhash-assq var seen))
  173. (values (proc var result)
  174. (vhash-consq var #t seen))
  175. (values result seen)))
  176. result
  177. seen
  178. (module-map (lambda (sym var)
  179. (false-if-exception (variable-ref var)))
  180. module)))
  181. init
  182. vlist-null
  183. modules)))
  184. ;;; discovery.scm ends here