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