ui: Add `args-fold*' and use it.
[jackhill/guix/guix.git] / guix / scripts / refresh.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (guix scripts refresh)
20 #:use-module (guix ui)
21 #:use-module (guix store)
22 #:use-module (guix utils)
23 #:use-module (guix packages)
24 #:use-module (guix gnu-maintenance)
25 #:use-module (gnu packages)
26 #:use-module ((gnu packages base) #:select (%final-inputs))
27 #:use-module (ice-9 match)
28 #:use-module (ice-9 regex)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-11)
31 #:use-module (srfi srfi-26)
32 #:use-module (srfi srfi-37)
33 #:use-module (rnrs io ports)
34 #:export (guix-refresh))
35
36 \f
37 ;;;
38 ;;; Command-line options.
39 ;;;
40
41 (define %default-options
42 ;; Alist of default option values.
43 '())
44
45 (define %options
46 ;; Specification of the command-line options.
47 (list (option '(#\n "dry-run") #f #f
48 (lambda (opt name arg result)
49 (alist-cons 'dry-run? #t result)))
50 (option '(#\s "select") #t #f
51 (lambda (opt name arg result)
52 (match arg
53 ((or "core" "non-core")
54 (alist-cons 'select (string->symbol arg)
55 result))
56 (x
57 (leave (_ "~a: invalid selection; expected `core' or `non-core'")
58 arg)))))
59
60 (option '(#\h "help") #f #f
61 (lambda args
62 (show-help)
63 (exit 0)))
64 (option '(#\V "version") #f #f
65 (lambda args
66 (show-version-and-exit "guix refresh")))))
67
68 (define (show-help)
69 (display (_ "Usage: guix refresh [OPTION]... PACKAGE...
70 Update package definitions to match the latest upstream version.
71
72 When PACKAGE... is given, update only the specified packages. Otherwise
73 update all the packages of the distribution, or the subset thereof
74 specified with `--select'.\n"))
75 (display (_ "
76 -n, --dry-run do not build the derivations"))
77 (display (_ "
78 -s, --select=SUBSET select all the packages in SUBSET, one of
79 `core' or `non-core'"))
80 (newline)
81 (display (_ "
82 -h, --help display this help and exit"))
83 (display (_ "
84 -V, --version display version information and exit"))
85 (newline)
86 (show-bug-report-information))
87
88 \f
89 ;;;
90 ;;; Entry point.
91 ;;;
92
93 (define (guix-refresh . args)
94 (define (parse-options)
95 ;; Return the alist of option values.
96 (args-fold* args %options
97 (lambda (opt name arg result)
98 (leave (_ "~A: unrecognized option~%") name))
99 (lambda (arg result)
100 (alist-cons 'argument arg result))
101 %default-options))
102
103 (define core-package?
104 (let* ((input->package (match-lambda
105 ((name (? package? package) _ ...) package)
106 (_ #f)))
107 (final-inputs (map input->package %final-inputs))
108 (core (append final-inputs
109 (append-map (compose (cut filter-map input->package <>)
110 package-transitive-inputs)
111 final-inputs)))
112 (names (delete-duplicates (map package-name core))))
113 (lambda (package)
114 "Return true if PACKAGE is likely a \"core package\"---i.e., one whose
115 update would trigger a complete rebuild."
116 ;; Compare by name because packages in base.scm basically inherit
117 ;; other packages. So, even if those packages are not core packages
118 ;; themselves, updating them would also update those who inherit from
119 ;; them.
120 ;; XXX: Fails to catch MPFR/MPC, whose *source* is used as input.
121 (member (package-name package) names))))
122
123 (let* ((opts (parse-options))
124 (dry-run? (assoc-ref opts 'dry-run?))
125 (packages (match (concatenate
126 (filter-map (match-lambda
127 (('argument . value)
128 (let ((p (find-packages-by-name value)))
129 (unless p
130 (leave (_ "~a: no package by that name")
131 value))
132 p))
133 (_ #f))
134 opts))
135 (() ; default to all packages
136 (let ((select? (match (assoc-ref opts 'select)
137 ('core core-package?)
138 ('non-core (negate core-package?))
139 (_ (const #t)))))
140 ;; TODO: Keep only the newest of each package.
141 (fold-packages (lambda (package result)
142 (if (select? package)
143 (cons package result)
144 result))
145 '())))
146 (some ; user-specified packages
147 some))))
148 (with-error-handling
149 (if dry-run?
150 (for-each (lambda (package)
151 (match (false-if-exception (package-update-path package))
152 ((new-version . directory)
153 (let ((loc (or (package-field-location package 'version)
154 (package-location package))))
155 (format (current-error-port)
156 (_ "~a: ~a would be upgraded from ~a to ~a~%")
157 (location->string loc)
158 (package-name package) (package-version package)
159 new-version)))
160 (_ #f)))
161 packages)
162 (let ((store (open-connection)))
163 (for-each (lambda (package)
164 (let-values (((version tarball)
165 (catch #t
166 (lambda ()
167 (package-update store package))
168 (lambda _
169 (values #f #f))))
170 ((loc)
171 (or (package-field-location package
172 'version)
173 (package-location package))))
174 (when version
175 (format (current-error-port)
176 (_ "~a: ~a: updating from version ~a to version ~a...~%")
177 (location->string loc) (package-name package)
178 (package-version package) version)
179 (let ((hash (call-with-input-file tarball
180 (compose sha256 get-bytevector-all))))
181 (update-package-source package version hash)))))
182 packages))))))