guix: lint: Use string-suffix? and string-prefix? where appropriate.
[jackhill/guix/guix.git] / guix / scripts / lint.scm
CommitLineData
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)
26 #:use-module (gnu packages)
27 #:use-module (ice-9 match)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-9)
30 #:use-module (srfi srfi-11)
31 #:use-module (srfi srfi-37)
32 #:export (guix-lint
8202a513 33 check-description-style
b4f5e0e8
CR
34 check-inputs-should-be-native
35 check-patches
36 check-synopsis-style))
37
38\f
b4f5e0e8
CR
39;;;
40;;; Helpers
41;;;
42(define* (emit-warning package message #:optional field)
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 (let ((loc (or (package-field-location package field)
47 (package-location package))))
b002e9d0
LC
48 (format (guix-warning-port) (_ "~a: ~a: ~a~%")
49 (location->string loc)
50 (package-full-name package)
51 message)))
b4f5e0e8
CR
52
53\f
54;;;
55;;; Checkers
56;;;
57(define-record-type* <lint-checker>
58 lint-checker make-lint-checker
59 lint-checker?
60 ;; TODO: add a 'certainty' field that shows how confident we are in the
61 ;; checker. Then allow users to only run checkers that have a certain
62 ;; 'certainty' level.
63 (name lint-checker-name)
64 (description lint-checker-description)
65 (check lint-checker-check))
66
67(define (list-checkers-and-exit)
68 ;; Print information about all available checkers and exit.
69 (format #t (_ "Available checkers:~%"))
70 (for-each (lambda (checker)
71 (format #t "- ~a: ~a~%"
72 (lint-checker-name checker)
73 (lint-checker-description checker)))
74 %checkers)
75 (exit 0))
76
8202a513
CR
77(define (start-with-capital-letter? s)
78 (char-set-contains? char-set:upper-case (string-ref s 0)))
79
80(define (check-description-style package)
81 ;; Emit a warning if stylistic issues are found in the description of PACKAGE.
82 (let ((description (package-description package)))
83 (when (and (string? description)
84 (not (string-null? description))
85 (not (start-with-capital-letter? description)))
86 (emit-warning package
87 "description should start with an upper-case letter"
88 'description))))
89
b4f5e0e8
CR
90(define (check-inputs-should-be-native package)
91 ;; Emit a warning if some inputs of PACKAGE are likely to belong to its
92 ;; native inputs.
93 (let ((inputs (package-inputs package)))
94 (match inputs
95 (((labels packages . _) ...)
96 (when (member "pkg-config"
97 (map package-name (filter package? packages)))
98 (emit-warning package
99 "pkg-config should probably be a native input"
100 'inputs))))))
101
102
103(define (check-synopsis-style package)
104 ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE.
105 (define (check-final-period synopsis)
106 ;; Synopsis should not end with a period, except for some special cases.
86a41263
EB
107 (if (and (string-suffix? "." synopsis)
108 (not (string-suffix? "etc." synopsis)))
b4f5e0e8
CR
109 (emit-warning package
110 "no period allowed at the end of the synopsis"
111 'synopsis)))
112
113 (define (check-start-article synopsis)
86a41263
EB
114 (if (or (string-prefix-ci? "A " synopsis)
115 (string-prefix-ci? "An " synopsis))
b4f5e0e8
CR
116 (emit-warning package
117 "no article allowed at the beginning of the synopsis"
118 'synopsis)))
119
5622953d
CR
120 (define (check-synopsis-length synopsis)
121 (if (>= (string-length synopsis) 80)
122 (emit-warning package
123 "synopsis should be less than 80 characters long"
124 'synopsis)))
125
8202a513
CR
126 (define (check-synopsis-start-upper-case synopsis)
127 (when (and (not (string-null? synopsis))
128 (not (start-with-capital-letter? synopsis)))
129 (emit-warning package
130 "synopsis should start with an upper-case letter"
131 'synopsis)))
132
3c762a13 133 (define (check-start-with-package-name synopsis)
86a41263
EB
134 (when (string-prefix-ci? (package-name package) synopsis)
135 (emit-warning package
136 "synopsis should not start with the package name"
137 'synopsis)))
3c762a13 138
b4f5e0e8
CR
139 (let ((synopsis (package-synopsis package)))
140 (if (string? synopsis)
141 (begin
8202a513 142 (check-synopsis-start-upper-case synopsis)
b4f5e0e8 143 (check-final-period synopsis)
5622953d 144 (check-start-article synopsis)
3c762a13 145 (check-start-with-package-name synopsis)
5622953d 146 (check-synopsis-length synopsis)))))
b4f5e0e8
CR
147
148(define (check-patches package)
149 ;; Emit a warning if the patches requires by PACKAGE are badly named.
150 (let ((patches (and=> (package-source package) origin-patches))
151 (name (package-name package))
152 (full-name (package-full-name package)))
153 (if (and patches
90d104ba
CR
154 (any (match-lambda
155 ((? string? patch)
b4f5e0e8
CR
156 (let ((filename (basename patch)))
157 (not (or (eq? (string-contains filename name) 0)
90d104ba
CR
158 (eq? (string-contains filename full-name)
159 0)))))
160 (_
161 ;; This must be an <origin> or something like that.
162 #f))
b4f5e0e8
CR
163 patches))
164 (emit-warning package
165 "file names of patches should start with the package name"
166 'patches))))
167
168(define %checkers
169 (list
8202a513
CR
170 (lint-checker
171 (name "description")
172 (description "Validate package descriptions")
173 (check check-description-style))
b4f5e0e8
CR
174 (lint-checker
175 (name "inputs-should-be-native")
176 (description "Identify inputs that should be native inputs")
177 (check check-inputs-should-be-native))
178 (lint-checker
179 (name "patch-filenames")
180 (description "Validate filenames of patches")
181 (check check-patches))
182 (lint-checker
183 (name "synopsis")
184 (description "Validate package synopsis")
185 (check check-synopsis-style))))
186
dd7c013d
CR
187(define (run-checkers package checkers)
188 ;; Run the given CHECKERS on PACKAGE.
b4f5e0e8
CR
189 (for-each (lambda (checker)
190 ((lint-checker-check checker) package))
dd7c013d
CR
191 checkers))
192
193\f
194;;;
195;;; Command-line options.
196;;;
197
198(define %default-options
199 ;; Alist of default option values.
200 '())
201
202(define (show-help)
203 (display (_ "Usage: guix lint [OPTION]... [PACKAGE]...
204Run a set of checkers on the specified package; if none is specified, run the checkers on all packages.\n"))
205 (display (_ "
206 -c, --checkers=CHECKER1,CHECKER2...
207 only run the specificed checkers"))
208 (display (_ "
209 -h, --help display this help and exit"))
210 (display (_ "
211 -l, --list-checkers display the list of available lint checkers"))
212 (display (_ "
213 -V, --version display version information and exit"))
214 (newline)
215 (show-bug-report-information))
216
217
218(define %options
219 ;; Specification of the command-line options.
220 ;; TODO: add some options:
221 ;; * --certainty=[low,medium,high]: only run checkers that have at least this
222 ;; 'certainty'.
223 (list (option '(#\c "checkers") #t #f
224 (lambda (opt name arg result arg-handler)
225 (let ((names (string-split arg #\,)))
226 (for-each (lambda (c)
227 (when (not (member c (map lint-checker-name
228 %checkers)))
229 (leave (_ "~a: invalid checker") c)))
230 names)
231 (values (alist-cons 'checkers
232 (filter (lambda (checker)
233 (member (lint-checker-name checker)
234 names))
235 %checkers)
236 result)
237 #f))))
238 (option '(#\h "help") #f #f
239 (lambda args
240 (show-help)
241 (exit 0)))
242 (option '(#\l "list-checkers") #f #f
243 (lambda args
244 (list-checkers-and-exit)))
245 (option '(#\V "version") #f #f
246 (lambda args
247 (show-version-and-exit "guix lint")))))
b4f5e0e8
CR
248
249\f
250;;;
251;;; Entry Point
252;;;
253
254(define (guix-lint . args)
255 (define (parse-options)
256 ;; Return the alist of option values.
257 (args-fold* args %options
dd7c013d 258 (lambda (opt name arg result arg-handler)
b4f5e0e8 259 (leave (_ "~A: unrecognized option~%") name))
dd7c013d 260 (lambda (arg result arg-handler)
b4f5e0e8 261 (alist-cons 'argument arg result))
dd7c013d 262 %default-options #f))
b4f5e0e8
CR
263
264 (let* ((opts (parse-options))
265 (args (filter-map (match-lambda
266 (('argument . value)
267 value)
268 (_ #f))
dd7c013d
CR
269 (reverse opts)))
270 (checkers (or (assoc-ref opts 'checkers) %checkers)))
271 (if (null? args)
272 (fold-packages (lambda (p r) (run-checkers p checkers)) '())
273 (for-each (lambda (spec)
274 (run-checkers (specification->package spec) checkers))
275 args))))