lint.scm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 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 scripts)
  33. #:use-module (guix scripts build)
  34. #:use-module (gnu packages)
  35. #:use-module (ice-9 match)
  36. #:use-module (ice-9 format)
  37. #:use-module (srfi srfi-1)
  38. #:use-module (srfi srfi-37)
  39. #:export (guix-lint
  40. run-checkers))
  41. (define (emit-warnings warnings)
  42. ;; Emit a warning about PACKAGE, printing the location of FIELD if it is
  43. ;; given, the location of PACKAGE otherwise, the full name of PACKAGE and the
  44. ;; provided MESSAGE.
  45. (for-each
  46. (lambda (lint-warning)
  47. (let ((package (lint-warning-package lint-warning))
  48. (loc (lint-warning-location lint-warning)))
  49. (info loc (G_ "~a@~a: ~a~%")
  50. (package-name package) (package-version package)
  51. (lint-warning-message lint-warning))))
  52. warnings))
  53. (define (run-checkers package checkers)
  54. "Run the given CHECKERS on PACKAGE."
  55. (let ((tty? (isatty? (current-error-port))))
  56. (for-each (lambda (checker)
  57. (when tty?
  58. (format (current-error-port) "checking ~a@~a [~a]...\x1b[K\r"
  59. (package-name package) (package-version package)
  60. (lint-checker-name checker))
  61. (force-output (current-error-port)))
  62. (emit-warnings
  63. ((lint-checker-check checker) package)))
  64. checkers)
  65. (when tty?
  66. (format (current-error-port) "\x1b[K")
  67. (force-output (current-error-port)))))
  68. (define (list-checkers-and-exit checkers)
  69. ;; Print information about all available checkers and exit.
  70. (format #t (G_ "Available checkers:~%"))
  71. (for-each (lambda (checker)
  72. (format #t "- ~a: ~a~%"
  73. (lint-checker-name checker)
  74. (G_ (lint-checker-description checker))))
  75. checkers)
  76. (exit 0))
  77. ;;;
  78. ;;; Command-line options.
  79. ;;;
  80. (define %default-options
  81. ;; Alist of default option values.
  82. '())
  83. (define (show-help)
  84. (display (G_ "Usage: guix lint [OPTION]... [PACKAGE]...
  85. Run a set of checkers on the specified package; if none is specified,
  86. run the checkers on all packages.\n"))
  87. (display (G_ "
  88. -c, --checkers=CHECKER1,CHECKER2...
  89. only run the specified checkers"))
  90. (display (G_ "
  91. -L, --load-path=DIR prepend DIR to the package module search path"))
  92. (newline)
  93. (display (G_ "
  94. -h, --help display this help and exit"))
  95. (display (G_ "
  96. -l, --list-checkers display the list of available lint checkers"))
  97. (display (G_ "
  98. -V, --version display version information and exit"))
  99. (newline)
  100. (show-bug-report-information))
  101. (define %options
  102. ;; Specification of the command-line options.
  103. ;; TODO: add some options:
  104. ;; * --certainty=[low,medium,high]: only run checkers that have at least this
  105. ;; 'certainty'.
  106. (list (option '(#\c "checkers") #t #f
  107. (lambda (opt name arg result)
  108. (let ((names (map string->symbol (string-split arg #\,)))
  109. (checker-names (map lint-checker-name %all-checkers)))
  110. (for-each (lambda (c)
  111. (unless (memq c checker-names)
  112. (leave (G_ "~a: invalid checker~%") c)))
  113. names)
  114. (alist-cons 'checkers
  115. (filter (lambda (checker)
  116. (member (lint-checker-name checker)
  117. names))
  118. %all-checkers)
  119. result))))
  120. (option '(#\n "no-network") #f #f
  121. (lambda (opt name arg result)
  122. (alist-cons 'checkers
  123. %local-checkers
  124. (alist-delete 'checkers
  125. result))))
  126. (find (lambda (option)
  127. (member "load-path" (option-names option)))
  128. %standard-build-options)
  129. (option '(#\h "help") #f #f
  130. (lambda args
  131. (show-help)
  132. (exit 0)))
  133. (option '(#\l "list-checkers") #f #f
  134. (lambda (opt name arg result)
  135. (alist-cons 'list?
  136. #t
  137. result)))
  138. (option '(#\V "version") #f #f
  139. (lambda args
  140. (show-version-and-exit "guix lint")))))
  141. ;;;
  142. ;;; Entry Point
  143. ;;;
  144. (define (guix-lint . args)
  145. (define (parse-options)
  146. ;; Return the alist of option values.
  147. (parse-command-line args %options (list %default-options)
  148. #:build-options? #f))
  149. (let* ((opts (parse-options))
  150. (args (filter-map (match-lambda
  151. (('argument . value)
  152. value)
  153. (_ #f))
  154. (reverse opts)))
  155. (checkers (or (assoc-ref opts 'checkers) %all-checkers)))
  156. (cond
  157. ((assoc-ref opts 'list?)
  158. (list-checkers-and-exit checkers))
  159. ((null? args)
  160. (fold-packages (lambda (p r) (run-checkers p checkers)) '()))
  161. (else
  162. (for-each (lambda (spec)
  163. (run-checkers (specification->package spec) checkers))
  164. args)))))