Merge branch 'master' into python-tests
[jackhill/guix/guix.git] / guix / gnu-maintenance.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2012, 2013 Nikita Karetnikov <nikita@karetnikov.org>
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 gnu-maintenance)
21 #:use-module (web uri)
22 #:use-module (web client)
23 #:use-module (web response)
24 #:use-module (ice-9 regex)
25 #:use-module (ice-9 match)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-11)
28 #:use-module (srfi srfi-26)
29 #:use-module (system foreign)
30 #:use-module (guix http-client)
31 #:use-module (guix ftp-client)
32 #:use-module (guix utils)
33 #:use-module (guix memoization)
34 #:use-module (guix records)
35 #:use-module (guix upstream)
36 #:use-module (guix packages)
37 #:export (gnu-package-name
38 gnu-package-mundane-name
39 gnu-package-copyright-holder
40 gnu-package-savannah
41 gnu-package-fsd
42 gnu-package-language
43 gnu-package-logo
44 gnu-package-doc-category
45 gnu-package-doc-summary
46 gnu-package-doc-description
47 gnu-package-doc-urls
48 gnu-package-download-url
49
50 official-gnu-packages
51 find-package
52 gnu-package?
53
54 release-file?
55 releases
56 latest-release
57 gnu-release-archive-types
58 gnu-package-name->name+version
59
60 %gnu-updater
61 %gnome-updater
62 %kde-updater
63 %xorg-updater
64 %kernel.org-updater))
65
66 ;;; Commentary:
67 ;;;
68 ;;; Code for dealing with the maintenance of GNU packages, such as
69 ;;; auto-updates.
70 ;;;
71 ;;; Code:
72
73 \f
74 ;;;
75 ;;; List of GNU packages.
76 ;;;
77
78 (define %gnumaint-base-url
79 "http://cvs.savannah.gnu.org/viewvc/*checkout*/womb/gnumaint/")
80
81 (define %package-list-url
82 (string->uri
83 (string-append %gnumaint-base-url "gnupackages.txt")))
84
85 (define %package-description-url
86 ;; This file contains package descriptions in recutils format.
87 ;; See <https://lists.gnu.org/archive/html/guix-devel/2013-10/msg00071.html>.
88 (string->uri
89 (string-append %gnumaint-base-url "pkgblurbs.txt")))
90
91 (define-record-type* <gnu-package-descriptor>
92 gnu-package-descriptor
93 make-gnu-package-descriptor
94
95 gnu-package-descriptor?
96
97 (name gnu-package-name)
98 (mundane-name gnu-package-mundane-name)
99 (copyright-holder gnu-package-copyright-holder)
100 (savannah gnu-package-savannah)
101 (fsd gnu-package-fsd)
102 (language gnu-package-language) ; list of strings
103 (logo gnu-package-logo)
104 (doc-category gnu-package-doc-category)
105 (doc-summary gnu-package-doc-summary)
106 (doc-description gnu-package-doc-description) ; taken from 'pkgdescr.txt'
107 (doc-urls gnu-package-doc-urls) ; list of strings
108 (download-url gnu-package-download-url))
109
110 (define* (official-gnu-packages
111 #:optional (fetch http-fetch/cached))
112 "Return a list of records, which are GNU packages. Use FETCH,
113 to fetch the list of GNU packages over HTTP."
114 (define (read-records port)
115 ;; Return a list of alists. Each alist contains fields of a GNU
116 ;; package.
117 (let loop ((alist (recutils->alist port))
118 (result '()))
119 (if (null? alist)
120 (reverse result)
121 (loop (recutils->alist port)
122 (cons alist result)))))
123
124 (define official-description
125 (let ((db (read-records (fetch %package-description-url #:text? #t))))
126 (lambda (name)
127 ;; Return the description found upstream for package NAME, or #f.
128 (and=> (find (lambda (alist)
129 (equal? name (assoc-ref alist "package")))
130 db)
131 (lambda (record)
132 (let ((field (assoc-ref record "blurb")))
133 ;; The upstream description file uses "redirect PACKAGE" as
134 ;; a blurb in cases where the description of the two
135 ;; packages should be considered the same (e.g., GTK+ has
136 ;; "redirect gnome".) This is usually not acceptable for
137 ;; us because we prefer to have distinct descriptions in
138 ;; such cases. Thus, ignore the 'blurb' field when that
139 ;; happens.
140 (and field
141 (not (string-prefix? "redirect " field))
142 field)))))))
143
144 (map (lambda (alist)
145 (let ((name (assoc-ref alist "package")))
146 (alist->record `(("description" . ,(official-description name))
147 ,@alist)
148 make-gnu-package-descriptor
149 (list "package" "mundane-name" "copyright-holder"
150 "savannah" "fsd" "language" "logo"
151 "doc-category" "doc-summary" "description"
152 "doc-url"
153 "download-url")
154 '("doc-url" "language"))))
155 (let* ((port (fetch %package-list-url #:text? #t))
156 (lst (read-records port)))
157 (close-port port)
158 lst)))
159
160 (define (find-package name)
161 "Find GNU package called NAME and return it. Return #f if it was not
162 found."
163 (find (lambda (package)
164 (string=? name (gnu-package-name package)))
165 (official-gnu-packages)))
166
167 (define gnu-package?
168 (let ((official-gnu-packages (memoize official-gnu-packages)))
169 (mlambdaq (package)
170 "Return true if PACKAGE is a GNU package. This procedure may access the
171 network to check in GNU's database."
172 (define (mirror-type url)
173 (let ((uri (string->uri url)))
174 (and (eq? (uri-scheme uri) 'mirror)
175 (cond
176 ((member (uri-host uri)
177 '("gnu" "gnupg" "gcc" "gnome"))
178 ;; Definitely GNU.
179 'gnu)
180 ((equal? (uri-host uri) "cran")
181 ;; Possibly GNU: mirror://cran could be either GNU R itself
182 ;; or a non-GNU package.
183 #f)
184 (else
185 ;; Definitely non-GNU.
186 'non-gnu)))))
187
188 (define (gnu-home-page? package)
189 (letrec-syntax ((>> (syntax-rules ()
190 ((_ value proc)
191 (and=> value proc))
192 ((_ value proc rest ...)
193 (and=> value
194 (lambda (next)
195 (>> (proc next) rest ...)))))))
196 (>> package package-home-page
197 string->uri uri-host
198 (lambda (host)
199 (member host '("www.gnu.org" "gnu.org"))))))
200
201 (or (gnu-home-page? package)
202 (let ((url (and=> (package-source package) origin-uri))
203 (name (package-upstream-name package)))
204 (case (and (string? url) (mirror-type url))
205 ((gnu) #t)
206 ((non-gnu) #f)
207 (else
208 (and (member name (map gnu-package-name (official-gnu-packages)))
209 #t))))))))
210
211 \f
212 ;;;
213 ;;; Latest release.
214 ;;;
215
216 (define (ftp-server/directory package)
217 "Return the FTP server and directory where PACKAGE's tarball are stored."
218 (let ((name (package-upstream-name package)))
219 (values (or (assoc-ref (package-properties package) 'ftp-server)
220 "ftp.gnu.org")
221 (or (assoc-ref (package-properties package) 'ftp-directory)
222 (string-append "/gnu/" name)))))
223
224 (define (sans-extension tarball)
225 "Return TARBALL without its .tar.* or .zip extension."
226 (let ((end (or (string-contains tarball ".tar")
227 (string-contains tarball ".zip"))))
228 (substring tarball 0 end)))
229
230 (define %tarball-rx
231 ;; The .zip extensions is notably used for freefont-ttf.
232 ;; The "-src" pattern is for "TeXmacs-1.0.7.9-src.tar.gz".
233 ;; The "-gnu[0-9]" pattern is for "icecat-38.4.0-gnu1.tar.bz2".
234 (make-regexp "^([^.]+)-([0-9]|[^-])+(-(src|gnu[0-9]))?\\.(tar\\.|zip$)"))
235
236 (define %alpha-tarball-rx
237 (make-regexp "^.*-.*[0-9](-|~)?(alpha|beta|rc|cvs|svn|git)-?[0-9\\.]*\\.tar\\."))
238
239 (define (release-file? project file)
240 "Return #f if FILE is not a release tarball of PROJECT, otherwise return
241 true."
242 (and (not (string-suffix? ".sig" file))
243 (and=> (regexp-exec %tarball-rx file)
244 (lambda (match)
245 ;; Filter out unrelated files, like `guile-www-1.1.1'.
246 ;; Case-insensitive for things like "TeXmacs" vs. "texmacs".
247 ;; The "-src" suffix is for "freefont-src-20120503.tar.gz".
248 (and=> (match:substring match 1)
249 (lambda (name)
250 (or (string-ci=? name project)
251 (string-ci=? name
252 (string-append project
253 "-src")))))))
254 (not (regexp-exec %alpha-tarball-rx file))
255 (let ((s (sans-extension file)))
256 (regexp-exec %package-name-rx s))))
257
258 (define (tarball->version tarball)
259 "Return the version TARBALL corresponds to. TARBALL is a file name like
260 \"coreutils-8.23.tar.xz\"."
261 (let-values (((name version)
262 (gnu-package-name->name+version (sans-extension tarball))))
263 version))
264
265 (define* (releases project
266 #:key
267 (server "ftp.gnu.org")
268 (directory (string-append "/gnu/" project)))
269 "Return the list of <upstream-release> of PROJECT as a list of release
270 name/directory pairs."
271 ;; TODO: Parse something like fencepost.gnu.org:/gd/gnuorg/packages-ftp.
272 (define conn (ftp-open server))
273
274 (let loop ((directories (list directory))
275 (result '()))
276 (match directories
277 (()
278 (ftp-close conn)
279 (coalesce-sources result))
280 ((directory rest ...)
281 (let* ((files (ftp-list conn directory))
282 (subdirs (filter-map (match-lambda
283 ((name 'directory . _) name)
284 (_ #f))
285 files)))
286 (define (file->url file)
287 (string-append "ftp://" server directory "/" file))
288
289 (define (file->source file)
290 (let ((url (file->url file)))
291 (upstream-source
292 (package project)
293 (version (tarball->version file))
294 (urls (list url))
295 (signature-urls (list (string-append url ".sig"))))))
296
297 (loop (append (map (cut string-append directory "/" <>)
298 subdirs)
299 rest)
300 (append
301 ;; Filter out signatures, deltas, and files which
302 ;; are potentially not releases of PROJECT--e.g.,
303 ;; in /gnu/guile, filter out guile-oops and
304 ;; guile-www; in mit-scheme, filter out binaries.
305 (filter-map (match-lambda
306 ((file 'file . _)
307 (and (release-file? project file)
308 (file->source file)))
309 (_ #f))
310 files)
311 result)))))))
312
313 (define* (latest-ftp-release project
314 #:key
315 (server "ftp.gnu.org")
316 (directory (string-append "/gnu/" project))
317 (keep-file? (const #t))
318 (file->signature (cut string-append <> ".sig"))
319 (ftp-open ftp-open) (ftp-close ftp-close))
320 "Return an <upstream-source> for the latest release of PROJECT on SERVER
321 under DIRECTORY, or #f. Use FTP-OPEN and FTP-CLOSE to open (resp. close) FTP
322 connections; this can be useful to reuse connections.
323
324 KEEP-FILE? is a predicate to decide whether to enter a directory and to
325 consider a given file (source tarball) as a valid candidate based on its name.
326
327 FILE->SIGNATURE must be a procedure; it is passed a source file URL and must
328 return the corresponding signature URL, or #f it signatures are unavailable."
329 (define (latest a b)
330 (if (version>? a b) a b))
331
332 (define (latest-release a b)
333 (if (version>? (upstream-source-version a) (upstream-source-version b))
334 a b))
335
336 (define contains-digit?
337 (cut string-any char-set:digit <>))
338
339 (define patch-directory-name?
340 ;; Return #t for patch directory names such as 'bash-4.2-patches'.
341 (cut string-suffix? "patches" <>))
342
343 (define conn (ftp-open server))
344
345 (define (file->url directory file)
346 (string-append "ftp://" server directory "/" file))
347
348 (define (file->source directory file)
349 (let ((url (file->url directory file)))
350 (upstream-source
351 (package project)
352 (version (tarball->version file))
353 (urls (list url))
354 (signature-urls (match (file->signature url)
355 (#f #f)
356 (sig (list sig)))))))
357
358 (let loop ((directory directory)
359 (result #f))
360 (let* ((entries (ftp-list conn directory))
361
362 ;; Filter out sub-directories that do not contain digits---e.g.,
363 ;; /gnuzilla/lang and /gnupg/patches. Filter out "w32"
364 ;; directories as found on ftp.gnutls.org.
365 (subdirs (filter-map (match-lambda
366 (((? patch-directory-name? dir)
367 'directory . _)
368 #f)
369 (("w32" 'directory . _)
370 #f)
371 (((? contains-digit? dir) 'directory . _)
372 (and (keep-file? dir) dir))
373 (_ #f))
374 entries))
375
376 ;; Whether or not SUBDIRS is empty, compute the latest releases
377 ;; for the current directory. This is necessary for packages
378 ;; such as 'sharutils' that have a sub-directory that contains
379 ;; only an older release.
380 (releases (filter-map (match-lambda
381 ((file 'file . _)
382 (and (release-file? project file)
383 (keep-file? file)
384 (file->source directory file)))
385 (_ #f))
386 entries)))
387
388 ;; Assume that SUBDIRS correspond to versions, and jump into the
389 ;; one with the highest version number.
390 (let* ((release (reduce latest-release #f
391 (coalesce-sources releases)))
392 (result (if (and result release)
393 (latest-release release result)
394 (or release result)))
395 (target (reduce latest #f subdirs)))
396 (if target
397 (loop (string-append directory "/" target)
398 result)
399 (begin
400 (ftp-close conn)
401 result))))))
402
403 (define* (latest-release package
404 #:key
405 (server "ftp.gnu.org")
406 (directory (string-append "/gnu/" package)))
407 "Return the <upstream-source> for the latest version of PACKAGE or #f.
408 PACKAGE must be the canonical name of a GNU package."
409 (latest-ftp-release package
410 #:server server
411 #:directory directory))
412
413 (define-syntax-rule (false-if-ftp-error exp)
414 "Return #f if an FTP error is raise while evaluating EXP; return the result
415 of EXP otherwise."
416 (catch 'ftp-error
417 (lambda ()
418 exp)
419 (lambda (key port . rest)
420 (if (ftp-connection? port)
421 (ftp-close port)
422 (close-port port))
423 #f)))
424
425 (define (latest-release* package)
426 "Like 'latest-release', but (1) take a <package> object, and (2) ignore FTP
427 errors that might occur when PACKAGE is not actually a GNU package, or not
428 hosted on ftp.gnu.org, or not under that name (this is the case for
429 \"emacs-auctex\", for instance.)"
430 (let-values (((server directory)
431 (ftp-server/directory package)))
432 (false-if-ftp-error (latest-release (package-upstream-name package)
433 #:server server
434 #:directory directory))))
435
436 (define %package-name-rx
437 ;; Regexp for a package name, e.g., "foo-X.Y". Since TeXmacs uses
438 ;; "TeXmacs-X.Y-src", the `-src' suffix is allowed.
439 (make-regexp "^(.*)-(([0-9]|\\.)+)(-src)?"))
440
441 (define (gnu-package-name->name+version name+version)
442 "Return the package name and version number extracted from NAME+VERSION."
443 (let ((match (regexp-exec %package-name-rx name+version)))
444 (if (not match)
445 (values name+version #f)
446 (values (match:substring match 1) (match:substring match 2)))))
447
448 (define (pure-gnu-package? package)
449 "Return true if PACKAGE is a non-Emacs and non-GNOME GNU package. This
450 excludes AucTeX, for instance, whose releases are now uploaded to
451 elpa.gnu.org, and all the GNOME packages; EMMS is included though, because its
452 releases are on gnu.org."
453 (and (or (not (string-prefix? "emacs-" (package-name package)))
454 (gnu-hosted? package))
455 (not (gnome-package? package))
456 (gnu-package? package)))
457
458 (define (url-prefix-predicate prefix)
459 "Return a predicate that returns true when passed a package where one of its
460 source URLs starts with PREFIX."
461 (lambda (package)
462 (define matching-uri?
463 (match-lambda
464 ((? string? uri)
465 (string-prefix? prefix uri))
466 (_
467 #f)))
468
469 (match (package-source package)
470 ((? origin? origin)
471 (match (origin-uri origin)
472 ((? matching-uri?) #t)
473 (_ #f)))
474 (_ #f))))
475
476 (define gnu-hosted?
477 (url-prefix-predicate "mirror://gnu/"))
478
479 (define gnome-package?
480 (url-prefix-predicate "mirror://gnome/"))
481
482 (define (latest-gnome-release package)
483 "Return the latest release of PACKAGE, the name of a GNOME package."
484 (define %not-dot
485 (char-set-complement (char-set #\.)))
486
487 (define (even-minor-version? version)
488 (match (string-tokenize version %not-dot)
489 (((= string->number major) (= string->number minor) . rest)
490 (and minor (even? minor)))
491 (_
492 #t))) ;cross fingers
493
494 (define (even-numbered? file)
495 ;; Return true if FILE somehow denotes an even-numbered file name. The
496 ;; trick here is that we want this to match both directories such as
497 ;; "3.18.6" and actual file names such as "gtk+-3.18.6.tar.bz2".
498 (let-values (((name version) (package-name->name+version file)))
499 (even-minor-version? (or version name))))
500
501 (define upstream-name
502 ;; Some packages like "NetworkManager" have camel-case names.
503 (package-upstream-name package))
504
505 (false-if-ftp-error
506 (latest-ftp-release upstream-name
507 #:server "ftp.gnome.org"
508 #:directory (string-append "/pub/gnome/sources/"
509 upstream-name)
510
511
512 ;; <https://www.gnome.org/gnome-3/source/> explains
513 ;; that odd minor version numbers represent development
514 ;; releases, which we are usually not interested in.
515 #:keep-file? even-numbered?
516
517 ;; ftp.gnome.org provides no signatures, only
518 ;; checksums.
519 #:file->signature (const #f))))
520
521
522 (define (latest-kde-release package)
523 "Return the latest release of PACKAGE, the name of an KDE.org package."
524 (let ((uri (string->uri (origin-uri (package-source package)))))
525 (false-if-ftp-error
526 (latest-ftp-release
527 (package-upstream-name package)
528 #:server "mirrors.mit.edu"
529 #:directory
530 (string-append "/kde" (dirname (dirname (uri-path uri))))
531 #:file->signature (const #f)))))
532
533 (define (latest-xorg-release package)
534 "Return the latest release of PACKAGE, the name of an X.org package."
535 (let ((uri (string->uri (origin-uri (package-source package)))))
536 (false-if-ftp-error
537 (latest-ftp-release
538 (package-name package)
539 #:server "ftp.freedesktop.org"
540 #:directory
541 (string-append "/pub/xorg/" (dirname (uri-path uri)))))))
542
543 (define (latest-kernel.org-release package)
544 "Return the latest release of PACKAGE, the name of a kernel.org package."
545 (let ((uri (string->uri (origin-uri (package-source package)))))
546 (false-if-ftp-error
547 (latest-ftp-release
548 (package-name package)
549 #:server "ftp.free.fr" ;a mirror reachable over FTP
550 #:directory (string-append "/mirrors/ftp.kernel.org"
551 (dirname (uri-path uri)))
552
553 ;; kernel.org provides "foo-x.y.tar.sign" files, which are signatures of
554 ;; the uncompressed tarball.
555 #:file->signature (lambda (tarball)
556 (string-append (file-sans-extension tarball)
557 ".sign"))))))
558
559 (define %gnu-updater
560 (upstream-updater
561 (name 'gnu)
562 (description "Updater for GNU packages")
563 (pred pure-gnu-package?)
564 (latest latest-release*)))
565
566 (define %gnome-updater
567 (upstream-updater
568 (name 'gnome)
569 (description "Updater for GNOME packages")
570 (pred gnome-package?)
571 (latest latest-gnome-release)))
572
573 (define %kde-updater
574 (upstream-updater
575 (name 'kde)
576 (description "Updater for KDE packages")
577 (pred (url-prefix-predicate "mirror://kde/"))
578 (latest latest-kde-release)))
579
580 (define %xorg-updater
581 (upstream-updater
582 (name 'xorg)
583 (description "Updater for X.org packages")
584 (pred (url-prefix-predicate "mirror://xorg/"))
585 (latest latest-xorg-release)))
586
587 (define %kernel.org-updater
588 (upstream-updater
589 (name 'kernel.org)
590 (description "Updater for packages hosted on kernel.org")
591 (pred (url-prefix-predicate "mirror://kernel.org/"))
592 (latest latest-kernel.org-release)))
593
594 ;;; gnu-maintenance.scm ends here