unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob e8cc853c584b1d9cf9c1ea0bbaa5f696c61110a3 11338 bytes (raw)
name: guix/scripts/lint.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2014 Cyril Roelandt <tipecaml@gmail.com>
;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix scripts lint)
  #:use-module (guix base32)
  #:use-module (guix packages)
  #:use-module (guix records)
  #:use-module (guix ui)
  #:use-module (guix utils)
  #:use-module (gnu packages)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 format)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-11)
  #:use-module (srfi srfi-37)
  #:export (guix-lint
            check-description-style
            check-inputs-should-be-native
            check-patches
            check-synopsis-style))

\f
;;;
;;; Helpers
;;;
(define* (emit-warning package message #:optional field)
  ;; Emit a warning about PACKAGE, printing the location of FIELD if it is
  ;; given, the location of PACKAGE otherwise, the full name of PACKAGE and the
  ;; provided MESSAGE.
  (let ((loc (or (package-field-location package field)
                 (package-location package))))
    (format (guix-warning-port) (_ "~a: ~a: ~a~%")
            (location->string loc)
            (package-full-name package)
            message)))

\f
;;;
;;; Checkers
;;;
(define-record-type* <lint-checker>
  lint-checker make-lint-checker
  lint-checker?
  ;; TODO: add a 'certainty' field that shows how confident we are in the
  ;; checker. Then allow users to only run checkers that have a certain
  ;; 'certainty' level.
  (name        lint-checker-name)
  (description lint-checker-description)
  (check       lint-checker-check))

(define (list-checkers-and-exit)
  ;; Print information about all available checkers and exit.
  (format #t (_ "Available checkers:~%"))
  (for-each (lambda (checker)
              (format #t "- ~a: ~a~%"
                      (lint-checker-name checker)
                      (lint-checker-description checker)))
            %checkers)
  (exit 0))

(define (start-with-capital-letter? s)
  (and (not (string-null? s))
       (char-set-contains? char-set:upper-case (string-ref s 0))))

(define (check-description-style package)
  ;; Emit a warning if stylistic issues are found in the description of PACKAGE.
  (define (check-starts-with-upper-case description)
    (unless (start-with-capital-letter? description)
      (emit-warning package
                    "description should start with an upper-case letter"
                    'description)))

  (define (check-end-of-sentence-space description)
    "Check that an end-of-sentence period is followed by two spaces."
    (let ((infractions (filter-map
                        (lambda (m)
                          (and (not
                                ;; Filter out matches of common abbrevs.
                                (find (lambda (s)
                                        (string-suffix? s (match:prefix m)))
                                      '("i.e" "e.g" "a.k.a" "resp")))
                               (match:start m)))
                        (list-matches "\\. [A-Z]" description))))
     (when (not (null? infractions))
       (emit-warning package
                     (format #f "sentences in description should be followed ~
by two spaces; possible infraction~p at ~{~a~^, ~}"
                             (length infractions)
                             infractions)
                     'description))))

  (let ((description (package-description package)))
    (when (string? description)
      (begin
        (check-starts-with-upper-case description)
        (check-end-of-sentence-space description)))))

(define (check-inputs-should-be-native package)
  ;; Emit a warning if some inputs of PACKAGE are likely to belong to its
  ;; native inputs.
  (let ((inputs (package-inputs package)))
    (match inputs
      (((labels packages . _) ...)
       (when (member "pkg-config"
                     (map package-name (filter package? packages)))
        (emit-warning package
                      "pkg-config should probably be a native input"
                      'inputs))))))


(define (check-synopsis-style package)
  ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE.
  (define (check-final-period synopsis)
    ;; Synopsis should not end with a period, except for some special cases.
    (when (and (string-suffix? "." synopsis)
               (not (string-suffix? "etc." synopsis)))
      (emit-warning package
                    "no period allowed at the end of the synopsis"
                    'synopsis)))

  (define (check-start-article synopsis)
    (when (or (string-prefix-ci? "A " synopsis)
              (string-prefix-ci? "An " synopsis))
      (emit-warning package
                    "no article allowed at the beginning of the synopsis"
                    'synopsis)))

  (define (check-synopsis-length synopsis)
    (when (>= (string-length synopsis) 80)
      (emit-warning package
                    "synopsis should be less than 80 characters long"
                    'synopsis)))

  (define (check-synopsis-start-upper-case synopsis)
   (when (and (not (string-null? synopsis))
              (not (start-with-capital-letter? synopsis)))
     (emit-warning package
                   "synopsis should start with an upper-case letter"
                   'synopsis)))

  (define (check-start-with-package-name synopsis)
    (when (string-prefix-ci? (package-name package) synopsis)
      (emit-warning package
                    "synopsis should not start with the package name"
                    'synopsis)))

 (let ((synopsis (package-synopsis package)))
   (if (string? synopsis)
       (begin
        (check-synopsis-start-upper-case synopsis)
        (check-final-period synopsis)
        (check-start-article synopsis)
        (check-start-with-package-name synopsis)
        (check-synopsis-length synopsis)))))

(define (check-patches package)
  ;; Emit a warning if the patches requires by PACKAGE are badly named.
  (let ((patches   (and=> (package-source package) origin-patches))
        (name      (package-name package))
        (full-name (package-full-name package)))
    (if (and patches
             (any (match-lambda
                   ((? string? patch)
                    (let ((filename (basename patch)))
                      (not (or (eq? (string-contains filename name) 0)
                               (eq? (string-contains filename full-name)
                                    0)))))
                   (_
                    ;; This must be an <origin> or something like that.
                    #f))
                  patches))
        (emit-warning package
          "file names of patches should start with the package name"
          'patches))))

(define %checkers
  (list
   (lint-checker
     (name        "description")
     (description "Validate package descriptions")
     (check       check-description-style))
   (lint-checker
     (name        "inputs-should-be-native")
     (description "Identify inputs that should be native inputs")
     (check       check-inputs-should-be-native))
   (lint-checker
     (name        "patch-filenames")
     (description "Validate filenames of patches")
     (check       check-patches))
   (lint-checker
     (name        "synopsis")
     (description "Validate package synopsis")
     (check       check-synopsis-style))))

(define (run-checkers package checkers)
  ;; Run the given CHECKERS on PACKAGE.
  (for-each (lambda (checker)
              ((lint-checker-check checker) package))
            checkers))

\f
;;;
;;; Command-line options.
;;;

(define %default-options
  ;; Alist of default option values.
  '())

(define (show-help)
  (display (_ "Usage: guix lint [OPTION]... [PACKAGE]...
Run a set of checkers on the specified package; if none is specified, run the checkers on all packages.\n"))
  (display (_ "
  -c, --checkers=CHECKER1,CHECKER2...
                         only run the specificed checkers"))
  (display (_ "
  -h, --help             display this help and exit"))
  (display (_ "
  -l, --list-checkers    display the list of available lint checkers"))
  (display (_ "
  -V, --version          display version information and exit"))
  (newline)
  (show-bug-report-information))


(define %options
  ;; Specification of the command-line options.
  ;; TODO: add some options:
  ;; * --certainty=[low,medium,high]: only run checkers that have at least this
  ;;                                  'certainty'.
  (list (option '(#\c "checkers") #t #f
                (lambda (opt name arg result arg-handler)
                  (let ((names (string-split arg #\,)))
                    (for-each (lambda (c)
                                (when (not (member c (map lint-checker-name
                                                          %checkers)))
                                  (leave (_ "~a: invalid checker") c)))
                              names)
                    (values (alist-cons 'checkers
                             (filter (lambda (checker)
                                       (member (lint-checker-name checker)
                                               names))
                                     %checkers)
                             result)
                            #f))))
        (option '(#\h "help") #f #f
                (lambda args
                  (show-help)
                  (exit 0)))
        (option '(#\l "list-checkers") #f #f
                (lambda args
                   (list-checkers-and-exit)))
        (option '(#\V "version") #f #f
                (lambda args
                  (show-version-and-exit "guix lint")))))

\f
;;;
;;; Entry Point
;;;

(define (guix-lint . args)
  (define (parse-options)
    ;; Return the alist of option values.
    (args-fold* args %options
                (lambda (opt name arg result arg-handler)
                  (leave (_ "~A: unrecognized option~%") name))
                (lambda (arg result arg-handler)
                  (alist-cons 'argument arg result))
                %default-options #f))

  (let* ((opts (parse-options))
         (args (filter-map (match-lambda
                            (('argument . value)
                             value)
                            (_ #f))
                           (reverse opts)))
         (checkers (or (assoc-ref opts 'checkers) %checkers)))
     (if (null? args)
          (fold-packages (lambda (p r) (run-checkers p checkers)) '())
          (for-each (lambda (spec)
                      (run-checkers (specification->package spec) checkers))
                    args))))

debug log:

solving e8cc853 ...
found e8cc853 in https://yhetil.org/guix-devel/87wq7qxdj5.fsf@member.fsf.org/
found 84d9fe9 in https://yhetil.org/guix-devel/87y4s6xdkk.fsf@member.fsf.org/
found be866ab in https://git.savannah.gnu.org/cgit/guix.git
preparing index
index prepared:
100644 be866ab4aed2f24e62d04c45d2182471df63df3b	guix/scripts/lint.scm

applying [1/2] https://yhetil.org/guix-devel/87y4s6xdkk.fsf@member.fsf.org/
diff --git a/guix/scripts/lint.scm b/guix/scripts/lint.scm
index be866ab..84d9fe9 100644


applying [2/2] https://yhetil.org/guix-devel/87wq7qxdj5.fsf@member.fsf.org/
diff --git a/guix/scripts/lint.scm b/guix/scripts/lint.scm
index 84d9fe9..e8cc853 100644

Checking patch guix/scripts/lint.scm...
Applied patch guix/scripts/lint.scm cleanly.
Checking patch guix/scripts/lint.scm...
Applied patch guix/scripts/lint.scm cleanly.

index at:
100644 e8cc853c584b1d9cf9c1ea0bbaa5f696c61110a3	guix/scripts/lint.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).