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