discovery.scm 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #:export (scheme-files
  27. scheme-modules
  28. scheme-modules*
  29. fold-modules
  30. all-modules
  31. fold-module-public-variables
  32. fold-module-public-variables*))
  33. ;;; Commentary:
  34. ;;;
  35. ;;; This module provides tools to discover Guile modules and the variables
  36. ;;; they export.
  37. ;;;
  38. ;;; Code:
  39. (define* (scheme-files directory)
  40. "Return the list of Scheme files found under DIRECTORY, recursively. The
  41. returned list is sorted in alphabetical order. Return the empty list if
  42. DIRECTORY is not accessible."
  43. (define (entry-type name properties)
  44. (match (assoc-ref properties 'type)
  45. ('unknown
  46. (stat:type (lstat name)))
  47. ((? symbol? type)
  48. type)))
  49. (define (dot-prefixed? file)
  50. (string-prefix? "." file))
  51. ;; Use 'scandir*' so we can avoid an extra 'lstat' for each entry, as
  52. ;; opposed to Guile's 'scandir' or 'file-system-fold'.
  53. (fold-right (lambda (entry result)
  54. (match entry
  55. (((? dot-prefixed?) . _)
  56. ;; Exclude ".", "..", and hidden files such as backups.
  57. result)
  58. ((name . properties)
  59. (let ((absolute (string-append directory "/" name)))
  60. (case (entry-type absolute properties)
  61. ((directory)
  62. (append (scheme-files absolute) result))
  63. ((regular)
  64. (if (string-suffix? ".scm" name)
  65. (cons absolute result)
  66. result))
  67. ((symlink)
  68. (cond ((string-suffix? ".scm" name)
  69. (cons absolute result))
  70. ((stat absolute #f)
  71. =>
  72. (match-lambda
  73. (#f result)
  74. ((= stat:type 'directory)
  75. (append (scheme-files absolute)
  76. result))
  77. (_ result)))
  78. (else
  79. result)))
  80. (else
  81. result))))))
  82. '()
  83. (catch 'system-error
  84. (lambda ()
  85. (scandir* directory))
  86. (lambda args
  87. (let ((errno (system-error-errno args)))
  88. (unless (= errno ENOENT)
  89. (format (current-error-port) ;XXX
  90. (G_ "cannot access `~a': ~a~%")
  91. directory (strerror errno)))
  92. '())))))
  93. (define* (scheme-modules directory #:optional sub-directory
  94. #:key (warn (const #f)))
  95. "Return the list of Scheme modules available under DIRECTORY.
  96. Optionally, narrow the search to SUB-DIRECTORY.
  97. WARN is called when a module could not be loaded. It is passed the module
  98. name and the exception key and arguments."
  99. (define prefix-len
  100. (string-length directory))
  101. ;; Hide Guile warnings such as "source file [...] newer than compiled" when
  102. ;; loading user code, unless we're hacking on Guix proper. See
  103. ;; <https://issues.guix.gnu.org/43747>.
  104. (parameterize ((current-warning-port (if (getenv "GUIX_UNINSTALLED")
  105. (current-warning-port)
  106. (%make-void-port "w"))))
  107. (filter-map (lambda (file)
  108. (let* ((relative (string-drop file prefix-len))
  109. (module (file-name->module-name relative)))
  110. (catch #t
  111. (lambda ()
  112. (resolve-interface module))
  113. (lambda args
  114. ;; Report the error, but keep going.
  115. (warn file module args)
  116. #f))))
  117. (scheme-files (if sub-directory
  118. (string-append directory "/" sub-directory)
  119. 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)))))
  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)
  133. (match spec
  134. ((? string? directory)
  135. (fold proc result (scheme-modules directory #:warn warn)))
  136. ((directory . sub-directory)
  137. (fold proc result
  138. (scheme-modules directory sub-directory
  139. #:warn warn)))))
  140. '()
  141. path))
  142. (define* (all-modules path #:key (warn (const #f)))
  143. "Return the list of package modules found in PATH, a list of directories to
  144. search. Entries in PATH can be directory names (strings) or (DIRECTORY
  145. . SUB-DIRECTORY) pairs, in which case modules are searched for beneath
  146. SUB-DIRECTORY. Modules are listed in the order they appear on the path."
  147. (reverse (fold-modules cons '() path #:warn warn)))
  148. (define (fold-module-public-variables* proc init modules)
  149. "Call (PROC MODULE SYMBOL VARIABLE RESULT) for each variable exported by one
  150. of MODULES, using INIT as the initial value of RESULT. It is guaranteed to
  151. never traverse the same object twice."
  152. ;; Here SEEN is populated by variables; if two different variables refer to
  153. ;; the same object, we still let them through.
  154. (identity ;discard second return value
  155. (fold2 (lambda (module result seen)
  156. (fold2 (lambda (sym+var result seen)
  157. (match sym+var
  158. ((sym . var)
  159. (if (not (vhash-assq var seen))
  160. (values (proc module sym var result)
  161. (vhash-consq var #t seen))
  162. (values result seen)))))
  163. result
  164. seen
  165. (module-map cons module)))
  166. init
  167. vlist-null
  168. modules)))
  169. (define (fold-module-public-variables proc init modules)
  170. "Call (PROC OBJECT RESULT) for each variable exported by one of MODULES,
  171. using INIT as the initial value of RESULT. It is guaranteed to never traverse
  172. the same object twice."
  173. ;; Note: here SEEN is populated by objects, not by variables.
  174. (identity ; discard second return value
  175. (fold2 (lambda (module result seen)
  176. (fold2 (lambda (var result seen)
  177. (if (not (vhash-assq var seen))
  178. (values (proc var result)
  179. (vhash-consq var #t seen))
  180. (values result seen)))
  181. result
  182. seen
  183. (module-map (lambda (sym var)
  184. (false-if-exception (variable-ref var)))
  185. module)))
  186. init
  187. vlist-null
  188. modules)))
  189. ;;; discovery.scm ends here