gnu-maintenance: Honor 'upstream-name' property in GNU updater.
[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 combinators)
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 (memoize
169 (let ((official-gnu-packages (memoize official-gnu-packages)))
170 (lambda (package)
171 "Return true if PACKAGE is a GNU package. This procedure may access the
172 network to check in GNU's database."
173 (define (mirror-type url)
174 (let ((uri (string->uri url)))
175 (and (eq? (uri-scheme uri) 'mirror)
176 (cond
177 ((member (uri-host uri)
178 '("gnu" "gnupg" "gcc" "gnome"))
179 ;; Definitely GNU.
180 'gnu)
181 ((equal? (uri-host uri) "cran")
182 ;; Possibly GNU: mirror://cran could be either GNU R itself
183 ;; or a non-GNU package.
184 #f)
185 (else
186 ;; Definitely non-GNU.
187 'non-gnu)))))
188
189 (define (gnu-home-page? package)
190 (and=> (package-home-page package)
191 (lambda (url)
192 (and=> (uri-host (string->uri url))
193 (lambda (host)
194 (member host '("www.gnu.org" "gnu.org")))))))
195
196 (or (gnu-home-page? package)
197 (let ((url (and=> (package-source package) origin-uri))
198 (name (or (assq-ref (package-properties package)
199 'upstream-name)
200 (package-name package))))
201 (case (and (string? url) (mirror-type url))
202 ((gnu) #t)
203 ((non-gnu) #f)
204 (else
205 (and (member name (map gnu-package-name (official-gnu-packages)))
206 #t)))))))))
207
208 \f
209 ;;;
210 ;;; Latest release.
211 ;;;
212
213 (define (ftp-server/directory package)
214 "Return the FTP server and directory where PACKAGE's tarball are stored."
215 (let ((name (or (assq-ref (package-properties package) 'upstream-name)
216 (package-name package))))
217 (values (or (assoc-ref (package-properties package) 'ftp-server)
218 "ftp.gnu.org")
219 (or (assoc-ref (package-properties package) 'ftp-directory)
220 (string-append "/gnu/" name)))))
221
222 (define (sans-extension tarball)
223 "Return TARBALL without its .tar.* or .zip extension."
224 (let ((end (or (string-contains tarball ".tar")
225 (string-contains tarball ".zip"))))
226 (substring tarball 0 end)))
227
228 (define %tarball-rx
229 ;; The .zip extensions is notably used for freefont-ttf.
230 ;; The "-src" pattern is for "TeXmacs-1.0.7.9-src.tar.gz".
231 ;; The "-gnu[0-9]" pattern is for "icecat-38.4.0-gnu1.tar.bz2".
232 (make-regexp "^([^.]+)-([0-9]|[^-])+(-(src|gnu[0-9]))?\\.(tar\\.|zip$)"))
233
234 (define %alpha-tarball-rx
235 (make-regexp "^.*-.*[0-9](-|~)?(alpha|beta|rc|cvs|svn|git)-?[0-9\\.]*\\.tar\\."))
236
237 (define (release-file? project file)
238 "Return #f if FILE is not a release tarball of PROJECT, otherwise return
239 true."
240 (and (not (string-suffix? ".sig" file))
241 (and=> (regexp-exec %tarball-rx file)
242 (lambda (match)
243 ;; Filter out unrelated files, like `guile-www-1.1.1'.
244 ;; Case-insensitive for things like "TeXmacs" vs. "texmacs".
245 ;; The "-src" suffix is for "freefont-src-20120503.tar.gz".
246 (and=> (match:substring match 1)
247 (lambda (name)
248 (or (string-ci=? name project)
249 (string-ci=? name
250 (string-append project
251 "-src")))))))
252 (not (regexp-exec %alpha-tarball-rx file))
253 (let ((s (sans-extension file)))
254 (regexp-exec %package-name-rx s))))
255
256 (define (tarball->version tarball)
257 "Return the version TARBALL corresponds to. TARBALL is a file name like
258 \"coreutils-8.23.tar.xz\"."
259 (let-values (((name version)
260 (gnu-package-name->name+version (sans-extension tarball))))
261 version))
262
263 (define* (releases project
264 #:key
265 (server "ftp.gnu.org")
266 (directory (string-append "/gnu/" project)))
267 "Return the list of <upstream-release> of PROJECT as a list of release
268 name/directory pairs."
269 ;; TODO: Parse something like fencepost.gnu.org:/gd/gnuorg/packages-ftp.
270 (define conn (ftp-open server))
271
272 (let loop ((directories (list directory))
273 (result '()))
274 (match directories
275 (()
276 (ftp-close conn)
277 (coalesce-sources result))
278 ((directory rest ...)
279 (let* ((files (ftp-list conn directory))
280 (subdirs (filter-map (match-lambda
281 ((name 'directory . _) name)
282 (_ #f))
283 files)))
284 (define (file->url file)
285 (string-append "ftp://" server directory "/" file))
286
287 (define (file->source file)
288 (let ((url (file->url file)))
289 (upstream-source
290 (package project)
291 (version (tarball->version file))
292 (urls (list url))
293 (signature-urls (list (string-append url ".sig"))))))
294
295 (loop (append (map (cut string-append directory "/" <>)
296 subdirs)
297 rest)
298 (append
299 ;; Filter out signatures, deltas, and files which
300 ;; are potentially not releases of PROJECT--e.g.,
301 ;; in /gnu/guile, filter out guile-oops and
302 ;; guile-www; in mit-scheme, filter out binaries.
303 (filter-map (match-lambda
304 ((file 'file . _)
305 (and (release-file? project file)
306 (file->source file)))
307 (_ #f))
308 files)
309 result)))))))
310
311 (define* (latest-ftp-release project
312 #:key
313 (server "ftp.gnu.org")
314 (directory (string-append "/gnu/" project))
315 (keep-file? (const #t))
316 (file->signature (cut string-append <> ".sig"))
317 (ftp-open ftp-open) (ftp-close ftp-close))
318 "Return an <upstream-source> for the latest release of PROJECT on SERVER
319 under DIRECTORY, or #f. Use FTP-OPEN and FTP-CLOSE to open (resp. close) FTP
320 connections; this can be useful to reuse connections.
321
322 KEEP-FILE? is a predicate to decide whether to enter a directory and to
323 consider a given file (source tarball) as a valid candidate based on its name.
324
325 FILE->SIGNATURE must be a procedure; it is passed a source file URL and must
326 return the corresponding signature URL, or #f it signatures are unavailable."
327 (define (latest a b)
328 (if (version>? a b) a b))
329
330 (define (latest-release a b)
331 (if (version>? (upstream-source-version a) (upstream-source-version b))
332 a b))
333
334 (define contains-digit?
335 (cut string-any char-set:digit <>))
336
337 (define patch-directory-name?
338 ;; Return #t for patch directory names such as 'bash-4.2-patches'.
339 (cut string-suffix? "patches" <>))
340
341 (define conn (ftp-open server))
342
343 (define (file->url directory file)
344 (string-append "ftp://" server directory "/" file))
345
346 (define (file->source directory file)
347 (let ((url (file->url directory file)))
348 (upstream-source
349 (package project)
350 (version (tarball->version file))
351 (urls (list url))
352 (signature-urls (match (file->signature url)
353 (#f #f)
354 (sig (list sig)))))))
355
356 (let loop ((directory directory)
357 (result #f))
358 (let* ((entries (ftp-list conn directory))
359
360 ;; Filter out sub-directories that do not contain digits---e.g.,
361 ;; /gnuzilla/lang and /gnupg/patches. Filter out "w32"
362 ;; directories as found on ftp.gnutls.org.
363 (subdirs (filter-map (match-lambda
364 (((? patch-directory-name? dir)
365 'directory . _)
366 #f)
367 (("w32" 'directory . _)
368 #f)
369 (((? contains-digit? dir) 'directory . _)
370 (and (keep-file? dir) dir))
371 (_ #f))
372 entries))
373
374 ;; Whether or not SUBDIRS is empty, compute the latest releases
375 ;; for the current directory. This is necessary for packages
376 ;; such as 'sharutils' that have a sub-directory that contains
377 ;; only an older release.
378 (releases (filter-map (match-lambda
379 ((file 'file . _)
380 (and (release-file? project file)
381 (keep-file? file)
382 (file->source directory file)))
383 (_ #f))
384 entries)))
385
386 ;; Assume that SUBDIRS correspond to versions, and jump into the
387 ;; one with the highest version number.
388 (let* ((release (reduce latest-release #f
389 (coalesce-sources releases)))
390 (result (if (and result release)
391 (latest-release release result)
392 (or release result)))
393 (target (reduce latest #f subdirs)))
394 (if target
395 (loop (string-append directory "/" target)
396 result)
397 (begin
398 (ftp-close conn)
399 result))))))
400
401 (define* (latest-release package
402 #:key
403 (server "ftp.gnu.org")
404 (directory (string-append "/gnu/" package)))
405 "Return the <upstream-source> for the latest version of PACKAGE or #f.
406 PACKAGE must be the canonical name of a GNU package."
407 (latest-ftp-release package
408 #:server server
409 #:directory directory))
410
411 (define-syntax-rule (false-if-ftp-error exp)
412 "Return #f if an FTP error is raise while evaluating EXP; return the result
413 of EXP otherwise."
414 (catch 'ftp-error
415 (lambda ()
416 exp)
417 (lambda (key port . rest)
418 (if (ftp-connection? port)
419 (ftp-close port)
420 (close-port port))
421 #f)))
422
423 (define (latest-release* package)
424 "Like 'latest-release', but (1) take a <package> object, and (2) ignore FTP
425 errors that might occur when PACKAGE is not actually a GNU package, or not
426 hosted on ftp.gnu.org, or not under that name (this is the case for
427 \"emacs-auctex\", for instance.)"
428 (let-values (((server directory)
429 (ftp-server/directory package)))
430 (let ((name (or (assoc-ref (package-properties package) 'upstream-name)
431 (package-name package))))
432 (false-if-ftp-error (latest-release name
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."
452 (and (not (string-prefix? "emacs-" (package-name package)))
453 (not (gnome-package? package))
454 (gnu-package? package)))
455
456 (define (url-prefix-predicate prefix)
457 "Return a predicate that returns true when passed a package where one of its
458 source URLs starts with PREFIX."
459 (lambda (package)
460 (define matching-uri?
461 (match-lambda
462 ((? string? uri)
463 (string-prefix? prefix uri))
464 (_
465 #f)))
466
467 (match (package-source package)
468 ((? origin? origin)
469 (match (origin-uri origin)
470 ((? matching-uri?) #t)
471 (_ #f)))
472 (_ #f))))
473
474 (define gnome-package?
475 (url-prefix-predicate "mirror://gnome/"))
476
477 (define (latest-gnome-release package)
478 "Return the latest release of PACKAGE, the name of a GNOME package."
479 (define %not-dot
480 (char-set-complement (char-set #\.)))
481
482 (define (even-minor-version? version)
483 (match (string-tokenize version %not-dot)
484 (((= string->number major) (= string->number minor) . rest)
485 (and minor (even? minor)))
486 (_
487 #t))) ;cross fingers
488
489 (define (even-numbered? file)
490 ;; Return true if FILE somehow denotes an even-numbered file name. The
491 ;; trick here is that we want this to match both directories such as
492 ;; "3.18.6" and actual file names such as "gtk+-3.18.6.tar.bz2".
493 (let-values (((name version) (package-name->name+version file)))
494 (even-minor-version? (or version name))))
495
496 (define upstream-name
497 ;; Some packages like "NetworkManager" have camel-case names.
498 (or (assoc-ref (package-properties package) 'upstream-name)
499 (package-name package)))
500
501 (false-if-ftp-error
502 (latest-ftp-release upstream-name
503 #:server "ftp.gnome.org"
504 #:directory (string-append "/pub/gnome/sources/"
505 upstream-name)
506
507
508 ;; <https://www.gnome.org/gnome-3/source/> explains
509 ;; that odd minor version numbers represent development
510 ;; releases, which we are usually not interested in.
511 #:keep-file? even-numbered?
512
513 ;; ftp.gnome.org provides no signatures, only
514 ;; checksums.
515 #:file->signature (const #f))))
516
517
518 (define (latest-kde-release package)
519 "Return the latest release of PACKAGE, the name of an KDE.org package."
520 (let ((uri (string->uri (origin-uri (package-source package)))))
521 (false-if-ftp-error
522 (latest-ftp-release
523 (or (assoc-ref (package-properties package) 'upstream-name)
524 (package-name package))
525 #:server "mirrors.mit.edu"
526 #:directory
527 (string-append "/kde" (dirname (dirname (uri-path uri))))
528 #:file->signature (const #f)))))
529
530 (define (latest-xorg-release package)
531 "Return the latest release of PACKAGE, the name of an X.org package."
532 (let ((uri (string->uri (origin-uri (package-source package)))))
533 (false-if-ftp-error
534 (latest-ftp-release
535 (package-name package)
536 #:server "ftp.freedesktop.org"
537 #:directory
538 (string-append "/pub/xorg/" (dirname (uri-path uri)))))))
539
540 (define (latest-kernel.org-release package)
541 "Return the latest release of PACKAGE, the name of a kernel.org package."
542 (let ((uri (string->uri (origin-uri (package-source package)))))
543 (false-if-ftp-error
544 (latest-ftp-release
545 (package-name package)
546 #:server "ftp.free.fr" ;a mirror reachable over FTP
547 #:directory (string-append "/mirrors/ftp.kernel.org"
548 (dirname (uri-path uri)))
549
550 ;; kernel.org provides "foo-x.y.tar.sign" files, which are signatures of
551 ;; the uncompressed tarball.
552 #:file->signature (lambda (tarball)
553 (string-append (file-sans-extension tarball)
554 ".sign"))))))
555
556 (define %gnu-updater
557 (upstream-updater
558 (name 'gnu)
559 (description "Updater for GNU packages")
560 (pred pure-gnu-package?)
561 (latest latest-release*)))
562
563 (define %gnome-updater
564 (upstream-updater
565 (name 'gnome)
566 (description "Updater for GNOME packages")
567 (pred gnome-package?)
568 (latest latest-gnome-release)))
569
570 (define %kde-updater
571 (upstream-updater
572 (name 'kde)
573 (description "Updater for KDE packages")
574 (pred (url-prefix-predicate "mirror://kde/"))
575 (latest latest-kde-release)))
576
577 (define %xorg-updater
578 (upstream-updater
579 (name 'xorg)
580 (description "Updater for X.org packages")
581 (pred (url-prefix-predicate "mirror://xorg/"))
582 (latest latest-xorg-release)))
583
584 (define %kernel.org-updater
585 (upstream-updater
586 (name 'kernel.org)
587 (description "Updater for packages hosted on kernel.org")
588 (pred (url-prefix-predicate "mirror://kernel.org/"))
589 (latest latest-kernel.org-release)))
590
591 ;;; gnu-maintenance.scm ends here