1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2010, 2011, 2012, 2013 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 %package-list-url
80 (string-append "http://cvs.savannah.gnu.org/"
81 "viewvc/*checkout*/gnumaint/"
82 "gnupackages.txt?root=womb")))
84 (define %gsrc-package-list-url
85 ;; This file is normally kept in sync with GSRC.
86 ;; See <http://lists.gnu.org/archive/html/bug-guix/2013-04/msg00117.html>.
87 (string->uri "http://www.gnu.org/software/gsrc/MANIFEST.rec"))
89 (define-record-type* <gnu-package-descriptor>
90 gnu-package-descriptor
91 make-gnu-package-descriptor
93 gnu-package-descriptor?
95 (name gnu-package-name)
96 (mundane-name gnu-package-mundane-name)
97 (copyright-holder gnu-package-copyright-holder)
98 (savannah gnu-package-savannah)
100 (language gnu-package-language) ; list of strings
101 (logo gnu-package-logo)
102 (doc-category gnu-package-doc-category)
103 (doc-summary gnu-package-doc-summary)
104 (doc-description gnu-package-doc-description) ; taken from GSRC
105 (doc-urls gnu-package-doc-urls) ; list of strings
106 (download-url gnu-package-download-url))
108 (define (official-gnu-packages)
109 "Return a list of records, which are GNU packages."
110 (define (read-records port)
111 ;; Return a list of alists. Each alist contains fields of a GNU
113 (let loop ((alist (recutils->alist port))
117 (loop (recutils->alist port)
118 (cons alist result)))))
120 (define gsrc-description
121 (let ((gsrc (read-records (http-fetch %gsrc-package-list-url
124 ;; Return the description found in GSRC for package NAME, or #f.
125 (and=> (find (lambda (alist)
126 (equal? name (assoc-ref alist "Upstream_name")))
128 (cut assoc-ref <> "Blurb")))))
131 (let ((name (assoc-ref alist "package")))
132 (alist->record `(("description" . ,(gsrc-description name))
134 make-gnu-package-descriptor
135 (list "package" "mundane-name" "copyright-holder"
136 "savannah" "fsd" "language" "logo"
137 "doc-category" "doc-summary" "description"
140 '("doc-url" "language"))))
141 (read-records (http-fetch %package-list-url #:text? #t))))
143 (define (find-packages regexp)
144 "Find GNU packages which satisfy REGEXP."
145 (let ((name-rx (make-regexp regexp)))
146 (filter (lambda (package)
148 (regexp-exec name-rx (gnu-package-name package))))
149 (official-gnu-packages))))
153 (let ((official-gnu-packages (memoize official-gnu-packages)))
155 "Return true if PACKAGE is a GNU package. This procedure may access the
156 network to check in GNU's database."
157 ;; TODO: Find a way to determine that a package is non-GNU without going
158 ;; through the network.
159 (let ((url (and=> (package-source package) origin-uri))
160 (name (package-name package)))
161 (or (and (string? url) (string-prefix? "mirror://gnu" url))
162 (and (member name (map gnu-package-name (official-gnu-packages)))
170 (define (ftp-server/directory project)
171 "Return the FTP server and directory where PROJECT's tarball are
174 '(("commoncpp2" "ftp.gnu.org" "/gnu/commoncpp")
175 ("ucommon" "ftp.gnu.org" "/gnu/commoncpp")
176 ("libzrtpcpp" "ftp.gnu.org" "/gnu/ccrtp")
177 ("libosip2" "ftp.gnu.org" "/gnu/osip")
178 ("libgcrypt" "ftp.gnupg.org" "/gcrypt/libgcrypt")
179 ("libgpg-error" "ftp.gnupg.org" "/gcrypt/libgpg-error")
180 ("libassuan" "ftp.gnupg.org" "/gcrypt/libassuan")
181 ("gnupg" "ftp.gnupg.org" "/gcrypt/gnupg")
182 ("freefont-ttf" "ftp.gnu.org" "/gnu/freefont")
183 ("gnu-ghostscript" "ftp.gnu.org" "/gnu/ghostscript")
184 ("mit-scheme" "ftp.gnu.org" "/gnu/mit-scheme/stable.pkg")
185 ("icecat" "ftp.gnu.org" "/gnu/gnuzilla")
186 ("source-highlight" "ftp.gnu.org" "/gnu/src-highlite")
187 ("glib" "ftp.gnome.org" "/pub/gnome/sources/glib")
188 ("gnutls" "ftp.gnutls.org" "/gcrypt/gnutls")
189 ("TeXmacs" "ftp.texmacs.org" "/TeXmacs/targz")))
191 (match (assoc project quirks)
192 ((_ server directory)
193 (values server directory))
195 (values "ftp.gnu.org" (string-append "/gnu/" project)))))
197 (define (sans-extension tarball)
198 "Return TARBALL without its .tar.* extension."
199 (let ((end (string-contains tarball ".tar")))
200 (substring tarball 0 end)))
203 (make-regexp "^(.+)-([0-9]|[^-])*(-src)?\\.tar\\."))
205 (define %alpha-tarball-rx
206 (make-regexp "^.*-.*[0-9](-|~)?(alpha|beta|rc|cvs|svn|git)-?[0-9\\.]*\\.tar\\."))
208 (define (release-file project file)
209 "Return #f if FILE is not a release tarball of PROJECT, otherwise return
211 (and (not (string-suffix? ".sig" file))
212 (and=> (regexp-exec %tarball-rx file)
214 ;; Filter out unrelated files, like `guile-www-1.1.1'.
215 (equal? project (match:substring match 1))))
216 (not (regexp-exec %alpha-tarball-rx file))
217 (let ((s (sans-extension file)))
218 (and (regexp-exec %package-name-rx s) s))))
220 (define (releases project)
221 "Return the list of releases of PROJECT as a list of release name/directory
222 pairs. Example: (\"mit-scheme-9.0.1\" . \"/gnu/mit-scheme/stable.pkg/9.0.1\"). "
223 ;; TODO: Parse something like fencepost.gnu.org:/gd/gnuorg/packages-ftp.
224 (let-values (((server directory) (ftp-server/directory project)))
225 (define conn (ftp-open server))
227 (let loop ((directories (list directory))
233 ((directory rest ...)
234 (let* ((files (ftp-list conn directory))
235 (subdirs (filter-map (match-lambda
236 ((name 'directory . _) name)
239 (loop (append (map (cut string-append directory "/" <>)
243 ;; Filter out signatures, deltas, and files which
244 ;; are potentially not releases of PROJECT--e.g.,
245 ;; in /gnu/guile, filter out guile-oops and
246 ;; guile-www; in mit-scheme, filter out binaries.
247 (filter-map (match-lambda
249 (and=> (release-file project file)
250 (cut cons <> directory)))
255 (define* (latest-release project
256 #:key (ftp-open ftp-open) (ftp-close ftp-close))
257 "Return (\"FOO-X.Y\" . \"/bar/foo\") or #f. Use FTP-OPEN and FTP-CLOSE to
258 open (resp. close) FTP connections; this can be useful to reuse connections."
260 (if (version>? a b) a b))
262 (define contains-digit?
263 (cut string-any char-set:digit <>))
265 (let-values (((server directory) (ftp-server/directory project)))
266 (define conn (ftp-open server))
268 (let loop ((directory directory))
269 (let* ((entries (ftp-list conn directory))
271 ;; Filter out sub-directories that do not contain digits---e.g.,
272 ;; /gnuzilla/lang and /gnupg/patches.
273 (subdirs (filter-map (match-lambda
274 (((? contains-digit? dir) 'directory . _)
280 ;; No sub-directories, so assume that tarballs are here.
281 (let ((files (filter-map (match-lambda
283 (release-file project file))
287 (and=> (reduce latest #f files)
288 (cut cons <> directory))))
290 ;; Assume that SUBDIRS correspond to versions, and jump into the
291 ;; one with the highest version number.
292 (let ((target (reduce latest #f subdirs)))
294 (loop (string-append directory "/" target))
299 (define %package-name-rx
300 ;; Regexp for a package name, e.g., "foo-X.Y". Since TeXmacs uses
301 ;; "TeXmacs-X.Y-src", the `-src' suffix is allowed.
302 (make-regexp "^(.*)-(([0-9]|\\.)+)(-src)?"))
304 (define (gnu-package-name->name+version name+version)
305 "Return the package name and version number extracted from NAME+VERSION."
306 (let ((match (regexp-exec %package-name-rx name+version)))
308 (values name+version #f)
309 (values (match:substring match 1) (match:substring match 2)))))
316 (define (package-update-path package)
317 "Return an update path for PACKAGE, or #f if no update is needed."
318 (and (gnu-package? package)
319 (match (latest-release (package-name package))
320 ((name+version . directory)
321 (let-values (((_ new-version)
322 (package-name->name+version name+version)))
323 (and (version>? name+version (package-full-name package))
324 `(,new-version . ,directory))))
327 (define* (download-tarball store project directory version
328 #:key (archive-type "gz")
329 (key-download 'interactive))
330 "Download PROJECT's tarball over FTP and check its OpenPGP signature. On
331 success, return the tarball file name. KEY-DOWNLOAD specifies a download
332 policy for missing OpenPGP keys; allowed values: 'interactive' (default),
333 'always', and 'never'."
334 (let* ((server (ftp-server/directory project))
335 (base (string-append project "-" version ".tar." archive-type))
336 (url (string-append "ftp://" server "/" directory "/" base))
337 (sig-url (string-append url ".sig"))
338 (tarball (download-to-store store url))
339 (sig (download-to-store store sig-url)))
340 (let ((ret (gnupg-verify* sig tarball #:key-download key-download)))
344 (warning (_ "signature verification failed for `~a'~%")
346 (warning (_ "(could be because the public key is not in your keyring)~%"))
349 (define* (package-update store package #:key (key-download 'interactive))
350 "Return the new version and the file name of the new version tarball for
351 PACKAGE, or #f and #f when PACKAGE is up-to-date. KEY-DOWNLOAD specifies a
352 download policy for missing OpenPGP keys; allowed values: 'always', 'never',
353 and 'interactive' (default)."
354 (match (package-update-path package)
355 ((version . directory)
357 (package-name package))
359 (let ((source (package-source package)))
360 (or (and (origin? source)
361 (file-extension (origin-uri source)))
363 (let ((tarball (download-tarball store name directory version
364 #:archive-type archive-type
365 #:key-download key-download)))
366 (values version tarball))))
370 (define (update-package-source package version hash)
371 "Modify the source file that defines PACKAGE to refer to VERSION,
372 whose tarball has SHA256 HASH (a bytevector). Return the new version string
373 if an update was made, and #f otherwise."
374 (define (new-line line matches replacement)
375 ;; Iterate over MATCHES and return the modified line based on LINE.
376 ;; Replace each match with REPLACEMENT.
377 (let loop ((m* matches) ; matches
382 (let ((r (cons (substring line o) r)))
383 (string-concatenate-reverse r)))
388 (substring line o (match:start m))
391 (define (update-source file old-version version
393 ;; Update source file FILE, replacing occurrences OLD-VERSION by VERSION
394 ;; and occurrences of OLD-HASH by HASH (base32 representation thereof).
396 ;; TODO: Currently this is a bit of a sledgehammer: if VERSION occurs in
397 ;; different unrelated places, we may modify it more than needed, for
398 ;; instance. We should try to make changes only within the sexp that
399 ;; corresponds to the definition of PACKAGE.
400 (let ((old-hash (bytevector->nix-base32-string old-hash))
401 (hash (bytevector->nix-base32-string hash)))
403 `((,(regexp-quote old-version)
404 . ,(cut new-line <> <> version))
405 (,(regexp-quote old-hash)
406 . ,(cut new-line <> <> hash))))
409 (let ((name (package-name package))
410 (loc (package-field-location package 'version)))
412 (let ((old-version (package-version package))
413 (old-hash (origin-sha256 (package-source package)))
414 (file (and=> (location-file loc)
415 (cut search-path %load-path <>))))
421 (warning (_ "~a: could not locate source file")
425 (format (current-error-port)
426 (_ "~a: ~a: no `version' field in source; skipping~%")
427 (location->string (package-location package))
430 ;;; gnu-maintenance.scm ends here