lint.scm 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 Cyril Roelandt <tipecaml@gmail.com>
  3. ;;; Copyright © 2014, 2015 Eric Bavier <bavier@member.fsf.org>
  4. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  5. ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
  6. ;;; Copyright © 2016 Danny Milosavljevic <dannym+a@scratchpost.org>
  7. ;;; Copyright © 2016 Hartmut Goebel <h.goebel@crazy-compilers.com>
  8. ;;; Copyright © 2017 Alex Kost <alezost@gmail.com>
  9. ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
  10. ;;; Copyright © 2017, 2018 Efraim Flashner <efraim@flashner.co.il>
  11. ;;; Copyright © 2018, 2019 Arun Isaac <arunisaac@systemreboot.net>
  12. ;;; Copyright © 2019, 2020 Simon Tournier <zimon.toutoune@gmail.com>
  13. ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
  14. ;;;
  15. ;;; This file is part of GNU Guix.
  16. ;;;
  17. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  18. ;;; under the terms of the GNU General Public License as published by
  19. ;;; the Free Software Foundation; either version 3 of the License, or (at
  20. ;;; your option) any later version.
  21. ;;;
  22. ;;; GNU Guix is distributed in the hope that it will be useful, but
  23. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  24. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. ;;; GNU General Public License for more details.
  26. ;;;
  27. ;;; You should have received a copy of the GNU General Public License
  28. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  29. (define-module (guix scripts lint)
  30. #:use-module (guix packages)
  31. #:use-module (guix lint)
  32. #:use-module (guix ui)
  33. #:use-module (guix store)
  34. #:use-module (guix scripts)
  35. #:use-module (guix scripts build)
  36. #:use-module (gnu packages)
  37. #:use-module (ice-9 match)
  38. #:use-module (ice-9 format)
  39. #:use-module (srfi srfi-1)
  40. #:use-module (srfi srfi-37)
  41. #:export (guix-lint
  42. run-checkers))
  43. (define (emit-warnings warnings)
  44. ;; Emit a warning about PACKAGE, printing the location of FIELD if it is
  45. ;; given, the location of PACKAGE otherwise, the full name of PACKAGE and the
  46. ;; provided MESSAGE.
  47. (for-each
  48. (lambda (lint-warning)
  49. (let* ((package (lint-warning-package lint-warning))
  50. (name (package-name package))
  51. (version (package-version package))
  52. (loc (lint-warning-location lint-warning))
  53. (message (lint-warning-message lint-warning)))
  54. (parameterize
  55. ((guix-warning-port (current-output-port)))
  56. (info loc (G_ "~a@~a: ~a~%")
  57. name version message))))
  58. warnings))
  59. (define* (run-checkers package checkers #:key store)
  60. "Run the given CHECKERS on PACKAGE."
  61. (let ((tty? (isatty? (current-error-port))))
  62. (for-each (lambda (checker)
  63. (when tty?
  64. (format (current-error-port) "checking ~a@~a [~a]...\x1b[K\r"
  65. (package-name package) (package-version package)
  66. (lint-checker-name checker))
  67. (force-output (current-error-port)))
  68. (emit-warnings
  69. (if (lint-checker-requires-store? checker)
  70. ((lint-checker-check checker) package #:store store)
  71. ((lint-checker-check checker) package))))
  72. checkers)
  73. (when tty?
  74. (format (current-error-port) "\x1b[K")
  75. (force-output (current-error-port)))))
  76. (define (list-checkers-and-exit checkers)
  77. ;; Print information about all available checkers and exit.
  78. (format #t (G_ "Available checkers:~%"))
  79. (for-each (lambda (checker)
  80. (format #t "- ~a: ~a~%"
  81. (lint-checker-name checker)
  82. (G_ (lint-checker-description checker))))
  83. checkers)
  84. (exit 0))
  85. ;;;
  86. ;;; Command-line options.
  87. ;;;
  88. (define %default-options
  89. ;; Alist of default option values.
  90. '())
  91. (define (show-help)
  92. (display (G_ "Usage: guix lint [OPTION]... [PACKAGE]...
  93. Run a set of checkers on the specified package; if none is specified,
  94. run the checkers on all packages.\n"))
  95. (display (G_ "
  96. -c, --checkers=CHECKER1,CHECKER2...
  97. only run the specified checkers"))
  98. (display (G_ "
  99. -x, --exclude=CHECKER1,CHECKER2...
  100. exclude the specified checkers"))
  101. (display (G_ "
  102. -n, --no-network only run checkers that do not access the network"))
  103. (display (G_ "
  104. -L, --load-path=DIR prepend DIR to the package module search path"))
  105. (newline)
  106. (display (G_ "
  107. -h, --help display this help and exit"))
  108. (display (G_ "
  109. -l, --list-checkers display the list of available lint checkers"))
  110. (display (G_ "
  111. -V, --version display version information and exit"))
  112. (newline)
  113. (show-bug-report-information))
  114. (define (option-checker short-long)
  115. ;; Factorize the creation of the two options -c/--checkers and -x/--exclude,
  116. ;; see %options. The parameter SHORT-LONG is the list containing the short
  117. ;; and long name. The alist uses the long name as symbol.
  118. (option short-long #t #f
  119. (lambda (opt name arg result)
  120. (let ((names (map string->symbol (string-split arg #\,)))
  121. (checker-names (map lint-checker-name %all-checkers))
  122. (option-name (string->symbol (match short-long
  123. ((short long) long)))))
  124. (for-each (lambda (c)
  125. (unless (memq c checker-names)
  126. (leave (G_ "~a: invalid checker~%") c)))
  127. names)
  128. (alist-cons option-name
  129. (filter (lambda (checker)
  130. (member (lint-checker-name checker)
  131. names))
  132. %all-checkers)
  133. result)))))
  134. (define %options
  135. ;; Specification of the command-line options.
  136. ;; TODO: add some options:
  137. ;; * --certainty=[low,medium,high]: only run checkers that have at least this
  138. ;; 'certainty'.
  139. (list (option-checker '(#\c "checkers"))
  140. (option-checker '(#\x "exclude"))
  141. (option '(#\n "no-network") #f #f
  142. (lambda (opt name arg result)
  143. (alist-cons 'no-network? #t result)))
  144. (find (lambda (option)
  145. (member "load-path" (option-names option)))
  146. %standard-build-options)
  147. (option '(#\h "help") #f #f
  148. (lambda args
  149. (show-help)
  150. (exit 0)))
  151. (option '(#\l "list-checkers") #f #f
  152. (lambda (opt name arg result)
  153. (alist-cons 'list?
  154. #t
  155. result)))
  156. (option '(#\V "version") #f #f
  157. (lambda args
  158. (show-version-and-exit "guix lint")))))
  159. ;;;
  160. ;;; Entry Point
  161. ;;;
  162. (define-command (guix-lint . args)
  163. (category packaging)
  164. (synopsis "validate package definitions")
  165. (define (parse-options)
  166. ;; Return the alist of option values.
  167. (parse-command-line args %options (list %default-options)
  168. #:build-options? #f))
  169. (let* ((opts (parse-options))
  170. (args (filter-map (match-lambda
  171. (('argument . value)
  172. value)
  173. (_ #f))
  174. (reverse opts)))
  175. (no-checkers (or (assoc-ref opts 'exclude) '()))
  176. (the-checkers (filter (lambda (checker)
  177. (not (member checker no-checkers)))
  178. (or (assoc-ref opts 'checkers) %all-checkers)))
  179. (checkers
  180. (if (assoc-ref opts 'no-network?)
  181. (filter (lambda (checker)
  182. (member checker %local-checkers))
  183. the-checkers)
  184. the-checkers)))
  185. (when (assoc-ref opts 'list?)
  186. (list-checkers-and-exit checkers))
  187. (with-error-handling
  188. (let ((any-lint-checker-requires-store?
  189. (any lint-checker-requires-store? checkers)))
  190. (define (call-maybe-with-store proc)
  191. (if any-lint-checker-requires-store?
  192. (with-store store
  193. (proc store))
  194. (proc #f)))
  195. (call-maybe-with-store
  196. (lambda (store)
  197. (cond
  198. ((null? args)
  199. (fold-packages (lambda (p r) (run-checkers p checkers
  200. #:store store)) '()))
  201. (else
  202. (for-each (lambda (spec)
  203. (run-checkers (specification->package spec) checkers
  204. #:store store))
  205. args)))))))))