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