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