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>
14 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
44 (define (emit-warnings warnings)
45 ;; Emit a warning about PACKAGE, printing the location of FIELD if it is
46 ;; given, the location of PACKAGE otherwise, the full name of PACKAGE and the
49 (lambda (lint-warning)
50 (let ((package (lint-warning-package lint-warning))
51 (loc (lint-warning-location lint-warning)))
52 (info loc (G_ "~a@~a: ~a~%")
53 (package-name package) (package-version package)
54 (lint-warning-message lint-warning))))
57 (define* (run-checkers package checkers #:key store)
58 "Run the given CHECKERS on PACKAGE."
59 (let ((tty? (isatty? (current-error-port))))
60 (for-each (lambda (checker)
62 (format (current-error-port) "checking ~a@~a [~a]...\x1b[K\r"
63 (package-name package) (package-version package)
64 (lint-checker-name checker))
65 (force-output (current-error-port)))
67 (if (lint-checker-requires-store? checker)
68 ((lint-checker-check checker) package #:store store)
69 ((lint-checker-check checker) package))))
72 (format (current-error-port) "\x1b[K")
73 (force-output (current-error-port)))))
75 (define (list-checkers-and-exit checkers)
76 ;; Print information about all available checkers and exit.
77 (format #t (G_ "Available checkers:~%"))
78 (for-each (lambda (checker)
79 (format #t "- ~a: ~a~%"
80 (lint-checker-name checker)
81 (G_ (lint-checker-description checker))))
87 ;;; Command-line options.
90 (define %default-options
91 ;; Alist of default option values.
95 (display (G_ "Usage: guix lint [OPTION]... [PACKAGE]...
96 Run a set of checkers on the specified package; if none is specified,
97 run the checkers on all packages.\n"))
99 -c, --checkers=CHECKER1,CHECKER2...
100 only run the specified checkers"))
102 -L, --load-path=DIR prepend DIR to the package module search path"))
105 -h, --help display this help and exit"))
107 -l, --list-checkers display the list of available lint checkers"))
109 -V, --version display version information and exit"))
111 (show-bug-report-information))
115 ;; Specification of the command-line options.
116 ;; TODO: add some options:
117 ;; * --certainty=[low,medium,high]: only run checkers that have at least this
119 (list (option '(#\c "checkers") #t #f
120 (lambda (opt name arg result)
121 (let ((names (map string->symbol (string-split arg #\,)))
122 (checker-names (map lint-checker-name %all-checkers)))
123 (for-each (lambda (c)
124 (unless (memq c checker-names)
125 (leave (G_ "~a: invalid checker~%") c)))
127 (alist-cons 'checkers
128 (filter (lambda (checker)
129 (member (lint-checker-name checker)
133 (option '(#\n "no-network") #f #f
134 (lambda (opt name arg result)
135 (alist-cons 'checkers
137 (alist-delete 'checkers
139 (find (lambda (option)
140 (member "load-path" (option-names option)))
141 %standard-build-options)
142 (option '(#\h "help") #f #f
146 (option '(#\l "list-checkers") #f #f
147 (lambda (opt name arg result)
151 (option '(#\V "version") #f #f
153 (show-version-and-exit "guix lint")))))
160 (define-command (guix-lint . args)
162 (synopsis "validate package definitions")
164 (define (parse-options)
165 ;; Return the alist of option values.
166 (parse-command-line args %options (list %default-options)
167 #:build-options? #f))
169 (let* ((opts (parse-options))
170 (args (filter-map (match-lambda
175 (checkers (or (assoc-ref opts 'checkers) %all-checkers)))
177 (when (assoc-ref opts 'list?)
178 (list-checkers-and-exit checkers))
181 (let ((any-lint-checker-requires-store?
182 (any lint-checker-requires-store? checkers)))
184 (define (call-maybe-with-store proc)
185 (if any-lint-checker-requires-store?
190 (call-maybe-with-store
194 (fold-packages (lambda (p r) (run-checkers p checkers
195 #:store store)) '()))
197 (for-each (lambda (spec)
198 (run-checkers (specification->package spec) checkers