discovery.scm 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. ;; Hide Guile warnings such as "source file [...] newer than compiled" when
  103. ;; loading user code, unless we're hacking on Guix proper. See
  104. ;; <https://issues.guix.gnu.org/43747>.
  105. (parameterize ((current-warning-port (if (getenv "GUIX_UNINSTALLED")
  106. (current-warning-port)
  107. (%make-void-port "w"))))
  108. (filter-map (lambda (file)
  109. (let* ((relative (string-drop file prefix-len))
  110. (module (file-name->module-name relative)))
  111. (catch #t
  112. (lambda ()
  113. (resolve-interface module))
  114. (lambda args
  115. ;; Report the error, but keep going.
  116. (warn file module args)
  117. #f))))
  118. (scheme-files (if sub-directory
  119. (string-append directory "/" sub-directory)
  120. directory)))))
  121. (define* (scheme-modules* directory #:optional sub-directory)
  122. "Return the list of module names found under SUB-DIRECTORY in DIRECTORY.
  123. This is a source-only variant that does not try to load files."
  124. (let ((prefix (string-length directory)))
  125. (map (lambda (file)
  126. (file-name->module-name (string-drop file prefix)))
  127. (scheme-files (if sub-directory
  128. (string-append directory "/" sub-directory)
  129. 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))
  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. Modules are listed in the order they appear on the path."
  148. (reverse (fold-modules cons '() path #:warn warn)))
  149. (define (fold-module-public-variables* proc init modules)
  150. "Call (PROC MODULE SYMBOL VARIABLE RESULT) for each variable exported by one
  151. of MODULES, using INIT as the initial value of RESULT. It is guaranteed to
  152. never traverse the same object twice."
  153. ;; Here SEEN is populated by variables; if two different variables refer to
  154. ;; the same object, we still let them through.
  155. (identity ;discard second return value
  156. (fold2 (lambda (module result seen)
  157. (fold2 (lambda (sym+var result seen)
  158. (match sym+var
  159. ((sym . var)
  160. (if (not (vhash-assq var seen))
  161. (values (proc module sym var result)
  162. (vhash-consq var #t seen))
  163. (values result seen)))))
  164. result
  165. seen
  166. (module-map cons module)))
  167. init
  168. vlist-null
  169. modules)))
  170. (define (fold-module-public-variables proc init modules)
  171. "Call (PROC OBJECT RESULT) for each variable exported by one of MODULES,
  172. using INIT as the initial value of RESULT. It is guaranteed to never traverse
  173. the same object twice."
  174. ;; Note: here SEEN is populated by objects, not by variables.
  175. (identity ; discard second return value
  176. (fold2 (lambda (module result seen)
  177. (fold2 (lambda (var result seen)
  178. (if (not (vhash-assq var seen))
  179. (values (proc var result)
  180. (vhash-consq var #t seen))
  181. (values result seen)))
  182. result
  183. seen
  184. (module-map (lambda (sym var)
  185. (false-if-exception (variable-ref var)))
  186. module)))
  187. init
  188. vlist-null
  189. modules)))
  190. ;;; discovery.scm ends here