1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 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 scripts refresh)
21 #:use-module (guix ui)
22 #:use-module (guix hash)
23 #:use-module (guix store)
24 #:use-module (guix utils)
25 #:use-module (guix packages)
26 #:use-module (guix gnu-maintenance)
27 #:use-module (guix gnupg)
28 #:use-module (gnu packages)
29 #:use-module ((gnu packages base) #:select (%final-inputs))
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 regex)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-11)
34 #:use-module (srfi srfi-26)
35 #:use-module (srfi srfi-37)
36 #:use-module (rnrs io ports)
37 #:export (guix-refresh))
41 ;;; Command-line options.
44 (define %default-options
45 ;; Alist of default option values.
49 ;; Specification of the command-line options.
50 (list (option '(#\u "update") #f #f
51 (lambda (opt name arg result)
52 (alist-cons 'update? #t result)))
53 (option '(#\s "select") #t #f
54 (lambda (opt name arg result)
56 ((or "core" "non-core")
57 (alist-cons 'select (string->symbol arg)
60 (leave (_ "~a: invalid selection; expected `core' or `non-core'")
63 (option '("key-server") #t #f
64 (lambda (opt name arg result)
65 (alist-cons 'key-server arg result)))
66 (option '("gpg") #t #f
67 (lambda (opt name arg result)
68 (alist-cons 'gpg-command arg result)))
69 (option '("key-download") #t #f
70 (lambda (opt name arg result)
72 ((or "interactive" "always" "never")
73 (alist-cons 'key-download (string->symbol arg)
76 (leave (_ "unsupported policy: ~a~%")
79 (option '(#\h "help") #f #f
83 (option '(#\V "version") #f #f
85 (show-version-and-exit "guix refresh")))))
88 (display (_ "Usage: guix refresh [OPTION]... PACKAGE...
89 Update package definitions to match the latest upstream version.
91 When PACKAGE... is given, update only the specified packages. Otherwise
92 update all the packages of the distribution, or the subset thereof
93 specified with `--select'.\n"))
95 -u, --update update source files in place"))
97 -s, --select=SUBSET select all the packages in SUBSET, one of
98 `core' or `non-core'"))
101 --key-server=HOST use HOST as the OpenPGP key server"))
103 --gpg=COMMAND use COMMAND as the GnuPG 2.x command"))
105 --key-download=POLICY
106 handle missing OpenPGP keys according to POLICY:
107 'always', 'never', and 'interactive', which is also
108 used when 'key-download' is not specified"))
111 -h, --help display this help and exit"))
113 -V, --version display version information and exit"))
115 (show-bug-report-information))
117 (define* (update-package store package #:key (key-download 'interactive))
118 "Update the source file that defines PACKAGE with the new version.
119 KEY-DOWNLOAD specifies a download policy for missing OpenPGP keys; allowed
120 values: 'interactive' (default), 'always', and 'never'."
121 (let-values (((version tarball)
124 (package-update store package #:key-download key-download))
128 (or (package-field-location package
130 (package-location package))))
132 (if (and=> tarball file-exists?)
134 (format (current-error-port)
135 (_ "~a: ~a: updating from version ~a to version ~a...~%")
136 (location->string loc)
137 (package-name package)
138 (package-version package) version)
139 (let ((hash (call-with-input-file tarball
141 (update-package-source package version hash)))
142 (warning (_ "~a: version ~a could not be \
143 downloaded and authenticated; not updating")
144 (package-name package) version)))))
152 (define (guix-refresh . args)
153 (define (parse-options)
154 ;; Return the alist of option values.
155 (args-fold* args %options
156 (lambda (opt name arg result)
157 (leave (_ "~A: unrecognized option~%") name))
159 (alist-cons 'argument arg result))
162 (define (keep-newest package lst)
163 ;; If a newer version of PACKAGE is already in LST, return LST; otherwise
164 ;; return LST minus the other version of PACKAGE in it, plus PACKAGE.
165 (let ((name (package-name package)))
166 (match (find (lambda (p)
167 (string=? (package-name p) name))
170 (if (version>? (package-version other) (package-version package))
172 (cons package (delq other lst))))
174 (cons package lst)))))
176 (define core-package?
177 (let* ((input->package (match-lambda
178 ((name (? package? package) _ ...) package)
180 (final-inputs (map input->package %final-inputs))
181 (core (append final-inputs
182 (append-map (compose (cut filter-map input->package <>)
183 package-transitive-inputs)
185 (names (delete-duplicates (map package-name core))))
187 "Return true if PACKAGE is likely a \"core package\"---i.e., one whose
188 update would trigger a complete rebuild."
189 ;; Compare by name because packages in base.scm basically inherit
190 ;; other packages. So, even if those packages are not core packages
191 ;; themselves, updating them would also update those who inherit from
193 ;; XXX: Fails to catch MPFR/MPC, whose *source* is used as input.
194 (member (package-name package) names))))
196 (let* ((opts (parse-options))
197 (update? (assoc-ref opts 'update?))
198 (key-download (assoc-ref opts 'key-download))
201 (filter-map (match-lambda
203 (let ((p (find-packages-by-name value)))
205 (leave (_ "~a: no package by that name")
210 (() ; default to all packages
211 (let ((select? (match (assoc-ref opts 'select)
212 ('core core-package?)
213 ('non-core (negate core-package?))
215 (fold-packages (lambda (package result)
216 (if (select? package)
217 (keep-newest package result)
220 (some ; user-specified packages
224 (let ((store (open-connection)))
225 (parameterize ((%openpgp-key-server
226 (or (assoc-ref opts 'key-server)
227 (%openpgp-key-server)))
229 (or (assoc-ref opts 'gpg-command)
232 (cut update-package store <> #:key-download key-download)
234 (for-each (lambda (package)
235 (match (false-if-exception (package-update-path package))
236 ((new-version . directory)
237 (let ((loc (or (package-field-location package 'version)
238 (package-location package))))
239 (format (current-error-port)
240 (_ "~a: ~a would be upgraded from ~a to ~a~%")
241 (location->string loc)
242 (package-name package) (package-version package)