Commit | Line | Data |
---|---|---|
b4f5e0e8 CR |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2014 Cyril Roelandt <tipecaml@gmail.com> | |
86a41263 | 3 | ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org> |
b4f5e0e8 CR |
4 | ;;; |
5 | ;;; This file is part of GNU Guix. | |
6 | ;;; | |
7 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
8 | ;;; under the terms of the GNU General Public License as published by | |
9 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
10 | ;;; your option) any later version. | |
11 | ;;; | |
12 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
13 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;;; GNU General Public License for more details. | |
16 | ;;; | |
17 | ;;; You should have received a copy of the GNU General Public License | |
18 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
19 | ||
20 | (define-module (guix scripts lint) | |
21 | #:use-module (guix base32) | |
22 | #:use-module (guix packages) | |
23 | #:use-module (guix records) | |
24 | #:use-module (guix ui) | |
25 | #:use-module (guix utils) | |
105c260f | 26 | #:use-module (guix gnu-maintenance) |
b4f5e0e8 CR |
27 | #:use-module (gnu packages) |
28 | #:use-module (ice-9 match) | |
574e847b EB |
29 | #:use-module (ice-9 regex) |
30 | #:use-module (ice-9 format) | |
b4f5e0e8 CR |
31 | #:use-module (srfi srfi-1) |
32 | #:use-module (srfi srfi-9) | |
33 | #:use-module (srfi srfi-11) | |
34 | #:use-module (srfi srfi-37) | |
35 | #:export (guix-lint | |
8202a513 | 36 | check-description-style |
b4f5e0e8 CR |
37 | check-inputs-should-be-native |
38 | check-patches | |
39 | check-synopsis-style)) | |
40 | ||
41 | \f | |
b4f5e0e8 CR |
42 | ;;; |
43 | ;;; Helpers | |
44 | ;;; | |
45 | (define* (emit-warning package message #:optional field) | |
46 | ;; Emit a warning about PACKAGE, printing the location of FIELD if it is | |
47 | ;; given, the location of PACKAGE otherwise, the full name of PACKAGE and the | |
48 | ;; provided MESSAGE. | |
49 | (let ((loc (or (package-field-location package field) | |
50 | (package-location package)))) | |
b002e9d0 LC |
51 | (format (guix-warning-port) (_ "~a: ~a: ~a~%") |
52 | (location->string loc) | |
53 | (package-full-name package) | |
54 | message))) | |
b4f5e0e8 CR |
55 | |
56 | \f | |
57 | ;;; | |
58 | ;;; Checkers | |
59 | ;;; | |
60 | (define-record-type* <lint-checker> | |
61 | lint-checker make-lint-checker | |
62 | lint-checker? | |
63 | ;; TODO: add a 'certainty' field that shows how confident we are in the | |
64 | ;; checker. Then allow users to only run checkers that have a certain | |
65 | ;; 'certainty' level. | |
66 | (name lint-checker-name) | |
67 | (description lint-checker-description) | |
68 | (check lint-checker-check)) | |
69 | ||
70 | (define (list-checkers-and-exit) | |
71 | ;; Print information about all available checkers and exit. | |
72 | (format #t (_ "Available checkers:~%")) | |
73 | (for-each (lambda (checker) | |
74 | (format #t "- ~a: ~a~%" | |
75 | (lint-checker-name checker) | |
76 | (lint-checker-description checker))) | |
77 | %checkers) | |
78 | (exit 0)) | |
79 | ||
903581f9 | 80 | (define (properly-starts-sentence? s) |
431e5f5a | 81 | (string-match "^[(\"'[:upper:][:digit:]]" s)) |
8202a513 CR |
82 | |
83 | (define (check-description-style package) | |
84 | ;; Emit a warning if stylistic issues are found in the description of PACKAGE. | |
334c43e3 EB |
85 | (define (check-not-empty description) |
86 | (when (string-null? description) | |
87 | (emit-warning package | |
88 | "description should not be empty" | |
89 | 'description))) | |
90 | ||
903581f9 | 91 | (define (check-proper-start description) |
3c42965b EB |
92 | (unless (or (properly-starts-sentence? description) |
93 | (string-prefix-ci? (package-name package) description)) | |
574e847b | 94 | (emit-warning package |
903581f9 | 95 | "description should start with an upper-case letter or digit" |
574e847b EB |
96 | 'description))) |
97 | ||
98 | (define (check-end-of-sentence-space description) | |
99 | "Check that an end-of-sentence period is followed by two spaces." | |
100 | (let ((infractions | |
101 | (reverse (fold-matches | |
102 | "\\. [A-Z]" description '() | |
103 | (lambda (m r) | |
104 | ;; Filter out matches of common abbreviations. | |
105 | (if (find (lambda (s) | |
106 | (string-suffix-ci? s (match:prefix m))) | |
107 | '("i.e" "e.g" "a.k.a" "resp")) | |
108 | r (cons (match:start m) r))))))) | |
109 | (unless (null? infractions) | |
110 | (emit-warning package | |
111 | (format #f "sentences in description should be followed ~ | |
112 | by two spaces; possible infraction~p at ~{~a~^, ~}" | |
113 | (length infractions) | |
114 | infractions) | |
115 | 'description)))) | |
116 | ||
117 | (let ((description (package-description package))) | |
118 | (when (string? description) | |
903581f9 EB |
119 | (check-not-empty description) |
120 | (check-proper-start description) | |
121 | (check-end-of-sentence-space description)))) | |
8202a513 | 122 | |
b4f5e0e8 CR |
123 | (define (check-inputs-should-be-native package) |
124 | ;; Emit a warning if some inputs of PACKAGE are likely to belong to its | |
125 | ;; native inputs. | |
126 | (let ((inputs (package-inputs package))) | |
127 | (match inputs | |
128 | (((labels packages . _) ...) | |
129 | (when (member "pkg-config" | |
130 | (map package-name (filter package? packages))) | |
131 | (emit-warning package | |
132 | "pkg-config should probably be a native input" | |
133 | 'inputs)))))) | |
134 | ||
17854ef9 LC |
135 | (define (package-name-regexp package) |
136 | "Return a regexp that matches PACKAGE's name as a word at the beginning of a | |
137 | line." | |
138 | (make-regexp (string-append "^" (regexp-quote (package-name package)) | |
139 | "\\>") | |
140 | regexp/icase)) | |
b4f5e0e8 CR |
141 | |
142 | (define (check-synopsis-style package) | |
143 | ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE. | |
334c43e3 EB |
144 | (define (check-not-empty synopsis) |
145 | (when (string-null? synopsis) | |
146 | (emit-warning package | |
147 | "synopsis should not be empty" | |
148 | 'synopsis))) | |
149 | ||
b4f5e0e8 CR |
150 | (define (check-final-period synopsis) |
151 | ;; Synopsis should not end with a period, except for some special cases. | |
c04b82ff EB |
152 | (when (and (string-suffix? "." synopsis) |
153 | (not (string-suffix? "etc." synopsis))) | |
154 | (emit-warning package | |
155 | "no period allowed at the end of the synopsis" | |
156 | 'synopsis))) | |
b4f5e0e8 | 157 | |
105c260f LC |
158 | (define check-start-article |
159 | ;; Skip this check for GNU packages, as suggested by Karl Berry's reply to | |
160 | ;; <http://lists.gnu.org/archive/html/bug-womb/2014-11/msg00000.html>. | |
161 | (if (false-if-exception (gnu-package? package)) | |
162 | (const #t) | |
163 | (lambda (synopsis) | |
164 | (when (or (string-prefix-ci? "A " synopsis) | |
165 | (string-prefix-ci? "An " synopsis)) | |
166 | (emit-warning package | |
167 | "no article allowed at the beginning of the synopsis" | |
168 | 'synopsis))))) | |
b4f5e0e8 | 169 | |
5622953d | 170 | (define (check-synopsis-length synopsis) |
c04b82ff EB |
171 | (when (>= (string-length synopsis) 80) |
172 | (emit-warning package | |
173 | "synopsis should be less than 80 characters long" | |
174 | 'synopsis))) | |
5622953d | 175 | |
903581f9 EB |
176 | (define (check-proper-start synopsis) |
177 | (unless (properly-starts-sentence? synopsis) | |
178 | (emit-warning package | |
179 | "synopsis should start with an upper-case letter or digit" | |
180 | 'synopsis))) | |
8202a513 | 181 | |
3c762a13 | 182 | (define (check-start-with-package-name synopsis) |
17854ef9 | 183 | (when (regexp-exec (package-name-regexp package) synopsis) |
86a41263 EB |
184 | (emit-warning package |
185 | "synopsis should not start with the package name" | |
186 | 'synopsis))) | |
3c762a13 | 187 | |
b4f5e0e8 | 188 | (let ((synopsis (package-synopsis package))) |
c04b82ff | 189 | (when (string? synopsis) |
903581f9 EB |
190 | (check-not-empty synopsis) |
191 | (check-proper-start synopsis) | |
192 | (check-final-period synopsis) | |
193 | (check-start-article synopsis) | |
194 | (check-start-with-package-name synopsis) | |
195 | (check-synopsis-length synopsis)))) | |
b4f5e0e8 CR |
196 | |
197 | (define (check-patches package) | |
198 | ;; Emit a warning if the patches requires by PACKAGE are badly named. | |
199 | (let ((patches (and=> (package-source package) origin-patches)) | |
200 | (name (package-name package)) | |
201 | (full-name (package-full-name package))) | |
c04b82ff EB |
202 | (when (and patches |
203 | (any (match-lambda | |
204 | ((? string? patch) | |
205 | (let ((filename (basename patch))) | |
206 | (not (or (eq? (string-contains filename name) 0) | |
207 | (eq? (string-contains filename full-name) | |
208 | 0))))) | |
209 | (_ | |
210 | ;; This must be an <origin> or something like that. | |
211 | #f)) | |
212 | patches)) | |
213 | (emit-warning package | |
214 | "file names of patches should start with the package name" | |
215 | 'patches)))) | |
b4f5e0e8 CR |
216 | |
217 | (define %checkers | |
218 | (list | |
8202a513 CR |
219 | (lint-checker |
220 | (name "description") | |
221 | (description "Validate package descriptions") | |
222 | (check check-description-style)) | |
b4f5e0e8 CR |
223 | (lint-checker |
224 | (name "inputs-should-be-native") | |
225 | (description "Identify inputs that should be native inputs") | |
226 | (check check-inputs-should-be-native)) | |
227 | (lint-checker | |
228 | (name "patch-filenames") | |
229 | (description "Validate filenames of patches") | |
230 | (check check-patches)) | |
231 | (lint-checker | |
232 | (name "synopsis") | |
233 | (description "Validate package synopsis") | |
234 | (check check-synopsis-style)))) | |
235 | ||
dd7c013d CR |
236 | (define (run-checkers package checkers) |
237 | ;; Run the given CHECKERS on PACKAGE. | |
b4f5e0e8 CR |
238 | (for-each (lambda (checker) |
239 | ((lint-checker-check checker) package)) | |
dd7c013d CR |
240 | checkers)) |
241 | ||
242 | \f | |
243 | ;;; | |
244 | ;;; Command-line options. | |
245 | ;;; | |
246 | ||
247 | (define %default-options | |
248 | ;; Alist of default option values. | |
249 | '()) | |
250 | ||
251 | (define (show-help) | |
252 | (display (_ "Usage: guix lint [OPTION]... [PACKAGE]... | |
253 | Run a set of checkers on the specified package; if none is specified, run the checkers on all packages.\n")) | |
254 | (display (_ " | |
255 | -c, --checkers=CHECKER1,CHECKER2... | |
256 | only run the specificed checkers")) | |
257 | (display (_ " | |
258 | -h, --help display this help and exit")) | |
259 | (display (_ " | |
260 | -l, --list-checkers display the list of available lint checkers")) | |
261 | (display (_ " | |
262 | -V, --version display version information and exit")) | |
263 | (newline) | |
264 | (show-bug-report-information)) | |
265 | ||
266 | ||
267 | (define %options | |
268 | ;; Specification of the command-line options. | |
269 | ;; TODO: add some options: | |
270 | ;; * --certainty=[low,medium,high]: only run checkers that have at least this | |
271 | ;; 'certainty'. | |
272 | (list (option '(#\c "checkers") #t #f | |
273 | (lambda (opt name arg result arg-handler) | |
274 | (let ((names (string-split arg #\,))) | |
275 | (for-each (lambda (c) | |
276 | (when (not (member c (map lint-checker-name | |
277 | %checkers))) | |
278 | (leave (_ "~a: invalid checker") c))) | |
279 | names) | |
280 | (values (alist-cons 'checkers | |
281 | (filter (lambda (checker) | |
282 | (member (lint-checker-name checker) | |
283 | names)) | |
284 | %checkers) | |
285 | result) | |
286 | #f)))) | |
287 | (option '(#\h "help") #f #f | |
288 | (lambda args | |
289 | (show-help) | |
290 | (exit 0))) | |
291 | (option '(#\l "list-checkers") #f #f | |
292 | (lambda args | |
293 | (list-checkers-and-exit))) | |
294 | (option '(#\V "version") #f #f | |
295 | (lambda args | |
296 | (show-version-and-exit "guix lint"))))) | |
b4f5e0e8 CR |
297 | |
298 | \f | |
299 | ;;; | |
300 | ;;; Entry Point | |
301 | ;;; | |
302 | ||
303 | (define (guix-lint . args) | |
304 | (define (parse-options) | |
305 | ;; Return the alist of option values. | |
306 | (args-fold* args %options | |
dd7c013d | 307 | (lambda (opt name arg result arg-handler) |
b4f5e0e8 | 308 | (leave (_ "~A: unrecognized option~%") name)) |
dd7c013d | 309 | (lambda (arg result arg-handler) |
b4f5e0e8 | 310 | (alist-cons 'argument arg result)) |
dd7c013d | 311 | %default-options #f)) |
b4f5e0e8 CR |
312 | |
313 | (let* ((opts (parse-options)) | |
314 | (args (filter-map (match-lambda | |
315 | (('argument . value) | |
316 | value) | |
317 | (_ #f)) | |
dd7c013d CR |
318 | (reverse opts))) |
319 | (checkers (or (assoc-ref opts 'checkers) %checkers))) | |
320 | (if (null? args) | |
321 | (fold-packages (lambda (p r) (run-checkers p checkers)) '()) | |
322 | (for-each (lambda (spec) | |
323 | (run-checkers (specification->package spec) checkers)) | |
324 | args)))) |