1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2010, 2011, 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2012, 2013 Nikita Karetnikov <nikita@karetnikov.org>
5 ;;; This file is part of GNU Guix.
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.
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.
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 ui)
33 #:use-module (guix utils)
34 #:use-module (guix records)
35 #:use-module (guix packages)
36 #:use-module ((guix download) #:select (download-to-store))
37 #:use-module (guix gnupg)
38 #:use-module (rnrs io ports)
39 #:use-module (guix base32)
40 #:use-module ((guix build utils)
41 #:select (substitute))
42 #:export (gnu-package-name
43 gnu-package-mundane-name
44 gnu-package-copyright-holder
49 gnu-package-doc-category
50 gnu-package-doc-summary
51 gnu-package-doc-description
53 gnu-package-download-url
61 gnu-package-name->name+version
64 update-package-source))
68 ;;; Code for dealing with the maintenance of GNU packages, such as
75 ;;; List of GNU packages.
78 (define %gnumaint-base-url
79 "http://cvs.savannah.gnu.org/viewvc/*checkout*/gnumaint/")
81 (define %package-list-url
83 (string-append %gnumaint-base-url "gnupackages.txt?root=womb")))
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>.
89 (string-append %gnumaint-base-url "pkgblurbs.txt?root=womb")))
91 (define-record-type* <gnu-package-descriptor>
92 gnu-package-descriptor
93 make-gnu-package-descriptor
95 gnu-package-descriptor?
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))
110 (define (official-gnu-packages)
111 "Return a list of records, which are GNU packages."
112 (define (read-records port)
113 ;; Return a list of alists. Each alist contains fields of a GNU
115 (let loop ((alist (recutils->alist port))
119 (loop (recutils->alist port)
120 (cons alist result)))))
122 (define official-description
123 (let ((db (read-records (http-fetch %package-description-url
126 ;; Return the description found upstream for package NAME, or #f.
127 (and=> (find (lambda (alist)
128 (equal? name (assoc-ref alist "package")))
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
140 (not (string-prefix? "redirect " field))
144 (let ((name (assoc-ref alist "package")))
145 (alist->record `(("description" . ,(official-description name))
147 make-gnu-package-descriptor
148 (list "package" "mundane-name" "copyright-holder"
149 "savannah" "fsd" "language" "logo"
150 "doc-category" "doc-summary" "description"
153 '("doc-url" "language"))))
154 (read-records (http-fetch %package-list-url #:text? #t))))
156 (define (find-packages regexp)
157 "Find GNU packages which satisfy REGEXP."
158 (let ((name-rx (make-regexp regexp)))
159 (filter (lambda (package)
161 (regexp-exec name-rx (gnu-package-name package))))
162 (official-gnu-packages))))
166 (let ((official-gnu-packages (memoize official-gnu-packages)))
168 "Return true if PACKAGE is a GNU package. This procedure may access the
169 network to check in GNU's database."
170 ;; TODO: Find a way to determine that a package is non-GNU without going
171 ;; through the network.
172 (let ((url (and=> (package-source package) origin-uri))
173 (name (package-name package)))
174 (or (and (string? url) (string-prefix? "mirror://gnu" url))
175 (and (member name (map gnu-package-name (official-gnu-packages)))
183 (define (ftp-server/directory project)
184 "Return the FTP server and directory where PROJECT's tarball are
187 '(("commoncpp2" "ftp.gnu.org" "/gnu/commoncpp")
188 ("ucommon" "ftp.gnu.org" "/gnu/commoncpp")
189 ("libzrtpcpp" "ftp.gnu.org" "/gnu/ccrtp")
190 ("libosip2" "ftp.gnu.org" "/gnu/osip")
191 ("libgcrypt" "ftp.gnupg.org" "/gcrypt/libgcrypt")
192 ("libgpg-error" "ftp.gnupg.org" "/gcrypt/libgpg-error")
193 ("libassuan" "ftp.gnupg.org" "/gcrypt/libassuan")
194 ("gnupg" "ftp.gnupg.org" "/gcrypt/gnupg")
195 ("freefont-ttf" "ftp.gnu.org" "/gnu/freefont")
196 ("gnu-ghostscript" "ftp.gnu.org" "/gnu/ghostscript")
197 ("mit-scheme" "ftp.gnu.org" "/gnu/mit-scheme/stable.pkg")
198 ("icecat" "ftp.gnu.org" "/gnu/gnuzilla")
199 ("source-highlight" "ftp.gnu.org" "/gnu/src-highlite")
200 ("glib" "ftp.gnome.org" "/pub/gnome/sources/glib")
201 ("gnutls" "ftp.gnutls.org" "/gcrypt/gnutls")
202 ("TeXmacs" "ftp.texmacs.org" "/TeXmacs/targz")))
204 (match (assoc project quirks)
205 ((_ server directory)
206 (values server directory))
208 (values "ftp.gnu.org" (string-append "/gnu/" project)))))
210 (define (sans-extension tarball)
211 "Return TARBALL without its .tar.* extension."
212 (let ((end (string-contains tarball ".tar")))
213 (substring tarball 0 end)))
216 (make-regexp "^(.+)-([0-9]|[^-])*(-src)?\\.tar\\."))
218 (define %alpha-tarball-rx
219 (make-regexp "^.*-.*[0-9](-|~)?(alpha|beta|rc|cvs|svn|git)-?[0-9\\.]*\\.tar\\."))
221 (define (release-file project file)
222 "Return #f if FILE is not a release tarball of PROJECT, otherwise return
224 (and (not (string-suffix? ".sig" file))
225 (and=> (regexp-exec %tarball-rx file)
227 ;; Filter out unrelated files, like `guile-www-1.1.1'.
228 (equal? project (match:substring match 1))))
229 (not (regexp-exec %alpha-tarball-rx file))
230 (let ((s (sans-extension file)))
231 (and (regexp-exec %package-name-rx s) s))))
233 (define (releases project)
234 "Return the list of releases of PROJECT as a list of release name/directory
235 pairs. Example: (\"mit-scheme-9.0.1\" . \"/gnu/mit-scheme/stable.pkg/9.0.1\"). "
236 ;; TODO: Parse something like fencepost.gnu.org:/gd/gnuorg/packages-ftp.
237 (let-values (((server directory) (ftp-server/directory project)))
238 (define conn (ftp-open server))
240 (let loop ((directories (list directory))
246 ((directory rest ...)
247 (let* ((files (ftp-list conn directory))
248 (subdirs (filter-map (match-lambda
249 ((name 'directory . _) name)
252 (loop (append (map (cut string-append directory "/" <>)
256 ;; Filter out signatures, deltas, and files which
257 ;; are potentially not releases of PROJECT--e.g.,
258 ;; in /gnu/guile, filter out guile-oops and
259 ;; guile-www; in mit-scheme, filter out binaries.
260 (filter-map (match-lambda
262 (and=> (release-file project file)
263 (cut cons <> directory)))
268 (define* (latest-release project
269 #:key (ftp-open ftp-open) (ftp-close ftp-close))
270 "Return (\"FOO-X.Y\" . \"/bar/foo\") or #f. Use FTP-OPEN and FTP-CLOSE to
271 open (resp. close) FTP connections; this can be useful to reuse connections."
273 (if (version>? a b) a b))
275 (define contains-digit?
276 (cut string-any char-set:digit <>))
278 (define patch-directory-name?
279 ;; Return #t for patch directory names such as 'bash-4.2-patches'.
280 (cut string-suffix? "patches" <>))
282 (let-values (((server directory) (ftp-server/directory project)))
283 (define conn (ftp-open server))
285 (let loop ((directory directory))
286 (let* ((entries (ftp-list conn directory))
288 ;; Filter out sub-directories that do not contain digits---e.g.,
289 ;; /gnuzilla/lang and /gnupg/patches.
290 (subdirs (filter-map (match-lambda
291 (((? patch-directory-name? dir)
294 (((? contains-digit? dir) 'directory . _)
300 ;; No sub-directories, so assume that tarballs are here.
301 (let ((files (filter-map (match-lambda
303 (release-file project file))
307 (and=> (reduce latest #f files)
308 (cut cons <> directory))))
310 ;; Assume that SUBDIRS correspond to versions, and jump into the
311 ;; one with the highest version number.
312 (let ((target (reduce latest #f subdirs)))
314 (loop (string-append directory "/" target))
319 (define %package-name-rx
320 ;; Regexp for a package name, e.g., "foo-X.Y". Since TeXmacs uses
321 ;; "TeXmacs-X.Y-src", the `-src' suffix is allowed.
322 (make-regexp "^(.*)-(([0-9]|\\.)+)(-src)?"))
324 (define (gnu-package-name->name+version name+version)
325 "Return the package name and version number extracted from NAME+VERSION."
326 (let ((match (regexp-exec %package-name-rx name+version)))
328 (values name+version #f)
329 (values (match:substring match 1) (match:substring match 2)))))
336 (define (package-update-path package)
337 "Return an update path for PACKAGE, or #f if no update is needed."
338 (and (gnu-package? package)
339 (match (latest-release (package-name package))
340 ((name+version . directory)
341 (let-values (((_ new-version)
342 (package-name->name+version name+version)))
343 (and (version>? name+version (package-full-name package))
344 `(,new-version . ,directory))))
347 (define* (download-tarball store project directory version
348 #:key (archive-type "gz")
349 (key-download 'interactive))
350 "Download PROJECT's tarball over FTP and check its OpenPGP signature. On
351 success, return the tarball file name. KEY-DOWNLOAD specifies a download
352 policy for missing OpenPGP keys; allowed values: 'interactive' (default),
353 'always', and 'never'."
354 (let* ((server (ftp-server/directory project))
355 (base (string-append project "-" version ".tar." archive-type))
356 (url (string-append "ftp://" server "/" directory "/" base))
357 (sig-url (string-append url ".sig"))
358 (tarball (download-to-store store url))
359 (sig (download-to-store store sig-url)))
360 (let ((ret (gnupg-verify* sig tarball #:key-download key-download)))
364 (warning (_ "signature verification failed for `~a'~%")
366 (warning (_ "(could be because the public key is not in your keyring)~%"))
369 (define* (package-update store package #:key (key-download 'interactive))
370 "Return the new version and the file name of the new version tarball for
371 PACKAGE, or #f and #f when PACKAGE is up-to-date. KEY-DOWNLOAD specifies a
372 download policy for missing OpenPGP keys; allowed values: 'always', 'never',
373 and 'interactive' (default)."
374 (match (package-update-path package)
375 ((version . directory)
377 (package-name package))
379 (let ((source (package-source package)))
380 (or (and (origin? source)
381 (file-extension (origin-uri source)))
383 (let ((tarball (download-tarball store name directory version
384 #:archive-type archive-type
385 #:key-download key-download)))
386 (values version tarball))))
390 (define (update-package-source package version hash)
391 "Modify the source file that defines PACKAGE to refer to VERSION,
392 whose tarball has SHA256 HASH (a bytevector). Return the new version string
393 if an update was made, and #f otherwise."
394 (define (new-line line matches replacement)
395 ;; Iterate over MATCHES and return the modified line based on LINE.
396 ;; Replace each match with REPLACEMENT.
397 (let loop ((m* matches) ; matches
402 (let ((r (cons (substring line o) r)))
403 (string-concatenate-reverse r)))
408 (substring line o (match:start m))
411 (define (update-source file old-version version
413 ;; Update source file FILE, replacing occurrences OLD-VERSION by VERSION
414 ;; and occurrences of OLD-HASH by HASH (base32 representation thereof).
416 ;; TODO: Currently this is a bit of a sledgehammer: if VERSION occurs in
417 ;; different unrelated places, we may modify it more than needed, for
418 ;; instance. We should try to make changes only within the sexp that
419 ;; corresponds to the definition of PACKAGE.
420 (let ((old-hash (bytevector->nix-base32-string old-hash))
421 (hash (bytevector->nix-base32-string hash)))
423 `((,(regexp-quote old-version)
424 . ,(cut new-line <> <> version))
425 (,(regexp-quote old-hash)
426 . ,(cut new-line <> <> hash))))
429 (let ((name (package-name package))
430 (loc (package-field-location package 'version)))
432 (let ((old-version (package-version package))
433 (old-hash (origin-sha256 (package-source package)))
434 (file (and=> (location-file loc)
435 (cut search-path %load-path <>))))
441 (warning (_ "~a: could not locate source file")
445 (format (current-error-port)
446 (_ "~a: ~a: no `version' field in source; skipping~%")
447 (location->string (package-location package))
450 ;;; gnu-maintenance.scm ends here