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