Commit | Line | Data |
---|---|---|
233e7676 | 1 | ;;; GNU Guix --- Functional package management for GNU |
74d862e8 | 2 | ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org> |
e49951eb | 3 | ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org> |
98eb8cbe | 4 | ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org> |
88981dd3 AK |
5 | ;;; Copyright © 2014 Cyril Roelandt <tipecaml@gmail.com> |
6 | ;;; Copyright © 2014 Cyrill Schenkel <cyrill.schenkel@gmail.com> | |
caa6732e | 7 | ;;; Copyright © 2014, 2015 Alex Kost <alezost@gmail.com> |
88981dd3 | 8 | ;;; Copyright © 2015 David Thompson <davet@gnu.org> |
cf5f2ad3 | 9 | ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org> |
e95ae7c2 RJ |
10 | ;;; Copyright © 2016 Roel Janssen <roel@gnu.org> |
11 | ;;; Copyright © 2016 Benz Schenk <benz.schenk@uzh.ch> | |
073c34d7 | 12 | ;;; |
233e7676 | 13 | ;;; This file is part of GNU Guix. |
073c34d7 | 14 | ;;; |
233e7676 | 15 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
073c34d7 LC |
16 | ;;; under the terms of the GNU General Public License as published by |
17 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
18 | ;;; your option) any later version. | |
19 | ;;; | |
233e7676 | 20 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
073c34d7 LC |
21 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | ;;; GNU General Public License for more details. | |
24 | ;;; | |
25 | ;;; You should have received a copy of the GNU General Public License | |
233e7676 | 26 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
073c34d7 LC |
27 | |
28 | (define-module (guix ui) | |
29 | #:use-module (guix utils) | |
30 | #:use-module (guix store) | |
cdd5d6f9 | 31 | #:use-module (guix config) |
073c34d7 | 32 | #:use-module (guix packages) |
5d7a8584 | 33 | #:use-module (guix profiles) |
9bb2b96a | 34 | #:use-module (guix derivations) |
958dd3ce | 35 | #:use-module (guix combinators) |
b7071bc5 LC |
36 | #:use-module (guix build-system) |
37 | #:use-module (guix serialization) | |
59758816 | 38 | #:use-module ((guix build utils) #:select (mkdir-p)) |
299112d3 | 39 | #:use-module ((guix licenses) #:select (license? license-name)) |
9703fef4 | 40 | #:use-module ((guix build syscalls) #:select (terminal-columns)) |
299112d3 LC |
41 | #:use-module (srfi srfi-1) |
42 | #:use-module (srfi srfi-11) | |
2cd09108 | 43 | #:use-module (srfi srfi-19) |
073c34d7 | 44 | #:use-module (srfi srfi-26) |
2abcc97f | 45 | #:use-module (srfi srfi-31) |
073c34d7 | 46 | #:use-module (srfi srfi-34) |
c1d52c71 | 47 | #:use-module (srfi srfi-35) |
e31ff8b8 | 48 | #:autoload (ice-9 ftw) (scandir) |
64fc89b6 | 49 | #:use-module (ice-9 match) |
9bb2b96a | 50 | #:use-module (ice-9 format) |
2cd09108 | 51 | #:use-module (ice-9 regex) |
db030303 LC |
52 | #:autoload (system repl repl) (start-repl) |
53 | #:autoload (system repl debug) (make-debug stack->vector) | |
1cd4027c ML |
54 | #:use-module (texinfo) |
55 | #:use-module (texinfo plain-text) | |
56 | #:use-module (texinfo string-utils) | |
073c34d7 LC |
57 | #:export (_ |
58 | N_ | |
ee764179 | 59 | P_ |
70e629f5 | 60 | report-error |
073c34d7 | 61 | leave |
7ea1432e DT |
62 | make-user-module |
63 | load* | |
4ae7559f | 64 | warn-about-load-error |
cdd5d6f9 | 65 | show-version-and-exit |
3441e164 | 66 | show-bug-report-information |
fd688c82 | 67 | make-regexp* |
969e678e | 68 | string->number* |
1d6243cf | 69 | size->number |
fa394eb9 | 70 | show-derivation-outputs |
9bb2b96a | 71 | show-what-to-build |
4d043ab6 | 72 | show-what-to-build* |
5d7a8584 | 73 | show-manifest-transaction |
073c34d7 | 74 | call-with-error-handling |
64fc89b6 | 75 | with-error-handling |
df36e629 | 76 | leave-on-EPIPE |
ac5de156 | 77 | read/eval |
eb0880e7 | 78 | read/eval-package-expression |
299112d3 | 79 | location->string |
f651b477 | 80 | config-directory |
299112d3 | 81 | fill-paragraph |
2748ee3b | 82 | texi->plain-text |
1cd4027c | 83 | package-description-string |
299112d3 | 84 | string->recutils |
e49951eb | 85 | package->recutils |
2876b989 | 86 | package-specification->name+version+output |
2cd09108 NK |
87 | string->generations |
88 | string->duration | |
e49de93a | 89 | matching-generations |
ad18c7e6 LC |
90 | display-generation |
91 | display-profile-content | |
e95ae7c2 | 92 | display-profile-content-diff |
06d45f45 LC |
93 | roll-back* |
94 | switch-to-generation* | |
95 | delete-generation* | |
e49951eb | 96 | run-guix-command |
caa6732e | 97 | run-guix |
a2011be5 LC |
98 | program-name |
99 | guix-warning-port | |
100 | warning | |
240b57f0 | 101 | info |
e49951eb | 102 | guix-main)) |
073c34d7 LC |
103 | |
104 | ;;; Commentary: | |
105 | ;;; | |
106 | ;;; User interface facilities for command-line tools. | |
107 | ;;; | |
108 | ;;; Code: | |
109 | ||
110 | (define %gettext-domain | |
ee764179 | 111 | ;; Text domain for strings used in the tools. |
073c34d7 LC |
112 | "guix") |
113 | ||
ee764179 LC |
114 | (define %package-text-domain |
115 | ;; Text domain for package synopses and descriptions. | |
116 | "guix-packages") | |
117 | ||
073c34d7 LC |
118 | (define _ (cut gettext <> %gettext-domain)) |
119 | (define N_ (cut ngettext <> <> <> %gettext-domain)) | |
a5c0d8bc LC |
120 | |
121 | (define (P_ msgid) | |
122 | "Return the translation of the package description or synopsis MSGID." | |
123 | ;; Descriptions/synopses might occasionally be empty strings, even if that | |
124 | ;; is something we try to avoid. Since (gettext "") can return a non-empty | |
125 | ;; string, explicitly check for that case. | |
126 | (if (string-null? msgid) | |
127 | msgid | |
128 | (gettext msgid %package-text-domain))) | |
073c34d7 | 129 | |
b2a886f6 LC |
130 | (define-syntax-rule (define-diagnostic name prefix) |
131 | "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all | |
132 | messages." | |
133 | (define-syntax name | |
134 | (lambda (x) | |
135 | (define (augmented-format-string fmt) | |
136 | (string-append "~:[~*~;guix ~a: ~]~a" (syntax->datum fmt))) | |
137 | ||
89d02b98 LC |
138 | (syntax-case x () |
139 | ((name (underscore fmt) args (... ...)) | |
140 | (and (string? (syntax->datum #'fmt)) | |
141 | (free-identifier=? #'underscore #'_)) | |
b2a886f6 LC |
142 | (with-syntax ((fmt* (augmented-format-string #'fmt)) |
143 | (prefix (datum->syntax x prefix))) | |
144 | #'(format (guix-warning-port) (gettext fmt*) | |
145 | (program-name) (program-name) prefix | |
146 | args (... ...)))) | |
89d02b98 | 147 | ((name (N-underscore singular plural n) args (... ...)) |
b2a886f6 | 148 | (and (string? (syntax->datum #'singular)) |
89d02b98 LC |
149 | (string? (syntax->datum #'plural)) |
150 | (free-identifier=? #'N-underscore #'N_)) | |
b2a886f6 LC |
151 | (with-syntax ((s (augmented-format-string #'singular)) |
152 | (p (augmented-format-string #'plural)) | |
153 | (prefix (datum->syntax x prefix))) | |
154 | #'(format (guix-warning-port) | |
155 | (ngettext s p n %gettext-domain) | |
156 | (program-name) (program-name) prefix | |
157 | args (... ...)))))))) | |
158 | ||
159 | (define-diagnostic warning "warning: ") ; emit a warning | |
240b57f0 | 160 | (define-diagnostic info "") |
b2a886f6 LC |
161 | |
162 | (define-diagnostic report-error "error: ") | |
163 | (define-syntax-rule (leave args ...) | |
164 | "Emit an error message and exit." | |
165 | (begin | |
166 | (report-error args ...) | |
167 | (exit 1))) | |
168 | ||
7ea1432e DT |
169 | (define (make-user-module modules) |
170 | "Return a new user module with the additional MODULES loaded." | |
171 | ;; Module in which the machine description file is loaded. | |
172 | (let ((module (make-fresh-user-module))) | |
173 | (for-each (lambda (iface) | |
174 | (module-use! module (resolve-interface iface))) | |
175 | modules) | |
176 | module)) | |
177 | ||
db030303 LC |
178 | (define* (load* file user-module |
179 | #:key (on-error 'nothing-special)) | |
7ea1432e | 180 | "Load the user provided Scheme source code FILE." |
2abcc97f LC |
181 | (define (frame-with-source frame) |
182 | ;; Walk from FRAME upwards until source location information is found. | |
183 | (let loop ((frame frame) | |
184 | (previous frame)) | |
185 | (if (not frame) | |
186 | previous | |
187 | (if (frame-source frame) | |
188 | frame | |
189 | (loop (frame-previous frame) frame))))) | |
190 | ||
db030303 LC |
191 | (define (error-string frame args) |
192 | (call-with-output-string | |
193 | (lambda (port) | |
194 | (apply display-error frame port (cdr args))))) | |
195 | ||
196 | (define tag | |
197 | (make-prompt-tag "user-code")) | |
198 | ||
7ea1432e DT |
199 | (catch #t |
200 | (lambda () | |
2abcc97f | 201 | ;; XXX: Force a recompilation to avoid ABI issues. |
7ea1432e | 202 | (set! %fresh-auto-compile #t) |
2abcc97f | 203 | (set! %load-should-auto-compile #t) |
7ea1432e DT |
204 | |
205 | (save-module-excursion | |
206 | (lambda () | |
207 | (set-current-module user-module) | |
7ea1432e | 208 | |
2abcc97f LC |
209 | ;; Hide the "auto-compiling" messages. |
210 | (parameterize ((current-warning-port (%make-void-port "w"))) | |
db030303 LC |
211 | (call-with-prompt tag |
212 | (lambda () | |
213 | ;; Give 'load' an absolute file name so that it doesn't try to | |
214 | ;; search for FILE in %LOAD-PATH. Note: use 'load', not | |
215 | ;; 'primitive-load', so that FILE is compiled, which then allows us | |
216 | ;; to provide better error reporting with source line numbers. | |
217 | (load (canonicalize-path file))) | |
218 | (const #f)))))) | |
2abcc97f LC |
219 | (lambda _ |
220 | ;; XXX: Errors are reported from the pre-unwind handler below, but | |
221 | ;; calling 'exit' from there has no effect, so we call it here. | |
222 | (exit 1)) | |
223 | (rec (handle-error . args) | |
224 | ;; Capture the stack up to this procedure call, excluded, and pass | |
225 | ;; the faulty stack frame to 'report-load-error'. | |
db030303 | 226 | (let* ((stack (make-stack #t handle-error tag)) |
2abcc97f LC |
227 | (depth (stack-length stack)) |
228 | (last (and (> depth 0) (stack-ref stack 0))) | |
229 | (frame (frame-with-source | |
230 | (if (> depth 1) | |
231 | (stack-ref stack 1) ;skip the 'throw' frame | |
232 | last)))) | |
db030303 LC |
233 | |
234 | (report-load-error file args frame) | |
235 | ||
236 | (case on-error | |
237 | ((debug) | |
238 | (newline) | |
239 | (display (_ "entering debugger; type ',bt' for a backtrace\n")) | |
240 | (start-repl #:debug (make-debug (stack->vector stack) 0 | |
241 | (error-string frame args) | |
242 | #f))) | |
243 | ((backtrace) | |
244 | (newline (current-error-port)) | |
245 | (display-backtrace stack (current-error-port))) | |
246 | (else | |
247 | #t)))))) | |
2abcc97f LC |
248 | |
249 | (define* (report-load-error file args #:optional frame) | |
db030303 | 250 | "Report the failure to load FILE, a user-provided Scheme file. |
1151f6ae LC |
251 | ARGS is the list of arguments received by the 'throw' handler." |
252 | (match args | |
e465d9e1 | 253 | (('system-error . rest) |
1151f6ae | 254 | (let ((err (system-error-errno args))) |
db030303 | 255 | (report-error (_ "failed to load '~a': ~a~%") file (strerror err)))) |
1151f6ae LC |
256 | (('syntax-error proc message properties form . rest) |
257 | (let ((loc (source-properties->location properties))) | |
258 | (format (current-error-port) (_ "~a: error: ~a~%") | |
db030303 | 259 | (location->string loc) message))) |
23185cea LC |
260 | (('srfi-34 obj) |
261 | (report-error (_ "exception thrown: ~s~%") obj)) | |
1151f6ae LC |
262 | ((error args ...) |
263 | (report-error (_ "failed to load '~a':~%") file) | |
db030303 | 264 | (apply display-error frame (current-error-port) args)))) |
1151f6ae | 265 | |
4ae7559f LC |
266 | (define (warn-about-load-error file args) ;FIXME: factorize with ↑ |
267 | "Report the failure to load FILE, a user-provided Scheme file, without | |
268 | exiting. ARGS is the list of arguments received by the 'throw' handler." | |
269 | (match args | |
e465d9e1 | 270 | (('system-error . rest) |
4ae7559f LC |
271 | (let ((err (system-error-errno args))) |
272 | (warning (_ "failed to load '~a': ~a~%") file (strerror err)))) | |
273 | (('syntax-error proc message properties form . rest) | |
274 | (let ((loc (source-properties->location properties))) | |
275 | (format (current-error-port) (_ "~a: warning: ~a~%") | |
276 | (location->string loc) message))) | |
23185cea LC |
277 | (('srfi-34 obj) |
278 | (warning (_ "failed to load '~a': exception thrown: ~s~%") | |
279 | file obj)) | |
4ae7559f LC |
280 | ((error args ...) |
281 | (warning (_ "failed to load '~a':~%") file) | |
282 | (apply display-error #f (current-error-port) args)))) | |
283 | ||
b2a886f6 LC |
284 | (define (install-locale) |
285 | "Install the current locale settings." | |
286 | (catch 'system-error | |
287 | (lambda _ | |
288 | (setlocale LC_ALL "")) | |
289 | (lambda args | |
290 | (warning (_ "failed to install locale: ~a~%") | |
291 | (strerror (system-error-errno args)))))) | |
292 | ||
e49951eb | 293 | (define (initialize-guix) |
633f045f | 294 | "Perform the usual initialization for stand-alone Guix commands." |
e49951eb | 295 | (install-locale) |
39e9f95d | 296 | (textdomain %gettext-domain) |
e14c3929 LC |
297 | |
298 | ;; Ignore SIGPIPE. If the daemon closes the connection, we prefer to be | |
299 | ;; notified via an EPIPE later. | |
300 | (sigaction SIGPIPE SIG_IGN) | |
301 | ||
e49951eb MW |
302 | (setvbuf (current-output-port) _IOLBF) |
303 | (setvbuf (current-error-port) _IOLBF)) | |
304 | ||
cdd5d6f9 LC |
305 | (define* (show-version-and-exit #:optional (command (car (command-line)))) |
306 | "Display version information for COMMAND and `(exit 0)'." | |
307 | (simple-format #t "~a (~a) ~a~%" | |
308 | command %guix-package-name %guix-version) | |
74d862e8 | 309 | (display (_ "Copyright (C) 2017 the Guix authors |
64a967cc LC |
310 | License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> |
311 | This is free software: you are free to change and redistribute it. | |
312 | There is NO WARRANTY, to the extent permitted by law. | |
313 | ")) | |
cdd5d6f9 LC |
314 | (exit 0)) |
315 | ||
3441e164 | 316 | (define (show-bug-report-information) |
cf5f2ad3 ML |
317 | ;; TRANSLATORS: The placeholder indicates the bug-reporting address for this |
318 | ;; package. Please add another line saying "Report translation bugs to | |
319 | ;; ...\n" with the address for translation bugs (typically your translation | |
320 | ;; team's web or email address). | |
3441e164 LC |
321 | (format #t (_ " |
322 | Report bugs to: ~a.") %guix-bug-report-address) | |
323 | (format #t (_ " | |
324 | ~a home page: <~a>") %guix-package-name %guix-home-page-url) | |
325 | (display (_ " | |
326 | General help using GNU software: <http://www.gnu.org/gethelp/>")) | |
327 | (newline)) | |
328 | ||
7522a016 LC |
329 | (set! symlink |
330 | ;; We 'set!' the global binding because (gnu build ...) modules and similar | |
331 | ;; typically don't use (guix ui). | |
44fd6ef1 LC |
332 | (let ((real-symlink (@ (guile) symlink))) |
333 | (lambda (target link) | |
334 | "This is a 'symlink' replacement that provides proper error reporting." | |
335 | (catch 'system-error | |
336 | (lambda () | |
337 | (real-symlink target link)) | |
338 | (lambda (key proc fmt args errno) | |
339 | ;; Augment the FMT and ARGS with information about LINK (this | |
340 | ;; information is missing as of Guile 2.0.11, making the exception | |
341 | ;; uninformative.) | |
342 | (apply throw key proc "~A: ~S" | |
6ec1f4ca LC |
343 | (list (strerror (car errno)) link) |
344 | (list errno))))))) | |
44fd6ef1 | 345 | |
9b14107f LC |
346 | (set! copy-file |
347 | ;; Note: here we use 'set!', not #:replace, because UIs typically use | |
348 | ;; 'copy-recursively', which doesn't use (guix ui). | |
349 | (let ((real-copy-file (@ (guile) copy-file))) | |
350 | (lambda (source target) | |
351 | "This is a 'copy-file' replacement that provides proper error reporting." | |
352 | (catch 'system-error | |
353 | (lambda () | |
354 | (real-copy-file source target)) | |
355 | (lambda (key proc fmt args errno) | |
356 | ;; Augment the FMT and ARGS with information about TARGET (this | |
357 | ;; information is missing as of Guile 2.0.11, making the exception | |
358 | ;; uninformative.) | |
359 | (apply throw key proc "~A: ~S" | |
360 | (list (strerror (car errno)) target) | |
361 | (list errno))))))) | |
362 | ||
fd688c82 LC |
363 | (define (make-regexp* regexp . flags) |
364 | "Like 'make-regexp' but error out if REGEXP is invalid, reporting the error | |
365 | nicely." | |
366 | (catch 'regular-expression-syntax | |
367 | (lambda () | |
368 | (apply make-regexp regexp flags)) | |
369 | (lambda (key proc message . rest) | |
370 | (leave (_ "'~a' is not a valid regular expression: ~a~%") | |
371 | regexp message)))) | |
372 | ||
969e678e LC |
373 | (define (string->number* str) |
374 | "Like `string->number', but error out with an error message on failure." | |
375 | (or (string->number str) | |
376 | (leave (_ "~a: invalid number~%") str))) | |
377 | ||
1d6243cf LC |
378 | (define (size->number str) |
379 | "Convert STR, a storage measurement representation such as \"1024\" or | |
380 | \"1MiB\", to a number of bytes. Raise an error if STR could not be | |
381 | interpreted." | |
382 | (define unit-pos | |
383 | (string-rindex str char-set:digit)) | |
384 | ||
385 | (define unit | |
386 | (and unit-pos (substring str (+ 1 unit-pos)))) | |
387 | ||
388 | (let* ((numstr (if unit-pos | |
389 | (substring str 0 (+ 1 unit-pos)) | |
390 | str)) | |
391 | (num (string->number numstr))) | |
392 | (unless num | |
393 | (leave (_ "invalid number: ~a~%") numstr)) | |
394 | ||
395 | ((compose inexact->exact round) | |
396 | (* num | |
397 | (match unit | |
4a44d7bb LC |
398 | ((or "KiB" "K" "k") (expt 2 10)) |
399 | ((or "MiB" "M") (expt 2 20)) | |
400 | ((or "GiB" "G") (expt 2 30)) | |
401 | ((or "TiB" "T") (expt 2 40)) | |
402 | ((or "PiB" "P") (expt 2 50)) | |
403 | ((or "EiB" "E") (expt 2 60)) | |
404 | ((or "ZiB" "Z") (expt 2 70)) | |
405 | ((or "YiB" "Y") (expt 2 80)) | |
406 | ("kB" (expt 10 3)) | |
1d6243cf LC |
407 | ("MB" (expt 10 6)) |
408 | ("GB" (expt 10 9)) | |
409 | ("TB" (expt 10 12)) | |
4a44d7bb LC |
410 | ("PB" (expt 10 15)) |
411 | ("EB" (expt 10 18)) | |
412 | ("ZB" (expt 10 21)) | |
413 | ("YB" (expt 10 24)) | |
1d6243cf | 414 | ("" 1) |
e465d9e1 | 415 | (x |
1d6243cf LC |
416 | (leave (_ "unknown unit: ~a~%") unit))))))) |
417 | ||
073c34d7 LC |
418 | (define (call-with-error-handling thunk) |
419 | "Call THUNK within a user-friendly error handler." | |
bec7f352 LC |
420 | (define (port-filename* port) |
421 | ;; 'port-filename' returns #f for non-file ports, but it raises an | |
422 | ;; exception for file ports that are closed. Work around that. | |
423 | (and (not (port-closed? port)) | |
424 | (port-filename port))) | |
425 | ||
073c34d7 LC |
426 | (guard (c ((package-input-error? c) |
427 | (let* ((package (package-error-package c)) | |
428 | (input (package-error-invalid-input c)) | |
429 | (location (package-location package)) | |
430 | (file (location-file location)) | |
431 | (line (location-line location)) | |
432 | (column (location-column location))) | |
98eb8cbe | 433 | (leave (_ "~a:~a:~a: package `~a' has an invalid input: ~s~%") |
073c34d7 LC |
434 | file line column |
435 | (package-full-name package) input))) | |
9b222abe LC |
436 | ((package-cross-build-system-error? c) |
437 | (let* ((package (package-error-package c)) | |
438 | (loc (package-location package)) | |
439 | (system (package-build-system package))) | |
440 | (leave (_ "~a: ~a: build system `~a' does not support cross builds~%") | |
441 | (location->string loc) | |
442 | (package-full-name package) | |
443 | (build-system-name system)))) | |
c0c018f1 AK |
444 | ((profile-not-found-error? c) |
445 | (leave (_ "profile '~a' does not exist~%") | |
446 | (profile-error-profile c))) | |
447 | ((missing-generation-error? c) | |
448 | (leave (_ "generation ~a of profile '~a' does not exist~%") | |
449 | (missing-generation-error-generation c) | |
450 | (profile-error-profile c))) | |
b7071bc5 LC |
451 | ((nar-error? c) |
452 | (let ((file (nar-error-file c)) | |
453 | (port (nar-error-port c))) | |
454 | (if file | |
455 | (leave (_ "corrupt input while restoring '~a' from ~s~%") | |
bec7f352 | 456 | file (or (port-filename* port) port)) |
b7071bc5 | 457 | (leave (_ "corrupt input while restoring archive from ~s~%") |
bec7f352 | 458 | (or (port-filename* port) port))))) |
ef86c39f | 459 | ((nix-connection-error? c) |
98eb8cbe | 460 | (leave (_ "failed to connect to `~a': ~a~%") |
ef86c39f LC |
461 | (nix-connection-error-file c) |
462 | (strerror (nix-connection-error-code c)))) | |
073c34d7 LC |
463 | ((nix-protocol-error? c) |
464 | ;; FIXME: Server-provided error messages aren't i18n'd. | |
98eb8cbe | 465 | (leave (_ "build failed: ~a~%") |
c1d52c71 | 466 | (nix-protocol-error-message c))) |
f304c9c2 LC |
467 | ((derivation-missing-output-error? c) |
468 | (leave (_ "reference to invalid output '~a' of derivation '~a'~%") | |
469 | (derivation-missing-output c) | |
470 | (derivation-file-name (derivation-error-derivation c)))) | |
d26e1967 LC |
471 | ((file-search-error? c) |
472 | (leave (_ "file '~a' could not be found in these \ | |
473 | directories:~{ ~a~}~%") | |
474 | (file-search-error-file-name c) | |
475 | (file-search-error-search-path c))) | |
c1d52c71 LC |
476 | ((message-condition? c) |
477 | ;; Normally '&message' error conditions have an i18n'd message. | |
12703d08 LC |
478 | (leave (_ "~a~%") |
479 | (gettext (condition-message c) %gettext-domain)))) | |
e14c3929 LC |
480 | ;; Catch EPIPE and the likes. |
481 | (catch 'system-error | |
482 | thunk | |
bde8c0e6 LC |
483 | (lambda (key proc format-string format-args . rest) |
484 | (leave (_ "~a: ~a~%") proc | |
485 | (apply format #f format-string format-args)))))) | |
073c34d7 | 486 | |
df36e629 LC |
487 | (define-syntax-rule (leave-on-EPIPE exp ...) |
488 | "Run EXP... in a context when EPIPE errors are caught and lead to 'exit' | |
489 | with successful exit code. This is useful when writing to the standard output | |
490 | may lead to EPIPE, because the standard output is piped through 'head' or | |
491 | similar." | |
492 | (catch 'system-error | |
493 | (lambda () | |
494 | exp ...) | |
495 | (lambda args | |
496 | ;; We really have to exit this brutally, otherwise Guile eventually | |
497 | ;; attempts to flush all the ports, leading to an uncaught EPIPE down | |
498 | ;; the path. | |
499 | (if (= EPIPE (system-error-errno args)) | |
500 | (primitive-_exit 0) | |
501 | (apply throw args))))) | |
502 | ||
56b82106 LC |
503 | (define %guix-user-module |
504 | ;; Module in which user expressions are evaluated. | |
07254feb LC |
505 | ;; Compute lazily to avoid circularity with (guix gexp). |
506 | (delay | |
507 | (let ((module (make-module))) | |
508 | (beautify-user-module! module) | |
509 | ;; Use (guix gexp) so that one can use #~ & co. | |
510 | (module-use! module (resolve-interface '(guix gexp))) | |
511 | module))) | |
56b82106 | 512 | |
ac5de156 LC |
513 | (define (read/eval str) |
514 | "Read and evaluate STR, raising an error if something goes wrong." | |
eb0880e7 LC |
515 | (let ((exp (catch #t |
516 | (lambda () | |
517 | (call-with-input-string str read)) | |
518 | (lambda args | |
519 | (leave (_ "failed to read expression ~s: ~s~%") | |
520 | str args))))) | |
ac5de156 LC |
521 | (catch #t |
522 | (lambda () | |
07254feb | 523 | (eval exp (force %guix-user-module))) |
ac5de156 | 524 | (lambda args |
41766807 LC |
525 | (report-error (_ "failed to evaluate expression '~a':~%") exp) |
526 | (match args | |
527 | (('syntax-error proc message properties form . rest) | |
528 | (report-error (_ "syntax error: ~a~%") message)) | |
23185cea LC |
529 | (('srfi-34 obj) |
530 | (report-error (_ "exception thrown: ~s~%") obj)) | |
41766807 LC |
531 | ((error args ...) |
532 | (apply display-error #f (current-error-port) args)) | |
533 | (what? #f)) | |
534 | (exit 1))))) | |
ac5de156 LC |
535 | |
536 | (define (read/eval-package-expression str) | |
537 | "Read and evaluate STR and return the package it refers to, or exit an | |
538 | error." | |
539 | (match (read/eval str) | |
540 | ((? package? p) p) | |
e465d9e1 | 541 | (x |
ac5de156 LC |
542 | (leave (_ "expression ~s does not evaluate to a package~%") |
543 | str)))) | |
eb0880e7 | 544 | |
fa394eb9 AK |
545 | (define (show-derivation-outputs derivation) |
546 | "Show the output file names of DERIVATION." | |
547 | (format #t "~{~a~%~}" | |
548 | (map (match-lambda | |
549 | ((out-name . out) | |
550 | (derivation->output-path derivation out-name))) | |
551 | (derivation-outputs derivation)))) | |
552 | ||
dd36b51b | 553 | (define* (show-what-to-build store drv |
58c08df0 LC |
554 | #:key dry-run? (use-substitutes? #t) |
555 | (mode (build-mode normal))) | |
9bb2b96a | 556 | "Show what will or would (depending on DRY-RUN?) be built in realizing the |
58c08df0 LC |
557 | derivations listed in DRV using MODE, a 'build-mode' value. Return #t if |
558 | there's something to build, #f otherwise. When USE-SUBSTITUTES?, check and | |
559 | report what is prerequisites are available for download." | |
e9651e39 LC |
560 | (define substitutable? |
561 | ;; Call 'substitutation-oracle' upfront so we don't end up launching the | |
562 | ;; substituter many times. This makes a big difference, especially when | |
563 | ;; DRV is a long list as is the case with 'guix environment'. | |
564 | (if use-substitutes? | |
58c08df0 | 565 | (substitution-oracle store drv #:mode mode) |
e9651e39 LC |
566 | (const #f))) |
567 | ||
52ddf2ae | 568 | (define (built-or-substitutable? drv) |
f304c9c2 LC |
569 | (or (null? (derivation-outputs drv)) |
570 | (let ((out (derivation->output-path drv))) ;XXX: assume "out" exists | |
52ddf2ae | 571 | (or (valid-path? store out) |
e9651e39 | 572 | (substitutable? out))))) |
52ddf2ae | 573 | |
dd36b51b | 574 | (let*-values (((build download) |
59688fc4 LC |
575 | (fold2 (lambda (drv build download) |
576 | (let-values (((b d) | |
577 | (derivation-prerequisites-to-build | |
578 | store drv | |
58c08df0 | 579 | #:mode mode |
e9651e39 | 580 | #:substitutable? substitutable?))) |
59688fc4 LC |
581 | (values (append b build) |
582 | (append d download)))) | |
dd36b51b LC |
583 | '() '() |
584 | drv)) | |
585 | ((build) ; add the DRV themselves | |
586 | (delete-duplicates | |
59688fc4 | 587 | (append (map derivation-file-name |
52ddf2ae | 588 | (remove built-or-substitutable? drv)) |
dd36b51b LC |
589 | (map derivation-input-path build)))) |
590 | ((download) ; add the references of DOWNLOAD | |
1a8b7834 LC |
591 | (if use-substitutes? |
592 | (delete-duplicates | |
593 | (append download | |
594 | (remove (cut valid-path? store <>) | |
595 | (append-map | |
596 | substitutable-references | |
597 | (substitutable-path-info store | |
598 | download))))) | |
599 | download))) | |
00554b2a | 600 | ;; TODO: Show the installed size of DOWNLOAD. |
9bb2b96a | 601 | (if dry-run? |
dd36b51b LC |
602 | (begin |
603 | (format (current-error-port) | |
83e61a73 LC |
604 | (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]" |
605 | "~:[The following derivations would be built:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
606 | (length build)) |
607 | (null? build) build) | |
608 | (format (current-error-port) | |
83e61a73 LC |
609 | (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]" |
610 | "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
611 | (length download)) |
612 | (null? download) download)) | |
613 | (begin | |
614 | (format (current-error-port) | |
83e61a73 LC |
615 | (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]" |
616 | "~:[The following derivations will be built:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
617 | (length build)) |
618 | (null? build) build) | |
619 | (format (current-error-port) | |
83e61a73 LC |
620 | (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]" |
621 | "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
622 | (length download)) |
623 | (null? download) download))) | |
624 | (pair? build))) | |
9bb2b96a | 625 | |
4d043ab6 DT |
626 | (define show-what-to-build* |
627 | (store-lift show-what-to-build)) | |
628 | ||
5d7a8584 AK |
629 | (define (right-arrow port) |
630 | "Return either a string containing the 'RIGHT ARROW' character, or an ASCII | |
631 | replacement if PORT is not Unicode-capable." | |
632 | (with-fluids ((%default-port-encoding (port-encoding port))) | |
633 | (let ((arrow "→")) | |
634 | (catch 'encoding-error | |
635 | (lambda () | |
636 | (call-with-output-string | |
637 | (lambda (port) | |
638 | (set-port-conversion-strategy! port 'error) | |
639 | (display arrow port)))) | |
640 | (lambda (key . args) | |
641 | "->"))))) | |
642 | ||
643 | (define* (show-manifest-transaction store manifest transaction | |
644 | #:key dry-run?) | |
645 | "Display what will/would be installed/removed from MANIFEST by TRANSACTION." | |
646 | (define (package-strings name version output item) | |
647 | (map (lambda (name version output item) | |
648 | (format #f " ~a~:[:~a~;~*~]\t~a\t~a" | |
649 | name | |
650 | (equal? output "out") output version | |
651 | (if (package? item) | |
652 | (package-output store item output) | |
653 | item))) | |
654 | name version output item)) | |
655 | ||
656 | (define → ;an arrow that can be represented on stderr | |
657 | (right-arrow (current-error-port))) | |
658 | ||
659 | (define (upgrade-string name old-version new-version output item) | |
660 | (format #f " ~a~:[:~a~;~*~]\t~a ~a ~a\t~a" | |
661 | name (equal? output "out") output | |
662 | old-version → new-version | |
663 | (if (package? item) | |
664 | (package-output store item output) | |
665 | item))) | |
666 | ||
46b23e1a | 667 | (let-values (((remove install upgrade downgrade) |
5d7a8584 AK |
668 | (manifest-transaction-effects manifest transaction))) |
669 | (match remove | |
670 | ((($ <manifest-entry> name version output item) ..1) | |
671 | (let ((len (length name)) | |
672 | (remove (package-strings name version output item))) | |
673 | (if dry-run? | |
674 | (format (current-error-port) | |
675 | (N_ "The following package would be removed:~%~{~a~%~}~%" | |
676 | "The following packages would be removed:~%~{~a~%~}~%" | |
677 | len) | |
678 | remove) | |
679 | (format (current-error-port) | |
680 | (N_ "The following package will be removed:~%~{~a~%~}~%" | |
681 | "The following packages will be removed:~%~{~a~%~}~%" | |
682 | len) | |
683 | remove)))) | |
684 | (_ #f)) | |
46b23e1a LC |
685 | (match downgrade |
686 | (((($ <manifest-entry> name old-version) | |
687 | . ($ <manifest-entry> _ new-version output item)) ..1) | |
688 | (let ((len (length name)) | |
689 | (downgrade (map upgrade-string | |
690 | name old-version new-version output item))) | |
691 | (if dry-run? | |
692 | (format (current-error-port) | |
693 | (N_ "The following package would be downgraded:~%~{~a~%~}~%" | |
694 | "The following packages would be downgraded:~%~{~a~%~}~%" | |
695 | len) | |
696 | downgrade) | |
697 | (format (current-error-port) | |
698 | (N_ "The following package will be downgraded:~%~{~a~%~}~%" | |
699 | "The following packages will be downgraded:~%~{~a~%~}~%" | |
700 | len) | |
701 | downgrade)))) | |
702 | (_ #f)) | |
5d7a8584 AK |
703 | (match upgrade |
704 | (((($ <manifest-entry> name old-version) | |
705 | . ($ <manifest-entry> _ new-version output item)) ..1) | |
706 | (let ((len (length name)) | |
707 | (upgrade (map upgrade-string | |
708 | name old-version new-version output item))) | |
709 | (if dry-run? | |
710 | (format (current-error-port) | |
711 | (N_ "The following package would be upgraded:~%~{~a~%~}~%" | |
712 | "The following packages would be upgraded:~%~{~a~%~}~%" | |
713 | len) | |
714 | upgrade) | |
715 | (format (current-error-port) | |
716 | (N_ "The following package will be upgraded:~%~{~a~%~}~%" | |
717 | "The following packages will be upgraded:~%~{~a~%~}~%" | |
718 | len) | |
719 | upgrade)))) | |
720 | (_ #f)) | |
721 | (match install | |
722 | ((($ <manifest-entry> name version output item _) ..1) | |
723 | (let ((len (length name)) | |
724 | (install (package-strings name version output item))) | |
725 | (if dry-run? | |
726 | (format (current-error-port) | |
727 | (N_ "The following package would be installed:~%~{~a~%~}~%" | |
728 | "The following packages would be installed:~%~{~a~%~}~%" | |
729 | len) | |
730 | install) | |
731 | (format (current-error-port) | |
732 | (N_ "The following package will be installed:~%~{~a~%~}~%" | |
733 | "The following packages will be installed:~%~{~a~%~}~%" | |
734 | len) | |
735 | install)))) | |
736 | (_ #f)))) | |
737 | ||
073c34d7 LC |
738 | (define-syntax with-error-handling |
739 | (syntax-rules () | |
740 | "Run BODY within a user-friendly error condition handler." | |
741 | ((_ body ...) | |
742 | (call-with-error-handling | |
743 | (lambda () | |
744 | body ...))))) | |
745 | ||
64fc89b6 LC |
746 | (define (location->string loc) |
747 | "Return a human-friendly, GNU-standard representation of LOC." | |
748 | (match loc | |
749 | (#f (_ "<unknown location>")) | |
750 | (($ <location> file line column) | |
751 | (format #f "~a:~a:~a" file line column)))) | |
752 | ||
f651b477 LC |
753 | (define (config-directory) |
754 | "Return the name of the configuration directory, after making sure that it | |
755 | exists. Honor the XDG specs, | |
756 | <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>." | |
757 | (let ((dir (and=> (or (getenv "XDG_CONFIG_HOME") | |
758 | (and=> (getenv "HOME") | |
759 | (cut string-append <> "/.config"))) | |
760 | (cut string-append <> "/guix")))) | |
761 | (catch 'system-error | |
762 | (lambda () | |
59758816 | 763 | (mkdir-p dir) |
f651b477 LC |
764 | dir) |
765 | (lambda args | |
59758816 LC |
766 | (let ((err (system-error-errno args))) |
767 | ;; ERR is necessarily different from EEXIST. | |
768 | (leave (_ "failed to create configuration directory `~a': ~a~%") | |
769 | dir (strerror err))))))) | |
f651b477 | 770 | |
299112d3 LC |
771 | (define* (fill-paragraph str width #:optional (column 0)) |
772 | "Fill STR such that each line contains at most WIDTH characters, assuming | |
773 | that the first character is at COLUMN. | |
774 | ||
775 | When STR contains a single line break surrounded by other characters, it is | |
776 | converted to a space; sequences of more than one line break are preserved." | |
777 | (define (maybe-break chr result) | |
778 | (match result | |
779 | ((column newlines chars) | |
780 | (case chr | |
781 | ((#\newline) | |
782 | `(,column ,(+ 1 newlines) ,chars)) | |
783 | (else | |
3a09e1d2 CS |
784 | (let* ((spaces (if (and (pair? chars) (eqv? (car chars) #\.)) 2 1)) |
785 | (chars (case newlines | |
786 | ((0) chars) | |
787 | ((1) | |
788 | (append (make-list spaces #\space) chars)) | |
789 | (else | |
790 | (append (make-list newlines #\newline) chars)))) | |
791 | (column (case newlines | |
792 | ((0) column) | |
793 | ((1) (+ spaces column)) | |
794 | (else 0)))) | |
299112d3 LC |
795 | (let ((chars (cons chr chars)) |
796 | (column (+ 1 column))) | |
797 | (if (> column width) | |
798 | (let*-values (((before after) | |
799 | (break (cut eqv? #\space <>) chars)) | |
800 | ((len) | |
801 | (length before))) | |
802 | (if (<= len width) | |
803 | `(,len | |
804 | 0 | |
805 | ,(if (null? after) | |
806 | before | |
3a09e1d2 CS |
807 | (append before |
808 | (cons #\newline | |
809 | (drop-while (cut eqv? #\space <>) | |
810 | after))))) | |
299112d3 LC |
811 | `(,column 0 ,chars))) ; unbreakable |
812 | `(,column 0 ,chars))))))))) | |
813 | ||
814 | (match (string-fold maybe-break | |
815 | `(,column 0 ()) | |
816 | str) | |
817 | ((_ _ chars) | |
818 | (list->string (reverse chars))))) | |
819 | ||
2876b989 LC |
820 | \f |
821 | ;;; | |
822 | ;;; Packages. | |
823 | ;;; | |
824 | ||
1cd4027c | 825 | (define %text-width |
9703fef4 | 826 | (make-parameter (terminal-columns))) |
1cd4027c ML |
827 | |
828 | (set! (@@ (texinfo plain-text) wrap*) | |
829 | ;; XXX: Monkey patch this private procedure to let 'package->recutils' | |
830 | ;; parameterize the fill of description field correctly. | |
831 | (lambda strings | |
832 | (let ((indent (fluid-ref (@@ (texinfo plain-text) *indent*)))) | |
833 | (fill-string (string-concatenate strings) | |
834 | #:line-width (%text-width) #:initial-indent indent | |
835 | #:subsequent-indent indent)))) | |
836 | ||
837 | (define (texi->plain-text str) | |
838 | "Return a plain-text representation of texinfo fragment STR." | |
08d7e359 LC |
839 | ;; 'texi-fragment->stexi' uses a string port so make sure it's a |
840 | ;; Unicode-capable one (see <http://bugs.gnu.org/11197>.) | |
841 | (with-fluids ((%default-port-encoding "UTF-8")) | |
842 | (stexi->plain-text (texi-fragment->stexi str)))) | |
1cd4027c ML |
843 | |
844 | (define (package-description-string package) | |
845 | "Return a plain-text representation of PACKAGE description field." | |
846 | (and=> (package-description package) | |
847 | (compose texi->plain-text P_))) | |
848 | ||
299112d3 LC |
849 | (define (string->recutils str) |
850 | "Return a version of STR where newlines have been replaced by newlines | |
851 | followed by \"+ \", which makes for a valid multi-line field value in the | |
852 | `recutils' syntax." | |
853 | (list->string | |
854 | (string-fold-right (lambda (chr result) | |
855 | (if (eqv? chr #\newline) | |
856 | (cons* chr #\+ #\space result) | |
857 | (cons chr result))) | |
858 | '() | |
859 | str))) | |
860 | ||
1cd4027c | 861 | (define* (package->recutils p port #:optional (width (%text-width))) |
299112d3 LC |
862 | "Write to PORT a `recutils' record of package P, arranging to fit within |
863 | WIDTH columns." | |
069d43a7 LC |
864 | (define width* |
865 | ;; The available number of columns once we've taken into account space for | |
866 | ;; the initial "+ " prefix. | |
867 | (if (> width 2) (- width 2) width)) | |
868 | ||
20ffce82 LC |
869 | (define (dependencies->recutils packages) |
870 | (let ((list (string-join (map package-full-name | |
871 | (sort packages package<?)) " "))) | |
872 | (string->recutils | |
069d43a7 | 873 | (fill-paragraph list width* |
20ffce82 LC |
874 | (string-length "dependencies: "))))) |
875 | ||
9c0fc279 CR |
876 | (define (package<? p1 p2) |
877 | (string<? (package-full-name p1) (package-full-name p2))) | |
878 | ||
299112d3 LC |
879 | ;; Note: Don't i18n field names so that people can post-process it. |
880 | (format port "name: ~a~%" (package-name p)) | |
881 | (format port "version: ~a~%" (package-version p)) | |
6bf99bac | 882 | (format port "outputs: ~a~%" (string-join (package-outputs p))) |
5763ad92 LC |
883 | (format port "systems: ~a~%" |
884 | (string-join (package-transitive-supported-systems p))) | |
9c0fc279 CR |
885 | (format port "dependencies: ~a~%" |
886 | (match (package-direct-inputs p) | |
5e6feee6 EB |
887 | (((labels inputs . _) ...) |
888 | (dependencies->recutils (filter package? inputs))))) | |
299112d3 LC |
889 | (format port "location: ~a~%" |
890 | (or (and=> (package-location p) location->string) | |
891 | (_ "unknown"))) | |
8fa3e6b3 LC |
892 | |
893 | ;; Note: Starting from version 1.6 or recutils, hyphens are not allowed in | |
894 | ;; field identifiers. | |
895 | (format port "homepage: ~a~%" (package-home-page p)) | |
896 | ||
299112d3 LC |
897 | (format port "license: ~a~%" |
898 | (match (package-license p) | |
899 | (((? license? licenses) ...) | |
900 | (string-join (map license-name licenses) | |
901 | ", ")) | |
902 | ((? license? license) | |
903 | (license-name license)) | |
904 | (x | |
905 | (_ "unknown")))) | |
906 | (format port "synopsis: ~a~%" | |
907 | (string-map (match-lambda | |
908 | (#\newline #\space) | |
909 | (chr chr)) | |
ee764179 | 910 | (or (and=> (package-synopsis p) P_) |
299112d3 | 911 | ""))) |
1cd4027c ML |
912 | (format port "~a~2%" |
913 | (string->recutils | |
914 | (string-trim-right | |
069d43a7 | 915 | (parameterize ((%text-width width*)) |
1cd4027c ML |
916 | (texi->plain-text |
917 | (string-append "description: " | |
918 | (or (and=> (package-description p) P_) | |
919 | "")))) | |
920 | #\newline)))) | |
299112d3 | 921 | |
2cd09108 NK |
922 | (define (string->generations str) |
923 | "Return the list of generations matching a pattern in STR. This function | |
924 | accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", \"..9\"." | |
925 | (define (maybe-integer) | |
926 | (let ((x (string->number str))) | |
927 | (and (integer? x) | |
928 | x))) | |
929 | ||
930 | (define (maybe-comma-separated-integers) | |
931 | (let ((lst (delete-duplicates | |
932 | (map string->number | |
933 | (string-split str #\,))))) | |
934 | (and (every integer? lst) | |
935 | lst))) | |
936 | ||
937 | (cond ((maybe-integer) | |
938 | => | |
939 | list) | |
940 | ((maybe-comma-separated-integers) | |
941 | => | |
942 | identity) | |
943 | ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str) | |
944 | => | |
945 | (lambda (match) | |
946 | (let ((s (string->number (match:substring match 1))) | |
947 | (e (string->number (match:substring match 2)))) | |
948 | (and (every integer? (list s e)) | |
949 | (<= s e) | |
950 | (iota (1+ (- e s)) s))))) | |
951 | ((string-match "^([0-9]+)\\.\\.$" str) | |
952 | => | |
953 | (lambda (match) | |
954 | (let ((s (string->number (match:substring match 1)))) | |
955 | (and (integer? s) | |
956 | `(>= ,s))))) | |
957 | ((string-match "^\\.\\.([0-9]+)$" str) | |
958 | => | |
959 | (lambda (match) | |
960 | (let ((e (string->number (match:substring match 1)))) | |
961 | (and (integer? e) | |
962 | `(<= ,e))))) | |
963 | (else #f))) | |
964 | ||
965 | (define (string->duration str) | |
966 | "Return the duration matching a pattern in STR. This function accepts the | |
967 | following patterns: \"1d\", \"1w\", \"1m\"." | |
968 | (define (hours->duration hours match) | |
969 | (make-time time-duration 0 | |
970 | (* 3600 hours (string->number (match:substring match 1))))) | |
971 | ||
638c5b79 LC |
972 | (cond ((string-match "^([0-9]+)s$" str) |
973 | => | |
974 | (lambda (match) | |
975 | (make-time time-duration 0 | |
976 | (string->number (match:substring match 1))))) | |
977 | ((string-match "^([0-9]+)h$" str) | |
978 | (lambda (match) | |
979 | (hours->duration 1 match))) | |
980 | ((string-match "^([0-9]+)d$" str) | |
2cd09108 NK |
981 | => |
982 | (lambda (match) | |
983 | (hours->duration 24 match))) | |
984 | ((string-match "^([0-9]+)w$" str) | |
985 | => | |
986 | (lambda (match) | |
987 | (hours->duration (* 24 7) match))) | |
988 | ((string-match "^([0-9]+)m$" str) | |
989 | => | |
990 | (lambda (match) | |
991 | (hours->duration (* 24 30) match))) | |
992 | (else #f))) | |
993 | ||
e49de93a LC |
994 | (define* (matching-generations str profile |
995 | #:key (duration-relation <=)) | |
996 | "Return the list of available generations matching a pattern in STR. See | |
997 | 'string->generations' and 'string->duration' for the list of valid patterns. | |
998 | When STR is a duration pattern, return all the generations whose ctime has | |
999 | DURATION-RELATION with the current time." | |
1000 | (define (valid-generations lst) | |
1001 | (define (valid-generation? n) | |
1002 | (any (cut = n <>) (generation-numbers profile))) | |
1003 | ||
1004 | (fold-right (lambda (x acc) | |
1005 | (if (valid-generation? x) | |
1006 | (cons x acc) | |
1007 | acc)) | |
1008 | '() | |
1009 | lst)) | |
1010 | ||
1011 | (define (filter-generations generations) | |
1012 | (match generations | |
1013 | (() '()) | |
1014 | (('>= n) | |
1015 | (drop-while (cut > n <>) | |
1016 | (generation-numbers profile))) | |
1017 | (('<= n) | |
1018 | (valid-generations (iota n 1))) | |
1019 | ((lst ..1) | |
1020 | (valid-generations lst)) | |
1021 | (_ #f))) | |
1022 | ||
1023 | (define (filter-by-duration duration) | |
1024 | (define (time-at-midnight time) | |
1025 | ;; Return TIME at midnight by setting nanoseconds, seconds, minutes, and | |
1026 | ;; hours to zeros. | |
1027 | (let ((d (time-utc->date time))) | |
1028 | (date->time-utc | |
1029 | (make-date 0 0 0 0 | |
1030 | (date-day d) (date-month d) | |
1031 | (date-year d) (date-zone-offset d))))) | |
1032 | ||
1033 | (define generation-ctime-alist | |
1034 | (map (lambda (number) | |
1035 | (cons number | |
1036 | (time-second | |
1037 | (time-at-midnight | |
1038 | (generation-time profile number))))) | |
1039 | (generation-numbers profile))) | |
1040 | ||
1041 | (match duration | |
1042 | (#f #f) | |
1043 | (res | |
1044 | (let ((s (time-second | |
1045 | (subtract-duration (time-at-midnight (current-time)) | |
1046 | duration)))) | |
1047 | (delete #f (map (lambda (x) | |
1048 | (and (duration-relation s (cdr x)) | |
1049 | (first x))) | |
1050 | generation-ctime-alist)))))) | |
1051 | ||
1052 | (cond ((string->generations str) | |
1053 | => | |
1054 | filter-generations) | |
1055 | ((string->duration str) | |
1056 | => | |
1057 | filter-by-duration) | |
1058 | (else #f))) | |
1059 | ||
ad18c7e6 LC |
1060 | (define (display-generation profile number) |
1061 | "Display a one-line summary of generation NUMBER of PROFILE." | |
1062 | (unless (zero? number) | |
1063 | (let ((header (format #f (_ "Generation ~a\t~a") number | |
1064 | (date->string | |
1065 | (time-utc->date | |
1066 | (generation-time profile number)) | |
1067 | "~b ~d ~Y ~T"))) | |
1068 | (current (generation-number profile))) | |
1069 | (if (= number current) | |
3bd9672c LC |
1070 | ;; TRANSLATORS: The word "current" here is an adjective for |
1071 | ;; "Generation", as in "current generation". Use the appropriate | |
1072 | ;; gender where applicable. | |
ad18c7e6 LC |
1073 | (format #t (_ "~a\t(current)~%") header) |
1074 | (format #t "~a~%" header))))) | |
1075 | ||
e95ae7c2 RJ |
1076 | (define (display-profile-content-diff profile gen1 gen2) |
1077 | "Display the changed packages in PROFILE GEN2 compared to generation GEN2." | |
1078 | ||
1079 | (define (equal-entry? first second) | |
1080 | (string= (manifest-entry-item first) (manifest-entry-item second))) | |
1081 | ||
1082 | (define (display-entry entry prefix) | |
1083 | (match entry | |
1084 | (($ <manifest-entry> name version output location _) | |
1085 | (format #t " ~a ~a\t~a\t~a\t~a~%" prefix name version output location)))) | |
1086 | ||
1087 | (define (list-entries number) | |
1088 | (manifest-entries (profile-manifest (generation-file-name profile number)))) | |
1089 | ||
1090 | (define (display-diff profile old new) | |
1091 | (display-generation profile new) | |
1092 | (let ((added (lset-difference | |
1093 | equal-entry? (list-entries new) (list-entries old))) | |
1094 | (removed (lset-difference | |
1095 | equal-entry? (list-entries old) (list-entries new)))) | |
1096 | (for-each (cut display-entry <> "+") added) | |
88bdbb2a RJ |
1097 | (for-each (cut display-entry <> "-") removed) |
1098 | (newline))) | |
e95ae7c2 RJ |
1099 | |
1100 | (display-diff profile gen1 gen2)) | |
1101 | ||
ad18c7e6 LC |
1102 | (define (display-profile-content profile number) |
1103 | "Display the packages in PROFILE, generation NUMBER, in a human-readable | |
1104 | way." | |
1105 | (for-each (match-lambda | |
1106 | (($ <manifest-entry> name version output location _) | |
1107 | (format #t " ~a\t~a\t~a\t~a~%" | |
1108 | name version output location))) | |
1109 | ||
1110 | ;; Show most recently installed packages last. | |
1111 | (reverse | |
1112 | (manifest-entries | |
1113 | (profile-manifest (generation-file-name profile number)))))) | |
1114 | ||
06d45f45 LC |
1115 | (define (display-generation-change previous current) |
1116 | (format #t (_ "switched from generation ~a to ~a~%") previous current)) | |
1117 | ||
1118 | (define (roll-back* store profile) | |
1119 | "Like 'roll-back', but display what is happening." | |
1120 | (call-with-values | |
1121 | (lambda () | |
1122 | (roll-back store profile)) | |
1123 | display-generation-change)) | |
1124 | ||
1125 | (define (switch-to-generation* profile number) | |
1126 | "Like 'switch-generation', but display what is happening." | |
1127 | (let ((previous (switch-to-generation profile number))) | |
1128 | (display-generation-change previous number))) | |
1129 | ||
1130 | (define (delete-generation* store profile generation) | |
1131 | "Like 'delete-generation', but display what is going on." | |
1132 | (format #t (_ "deleting ~a~%") | |
1133 | (generation-file-name profile generation)) | |
1134 | (delete-generation store profile generation)) | |
1135 | ||
2876b989 LC |
1136 | (define* (package-specification->name+version+output spec |
1137 | #:optional (output "out")) | |
1138 | "Parse package specification SPEC and return three value: the specified | |
1139 | package name, version number (or #f), and output name (or OUTPUT). SPEC may | |
1140 | optionally contain a version number and an output name, as in these examples: | |
1141 | ||
1142 | guile | |
1b846da8 | 1143 | guile@2.0.9 |
2876b989 | 1144 | guile:debug |
1b846da8 | 1145 | guile@2.0.9:debug |
2876b989 LC |
1146 | " |
1147 | (let*-values (((name sub-drv) | |
1148 | (match (string-rindex spec #\:) | |
1149 | (#f (values spec output)) | |
1150 | (colon (values (substring spec 0 colon) | |
1151 | (substring spec (+ 1 colon)))))) | |
1152 | ((name version) | |
1153 | (package-name->name+version name))) | |
1154 | (values name version sub-drv))) | |
1155 | ||
1156 | \f | |
1157 | ;;; | |
1158 | ;;; Command-line option processing. | |
1159 | ;;; | |
1160 | ||
e49951eb | 1161 | (define (show-guix-usage) |
e49951eb | 1162 | (format (current-error-port) |
25c93676 LC |
1163 | (_ "Try `guix --help' for more information.~%")) |
1164 | (exit 1)) | |
e49951eb | 1165 | |
e31ff8b8 LC |
1166 | (define (command-files) |
1167 | "Return the list of source files that define Guix sub-commands." | |
1168 | (define directory | |
1169 | (and=> (search-path %load-path "guix.scm") | |
1170 | (compose (cut string-append <> "/guix/scripts") | |
1171 | dirname))) | |
1172 | ||
2b8cf44f LC |
1173 | (define dot-scm? |
1174 | (cut string-suffix? ".scm" <>)) | |
1175 | ||
e31ff8b8 | 1176 | (if directory |
09d809db | 1177 | (scandir directory dot-scm?) |
e31ff8b8 LC |
1178 | '())) |
1179 | ||
1180 | (define (commands) | |
1181 | "Return the list of Guix command names." | |
1182 | (map (compose (cut string-drop-right <> 4) | |
1183 | basename) | |
1184 | (command-files))) | |
1185 | ||
1186 | (define (show-guix-help) | |
59f734f3 | 1187 | (define (internal? command) |
94d92c77 LC |
1188 | (member command '("substitute" "authenticate" "offload" |
1189 | "perform-download"))) | |
59f734f3 | 1190 | |
e31ff8b8 LC |
1191 | (format #t (_ "Usage: guix COMMAND ARGS... |
1192 | Run COMMAND with ARGS.\n")) | |
1193 | (newline) | |
1194 | (format #t (_ "COMMAND must be one of the sub-commands listed below:\n")) | |
1195 | (newline) | |
1196 | ;; TODO: Display a synopsis of each command. | |
59f734f3 LC |
1197 | (format #t "~{ ~a~%~}" (sort (remove internal? (commands)) |
1198 | string<?)) | |
e31ff8b8 LC |
1199 | (show-bug-report-information)) |
1200 | ||
a2011be5 LC |
1201 | (define program-name |
1202 | ;; Name of the command-line program currently executing, or #f. | |
1203 | (make-parameter #f)) | |
1204 | ||
ec5d0a85 LC |
1205 | (define (run-guix-command command . args) |
1206 | "Run COMMAND with the given ARGS. Report an error when COMMAND is not | |
1207 | found." | |
1208 | (define module | |
1209 | (catch 'misc-error | |
1210 | (lambda () | |
1211 | (resolve-interface `(guix scripts ,command))) | |
1212 | (lambda - | |
25c93676 LC |
1213 | (format (current-error-port) |
1214 | (_ "guix: ~a: command not found~%") command) | |
1215 | (show-guix-usage)))) | |
ec5d0a85 LC |
1216 | |
1217 | (let ((command-main (module-ref module | |
1218 | (symbol-append 'guix- command)))) | |
1219 | (parameterize ((program-name command)) | |
14d5ca2e LC |
1220 | ;; Disable canonicalization so we don't don't stat unreasonably. |
1221 | (with-fluids ((%file-port-name-canonicalization #f)) | |
1222 | (apply command-main args))))) | |
ec5d0a85 | 1223 | |
caa6732e AK |
1224 | (define (run-guix . args) |
1225 | "Run the 'guix' command defined by command line ARGS. | |
1226 | Unlike 'guix-main', this procedure assumes that locale, i18n support, | |
1227 | and signal handling has already been set up." | |
1228 | (define option? (cut string-prefix? "-" <>)) | |
1229 | ||
1230 | (match args | |
1231 | (() | |
1232 | (format (current-error-port) | |
1233 | (_ "guix: missing command name~%")) | |
1234 | (show-guix-usage)) | |
1235 | ((or ("-h") ("--help")) | |
1236 | (show-guix-help)) | |
1237 | (("--version") | |
1238 | (show-version-and-exit "guix")) | |
1239 | (((? option? o) args ...) | |
1240 | (format (current-error-port) | |
1241 | (_ "guix: unrecognized option '~a'~%") o) | |
1242 | (show-guix-usage)) | |
2ab5fdc4 LC |
1243 | (("help" command) |
1244 | (apply run-guix-command (string->symbol command) | |
1245 | '("--help"))) | |
caa6732e AK |
1246 | (("help" args ...) |
1247 | (show-guix-help)) | |
1248 | ((command args ...) | |
1249 | (apply run-guix-command | |
1250 | (string->symbol command) | |
1251 | args)))) | |
1252 | ||
a2011be5 LC |
1253 | (define guix-warning-port |
1254 | (make-parameter (current-warning-port))) | |
1255 | ||
e49951eb MW |
1256 | (define (guix-main arg0 . args) |
1257 | (initialize-guix) | |
caa6732e | 1258 | (apply run-guix args)) |
e49951eb | 1259 | |
073c34d7 | 1260 | ;;; ui.scm ends here |