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