lint.scm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 Simon Tournier <zimon.toutoune@gmail.com>
  13. ;;;
  14. ;;; This file is part of GNU Guix.
  15. ;;;
  16. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  17. ;;; under the terms of the GNU General Public License as published by
  18. ;;; the Free Software Foundation; either version 3 of the License, or (at
  19. ;;; your option) any later version.
  20. ;;;
  21. ;;; GNU Guix is distributed in the hope that it will be useful, but
  22. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  23. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. ;;; GNU General Public License for more details.
  25. ;;;
  26. ;;; You should have received a copy of the GNU General Public License
  27. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  28. (define-module (guix scripts lint)
  29. #:use-module (guix packages)
  30. #:use-module (guix lint)
  31. #:use-module (guix ui)
  32. #:use-module (guix store)
  33. #:use-module (guix scripts)
  34. #:use-module (guix scripts build)
  35. #:use-module (gnu packages)
  36. #:use-module (ice-9 match)
  37. #:use-module (ice-9 format)
  38. #:use-module (srfi srfi-1)
  39. #:use-module (srfi srfi-37)
  40. #:export (guix-lint
  41. run-checkers))
  42. (define (emit-warnings warnings)
  43. ;; Emit a warning about PACKAGE, printing the location of FIELD if it is
  44. ;; given, the location of PACKAGE otherwise, the full name of PACKAGE and the
  45. ;; provided MESSAGE.
  46. (for-each
  47. (lambda (lint-warning)
  48. (let ((package (lint-warning-package lint-warning))
  49. (loc (lint-warning-location lint-warning)))
  50. (info loc (G_ "~a@~a: ~a~%")
  51. (package-name package) (package-version package)
  52. (lint-warning-message lint-warning))))
  53. warnings))
  54. (define* (run-checkers package checkers #:key store)
  55. "Run the given CHECKERS on PACKAGE."
  56. (let ((tty? (isatty? (current-error-port))))
  57. (for-each (lambda (checker)
  58. (when tty?
  59. (format (current-error-port) "checking ~a@~a [~a]...\x1b[K\r"
  60. (package-name package) (package-version package)
  61. (lint-checker-name checker))
  62. (force-output (current-error-port)))
  63. (emit-warnings
  64. (if (lint-checker-requires-store? checker)
  65. ((lint-checker-check checker) package #:store store)
  66. ((lint-checker-check checker) package))))
  67. checkers)
  68. (when tty?
  69. (format (current-error-port) "\x1b[K")
  70. (force-output (current-error-port)))))
  71. (define (list-checkers-and-exit checkers)
  72. ;; Print information about all available checkers and exit.
  73. (format #t (G_ "Available checkers:~%"))
  74. (for-each (lambda (checker)
  75. (format #t "- ~a: ~a~%"
  76. (lint-checker-name checker)
  77. (G_ (lint-checker-description checker))))
  78. checkers)
  79. (exit 0))
  80. ;;;
  81. ;;; Command-line options.
  82. ;;;
  83. (define %default-options
  84. ;; Alist of default option values.
  85. '())
  86. (define (show-help)
  87. (display (G_ "Usage: guix lint [OPTION]... [PACKAGE]...
  88. Run a set of checkers on the specified package; if none is specified,
  89. run the checkers on all packages.\n"))
  90. (display (G_ "
  91. -c, --checkers=CHECKER1,CHECKER2...
  92. only run the specified checkers"))
  93. (display (G_ "
  94. -L, --load-path=DIR prepend DIR to the package module search path"))
  95. (newline)
  96. (display (G_ "
  97. -h, --help display this help and exit"))
  98. (display (G_ "
  99. -l, --list-checkers display the list of available lint checkers"))
  100. (display (G_ "
  101. -V, --version display version information and exit"))
  102. (newline)
  103. (show-bug-report-information))
  104. (define %options
  105. ;; Specification of the command-line options.
  106. ;; TODO: add some options:
  107. ;; * --certainty=[low,medium,high]: only run checkers that have at least this
  108. ;; 'certainty'.
  109. (list (option '(#\c "checkers") #t #f
  110. (lambda (opt name arg result)
  111. (let ((names (map string->symbol (string-split arg #\,)))
  112. (checker-names (map lint-checker-name %all-checkers)))
  113. (for-each (lambda (c)
  114. (unless (memq c checker-names)
  115. (leave (G_ "~a: invalid checker~%") c)))
  116. names)
  117. (alist-cons 'checkers
  118. (filter (lambda (checker)
  119. (member (lint-checker-name checker)
  120. names))
  121. %all-checkers)
  122. result))))
  123. (option '(#\n "no-network") #f #f
  124. (lambda (opt name arg result)
  125. (alist-cons 'checkers
  126. %local-checkers
  127. (alist-delete 'checkers
  128. result))))
  129. (find (lambda (option)
  130. (member "load-path" (option-names option)))
  131. %standard-build-options)
  132. (option '(#\h "help") #f #f
  133. (lambda args
  134. (show-help)
  135. (exit 0)))
  136. (option '(#\l "list-checkers") #f #f
  137. (lambda (opt name arg result)
  138. (alist-cons 'list?
  139. #t
  140. result)))
  141. (option '(#\V "version") #f #f
  142. (lambda args
  143. (show-version-and-exit "guix lint")))))
  144. ;;;
  145. ;;; Entry Point
  146. ;;;
  147. (define-command (guix-lint . args)
  148. (category packaging)
  149. (synopsis "validate package definitions")
  150. (define (parse-options)
  151. ;; Return the alist of option values.
  152. (parse-command-line args %options (list %default-options)
  153. #:build-options? #f))
  154. (let* ((opts (parse-options))
  155. (args (filter-map (match-lambda
  156. (('argument . value)
  157. value)
  158. (_ #f))
  159. (reverse opts)))
  160. (checkers (or (assoc-ref opts 'checkers) %all-checkers)))
  161. (when (assoc-ref opts 'list?)
  162. (list-checkers-and-exit checkers))
  163. (with-error-handling
  164. (let ((any-lint-checker-requires-store?
  165. (any lint-checker-requires-store? checkers)))
  166. (define (call-maybe-with-store proc)
  167. (if any-lint-checker-requires-store?
  168. (with-store store
  169. (proc store))
  170. (proc #f)))
  171. (call-maybe-with-store
  172. (lambda (store)
  173. (cond
  174. ((null? args)
  175. (fold-packages (lambda (p r) (run-checkers p checkers
  176. #:store store)) '()))
  177. (else
  178. (for-each (lambda (spec)
  179. (run-checkers (specification->package spec) checkers
  180. #:store store))
  181. args)))))))))