Commit | Line | Data |
---|---|---|
233e7676 | 1 | ;;; GNU Guix --- Functional package management for GNU |
09edf136 | 2 | ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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> |
387e6b9e CM |
14 | ;;; Copyright © 2019 Chris Marusich <cmmarusich@gmail.com> |
15 | ;;; Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr> | |
d2cdef65 | 16 | ;;; Copyright © 2019 Simon Tournier <zimon.toutoune@gmail.com> |
e70a8847 | 17 | ;;; Copyright © 2020 Arun Isaac <arunisaac@systemreboot.net> |
7d3eb5e6 | 18 | ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com> |
073c34d7 | 19 | ;;; |
233e7676 | 20 | ;;; This file is part of GNU Guix. |
073c34d7 | 21 | ;;; |
233e7676 | 22 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
073c34d7 LC |
23 | ;;; under the terms of the GNU General Public License as published by |
24 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
25 | ;;; your option) any later version. | |
26 | ;;; | |
233e7676 | 27 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
073c34d7 LC |
28 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
29 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
30 | ;;; GNU General Public License for more details. | |
31 | ;;; | |
32 | ;;; You should have received a copy of the GNU General Public License | |
233e7676 | 33 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
073c34d7 LC |
34 | |
35 | (define-module (guix ui) | |
37eed374 | 36 | #:use-module (guix i18n) |
9e1e0460 | 37 | #:use-module (guix colors) |
1b5ee3bd | 38 | #:use-module (guix diagnostics) |
3e43166f | 39 | #:use-module (guix gexp) |
b5bfa477 | 40 | #:use-module (guix sets) |
073c34d7 LC |
41 | #:use-module (guix utils) |
42 | #:use-module (guix store) | |
cdd5d6f9 | 43 | #:use-module (guix config) |
073c34d7 | 44 | #:use-module (guix packages) |
5d7a8584 | 45 | #:use-module (guix profiles) |
9bb2b96a | 46 | #:use-module (guix derivations) |
b7071bc5 LC |
47 | #:use-module (guix build-system) |
48 | #:use-module (guix serialization) | |
edd25a8c LC |
49 | #:use-module ((guix licenses) |
50 | #:select (license? license-name license-uri)) | |
dcfc6f21 | 51 | #:use-module ((guix build syscalls) |
55e1dfa4 LC |
52 | #:select (free-disk-space terminal-columns terminal-rows |
53 | with-file-lock/no-wait)) | |
cbdfa50d | 54 | #:use-module ((guix build utils) |
24cb66d1 LC |
55 | ;; XXX: All we need are the bindings related to |
56 | ;; '&invoke-error'. However, to work around the bug described | |
57 | ;; in 5d669883ecc104403c5d3ba7d172e9c02234577c, #:hide | |
58 | ;; unwanted bindings instead of #:select'ing the needed | |
59 | ;; bindings. | |
5a2639f9 LC |
60 | #:hide (package-name->name+version |
61 | ;; Avoid "overrides core binding" warning. | |
62 | delete)) | |
299112d3 | 63 | #:use-module (srfi srfi-1) |
3794ce93 | 64 | #:use-module (srfi srfi-9 gnu) |
299112d3 | 65 | #:use-module (srfi srfi-11) |
2cd09108 | 66 | #:use-module (srfi srfi-19) |
073c34d7 | 67 | #:use-module (srfi srfi-26) |
2abcc97f | 68 | #:use-module (srfi srfi-31) |
073c34d7 | 69 | #:use-module (srfi srfi-34) |
c1d52c71 | 70 | #:use-module (srfi srfi-35) |
e31ff8b8 | 71 | #:autoload (ice-9 ftw) (scandir) |
64fc89b6 | 72 | #:use-module (ice-9 match) |
9bb2b96a | 73 | #:use-module (ice-9 format) |
2cd09108 | 74 | #:use-module (ice-9 regex) |
c39693d7 | 75 | #:autoload (ice-9 popen) (open-pipe* close-pipe) |
f44c7aac | 76 | #:autoload (system base compile) (compile-file) |
db030303 LC |
77 | #:autoload (system repl repl) (start-repl) |
78 | #:autoload (system repl debug) (make-debug stack->vector) | |
7f0f38b5 | 79 | #:autoload (web uri) (encode-and-join-uri-path) |
1cd4027c ML |
80 | #:use-module (texinfo) |
81 | #:use-module (texinfo plain-text) | |
82 | #:use-module (texinfo string-utils) | |
1b5ee3bd LC |
83 | |
84 | ;; Re-exports for backward compatibility. | |
85 | #:re-export (G_ N_ P_ ;now in (guix i18n) | |
86 | ||
87 | warning info report-error leave ;now in (guix diagnostics) | |
88 | location->string | |
89 | guix-warning-port program-name) | |
90 | #:export (display-hint | |
7ea1432e DT |
91 | make-user-module |
92 | load* | |
4ae7559f | 93 | warn-about-load-error |
cdd5d6f9 | 94 | show-version-and-exit |
3441e164 | 95 | show-bug-report-information |
fd688c82 | 96 | make-regexp* |
969e678e | 97 | string->number* |
1d6243cf | 98 | size->number |
fa394eb9 | 99 | show-derivation-outputs |
07ce23e0 | 100 | build-notifier |
9bb2b96a | 101 | show-what-to-build |
4d043ab6 | 102 | show-what-to-build* |
5d7a8584 | 103 | show-manifest-transaction |
073c34d7 | 104 | call-with-error-handling |
64fc89b6 | 105 | with-error-handling |
2d2f98ef | 106 | with-unbound-variable-handling |
df36e629 | 107 | leave-on-EPIPE |
ac5de156 | 108 | read/eval |
eb0880e7 | 109 | read/eval-package-expression |
8120b23e | 110 | check-available-space |
7b322d3c | 111 | indented-string |
299112d3 | 112 | fill-paragraph |
0649321d | 113 | %text-width |
2748ee3b | 114 | texi->plain-text |
1cd4027c | 115 | package-description-string |
689db38e | 116 | package-synopsis-string |
299112d3 | 117 | string->recutils |
e49951eb | 118 | package->recutils |
2876b989 | 119 | package-specification->name+version+output |
4311cf96 | 120 | |
7f0f38b5 | 121 | supports-hyperlinks? |
2d6bd5ed | 122 | hyperlink |
fa983b82 | 123 | file-hyperlink |
7f0f38b5 LC |
124 | location->hyperlink |
125 | ||
1acc5e11 | 126 | with-paginated-output-port |
c7ae219e LC |
127 | relevance |
128 | package-relevance | |
4311cf96 LC |
129 | display-search-results |
130 | ||
55e1dfa4 | 131 | with-profile-lock |
2cd09108 NK |
132 | string->generations |
133 | string->duration | |
e49de93a | 134 | matching-generations |
ad18c7e6 LC |
135 | display-generation |
136 | display-profile-content | |
e95ae7c2 | 137 | display-profile-content-diff |
06d45f45 LC |
138 | roll-back* |
139 | switch-to-generation* | |
140 | delete-generation* | |
a725504a LC |
141 | |
142 | %default-message-language | |
143 | current-message-language | |
144 | ||
e49951eb | 145 | run-guix-command |
caa6732e | 146 | run-guix |
5d9f9ad6 | 147 | guix-main)) |
073c34d7 LC |
148 | |
149 | ;;; Commentary: | |
150 | ;;; | |
151 | ;;; User interface facilities for command-line tools. | |
152 | ;;; | |
153 | ;;; Code: | |
154 | ||
dc856223 LC |
155 | (define (print-unbound-variable-error port key args default-printer) |
156 | ;; Print unbound variable errors more nicely, and in the right language. | |
157 | (match args | |
158 | ((proc message (variable) _ ...) | |
159 | ;; We can always omit PROC because when it's useful (i.e., different from | |
160 | ;; "module-lookup"), it gets displayed before. | |
2d2f98ef | 161 | (format port (G_ "error: ~a: unbound variable") variable)) |
dc856223 LC |
162 | (_ |
163 | (default-printer)))) | |
164 | ||
165 | (set-exception-printer! 'unbound-variable print-unbound-variable-error) | |
166 | ||
7ea1432e DT |
167 | (define (make-user-module modules) |
168 | "Return a new user module with the additional MODULES loaded." | |
169 | ;; Module in which the machine description file is loaded. | |
170 | (let ((module (make-fresh-user-module))) | |
171 | (for-each (lambda (iface) | |
172 | (module-use! module (resolve-interface iface))) | |
173 | modules) | |
174 | module)) | |
175 | ||
7f2f6a2c LC |
176 | (define (last-frame-with-source stack) |
177 | "Walk stack upwards and return the last frame that has source location | |
178 | information, or #f if it could not be found." | |
2abcc97f LC |
179 | (define (frame-with-source frame) |
180 | ;; Walk from FRAME upwards until source location information is found. | |
181 | (let loop ((frame frame) | |
182 | (previous frame)) | |
183 | (if (not frame) | |
184 | previous | |
abbb9871 LC |
185 | |
186 | ;; On Guile 3, the latest frame with source may be that of | |
187 | ;; 'raise-exception' in boot-9.scm. Skip it. | |
188 | (if (and (frame-source frame) | |
189 | (not (eq? 'raise-exception (frame-procedure-name frame)))) | |
2abcc97f LC |
190 | frame |
191 | (loop (frame-previous frame) frame))))) | |
192 | ||
7f2f6a2c LC |
193 | (let* ((depth (stack-length stack)) |
194 | (last (and (> depth 0) (stack-ref stack 0)))) | |
195 | (frame-with-source (if (> depth 1) | |
196 | (stack-ref stack 1) ;skip the 'throw' frame | |
197 | last)))) | |
198 | ||
199 | (define* (load* file user-module | |
200 | #:key (on-error 'nothing-special)) | |
201 | "Load the user provided Scheme source code FILE." | |
db030303 LC |
202 | (define (error-string frame args) |
203 | (call-with-output-string | |
f44c7aac LC |
204 | (lambda (port) |
205 | (apply display-error frame port (cdr args))))) | |
db030303 LC |
206 | |
207 | (define tag | |
208 | (make-prompt-tag "user-code")) | |
209 | ||
7ea1432e DT |
210 | (catch #t |
211 | (lambda () | |
2abcc97f | 212 | ;; XXX: Force a recompilation to avoid ABI issues. |
92423868 LC |
213 | ;; |
214 | ;; In 2.2.3, the bogus answer to <https://bugs.gnu.org/29226> was to | |
215 | ;; ignore all available .go, not just those from ~/.cache, which in turn | |
216 | ;; meant that we had to rebuild *everything*. Since this is too costly, | |
f44c7aac LC |
217 | ;; we have to turn off '%fresh-auto-compile' with that version, so to |
218 | ;; avoid ABI breakage in the user's config file, we explicitly compile | |
219 | ;; it (the problem remains if the user's config is spread on several | |
220 | ;; modules.) See <https://bugs.gnu.org/29881>. | |
7949c140 LC |
221 | (unless (string=? (version) "2.2.3") |
222 | (set! %fresh-auto-compile #t)) | |
92423868 | 223 | |
2abcc97f | 224 | (set! %load-should-auto-compile #t) |
7ea1432e DT |
225 | |
226 | (save-module-excursion | |
227 | (lambda () | |
228 | (set-current-module user-module) | |
7ea1432e | 229 | |
2abcc97f LC |
230 | ;; Hide the "auto-compiling" messages. |
231 | (parameterize ((current-warning-port (%make-void-port "w"))) | |
db030303 LC |
232 | (call-with-prompt tag |
233 | (lambda () | |
7949c140 LC |
234 | (when (string=? (version) "2.2.3") |
235 | (catch 'system-error | |
236 | (lambda () | |
237 | (compile-file file #:env user-module)) | |
238 | (const #f))) ;EACCES maybe, let's interpret it | |
239 | ||
db030303 LC |
240 | ;; Give 'load' an absolute file name so that it doesn't try to |
241 | ;; search for FILE in %LOAD-PATH. Note: use 'load', not | |
694e10af VL |
242 | ;; 'primitive-load', so that FILE is compiled, which then allows |
243 | ;; us to provide better error reporting with source line numbers. | |
db030303 LC |
244 | (load (canonicalize-path file))) |
245 | (const #f)))))) | |
2abcc97f LC |
246 | (lambda _ |
247 | ;; XXX: Errors are reported from the pre-unwind handler below, but | |
248 | ;; calling 'exit' from there has no effect, so we call it here. | |
249 | (exit 1)) | |
250 | (rec (handle-error . args) | |
251 | ;; Capture the stack up to this procedure call, excluded, and pass | |
252 | ;; the faulty stack frame to 'report-load-error'. | |
db030303 | 253 | (let* ((stack (make-stack #t handle-error tag)) |
7f2f6a2c | 254 | (frame (last-frame-with-source stack))) |
db030303 LC |
255 | |
256 | (report-load-error file args frame) | |
257 | ||
258 | (case on-error | |
259 | ((debug) | |
260 | (newline) | |
69daee23 | 261 | (display (G_ "entering debugger; type ',bt' for a backtrace\n")) |
db030303 LC |
262 | (start-repl #:debug (make-debug (stack->vector stack) 0 |
263 | (error-string frame args) | |
264 | #f))) | |
265 | ((backtrace) | |
266 | (newline (current-error-port)) | |
267 | (display-backtrace stack (current-error-port))) | |
268 | (else | |
269 | #t)))))) | |
2abcc97f | 270 | |
a2985bb1 LC |
271 | (define (known-variable-definition variable) |
272 | "Search among the currently loaded modules one that defines a variable named | |
273 | VARIABLE and return it, or #f if none was found." | |
274 | (define (module<? m1 m2) | |
275 | (match (module-name m2) | |
276 | (('gnu _ ...) #t) | |
277 | (('guix _ ...) | |
278 | (match (module-name m1) | |
279 | (('gnu _ ...) #f) | |
280 | (_ #t))) | |
281 | (_ #f))) | |
282 | ||
b5bfa477 LC |
283 | (let loop ((modules (list (resolve-module '() #f #f #:ensure #f))) |
284 | (suggestions '()) | |
285 | (visited (setq))) | |
a2985bb1 LC |
286 | (match modules |
287 | (() | |
288 | ;; Pick the "best" suggestion. | |
289 | (match (sort suggestions module<?) | |
290 | (() #f) | |
291 | ((first _ ...) first))) | |
292 | ((head tail ...) | |
b5bfa477 LC |
293 | (if (set-contains? visited head) |
294 | (loop tail suggestions visited) | |
295 | (let ((visited (set-insert head visited)) | |
296 | (next (append tail | |
297 | (hash-map->list (lambda (name module) | |
298 | module) | |
299 | (module-submodules head))))) | |
300 | (match (module-local-variable head variable) | |
301 | (#f (loop next suggestions visited)) | |
302 | (_ | |
303 | (match (module-name head) | |
304 | (('gnu _ ...) head) ;must be that one | |
305 | (_ (loop next (cons head suggestions) visited))))))))))) | |
a2985bb1 | 306 | |
1b5ee3bd LC |
307 | (define %hint-color (color BOLD CYAN)) |
308 | ||
935542fb LC |
309 | (define* (display-hint message #:optional (port (current-error-port))) |
310 | "Display MESSAGE, a l10n message possibly containing Texinfo markup, to | |
311 | PORT." | |
a7ae18b1 LC |
312 | (define colorize |
313 | (if (color-output? port) | |
314 | (lambda (str) | |
2569ef9d | 315 | (colorize-string str %hint-color)) |
a7ae18b1 LC |
316 | identity)) |
317 | ||
318 | (display (colorize (G_ "hint: ")) port) | |
319 | (display | |
320 | ;; XXX: We should arrange so that the initial indent is wider. | |
321 | (parameterize ((%text-width (max 15 (- (terminal-columns) 5)))) | |
322 | (texi->plain-text message)) | |
323 | port)) | |
935542fb | 324 | |
2d2f98ef LC |
325 | (define* (report-unbound-variable-error args #:key frame) |
326 | "Return the given unbound-variable error, where ARGS is the list of 'throw' | |
327 | arguments." | |
328 | (match args | |
329 | ((key . args) | |
330 | (print-exception (current-error-port) frame key args))) | |
331 | (match args | |
332 | (('unbound-variable proc message (variable) _ ...) | |
333 | (match (known-variable-definition variable) | |
334 | (#f | |
335 | (display-hint (G_ "Did you forget a @code{use-modules} form?"))) | |
336 | ((? module? module) | |
337 | (display-hint (format #f (G_ "Did you forget @code{(use-modules ~a)}?") | |
338 | (module-name module)))))))) | |
339 | ||
a2a94b6e LC |
340 | (define (check-module-matches-file module file) |
341 | "Check whether FILE starts with 'define-module MODULE' and print a hint if | |
342 | it doesn't." | |
343 | ;; This is a common mistake when people start writing their own package | |
344 | ;; definitions and try loading them with 'guix build -L …', so help them | |
345 | ;; diagnose the problem. | |
346 | (define (hint) | |
347 | (display-hint (format #f (G_ "File @file{~a} should probably start with: | |
348 | ||
349 | @example\n(define-module ~a)\n@end example") | |
350 | file module))) | |
351 | ||
352 | (catch 'system-error | |
353 | (lambda () | |
354 | (let* ((sexp (call-with-input-file file read)) | |
355 | (loc (and (pair? sexp) | |
356 | (source-properties->location (source-properties sexp))))) | |
357 | (match sexp | |
358 | (('define-module (names ...) _ ...) | |
359 | (unless (equal? module names) | |
360 | (warning loc | |
361 | (G_ "module name ~a does not match file name '~a'~%") | |
362 | names (module->source-file-name module)) | |
363 | (hint))) | |
364 | ((? eof-object?) | |
365 | (warning (G_ "~a: file is empty~%") file)) | |
366 | (else | |
367 | (hint))))) | |
368 | (const #f))) | |
369 | ||
2abcc97f | 370 | (define* (report-load-error file args #:optional frame) |
db030303 | 371 | "Report the failure to load FILE, a user-provided Scheme file. |
1151f6ae LC |
372 | ARGS is the list of arguments received by the 'throw' handler." |
373 | (match args | |
e465d9e1 | 374 | (('system-error . rest) |
1151f6ae | 375 | (let ((err (system-error-errno args))) |
69daee23 | 376 | (report-error (G_ "failed to load '~a': ~a~%") file (strerror err)))) |
a6e22d84 LC |
377 | (('read-error "scm_i_lreadparen" message _ ...) |
378 | ;; Guile's missing-paren messages are obscure so we make them more | |
379 | ;; intelligible here. | |
380 | (if (string-suffix? "end of file" message) | |
381 | (let ((location (string-drop-right message | |
382 | (string-length "end of file")))) | |
383 | (format (current-error-port) (G_ "~amissing closing parenthesis~%") | |
384 | location)) | |
385 | (apply throw args))) | |
47212fc7 | 386 | (('syntax-error proc message properties form subform . rest) |
1151f6ae | 387 | (let ((loc (source-properties->location properties))) |
47212fc7 LC |
388 | (report-error loc (G_ "~s: ~a~%") |
389 | (or subform form) message))) | |
2d2f98ef LC |
390 | (('unbound-variable _ ...) |
391 | (report-unbound-variable-error args #:frame frame)) | |
7f3bbfaf | 392 | (((or 'srfi-34 '%exception) obj) |
252a1926 LC |
393 | (cond ((message-condition? obj) |
394 | (report-error (and (error-location? obj) | |
395 | (error-location obj)) | |
396 | (G_ "~a~%") | |
397 | (gettext (condition-message obj) %gettext-domain))) | |
398 | ((formatted-message? obj) | |
399 | (apply report-error | |
400 | (and (error-location? obj) (error-location obj)) | |
401 | (gettext (formatted-message-string obj) %gettext-domain) | |
402 | (formatted-message-arguments obj))) | |
403 | (else | |
404 | (report-error (G_ "exception thrown: ~s~%") obj))) | |
23735137 | 405 | (when (fix-hint? obj) |
935542fb | 406 | (display-hint (condition-fix-hint obj)))) |
70a50305 | 407 | ((key args ...) |
69daee23 | 408 | (report-error (G_ "failed to load '~a':~%") file) |
70a50305 LC |
409 | (match args |
410 | (((? symbol? proc) (? string? message) (args ...) . rest) | |
411 | (display-error frame (current-error-port) proc message | |
412 | args rest)) | |
413 | (_ | |
414 | ;; Some exceptions like 'git-error' do not follow Guile's convention | |
415 | ;; above and need to be printed with 'print-exception'. | |
416 | (print-exception (current-error-port) frame key args)))))) | |
1151f6ae | 417 | |
a2a94b6e | 418 | (define (warn-about-load-error file module args) ;FIXME: factorize with ↑ |
4ae7559f LC |
419 | "Report the failure to load FILE, a user-provided Scheme file, without |
420 | exiting. ARGS is the list of arguments received by the 'throw' handler." | |
421 | (match args | |
e465d9e1 | 422 | (('system-error . rest) |
4ae7559f | 423 | (let ((err (system-error-errno args))) |
a2a94b6e | 424 | (warning (G_ "failed to load '~a': ~a~%") module (strerror err)))) |
4ae7559f LC |
425 | (('syntax-error proc message properties form . rest) |
426 | (let ((loc (source-properties->location properties))) | |
40262771 | 427 | (warning loc (G_ "~a~%") message))) |
723bdb8e LC |
428 | (('unbound-variable _ ...) |
429 | (report-unbound-variable-error args)) | |
7f3bbfaf | 430 | (((or 'srfi-34 '%exception) obj) |
252a1926 LC |
431 | (cond ((message-condition? obj) |
432 | (warning (G_ "failed to load '~a': ~a~%") | |
433 | file | |
434 | (gettext (condition-message obj) %gettext-domain))) | |
435 | ((formatted-message? obj) | |
436 | (warning (G_ "failed to load '~a': ~a~%") | |
437 | (apply format #f | |
438 | (gettext (formatted-message-string obj) | |
439 | %gettext-domain) | |
440 | (formatted-message-arguments obj)))) | |
441 | (else | |
442 | (warning (G_ "failed to load '~a': exception thrown: ~s~%") | |
443 | file obj)))) | |
4ae7559f | 444 | ((error args ...) |
a2a94b6e LC |
445 | (warning (G_ "failed to load '~a':~%") module) |
446 | (apply display-error #f (current-error-port) args) | |
447 | (check-module-matches-file module file)))) | |
4ae7559f | 448 | |
2d2f98ef LC |
449 | (define (call-with-unbound-variable-handling thunk) |
450 | (define tag | |
451 | (make-prompt-tag "user-code")) | |
452 | ||
453 | (catch 'unbound-variable | |
454 | (lambda () | |
455 | (call-with-prompt tag | |
456 | thunk | |
457 | (const #f))) | |
458 | (const #t) | |
459 | (rec (handle-error . args) | |
460 | (let* ((stack (make-stack #t handle-error tag)) | |
461 | (frame (and stack (last-frame-with-source stack)))) | |
462 | (report-unbound-variable-error args #:frame frame) | |
463 | (exit 1))))) | |
464 | ||
465 | (define-syntax-rule (with-unbound-variable-handling exp ...) | |
466 | "Capture 'unbound-variable' exceptions in the dynamic extent of EXP... and | |
467 | report them in a user-friendly way." | |
468 | (call-with-unbound-variable-handling (lambda () exp ...))) | |
469 | ||
a725504a LC |
470 | (define %default-message-language |
471 | ;; Default language to use for messages. | |
472 | (make-parameter "en")) | |
473 | ||
474 | (define (current-message-language) | |
475 | "Return the language used for messages according to the current locale. | |
476 | Return %DEFAULT-MESSAGE-LANGUAGE if that information could not be obtained. The | |
477 | result is an ISO-639-2 language code such as \"ar\", without the territory | |
478 | part." | |
479 | (let ((locale (setlocale LC_MESSAGES))) | |
480 | (match (string-index locale #\_) | |
481 | (#f locale) | |
482 | (index (string-take locale index))))) | |
483 | ||
b2a886f6 LC |
484 | (define (install-locale) |
485 | "Install the current locale settings." | |
486 | (catch 'system-error | |
487 | (lambda _ | |
488 | (setlocale LC_ALL "")) | |
489 | (lambda args | |
26db747a LC |
490 | (display-hint (G_ "Consider installing the @code{glibc-utf8-locales} or |
491 | @code{glibc-locales} package and defining @code{GUIX_LOCPATH}, along these | |
492 | lines: | |
493 | ||
494 | @example | |
495 | guix package -i glibc-utf8-locales | |
496 | export GUIX_LOCPATH=\"$HOME/.guix-profile/lib/locale\" | |
497 | @end example | |
498 | ||
fe4e770f LC |
499 | See the \"Application Setup\" section in the manual, for more info.\n")) |
500 | ;; We're now running in the "C" locale. Try to install a UTF-8 locale | |
501 | ;; instead. This one is guaranteed to be available in 'guix' from 'guix | |
502 | ;; pull'. | |
503 | (false-if-exception (setlocale LC_ALL "en_US.utf8"))))) | |
b2a886f6 | 504 | |
e49951eb | 505 | (define (initialize-guix) |
633f045f | 506 | "Perform the usual initialization for stand-alone Guix commands." |
ce92d269 LC |
507 | ;; By default don't annoy users with deprecation warnings. In practice, |
508 | ;; 'define-deprecated' in (ice-9 deprecated) arranges so that those warnings | |
509 | ;; are emitted at expansion-time only, but there are cases where they could | |
510 | ;; slip through, for instance when interpreting code. | |
511 | (unless (getenv "GUILE_WARN_DEPRECATED") | |
512 | (debug-disable 'warn-deprecated)) | |
513 | ||
e49951eb | 514 | (install-locale) |
39e9f95d | 515 | (textdomain %gettext-domain) |
e14c3929 LC |
516 | |
517 | ;; Ignore SIGPIPE. If the daemon closes the connection, we prefer to be | |
518 | ;; notified via an EPIPE later. | |
519 | (sigaction SIGPIPE SIG_IGN) | |
520 | ||
76832d34 LC |
521 | (setvbuf (current-output-port) 'line) |
522 | (setvbuf (current-error-port) 'line)) | |
e49951eb | 523 | |
cdd5d6f9 LC |
524 | (define* (show-version-and-exit #:optional (command (car (command-line)))) |
525 | "Display version information for COMMAND and `(exit 0)'." | |
526 | (simple-format #t "~a (~a) ~a~%" | |
527 | command %guix-package-name %guix-version) | |
09edf136 | 528 | (format #t "Copyright ~a 2020 ~a" |
d925cdc3 LC |
529 | ;; TRANSLATORS: Translate "(C)" to the copyright symbol |
530 | ;; (C-in-a-circle), if this symbol is available in the user's | |
531 | ;; locale. Otherwise, do not translate "(C)"; leave it as-is. */ | |
69daee23 LC |
532 | (G_ "(C)") |
533 | (G_ "the Guix authors\n")) | |
534 | (display (G_"\ | |
64a967cc LC |
535 | License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> |
536 | This is free software: you are free to change and redistribute it. | |
537 | There is NO WARRANTY, to the extent permitted by law. | |
538 | ")) | |
cdd5d6f9 LC |
539 | (exit 0)) |
540 | ||
3441e164 | 541 | (define (show-bug-report-information) |
cf5f2ad3 ML |
542 | ;; TRANSLATORS: The placeholder indicates the bug-reporting address for this |
543 | ;; package. Please add another line saying "Report translation bugs to | |
544 | ;; ...\n" with the address for translation bugs (typically your translation | |
545 | ;; team's web or email address). | |
69daee23 | 546 | (format #t (G_ " |
3441e164 | 547 | Report bugs to: ~a.") %guix-bug-report-address) |
69daee23 | 548 | (format #t (G_ " |
3441e164 | 549 | ~a home page: <~a>") %guix-package-name %guix-home-page-url) |
991fdb0d LC |
550 | (format #t (G_ " |
551 | General help using Guix and GNU software: <~a>") | |
552 | "https://guix.gnu.org/help/") | |
3441e164 LC |
553 | (newline)) |
554 | ||
e7ff0543 LC |
555 | (define (augmented-system-error-handler file) |
556 | "Return a 'system-error' handler that mentions FILE in its message." | |
557 | (lambda (key proc fmt args errno) | |
558 | ;; Augment the FMT and ARGS with information about TARGET (this | |
559 | ;; information is missing as of Guile 2.0.11, making the exception | |
560 | ;; uninformative.) | |
561 | (apply throw key proc "~A: ~S" | |
562 | (list (strerror (car errno)) file) | |
563 | (list errno)))) | |
564 | ||
6616a352 TS |
565 | (define-syntax apply-formals |
566 | (syntax-rules () | |
567 | ((_ proc (args ...)) (proc args ...)) | |
568 | ((_ proc (arg1 args ... . rest)) (apply proc arg1 args ... rest)))) | |
569 | ||
570 | (define-syntax-rule (error-reporting-wrapper proc formals file) | |
e7ff0543 LC |
571 | "Wrap PROC such that its 'system-error' exceptions are augmented to mention |
572 | FILE." | |
573 | (let ((real-proc (@ (guile) proc))) | |
6616a352 | 574 | (lambda formals |
e7ff0543 LC |
575 | (catch 'system-error |
576 | (lambda () | |
6616a352 | 577 | (apply-formals real-proc formals)) |
e7ff0543 LC |
578 | (augmented-system-error-handler file))))) |
579 | ||
7522a016 LC |
580 | (set! symlink |
581 | ;; We 'set!' the global binding because (gnu build ...) modules and similar | |
582 | ;; typically don't use (guix ui). | |
e7ff0543 | 583 | (error-reporting-wrapper symlink (source target) target)) |
44fd6ef1 | 584 | |
9b14107f LC |
585 | (set! copy-file |
586 | ;; Note: here we use 'set!', not #:replace, because UIs typically use | |
587 | ;; 'copy-recursively', which doesn't use (guix ui). | |
e7ff0543 LC |
588 | (error-reporting-wrapper copy-file (source target) target)) |
589 | ||
6d30b1b2 LC |
590 | (set! canonicalize-path |
591 | (error-reporting-wrapper canonicalize-path (file) file)) | |
592 | ||
90b144d2 LC |
593 | (set! delete-file |
594 | (error-reporting-wrapper delete-file (file) file)) | |
595 | ||
6616a352 TS |
596 | (set! execlp |
597 | (error-reporting-wrapper execlp (filename . args) filename)) | |
9b14107f | 598 | |
fd688c82 LC |
599 | (define (make-regexp* regexp . flags) |
600 | "Like 'make-regexp' but error out if REGEXP is invalid, reporting the error | |
601 | nicely." | |
602 | (catch 'regular-expression-syntax | |
603 | (lambda () | |
604 | (apply make-regexp regexp flags)) | |
605 | (lambda (key proc message . rest) | |
69daee23 | 606 | (leave (G_ "'~a' is not a valid regular expression: ~a~%") |
fd688c82 LC |
607 | regexp message)))) |
608 | ||
969e678e LC |
609 | (define (string->number* str) |
610 | "Like `string->number', but error out with an error message on failure." | |
611 | (or (string->number str) | |
69daee23 | 612 | (leave (G_ "~a: invalid number~%") str))) |
969e678e | 613 | |
1d6243cf LC |
614 | (define (size->number str) |
615 | "Convert STR, a storage measurement representation such as \"1024\" or | |
616 | \"1MiB\", to a number of bytes. Raise an error if STR could not be | |
617 | interpreted." | |
618 | (define unit-pos | |
fa99c4bb LC |
619 | (string-rindex str |
620 | (char-set-union (char-set #\.) char-set:digit))) | |
1d6243cf LC |
621 | |
622 | (define unit | |
623 | (and unit-pos (substring str (+ 1 unit-pos)))) | |
624 | ||
625 | (let* ((numstr (if unit-pos | |
626 | (substring str 0 (+ 1 unit-pos)) | |
627 | str)) | |
628 | (num (string->number numstr))) | |
629 | (unless num | |
69daee23 | 630 | (leave (G_ "invalid number: ~a~%") numstr)) |
1d6243cf LC |
631 | |
632 | ((compose inexact->exact round) | |
633 | (* num | |
634 | (match unit | |
4a44d7bb LC |
635 | ((or "KiB" "K" "k") (expt 2 10)) |
636 | ((or "MiB" "M") (expt 2 20)) | |
637 | ((or "GiB" "G") (expt 2 30)) | |
638 | ((or "TiB" "T") (expt 2 40)) | |
639 | ((or "PiB" "P") (expt 2 50)) | |
640 | ((or "EiB" "E") (expt 2 60)) | |
641 | ((or "ZiB" "Z") (expt 2 70)) | |
642 | ((or "YiB" "Y") (expt 2 80)) | |
643 | ("kB" (expt 10 3)) | |
1d6243cf LC |
644 | ("MB" (expt 10 6)) |
645 | ("GB" (expt 10 9)) | |
646 | ("TB" (expt 10 12)) | |
4a44d7bb LC |
647 | ("PB" (expt 10 15)) |
648 | ("EB" (expt 10 18)) | |
649 | ("ZB" (expt 10 21)) | |
650 | ("YB" (expt 10 24)) | |
1d6243cf | 651 | ("" 1) |
e465d9e1 | 652 | (x |
69daee23 | 653 | (leave (G_ "unknown unit: ~a~%") unit))))))) |
1d6243cf | 654 | |
3b80b813 LC |
655 | (define (display-collision-resolution-hint collision) |
656 | "Display hints on how to resolve COLLISION, a &profile-collistion-error." | |
657 | (define (top-most-entry entry) | |
658 | (let loop ((entry entry)) | |
659 | (match (force (manifest-entry-parent entry)) | |
660 | (#f entry) | |
661 | (parent (loop parent))))) | |
662 | ||
663 | (let* ((first (profile-collision-error-entry collision)) | |
664 | (second (profile-collision-error-conflict collision)) | |
665 | (name1 (manifest-entry-name (top-most-entry first))) | |
666 | (name2 (manifest-entry-name (top-most-entry second)))) | |
667 | (if (string=? name1 name2) | |
668 | (display-hint (format #f (G_ "You cannot have two different versions | |
669 | or variants of @code{~a} in the same profile.") | |
670 | name1)) | |
671 | (display-hint (format #f (G_ "Try upgrading both @code{~a} and @code{~a}, | |
672 | or remove one of them from the profile.") | |
673 | name1 name2))))) | |
674 | ||
a168c3e4 LC |
675 | (cond-expand |
676 | (guile-3 | |
677 | ;; On Guile 3.0, in 'call-with-error-handling' we need to re-raise. To | |
678 | ;; preserve useful backtraces in case of unhandled errors, we want that to | |
679 | ;; happen before the stack has been unwound, hence 'guard*'. | |
680 | (define-syntax-rule (guard* (var clauses ...) exp ...) | |
681 | "This variant of SRFI-34 'guard' does not unwind the stack before | |
682 | evaluating the tests and bodies of CLAUSES." | |
683 | (with-exception-handler | |
684 | (lambda (var) | |
685 | (cond clauses ... (else (raise var)))) | |
686 | (lambda () exp ...) | |
687 | #:unwind? #f))) | |
688 | (else | |
689 | (define-syntax-rule (guard* (var clauses ...) exp ...) | |
690 | (guard (var clauses ...) exp ...)))) | |
691 | ||
073c34d7 LC |
692 | (define (call-with-error-handling thunk) |
693 | "Call THUNK within a user-friendly error handler." | |
bec7f352 LC |
694 | (define (port-filename* port) |
695 | ;; 'port-filename' returns #f for non-file ports, but it raises an | |
696 | ;; exception for file ports that are closed. Work around that. | |
697 | (and (not (port-closed? port)) | |
698 | (port-filename port))) | |
699 | ||
a168c3e4 LC |
700 | (guard* (c ((package-input-error? c) |
701 | (let* ((package (package-error-package c)) | |
702 | (input (package-error-invalid-input c)) | |
703 | (location (package-location package)) | |
704 | (file (location-file location)) | |
705 | (line (location-line location)) | |
706 | (column (location-column location))) | |
707 | (leave (G_ "~a:~a:~a: package `~a' has an invalid input: ~s~%") | |
708 | file line column | |
709 | (package-full-name package) input))) | |
710 | ((package-cross-build-system-error? c) | |
711 | (let* ((package (package-error-package c)) | |
712 | (loc (package-location package)) | |
713 | (system (package-build-system package))) | |
714 | (leave (G_ "~a: ~a: build system `~a' does not support cross builds~%") | |
715 | (location->string loc) | |
716 | (package-full-name package) | |
717 | (build-system-name system)))) | |
718 | ((gexp-input-error? c) | |
719 | (let ((input (package-error-invalid-input c))) | |
720 | (leave (G_ "~s: invalid G-expression input~%") | |
721 | (gexp-error-invalid-input c)))) | |
722 | ((profile-not-found-error? c) | |
723 | (leave (G_ "profile '~a' does not exist~%") | |
724 | (profile-error-profile c))) | |
725 | ((missing-generation-error? c) | |
726 | (leave (G_ "generation ~a of profile '~a' does not exist~%") | |
727 | (missing-generation-error-generation c) | |
728 | (profile-error-profile c))) | |
729 | ((unmatched-pattern-error? c) | |
730 | (let ((pattern (unmatched-pattern-error-pattern c))) | |
731 | (leave (G_ "package '~a~@[@~a~]~@[:~a~]' not found in profile~%") | |
732 | (manifest-pattern-name pattern) | |
733 | (manifest-pattern-version pattern) | |
734 | (match (manifest-pattern-output pattern) | |
735 | ("out" #f) | |
736 | (output output))))) | |
737 | ((profile-collision-error? c) | |
738 | (let ((entry (profile-collision-error-entry c)) | |
739 | (conflict (profile-collision-error-conflict c))) | |
740 | (define (report-parent-entries entry) | |
741 | (let ((parent (force (manifest-entry-parent entry)))) | |
742 | (when (manifest-entry? parent) | |
743 | (report-error (G_ " ... propagated from ~a@~a~%") | |
744 | (manifest-entry-name parent) | |
745 | (manifest-entry-version parent)) | |
746 | (report-parent-entries parent)))) | |
747 | ||
748 | (define (manifest-entry-output* entry) | |
749 | (match (manifest-entry-output entry) | |
750 | ("out" "") | |
751 | (output (string-append ":" output)))) | |
752 | ||
753 | (report-error (G_ "profile contains conflicting entries for ~a~a~%") | |
754 | (manifest-entry-name entry) | |
755 | (manifest-entry-output* entry)) | |
756 | (report-error (G_ " first entry: ~a@~a~a ~a~%") | |
757 | (manifest-entry-name entry) | |
758 | (manifest-entry-version entry) | |
759 | (manifest-entry-output* entry) | |
760 | (manifest-entry-item entry)) | |
761 | (report-parent-entries entry) | |
762 | (report-error (G_ " second entry: ~a@~a~a ~a~%") | |
763 | (manifest-entry-name conflict) | |
764 | (manifest-entry-version conflict) | |
765 | (manifest-entry-output* conflict) | |
766 | (manifest-entry-item conflict)) | |
767 | (report-parent-entries conflict) | |
768 | (display-collision-resolution-hint c) | |
769 | (exit 1))) | |
770 | ((nar-error? c) | |
771 | (let ((file (nar-error-file c)) | |
772 | (port (nar-error-port c))) | |
773 | (if file | |
774 | (leave (G_ "corrupt input while restoring '~a' from ~s~%") | |
775 | file (or (port-filename* port) port)) | |
776 | (leave (G_ "corrupt input while restoring archive from ~s~%") | |
777 | (or (port-filename* port) port))))) | |
778 | ((store-connection-error? c) | |
779 | (leave (G_ "failed to connect to `~a': ~a~%") | |
780 | (store-connection-error-file c) | |
781 | (strerror (store-connection-error-code c)))) | |
782 | ((store-protocol-error? c) | |
783 | ;; FIXME: Server-provided error messages aren't i18n'd. | |
784 | (leave (G_ "~a~%") | |
785 | (store-protocol-error-message c))) | |
786 | ((derivation-missing-output-error? c) | |
787 | (leave (G_ "reference to invalid output '~a' of derivation '~a'~%") | |
788 | (derivation-missing-output c) | |
789 | (derivation-file-name (derivation-error-derivation c)))) | |
790 | ((file-search-error? c) | |
791 | (leave (G_ "file '~a' could not be found in these \ | |
d26e1967 | 792 | directories:~{ ~a~}~%") |
a168c3e4 LC |
793 | (file-search-error-file-name c) |
794 | (file-search-error-search-path c))) | |
795 | ((invoke-error? c) | |
796 | (leave (G_ "program exited\ | |
cbdfa50d MW |
797 | ~@[ with non-zero exit status ~a~]\ |
798 | ~@[ terminated by signal ~a~]\ | |
799 | ~@[ stopped by signal ~a~]: ~s~%") | |
a168c3e4 LC |
800 | (invoke-error-exit-status c) |
801 | (invoke-error-term-signal c) | |
802 | (invoke-error-stop-signal c) | |
803 | (cons (invoke-error-program c) | |
804 | (invoke-error-arguments c)))) | |
a168c3e4 | 805 | |
252a1926 LC |
806 | ((formatted-message? c) |
807 | (apply report-error | |
808 | (and (error-location? c) (error-location c)) | |
809 | (gettext (formatted-message-string c) %gettext-domain) | |
810 | (formatted-message-arguments c)) | |
811 | (when (fix-hint? c) | |
812 | (display-hint (condition-fix-hint c))) | |
813 | (exit 1)) | |
814 | ||
a168c3e4 LC |
815 | ;; On Guile 3.0.0, exceptions such as 'unbound-variable' are |
816 | ;; compound and include a '&message'. However, that message only | |
817 | ;; contains the format string. Thus, special-case it here to | |
818 | ;; avoid displaying a bare format string. | |
819 | ;; | |
820 | ;; Furthermore, use of 'guard*' ensures that the stack has not | |
821 | ;; been unwound when we re-raise, since that would otherwise show | |
822 | ;; useless backtraces. | |
823 | ((cond-expand | |
824 | (guile-3 | |
825 | ((exception-predicate &exception-with-kind-and-args) c)) | |
826 | (else #f)) | |
05f3d340 LC |
827 | (raise c)) |
828 | ||
829 | ((message-condition? c) | |
830 | ;; Normally '&message' error conditions have an i18n'd message. | |
831 | (report-error (and (error-location? c) (error-location c)) | |
832 | (G_ "~a~%") | |
833 | (gettext (condition-message c) %gettext-domain)) | |
834 | (when (fix-hint? c) | |
835 | (display-hint (condition-fix-hint c))) | |
836 | (exit 1))) | |
a168c3e4 LC |
837 | ;; Catch EPIPE and the likes. |
838 | (catch 'system-error | |
839 | thunk | |
840 | (lambda (key proc format-string format-args . rest) | |
841 | (leave (G_ "~a: ~a~%") proc | |
842 | (apply format #f format-string format-args)))))) | |
073c34d7 | 843 | |
df36e629 | 844 | (define-syntax-rule (leave-on-EPIPE exp ...) |
694e10af | 845 | "Run EXP... in a context where EPIPE errors are caught and lead to 'exit' |
df36e629 LC |
846 | with successful exit code. This is useful when writing to the standard output |
847 | may lead to EPIPE, because the standard output is piped through 'head' or | |
848 | similar." | |
849 | (catch 'system-error | |
850 | (lambda () | |
851 | exp ...) | |
852 | (lambda args | |
853 | ;; We really have to exit this brutally, otherwise Guile eventually | |
854 | ;; attempts to flush all the ports, leading to an uncaught EPIPE down | |
855 | ;; the path. | |
856 | (if (= EPIPE (system-error-errno args)) | |
857 | (primitive-_exit 0) | |
858 | (apply throw args))))) | |
859 | ||
56b82106 LC |
860 | (define %guix-user-module |
861 | ;; Module in which user expressions are evaluated. | |
07254feb LC |
862 | ;; Compute lazily to avoid circularity with (guix gexp). |
863 | (delay | |
864 | (let ((module (make-module))) | |
865 | (beautify-user-module! module) | |
866 | ;; Use (guix gexp) so that one can use #~ & co. | |
867 | (module-use! module (resolve-interface '(guix gexp))) | |
868 | module))) | |
56b82106 | 869 | |
ac5de156 LC |
870 | (define (read/eval str) |
871 | "Read and evaluate STR, raising an error if something goes wrong." | |
eb0880e7 LC |
872 | (let ((exp (catch #t |
873 | (lambda () | |
874 | (call-with-input-string str read)) | |
875 | (lambda args | |
69daee23 | 876 | (leave (G_ "failed to read expression ~s: ~s~%") |
eb0880e7 | 877 | str args))))) |
ac5de156 LC |
878 | (catch #t |
879 | (lambda () | |
07254feb | 880 | (eval exp (force %guix-user-module))) |
ac5de156 | 881 | (lambda args |
69daee23 | 882 | (report-error (G_ "failed to evaluate expression '~a':~%") exp) |
41766807 LC |
883 | (match args |
884 | (('syntax-error proc message properties form . rest) | |
69daee23 | 885 | (report-error (G_ "syntax error: ~a~%") message)) |
7f3bbfaf | 886 | (((or 'srfi-34 '%exception) obj) |
252a1926 LC |
887 | (cond ((message-condition? obj) |
888 | (report-error (G_ "~a~%") | |
889 | (gettext (condition-message obj) | |
890 | %gettext-domain))) | |
891 | ((formatted-message? obj) | |
892 | (apply report-error #f | |
893 | (gettext (formatted-message-string obj) | |
894 | %gettext-domain) | |
895 | (formatted-message-arguments obj))) | |
896 | (else | |
897 | (report-error (G_ "exception thrown: ~s~%") obj)))) | |
41766807 LC |
898 | ((error args ...) |
899 | (apply display-error #f (current-error-port) args)) | |
900 | (what? #f)) | |
901 | (exit 1))))) | |
ac5de156 LC |
902 | |
903 | (define (read/eval-package-expression str) | |
904 | "Read and evaluate STR and return the package it refers to, or exit an | |
905 | error." | |
906 | (match (read/eval str) | |
907 | ((? package? p) p) | |
e465d9e1 | 908 | (x |
69daee23 | 909 | (leave (G_ "expression ~s does not evaluate to a package~%") |
ac5de156 | 910 | str)))) |
eb0880e7 | 911 | |
fa394eb9 | 912 | (define (show-derivation-outputs derivation) |
08c95ba2 LC |
913 | "Show the output file names of DERIVATION, which can be a derivation or a |
914 | derivation input." | |
915 | (define (show-outputs derivation outputs) | |
916 | (format #t "~{~a~%~}" | |
917 | (map (cut derivation->output-path derivation <>) | |
918 | outputs))) | |
919 | ||
920 | (match derivation | |
921 | ((? derivation?) | |
922 | (show-outputs derivation (derivation-output-names derivation))) | |
923 | ((? derivation-input? input) | |
924 | (show-outputs (derivation-input-derivation input) | |
925 | (derivation-input-sub-derivations input))))) | |
fa394eb9 | 926 | |
8120b23e LC |
927 | (define* (check-available-space need |
928 | #:optional (directory (%store-prefix))) | |
929 | "Make sure at least NEED bytes are available in DIRECTORY. Otherwise emit a | |
dcfc6f21 LC |
930 | warning." |
931 | (let ((free (catch 'system-error | |
932 | (lambda () | |
8120b23e | 933 | (free-disk-space directory)) |
dcfc6f21 LC |
934 | (const #f)))) |
935 | (when (and free (>= need free)) | |
936 | (warning (G_ "at least ~,1h MB needed but only ~,1h MB available in ~a~%") | |
8120b23e | 937 | (/ need 1e6) (/ free 1e6) directory)))) |
dcfc6f21 | 938 | |
d4aa147e LC |
939 | (define (graft-derivation? drv) |
940 | "Return true if DRV is definitely a graft derivation, false otherwise." | |
941 | (match (assq-ref (derivation-properties drv) 'type) | |
942 | ('graft #t) | |
943 | (_ #f))) | |
944 | ||
80eebee9 RW |
945 | (define (profile-hook-derivation? drv) |
946 | "Return true if DRV is definitely a profile hook derivation, false otherwise." | |
947 | (match (assq-ref (derivation-properties drv) 'type) | |
948 | ('profile-hook #t) | |
949 | (_ #f))) | |
950 | ||
8b4615ab LC |
951 | (define (colorize-store-file-name file) |
952 | "Colorize FILE, a store file name, such that the hash part is less prominent | |
953 | that the rest." | |
954 | (let ((len (string-length file)) | |
955 | (prefix (+ (string-length (%store-prefix)) 32 2))) | |
956 | (if (< len prefix) | |
957 | file | |
958 | (string-append (colorize-string (string-take file prefix) | |
959 | (color DARK)) | |
960 | (string-drop file prefix))))) | |
961 | ||
a55d83b5 LC |
962 | (define %default-verbosity |
963 | ;; Default verbosity level for 'show-what-to-build'. | |
964 | 2) | |
965 | ||
dd36b51b | 966 | (define* (show-what-to-build store drv |
58c08df0 | 967 | #:key dry-run? (use-substitutes? #t) |
a55d83b5 | 968 | (verbosity %default-verbosity) |
58c08df0 | 969 | (mode (build-mode normal))) |
9bb2b96a | 970 | "Show what will or would (depending on DRY-RUN?) be built in realizing the |
d38d4ffa LC |
971 | derivations listed in DRV using MODE, a 'build-mode' value. The elements of |
972 | DRV can be either derivations or derivation inputs. | |
973 | ||
883a1765 | 974 | Return two values: a Boolean indicating whether there's something to build, |
a55d83b5 LC |
975 | and a Boolean indicating whether there's something to download. |
976 | ||
977 | When USE-SUBSTITUTES?, check and report what is prerequisites are available | |
978 | for download. VERBOSITY is an integer indicating the level of details to be | |
979 | shown: level 2 and higher provide all the details, level 1 shows a high-level | |
980 | summary, and level 0 shows nothing." | |
d38d4ffa LC |
981 | (define inputs |
982 | (map (match-lambda | |
983 | ((? derivation? drv) (derivation-input drv)) | |
984 | ((? derivation-input? input) input)) | |
985 | drv)) | |
986 | ||
2dc98729 | 987 | (define substitutable-info |
694e10af | 988 | ;; Call 'substitution-oracle' upfront so we don't end up launching the |
e9651e39 LC |
989 | ;; substituter many times. This makes a big difference, especially when |
990 | ;; DRV is a long list as is the case with 'guix environment'. | |
991 | (if use-substitutes? | |
d74392a8 | 992 | (substitution-oracle store inputs #:mode mode) |
e9651e39 LC |
993 | (const #f))) |
994 | ||
8b4615ab LC |
995 | (define colorized-store-item |
996 | (if (color-output? (current-error-port)) | |
997 | colorize-store-file-name | |
998 | identity)) | |
999 | ||
bec34741 | 1000 | (let*-values (((build/full download) |
d38d4ffa | 1001 | (derivation-build-plan store inputs |
9844d009 LC |
1002 | #:mode mode |
1003 | #:substitutable-info | |
1004 | substitutable-info)) | |
80eebee9 | 1005 | ((graft hook build) |
9844d009 LC |
1006 | (match (fold (lambda (drv acc) |
1007 | (let ((file (derivation-file-name drv))) | |
80eebee9 RW |
1008 | (match acc |
1009 | ((#:graft graft #:hook hook #:build build) | |
1010 | (cond | |
1011 | ((graft-derivation? drv) | |
1012 | `(#:graft ,(cons file graft) | |
1013 | #:hook ,hook | |
1014 | #:build ,build)) | |
1015 | ((profile-hook-derivation? drv) | |
1016 | `(#:graft ,graft | |
1017 | #:hook ,(cons file hook) | |
1018 | #:build ,build)) | |
1019 | (else | |
1020 | `(#:graft ,graft | |
1021 | #:hook ,hook | |
1022 | #:build ,(cons file build)))))))) | |
1023 | '(#:graft () #:hook () #:build ()) | |
bec34741 | 1024 | build/full) |
80eebee9 RW |
1025 | ((#:graft graft #:hook hook #:build build) |
1026 | (values graft hook build))))) | |
dcfc6f21 LC |
1027 | (define installed-size |
1028 | (reduce + 0 (map substitutable-nar-size download))) | |
1029 | ||
d9bad2f0 LC |
1030 | (define download-size |
1031 | (/ (reduce + 0 (map substitutable-download-size download)) | |
1032 | 1e6)) | |
1033 | ||
1034 | (define display-download-size? | |
1035 | ;; Sometimes narinfos lack information about the download size. Only | |
1036 | ;; display when we have information for all of DOWNLOAD. | |
1037 | (not (any (compose zero? substitutable-download-size) download))) | |
1038 | ||
a55d83b5 LC |
1039 | ;; Combinatorial explosion ahead along two axes: DRY-RUN? and VERBOSITY. |
1040 | ;; Unfortunately, this is hardly avoidable for proper i18n. | |
9bb2b96a | 1041 | (if dry-run? |
dd36b51b | 1042 | (begin |
a55d83b5 LC |
1043 | (unless (zero? verbosity) |
1044 | (format (current-error-port) | |
1045 | (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]" | |
1046 | "~:[The following derivations would be built:~%~{ ~a~%~}~;~]" | |
1047 | (length build)) | |
1048 | (null? build) (map colorized-store-item build))) | |
1049 | (cond ((>= verbosity 2) | |
1050 | (if display-download-size? | |
1051 | (format (current-error-port) | |
1052 | ;; TRANSLATORS: "MB" is for "megabyte"; it should be | |
1053 | ;; translated to the corresponding abbreviation. | |
1054 | (G_ "~:[~,1h MB would be downloaded:~%~{ ~a~%~}~;~]") | |
1055 | (null? download) | |
1056 | download-size | |
1057 | (map (compose colorized-store-item substitutable-path) | |
1058 | download)) | |
1059 | (format (current-error-port) | |
1060 | (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]" | |
1061 | "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]" | |
1062 | (length download)) | |
1063 | (null? download) | |
1064 | (map (compose colorized-store-item substitutable-path) | |
1065 | download))) | |
1066 | (format (current-error-port) | |
1067 | (N_ "~:[The following graft would be made:~%~{ ~a~%~}~;~]" | |
1068 | "~:[The following grafts would be made:~%~{ ~a~%~}~;~]" | |
1069 | (length graft)) | |
1070 | (null? graft) (map colorized-store-item graft)) | |
1071 | (format (current-error-port) | |
1072 | (N_ "~:[The following profile hook would be built:~%~{ ~a~%~}~;~]" | |
1073 | "~:[The following profile hooks would be built:~%~{ ~a~%~}~;~]" | |
1074 | (length hook)) | |
1075 | (null? hook) (map colorized-store-item hook))) | |
1076 | ((= verbosity 1) | |
1077 | ;; Display the bare minimum; don't mention grafts and hooks. | |
846403ef LC |
1078 | (unless (null? build) |
1079 | (newline (current-error-port))) | |
a55d83b5 LC |
1080 | (if display-download-size? |
1081 | (format (current-error-port) | |
1082 | ;; TRANSLATORS: "MB" is for "megabyte"; it should be | |
1083 | ;; translated to the corresponding abbreviation. | |
846403ef | 1084 | (highlight (G_ "~:[~,1h MB would be downloaded~%~;~]")) |
a55d83b5 LC |
1085 | (null? download) download-size) |
1086 | (format (current-error-port) | |
846403ef LC |
1087 | (highlight |
1088 | (N_ "~:[~h item would be downloaded~%~;~]" | |
1089 | "~:[~h items would be downloaded~%~;~]" | |
1090 | (length download))) | |
a55d83b5 LC |
1091 | (null? download) (length download)))))) |
1092 | ||
dd36b51b | 1093 | (begin |
a55d83b5 LC |
1094 | (unless (zero? verbosity) |
1095 | (format (current-error-port) | |
1096 | (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]" | |
1097 | "~:[The following derivations will be built:~%~{ ~a~%~}~;~]" | |
1098 | (length build)) | |
1099 | (null? build) (map colorized-store-item build))) | |
1100 | (cond ((>= verbosity 2) | |
1101 | (if display-download-size? | |
1102 | (format (current-error-port) | |
1103 | ;; TRANSLATORS: "MB" is for "megabyte"; it should be | |
1104 | ;; translated to the corresponding abbreviation. | |
1105 | (G_ "~:[~,1h MB will be downloaded:~%~{ ~a~%~}~;~]") | |
1106 | (null? download) | |
1107 | download-size | |
1108 | (map (compose colorized-store-item substitutable-path) | |
1109 | download)) | |
1110 | (format (current-error-port) | |
1111 | (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]" | |
1112 | "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]" | |
1113 | (length download)) | |
1114 | (null? download) | |
1115 | (map (compose colorized-store-item substitutable-path) | |
1116 | download))) | |
1117 | (format (current-error-port) | |
1118 | (N_ "~:[The following graft will be made:~%~{ ~a~%~}~;~]" | |
1119 | "~:[The following grafts will be made:~%~{ ~a~%~}~;~]" | |
1120 | (length graft)) | |
1121 | (null? graft) (map colorized-store-item graft)) | |
1122 | (format (current-error-port) | |
1123 | (N_ "~:[The following profile hook will be built:~%~{ ~a~%~}~;~]" | |
1124 | "~:[The following profile hooks will be built:~%~{ ~a~%~}~;~]" | |
1125 | (length hook)) | |
1126 | (null? hook) (map colorized-store-item hook))) | |
1127 | ((= verbosity 1) | |
1128 | ;; Display the bare minimum; don't mention grafts and hooks. | |
846403ef LC |
1129 | (unless (null? build) |
1130 | (newline (current-error-port))) | |
a55d83b5 LC |
1131 | (if display-download-size? |
1132 | (format (current-error-port) | |
1133 | ;; TRANSLATORS: "MB" is for "megabyte"; it should be | |
1134 | ;; translated to the corresponding abbreviation. | |
846403ef | 1135 | (highlight (G_ "~:[~,1h MB will be downloaded~%~;~]")) |
a55d83b5 LC |
1136 | (null? download) download-size) |
1137 | (format (current-error-port) | |
846403ef LC |
1138 | (highlight |
1139 | (N_ "~:[~h item will be downloaded~%~;~]" | |
1140 | "~:[~h items will be downloaded~%~;~]" | |
1141 | (length download))) | |
a55d83b5 | 1142 | (null? download) (length download))))))) |
dcfc6f21 LC |
1143 | |
1144 | (check-available-space installed-size) | |
1145 | ||
883a1765 | 1146 | (values (pair? build/full) (pair? download)))) |
9bb2b96a | 1147 | |
4d043ab6 DT |
1148 | (define show-what-to-build* |
1149 | (store-lift show-what-to-build)) | |
1150 | ||
a55d83b5 LC |
1151 | (define* (build-notifier #:key (dry-run? #f) (use-substitutes? #t) |
1152 | (verbosity %default-verbosity)) | |
07ce23e0 LC |
1153 | "Return a procedure suitable for 'with-build-handler' that, when |
1154 | 'build-things' is called, invokes 'show-what-to-build' to display the build | |
1155 | plan. When DRY-RUN? is true, the 'with-build-handler' form returns without | |
1156 | any build happening." | |
1157 | (define not-comma | |
1158 | (char-set-complement (char-set #\,))) | |
1159 | ||
1160 | (define (read-derivation-from-file* item) | |
1161 | (catch 'system-error | |
1162 | (lambda () | |
1163 | (read-derivation-from-file item)) | |
1164 | (const #f))) | |
1165 | ||
1166 | (lambda (continue store things mode) | |
1167 | (define inputs | |
1168 | ;; List of derivation inputs to build. Filter out non-existent '.drv' | |
1169 | ;; files because the daemon transparently tries to substitute them. | |
1170 | (filter-map (match-lambda | |
1171 | (((? derivation-path? drv) . output) | |
1172 | (let ((drv (read-derivation-from-file* drv)) | |
1173 | (outputs (string-tokenize output not-comma))) | |
1174 | (and drv (derivation-input drv outputs)))) | |
1175 | ((? derivation-path? drv) | |
1176 | (and=> (read-derivation-from-file* drv) | |
1177 | derivation-input)) | |
1178 | (_ | |
1179 | #f)) | |
1180 | things)) | |
1181 | ||
9b771305 LC |
1182 | (let-values (((build? download?) |
1183 | (show-what-to-build store inputs | |
1184 | #:dry-run? dry-run? | |
1185 | #:use-substitutes? use-substitutes? | |
a55d83b5 | 1186 | #:verbosity verbosity |
9b771305 LC |
1187 | #:mode mode))) |
1188 | ||
1189 | (unless (and (or build? download?) | |
1190 | dry-run?) | |
1191 | (continue #t))))) | |
07ce23e0 | 1192 | |
5d7a8584 AK |
1193 | (define (right-arrow port) |
1194 | "Return either a string containing the 'RIGHT ARROW' character, or an ASCII | |
1195 | replacement if PORT is not Unicode-capable." | |
1062063a LC |
1196 | (let ((encoding (port-encoding port)) |
1197 | (arrow "→")) | |
1198 | (catch 'encoding-error | |
1199 | (lambda () | |
1200 | (call-with-output-string | |
1201 | (lambda (port) | |
1202 | (set-port-encoding! port encoding) | |
1203 | (set-port-conversion-strategy! port 'error) | |
1204 | (display arrow port)))) | |
1205 | (lambda (key . args) | |
1206 | "->")))) | |
5d7a8584 | 1207 | |
3e5ab0a7 LC |
1208 | (define* (tabulate rows #:key (initial-indent 0) (max-width 25) |
1209 | (inter-column " ")) | |
1210 | "Return a list of strings where each string is a tabulated representation of | |
1211 | an element of ROWS. All the ROWS must be lists of the same number of cells. | |
1212 | ||
1213 | Add INITIAL-INDENT white space at the beginning of each row. Ensure that | |
1214 | columns are at most MAX-WIDTH characters wide. Use INTER-COLUMN as a | |
1215 | separator between subsequent columns." | |
1216 | (define column-widths | |
1217 | ;; List of column widths. | |
1218 | (let loop ((rows rows) | |
1219 | (widths '())) | |
1220 | (match rows | |
1221 | (((? null?) ...) | |
1222 | (reverse widths)) | |
1223 | (((column rest ...) ...) | |
1224 | (loop rest | |
1225 | (cons (min (apply max (map string-length column)) | |
1226 | max-width) | |
1227 | widths)))))) | |
1228 | ||
1229 | (define indent | |
1230 | (make-string initial-indent #\space)) | |
1231 | ||
1232 | (define (string-pad-right* str len) | |
1233 | (if (> (string-length str) len) | |
1234 | str | |
1235 | (string-pad-right str len))) | |
1236 | ||
1237 | (map (lambda (row) | |
1238 | (string-trim-right | |
1239 | (string-append indent | |
1240 | (string-join | |
1241 | (map string-pad-right* row column-widths) | |
1242 | inter-column)))) | |
1243 | rows)) | |
1244 | ||
5d7a8584 AK |
1245 | (define* (show-manifest-transaction store manifest transaction |
1246 | #:key dry-run?) | |
1247 | "Display what will/would be installed/removed from MANIFEST by TRANSACTION." | |
7d3eb5e6 | 1248 | (define* (package-strings names versions outputs #:key old-versions) |
140da556 MC |
1249 | (tabulate (stable-sort |
1250 | (zip (map (lambda (name output) | |
1251 | (if (string=? output "out") | |
1252 | name | |
1253 | (string-append name ":" output))) | |
1254 | names outputs) | |
1255 | (if old-versions | |
1256 | (map (lambda (old new) | |
1257 | (if (string=? old new) | |
1258 | (G_ "(dependencies or package changed)") | |
1259 | (string-append old " " → " " new))) | |
1260 | old-versions versions) | |
1261 | versions)) | |
1262 | (lambda (x y) | |
1263 | (string<? (first x) (first y)))) | |
3e5ab0a7 | 1264 | #:initial-indent 3)) |
5d7a8584 | 1265 | |
7d3eb5e6 MC |
1266 | (define → ;an arrow that can be represented on stderr |
1267 | (right-arrow (current-error-port))) | |
1268 | ||
46b23e1a | 1269 | (let-values (((remove install upgrade downgrade) |
5d7a8584 AK |
1270 | (manifest-transaction-effects manifest transaction))) |
1271 | (match remove | |
1272 | ((($ <manifest-entry> name version output item) ..1) | |
1273 | (let ((len (length name)) | |
8465f1f6 | 1274 | (remove (package-strings name version output))) |
5d7a8584 AK |
1275 | (if dry-run? |
1276 | (format (current-error-port) | |
1277 | (N_ "The following package would be removed:~%~{~a~%~}~%" | |
1278 | "The following packages would be removed:~%~{~a~%~}~%" | |
1279 | len) | |
1280 | remove) | |
1281 | (format (current-error-port) | |
1282 | (N_ "The following package will be removed:~%~{~a~%~}~%" | |
1283 | "The following packages will be removed:~%~{~a~%~}~%" | |
1284 | len) | |
1285 | remove)))) | |
30906825 | 1286 | (x #f)) |
46b23e1a LC |
1287 | (match downgrade |
1288 | (((($ <manifest-entry> name old-version) | |
1289 | . ($ <manifest-entry> _ new-version output item)) ..1) | |
1290 | (let ((len (length name)) | |
7d3eb5e6 MC |
1291 | (downgrade (package-strings name new-version output |
1292 | #:old-versions old-version))) | |
46b23e1a LC |
1293 | (if dry-run? |
1294 | (format (current-error-port) | |
1295 | (N_ "The following package would be downgraded:~%~{~a~%~}~%" | |
1296 | "The following packages would be downgraded:~%~{~a~%~}~%" | |
1297 | len) | |
1298 | downgrade) | |
1299 | (format (current-error-port) | |
1300 | (N_ "The following package will be downgraded:~%~{~a~%~}~%" | |
1301 | "The following packages will be downgraded:~%~{~a~%~}~%" | |
1302 | len) | |
1303 | downgrade)))) | |
30906825 | 1304 | (x #f)) |
5d7a8584 AK |
1305 | (match upgrade |
1306 | (((($ <manifest-entry> name old-version) | |
1307 | . ($ <manifest-entry> _ new-version output item)) ..1) | |
1308 | (let ((len (length name)) | |
7d3eb5e6 MC |
1309 | (upgrade (package-strings name new-version output |
1310 | #:old-versions old-version))) | |
5d7a8584 AK |
1311 | (if dry-run? |
1312 | (format (current-error-port) | |
1313 | (N_ "The following package would be upgraded:~%~{~a~%~}~%" | |
1314 | "The following packages would be upgraded:~%~{~a~%~}~%" | |
1315 | len) | |
1316 | upgrade) | |
1317 | (format (current-error-port) | |
1318 | (N_ "The following package will be upgraded:~%~{~a~%~}~%" | |
1319 | "The following packages will be upgraded:~%~{~a~%~}~%" | |
1320 | len) | |
1321 | upgrade)))) | |
30906825 | 1322 | (x #f)) |
5d7a8584 AK |
1323 | (match install |
1324 | ((($ <manifest-entry> name version output item _) ..1) | |
1325 | (let ((len (length name)) | |
8465f1f6 | 1326 | (install (package-strings name version output))) |
5d7a8584 AK |
1327 | (if dry-run? |
1328 | (format (current-error-port) | |
1329 | (N_ "The following package would be installed:~%~{~a~%~}~%" | |
1330 | "The following packages would be installed:~%~{~a~%~}~%" | |
1331 | len) | |
1332 | install) | |
1333 | (format (current-error-port) | |
1334 | (N_ "The following package will be installed:~%~{~a~%~}~%" | |
1335 | "The following packages will be installed:~%~{~a~%~}~%" | |
1336 | len) | |
1337 | install)))) | |
30906825 | 1338 | (x #f)))) |
5d7a8584 | 1339 | |
073c34d7 LC |
1340 | (define-syntax with-error-handling |
1341 | (syntax-rules () | |
1342 | "Run BODY within a user-friendly error condition handler." | |
1343 | ((_ body ...) | |
1344 | (call-with-error-handling | |
1345 | (lambda () | |
1346 | body ...))))) | |
1347 | ||
7b322d3c LC |
1348 | (define* (indented-string str indent |
1349 | #:key (initial-indent? #t)) | |
694e10af | 1350 | "Return STR with each newline preceded by INDENT spaces. When |
7b322d3c LC |
1351 | INITIAL-INDENT? is true, the first line is also indented." |
1352 | (define indent-string | |
1353 | (make-list indent #\space)) | |
1354 | ||
1355 | (list->string | |
1356 | (string-fold-right (lambda (chr result) | |
1357 | (if (eqv? chr #\newline) | |
1358 | (cons chr (append indent-string result)) | |
1359 | (cons chr result))) | |
1360 | '() | |
1361 | (if initial-indent? | |
1362 | (string-append (list->string indent-string) str) | |
1363 | str)))) | |
1364 | ||
299112d3 LC |
1365 | (define* (fill-paragraph str width #:optional (column 0)) |
1366 | "Fill STR such that each line contains at most WIDTH characters, assuming | |
1367 | that the first character is at COLUMN. | |
1368 | ||
1369 | When STR contains a single line break surrounded by other characters, it is | |
1370 | converted to a space; sequences of more than one line break are preserved." | |
1371 | (define (maybe-break chr result) | |
1372 | (match result | |
1373 | ((column newlines chars) | |
1374 | (case chr | |
1375 | ((#\newline) | |
1376 | `(,column ,(+ 1 newlines) ,chars)) | |
1377 | (else | |
3a09e1d2 CS |
1378 | (let* ((spaces (if (and (pair? chars) (eqv? (car chars) #\.)) 2 1)) |
1379 | (chars (case newlines | |
1380 | ((0) chars) | |
1381 | ((1) | |
1382 | (append (make-list spaces #\space) chars)) | |
1383 | (else | |
1384 | (append (make-list newlines #\newline) chars)))) | |
1385 | (column (case newlines | |
1386 | ((0) column) | |
1387 | ((1) (+ spaces column)) | |
1388 | (else 0)))) | |
299112d3 LC |
1389 | (let ((chars (cons chr chars)) |
1390 | (column (+ 1 column))) | |
1391 | (if (> column width) | |
1392 | (let*-values (((before after) | |
1393 | (break (cut eqv? #\space <>) chars)) | |
1394 | ((len) | |
1395 | (length before))) | |
1396 | (if (<= len width) | |
1397 | `(,len | |
1398 | 0 | |
1399 | ,(if (null? after) | |
1400 | before | |
3a09e1d2 CS |
1401 | (append before |
1402 | (cons #\newline | |
1403 | (drop-while (cut eqv? #\space <>) | |
1404 | after))))) | |
299112d3 LC |
1405 | `(,column 0 ,chars))) ; unbreakable |
1406 | `(,column 0 ,chars))))))))) | |
1407 | ||
1408 | (match (string-fold maybe-break | |
1409 | `(,column 0 ()) | |
1410 | str) | |
30906825 | 1411 | ((column newlines chars) |
299112d3 LC |
1412 | (list->string (reverse chars))))) |
1413 | ||
2876b989 LC |
1414 | \f |
1415 | ;;; | |
1416 | ;;; Packages. | |
1417 | ;;; | |
1418 | ||
1cd4027c | 1419 | (define %text-width |
3c1ea8dc LC |
1420 | ;; '*line-width*' was introduced in Guile 2.2.7/3.0.1. On older versions of |
1421 | ;; Guile, monkey-patch 'wrap*' below. | |
1422 | (if (defined? '*line-width*) | |
1423 | (let ((parameter (fluid->parameter *line-width*))) | |
1424 | (parameter (terminal-columns)) | |
1425 | parameter) | |
1426 | (make-parameter (terminal-columns)))) | |
1427 | ||
1428 | (unless (defined? '*line-width*) ;Guile < 2.2.7 | |
1429 | (set! (@@ (texinfo plain-text) wrap*) | |
1430 | ;; XXX: Monkey patch this private procedure to let 'package->recutils' | |
1431 | ;; parameterize the fill of description field correctly. | |
1432 | (lambda strings | |
1433 | (let ((indent (fluid-ref (@@ (texinfo plain-text) *indent*)))) | |
1434 | (fill-string (string-concatenate strings) | |
1435 | #:line-width (%text-width) #:initial-indent indent | |
1436 | #:subsequent-indent indent))))) | |
1cd4027c ML |
1437 | |
1438 | (define (texi->plain-text str) | |
1439 | "Return a plain-text representation of texinfo fragment STR." | |
08d7e359 LC |
1440 | ;; 'texi-fragment->stexi' uses a string port so make sure it's a |
1441 | ;; Unicode-capable one (see <http://bugs.gnu.org/11197>.) | |
1442 | (with-fluids ((%default-port-encoding "UTF-8")) | |
1443 | (stexi->plain-text (texi-fragment->stexi str)))) | |
1cd4027c | 1444 | |
689db38e AK |
1445 | (define (package-field-string package field-accessor) |
1446 | "Return a plain-text representation of PACKAGE field." | |
1447 | (and=> (field-accessor package) | |
1448 | (compose texi->plain-text P_))) | |
1449 | ||
1cd4027c ML |
1450 | (define (package-description-string package) |
1451 | "Return a plain-text representation of PACKAGE description field." | |
689db38e AK |
1452 | (package-field-string package package-description)) |
1453 | ||
1454 | (define (package-synopsis-string package) | |
1455 | "Return a plain-text representation of PACKAGE synopsis field." | |
1456 | (package-field-string package package-synopsis)) | |
1cd4027c | 1457 | |
299112d3 LC |
1458 | (define (string->recutils str) |
1459 | "Return a version of STR where newlines have been replaced by newlines | |
1460 | followed by \"+ \", which makes for a valid multi-line field value in the | |
1461 | `recutils' syntax." | |
1462 | (list->string | |
1463 | (string-fold-right (lambda (chr result) | |
1464 | (if (eqv? chr #\newline) | |
1465 | (cons* chr #\+ #\space result) | |
1466 | (cons chr result))) | |
1467 | '() | |
1468 | str))) | |
1469 | ||
7f0f38b5 LC |
1470 | (define (hyperlink uri text) |
1471 | "Return a string that denotes a hyperlink using an OSC escape sequence as | |
1472 | documented at | |
1473 | <https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda>." | |
1474 | (string-append "\x1b]8;;" uri "\x1b\\" | |
1475 | text "\x1b]8;;\x1b\\")) | |
1476 | ||
055f0525 | 1477 | (define* (supports-hyperlinks? #:optional (port (current-output-port))) |
7f0f38b5 LC |
1478 | "Return true if PORT is a terminal that supports hyperlink escapes." |
1479 | ;; Note that terminals are supposed to ignore OSC escapes they don't | |
1480 | ;; understand (this is the case of xterm as of version 349, for instance.) | |
1481 | ;; However, Emacs comint as of 26.3 does not ignore it and instead lets it | |
1482 | ;; through, hence the 'INSIDE_EMACS' special case below. | |
1483 | (and (isatty?* port) | |
1484 | (not (getenv "INSIDE_EMACS")))) | |
1485 | ||
fa983b82 LC |
1486 | (define* (file-hyperlink file #:optional (text file)) |
1487 | "Return TEXT with escapes for a hyperlink to FILE." | |
1488 | (hyperlink (string-append "file://" (gethostname) | |
1489 | (encode-and-join-uri-path | |
1490 | (string-split file #\/))) | |
1491 | text)) | |
1492 | ||
7f0f38b5 LC |
1493 | (define (location->hyperlink location) |
1494 | "Return a string corresponding to LOCATION, with escapes for a hyperlink." | |
1495 | (let ((str (location->string location)) | |
1496 | (file (if (string-prefix? "/" (location-file location)) | |
1497 | (location-file location) | |
1498 | (search-path %load-path (location-file location))))) | |
1499 | (if file | |
fa983b82 | 1500 | (file-hyperlink file str) |
7f0f38b5 LC |
1501 | str))) |
1502 | ||
4ee79f22 | 1503 | (define* (package->recutils p port #:optional (width (%text-width)) |
7f0f38b5 LC |
1504 | #:key |
1505 | (hyperlinks? (supports-hyperlinks? port)) | |
1506 | (extra-fields '())) | |
299112d3 | 1507 | "Write to PORT a `recutils' record of package P, arranging to fit within |
7f0f38b5 LC |
1508 | WIDTH columns. EXTRA-FIELDS is a list of symbol/value pairs to emit. When |
1509 | HYPERLINKS? is true, emit hyperlink escape sequences when appropriate." | |
069d43a7 LC |
1510 | (define width* |
1511 | ;; The available number of columns once we've taken into account space for | |
1512 | ;; the initial "+ " prefix. | |
1513 | (if (> width 2) (- width 2) width)) | |
1514 | ||
20ffce82 | 1515 | (define (dependencies->recutils packages) |
dbde386e SB |
1516 | (let ((list (string-join (delete-duplicates |
1517 | (map package-full-name | |
1518 | (sort packages package<?))) " "))) | |
20ffce82 | 1519 | (string->recutils |
069d43a7 | 1520 | (fill-paragraph list width* |
20ffce82 LC |
1521 | (string-length "dependencies: "))))) |
1522 | ||
9c0fc279 CR |
1523 | (define (package<? p1 p2) |
1524 | (string<? (package-full-name p1) (package-full-name p2))) | |
1525 | ||
299112d3 LC |
1526 | ;; Note: Don't i18n field names so that people can post-process it. |
1527 | (format port "name: ~a~%" (package-name p)) | |
1528 | (format port "version: ~a~%" (package-version p)) | |
6bf99bac | 1529 | (format port "outputs: ~a~%" (string-join (package-outputs p))) |
5763ad92 LC |
1530 | (format port "systems: ~a~%" |
1531 | (string-join (package-transitive-supported-systems p))) | |
9c0fc279 CR |
1532 | (format port "dependencies: ~a~%" |
1533 | (match (package-direct-inputs p) | |
5e6feee6 EB |
1534 | (((labels inputs . _) ...) |
1535 | (dependencies->recutils (filter package? inputs))))) | |
299112d3 | 1536 | (format port "location: ~a~%" |
7f0f38b5 LC |
1537 | (or (and=> (package-location p) |
1538 | (if hyperlinks? location->hyperlink location->string)) | |
69daee23 | 1539 | (G_ "unknown"))) |
8fa3e6b3 LC |
1540 | |
1541 | ;; Note: Starting from version 1.6 or recutils, hyphens are not allowed in | |
1542 | ;; field identifiers. | |
1543 | (format port "homepage: ~a~%" (package-home-page p)) | |
1544 | ||
299112d3 LC |
1545 | (format port "license: ~a~%" |
1546 | (match (package-license p) | |
1547 | (((? license? licenses) ...) | |
1548 | (string-join (map license-name licenses) | |
1549 | ", ")) | |
1550 | ((? license? license) | |
edd25a8c LC |
1551 | (let ((text (license-name license)) |
1552 | (uri (license-uri license))) | |
1553 | (if (and hyperlinks? uri (string-prefix? "http" uri)) | |
1554 | (hyperlink uri text) | |
1555 | text))) | |
299112d3 | 1556 | (x |
69daee23 | 1557 | (G_ "unknown")))) |
299112d3 LC |
1558 | (format port "synopsis: ~a~%" |
1559 | (string-map (match-lambda | |
4ee79f22 LC |
1560 | (#\newline #\space) |
1561 | (chr chr)) | |
a1da0904 | 1562 | (or (package-synopsis-string p) ""))) |
4ee79f22 | 1563 | (format port "~a~%" |
1cd4027c ML |
1564 | (string->recutils |
1565 | (string-trim-right | |
069d43a7 | 1566 | (parameterize ((%text-width width*)) |
31522dfa LC |
1567 | ;; Call 'texi->plain-text' on the concatenated string to account |
1568 | ;; for the width of "description:" in paragraph filling. | |
1569 | (texi->plain-text | |
1570 | (string-append "description: " | |
1571 | (or (and=> (package-description p) P_) | |
1572 | "")))) | |
4ee79f22 LC |
1573 | #\newline))) |
1574 | (for-each (match-lambda | |
1575 | ((field . value) | |
1576 | (let ((field (symbol->string field))) | |
1577 | (format port "~a: ~a~%" | |
1578 | field | |
1579 | (fill-paragraph (object->string value) width* | |
1580 | (string-length field)))))) | |
1581 | extra-fields) | |
1582 | (newline port)) | |
299112d3 | 1583 | |
4311cf96 LC |
1584 | \f |
1585 | ;;; | |
1586 | ;;; Searching. | |
1587 | ;;; | |
1588 | ||
c7ae219e LC |
1589 | (define (relevance obj regexps metrics) |
1590 | "Compute a \"relevance score\" for OBJ as a function of its number of | |
1591 | matches of REGEXPS and accordingly to METRICS. METRICS is list of | |
387e6b9e CM |
1592 | field/weight pairs, where FIELD is a procedure that returns a string or list |
1593 | of strings describing OBJ, and WEIGHT is a positive integer denoting the | |
1594 | weight of this field in the final score. | |
c7ae219e LC |
1595 | |
1596 | A score of zero means that OBJ does not match any of REGEXPS. The higher the | |
1597 | score, the more relevant OBJ is to REGEXPS." | |
d2cdef65 | 1598 | (define (score regexp str) |
1599 | (fold-matches regexp str 0 | |
1600 | (lambda (m score) | |
1601 | (+ score | |
1602 | (if (string=? (match:substring m) str) | |
1603 | 5 ;exact match | |
1604 | 1))))) | |
1605 | ||
1606 | (define (regexp->score regexp) | |
1607 | (let ((score-regexp (lambda (str) (score regexp str)))) | |
1608 | (fold (lambda (metric relevance) | |
1609 | (match metric | |
1610 | ((field . weight) | |
1611 | (match (field obj) | |
1612 | (#f relevance) | |
1613 | ((? string? str) | |
1614 | (+ relevance (* (score-regexp str) weight))) | |
1615 | ((lst ...) | |
1616 | (+ relevance (* weight (apply + (map score-regexp lst))))))))) | |
1617 | 0 metrics))) | |
1618 | ||
e70a8847 AI |
1619 | (let loop ((regexps regexps) |
1620 | (total-score 0)) | |
1621 | (match regexps | |
1622 | ((head . tail) | |
1623 | (let ((score (regexp->score head))) | |
1624 | ;; Return zero if one of PATTERNS doesn't match. | |
1625 | (if (zero? score) | |
1626 | 0 | |
1627 | (loop tail (+ total-score score))))) | |
1628 | (() total-score)))) | |
c7ae219e LC |
1629 | |
1630 | (define %package-metrics | |
1631 | ;; Metrics used to compute the "relevance score" of a package against a set | |
1632 | ;; of regexps. | |
304a53f6 | 1633 | `((,package-name . 4) |
94aeec0a | 1634 | |
387e6b9e CM |
1635 | ;; Match against uncommon outputs. |
1636 | (,(lambda (package) | |
1637 | (filter (lambda (output) | |
1638 | (not (member output | |
694e10af | 1639 | ;; Some common outputs shared by many packages. |
387e6b9e CM |
1640 | '("out" "doc" "debug" "lib" "include" "bin")))) |
1641 | (package-outputs package))) | |
1642 | . 1) | |
1643 | ||
94aeec0a LC |
1644 | ;; Match regexps on the raw Texinfo since formatting it is quite expensive |
1645 | ;; and doesn't have much of an effect on search results. | |
1646 | (,(lambda (package) | |
1647 | (and=> (package-synopsis package) P_)) . 3) | |
1648 | (,(lambda (package) | |
1649 | (and=> (package-description package) P_)) . 2) | |
1650 | ||
304a53f6 LC |
1651 | (,(lambda (type) |
1652 | (match (and=> (package-location type) location-file) | |
1653 | ((? string? file) (basename file ".scm")) | |
1654 | (#f ""))) | |
1655 | . 1))) | |
c7ae219e LC |
1656 | |
1657 | (define (package-relevance package regexps) | |
1658 | "Return a score denoting the relevance of PACKAGE for REGEXPS. A score of | |
1659 | zero means that PACKAGE does not match any of REGEXPS." | |
1660 | (relevance package regexps %package-metrics)) | |
1661 | ||
9296a2e5 LC |
1662 | (define* (call-with-paginated-output-port proc |
1663 | #:key (less-options "FrX")) | |
c39693d7 LC |
1664 | (if (isatty?* (current-output-port)) |
1665 | ;; Set 'LESS' so that 'less' exits if everything fits on the screen (F), | |
1666 | ;; lets ANSI escapes through (r), does not send the termcap | |
e66f243e LC |
1667 | ;; initialization string (X). Set it unconditionally because some |
1668 | ;; distros set it to something that doesn't work here. | |
9296a2e5 LC |
1669 | ;; |
1670 | ;; For things that produce long lines, such as 'guix processes', use 'R' | |
1671 | ;; instead of 'r': this strips hyperlinks but allows 'less' to make a | |
1672 | ;; good estimate of the line length. | |
1673 | (let ((pager (with-environment-variables `(("LESS" ,less-options)) | |
c39693d7 LC |
1674 | (open-pipe* OPEN_WRITE |
1675 | (or (getenv "GUIX_PAGER") (getenv "PAGER") | |
1676 | "less"))))) | |
1677 | (dynamic-wind | |
1678 | (const #t) | |
1679 | (lambda () (proc pager)) | |
1680 | (lambda () (close-pipe pager)))) | |
1681 | (proc (current-output-port)))) | |
1682 | ||
9296a2e5 LC |
1683 | (define-syntax with-paginated-output-port |
1684 | (syntax-rules () | |
1685 | "Evaluate EXP... with PORT bound to a port that talks to the pager if | |
c39693d7 | 1686 | standard output is a tty, or with PORT set to the current output port." |
9296a2e5 LC |
1687 | ((_ port exp ... #:less-options opts) |
1688 | (call-with-paginated-output-port (lambda (port) exp ...) | |
1689 | #:less-options opts)) | |
1690 | ((_ port exp ...) | |
1691 | (call-with-paginated-output-port (lambda (port) exp ...))))) | |
c39693d7 | 1692 | |
4311cf96 LC |
1693 | (define* (display-search-results matches port |
1694 | #:key | |
1695 | (command "guix search") | |
1696 | (print package->recutils)) | |
1697 | "Display MATCHES, a list of object/score pairs, by calling PRINT on each of | |
1698 | them. If PORT is a terminal, print at most a full screen of results." | |
1699 | (define first-line | |
1700 | (port-line port)) | |
1701 | ||
1702 | (define max-rows | |
1703 | (and first-line (isatty? port) | |
1704 | (terminal-rows port))) | |
1705 | ||
1706 | (define (line-count str) | |
1707 | (string-count str #\newline)) | |
1708 | ||
c39693d7 LC |
1709 | (with-paginated-output-port paginated |
1710 | (let loop ((matches matches)) | |
1711 | (match matches | |
1712 | (((package . score) rest ...) | |
1713 | (let* ((links? (supports-hyperlinks? port))) | |
1714 | (print package paginated | |
1715 | #:hyperlinks? links? | |
1716 | #:extra-fields `((relevance . ,score))) | |
1717 | (loop rest))) | |
1718 | (() | |
1719 | #t))))) | |
4311cf96 LC |
1720 | |
1721 | \f | |
2cd09108 NK |
1722 | (define (string->generations str) |
1723 | "Return the list of generations matching a pattern in STR. This function | |
1724 | accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", \"..9\"." | |
1725 | (define (maybe-integer) | |
1726 | (let ((x (string->number str))) | |
1727 | (and (integer? x) | |
1728 | x))) | |
1729 | ||
1730 | (define (maybe-comma-separated-integers) | |
1731 | (let ((lst (delete-duplicates | |
1732 | (map string->number | |
1733 | (string-split str #\,))))) | |
1734 | (and (every integer? lst) | |
1735 | lst))) | |
1736 | ||
1737 | (cond ((maybe-integer) | |
1738 | => | |
1739 | list) | |
1740 | ((maybe-comma-separated-integers) | |
1741 | => | |
1742 | identity) | |
1743 | ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str) | |
1744 | => | |
1745 | (lambda (match) | |
1746 | (let ((s (string->number (match:substring match 1))) | |
1747 | (e (string->number (match:substring match 2)))) | |
1748 | (and (every integer? (list s e)) | |
1749 | (<= s e) | |
1750 | (iota (1+ (- e s)) s))))) | |
1751 | ((string-match "^([0-9]+)\\.\\.$" str) | |
1752 | => | |
1753 | (lambda (match) | |
1754 | (let ((s (string->number (match:substring match 1)))) | |
1755 | (and (integer? s) | |
1756 | `(>= ,s))))) | |
1757 | ((string-match "^\\.\\.([0-9]+)$" str) | |
1758 | => | |
1759 | (lambda (match) | |
1760 | (let ((e (string->number (match:substring match 1)))) | |
1761 | (and (integer? e) | |
1762 | `(<= ,e))))) | |
1763 | (else #f))) | |
1764 | ||
1765 | (define (string->duration str) | |
1766 | "Return the duration matching a pattern in STR. This function accepts the | |
1767 | following patterns: \"1d\", \"1w\", \"1m\"." | |
1768 | (define (hours->duration hours match) | |
1769 | (make-time time-duration 0 | |
1770 | (* 3600 hours (string->number (match:substring match 1))))) | |
1771 | ||
638c5b79 LC |
1772 | (cond ((string-match "^([0-9]+)s$" str) |
1773 | => | |
1774 | (lambda (match) | |
1775 | (make-time time-duration 0 | |
1776 | (string->number (match:substring match 1))))) | |
1777 | ((string-match "^([0-9]+)h$" str) | |
30d2397f | 1778 | => |
638c5b79 LC |
1779 | (lambda (match) |
1780 | (hours->duration 1 match))) | |
1781 | ((string-match "^([0-9]+)d$" str) | |
2cd09108 NK |
1782 | => |
1783 | (lambda (match) | |
1784 | (hours->duration 24 match))) | |
1785 | ((string-match "^([0-9]+)w$" str) | |
1786 | => | |
1787 | (lambda (match) | |
1788 | (hours->duration (* 24 7) match))) | |
1789 | ((string-match "^([0-9]+)m$" str) | |
1790 | => | |
1791 | (lambda (match) | |
1792 | (hours->duration (* 24 30) match))) | |
1793 | (else #f))) | |
1794 | ||
e49de93a LC |
1795 | (define* (matching-generations str profile |
1796 | #:key (duration-relation <=)) | |
1797 | "Return the list of available generations matching a pattern in STR. See | |
1798 | 'string->generations' and 'string->duration' for the list of valid patterns. | |
1799 | When STR is a duration pattern, return all the generations whose ctime has | |
1800 | DURATION-RELATION with the current time." | |
1801 | (define (valid-generations lst) | |
1802 | (define (valid-generation? n) | |
1803 | (any (cut = n <>) (generation-numbers profile))) | |
1804 | ||
1805 | (fold-right (lambda (x acc) | |
1806 | (if (valid-generation? x) | |
1807 | (cons x acc) | |
1808 | acc)) | |
1809 | '() | |
1810 | lst)) | |
1811 | ||
1812 | (define (filter-generations generations) | |
1813 | (match generations | |
1814 | (() '()) | |
1815 | (('>= n) | |
1816 | (drop-while (cut > n <>) | |
1817 | (generation-numbers profile))) | |
1818 | (('<= n) | |
1819 | (valid-generations (iota n 1))) | |
1820 | ((lst ..1) | |
1821 | (valid-generations lst)) | |
30906825 | 1822 | (x #f))) |
e49de93a LC |
1823 | |
1824 | (define (filter-by-duration duration) | |
1825 | (define (time-at-midnight time) | |
1826 | ;; Return TIME at midnight by setting nanoseconds, seconds, minutes, and | |
1827 | ;; hours to zeros. | |
1828 | (let ((d (time-utc->date time))) | |
1829 | (date->time-utc | |
1830 | (make-date 0 0 0 0 | |
1831 | (date-day d) (date-month d) | |
1832 | (date-year d) (date-zone-offset d))))) | |
1833 | ||
1834 | (define generation-ctime-alist | |
1835 | (map (lambda (number) | |
1836 | (cons number | |
1837 | (time-second | |
1838 | (time-at-midnight | |
1839 | (generation-time profile number))))) | |
1840 | (generation-numbers profile))) | |
1841 | ||
1842 | (match duration | |
1843 | (#f #f) | |
1844 | (res | |
1845 | (let ((s (time-second | |
1846 | (subtract-duration (time-at-midnight (current-time)) | |
1847 | duration)))) | |
1848 | (delete #f (map (lambda (x) | |
1849 | (and (duration-relation s (cdr x)) | |
1850 | (first x))) | |
1851 | generation-ctime-alist)))))) | |
1852 | ||
1853 | (cond ((string->generations str) | |
1854 | => | |
1855 | filter-generations) | |
1856 | ((string->duration str) | |
1857 | => | |
1858 | filter-by-duration) | |
5c3d4430 LC |
1859 | (else |
1860 | (raise | |
d51bfe24 | 1861 | (formatted-message (G_ "invalid syntax: ~a~%") str))))) |
e49de93a | 1862 | |
ad18c7e6 LC |
1863 | (define (display-generation profile number) |
1864 | "Display a one-line summary of generation NUMBER of PROFILE." | |
1865 | (unless (zero? number) | |
055f0525 LC |
1866 | (let* ((file (generation-file-name profile number)) |
1867 | (link (if (supports-hyperlinks?) | |
1868 | (cut file-hyperlink file <>) | |
c2f9ea2b | 1869 | identity)) |
055f0525 LC |
1870 | (header (format #f (link (highlight (G_ "Generation ~a\t~a"))) |
1871 | number | |
1872 | (date->string | |
1873 | (time-utc->date | |
1874 | (generation-time profile number)) | |
1875 | ;; TRANSLATORS: This is a format-string for date->string. | |
1876 | ;; Please choose a format that corresponds to the | |
1877 | ;; usual way of presenting dates in your locale. | |
1878 | ;; See https://www.gnu.org/software/guile/manual/html_node/SRFI_002d19-Date-to-string.html | |
1879 | ;; for details. | |
1880 | (G_ "~b ~d ~Y ~T")))) | |
1881 | (current (generation-number profile))) | |
ad18c7e6 | 1882 | (if (= number current) |
3bd9672c LC |
1883 | ;; TRANSLATORS: The word "current" here is an adjective for |
1884 | ;; "Generation", as in "current generation". Use the appropriate | |
1885 | ;; gender where applicable. | |
69daee23 | 1886 | (format #t (G_ "~a\t(current)~%") header) |
ad18c7e6 LC |
1887 | (format #t "~a~%" header))))) |
1888 | ||
e95ae7c2 | 1889 | (define (display-profile-content-diff profile gen1 gen2) |
dc013aff | 1890 | "Display the changed packages in PROFILE GEN2 compared to generation GEN1." |
e95ae7c2 RJ |
1891 | |
1892 | (define (equal-entry? first second) | |
1893 | (string= (manifest-entry-item first) (manifest-entry-item second))) | |
1894 | ||
1895 | (define (display-entry entry prefix) | |
1896 | (match entry | |
1897 | (($ <manifest-entry> name version output location _) | |
1898 | (format #t " ~a ~a\t~a\t~a\t~a~%" prefix name version output location)))) | |
1899 | ||
1900 | (define (list-entries number) | |
1901 | (manifest-entries (profile-manifest (generation-file-name profile number)))) | |
1902 | ||
1903 | (define (display-diff profile old new) | |
1904 | (display-generation profile new) | |
1905 | (let ((added (lset-difference | |
1906 | equal-entry? (list-entries new) (list-entries old))) | |
1907 | (removed (lset-difference | |
1908 | equal-entry? (list-entries old) (list-entries new)))) | |
1909 | (for-each (cut display-entry <> "+") added) | |
88bdbb2a RJ |
1910 | (for-each (cut display-entry <> "-") removed) |
1911 | (newline))) | |
e95ae7c2 RJ |
1912 | |
1913 | (display-diff profile gen1 gen2)) | |
1914 | ||
55e1dfa4 LC |
1915 | (define (profile-lock-handler profile errno . _) |
1916 | "Handle failure to acquire PROFILE's lock." | |
3f8e890d LC |
1917 | ;; NFS mounts can return ENOLCK. When that happens, there's not much that |
1918 | ;; can be done, so warn the user and keep going. | |
1919 | (if (= errno ENOLCK) | |
1920 | (warning (G_ "cannot lock profile ~a: ~a~%") | |
1921 | profile (strerror errno)) | |
1922 | (leave (G_ "profile ~a is locked by another process~%") | |
1923 | profile))) | |
55e1dfa4 LC |
1924 | |
1925 | (define profile-lock-file | |
1926 | (cut string-append <> ".lock")) | |
1927 | ||
1928 | (define-syntax-rule (with-profile-lock profile exp ...) | |
1929 | "Grab PROFILE's lock and evaluate EXP... Call 'leave' if the lock is | |
1930 | already taken." | |
1931 | (with-file-lock/no-wait (profile-lock-file profile) | |
1932 | (cut profile-lock-handler profile <...>) | |
1933 | exp ...)) | |
1934 | ||
ad18c7e6 LC |
1935 | (define (display-profile-content profile number) |
1936 | "Display the packages in PROFILE, generation NUMBER, in a human-readable | |
1937 | way." | |
1938 | (for-each (match-lambda | |
1939 | (($ <manifest-entry> name version output location _) | |
1940 | (format #t " ~a\t~a\t~a\t~a~%" | |
1941 | name version output location))) | |
1942 | ||
1943 | ;; Show most recently installed packages last. | |
1944 | (reverse | |
1945 | (manifest-entries | |
1946 | (profile-manifest (generation-file-name profile number)))))) | |
1947 | ||
06d45f45 | 1948 | (define (display-generation-change previous current) |
69daee23 | 1949 | (format #t (G_ "switched from generation ~a to ~a~%") previous current)) |
06d45f45 LC |
1950 | |
1951 | (define (roll-back* store profile) | |
1952 | "Like 'roll-back', but display what is happening." | |
1953 | (call-with-values | |
1954 | (lambda () | |
1955 | (roll-back store profile)) | |
1956 | display-generation-change)) | |
1957 | ||
1958 | (define (switch-to-generation* profile number) | |
1959 | "Like 'switch-generation', but display what is happening." | |
1960 | (let ((previous (switch-to-generation profile number))) | |
1961 | (display-generation-change previous number))) | |
1962 | ||
1963 | (define (delete-generation* store profile generation) | |
1964 | "Like 'delete-generation', but display what is going on." | |
69daee23 | 1965 | (format #t (G_ "deleting ~a~%") |
06d45f45 LC |
1966 | (generation-file-name profile generation)) |
1967 | (delete-generation store profile generation)) | |
1968 | ||
2876b989 LC |
1969 | (define* (package-specification->name+version+output spec |
1970 | #:optional (output "out")) | |
1971 | "Parse package specification SPEC and return three value: the specified | |
1972 | package name, version number (or #f), and output name (or OUTPUT). SPEC may | |
1973 | optionally contain a version number and an output name, as in these examples: | |
1974 | ||
1975 | guile | |
1b846da8 | 1976 | guile@2.0.9 |
2876b989 | 1977 | guile:debug |
1b846da8 | 1978 | guile@2.0.9:debug |
2876b989 LC |
1979 | " |
1980 | (let*-values (((name sub-drv) | |
1981 | (match (string-rindex spec #\:) | |
1982 | (#f (values spec output)) | |
1983 | (colon (values (substring spec 0 colon) | |
1984 | (substring spec (+ 1 colon)))))) | |
1985 | ((name version) | |
1986 | (package-name->name+version name))) | |
1987 | (values name version sub-drv))) | |
1988 | ||
1989 | \f | |
1990 | ;;; | |
1991 | ;;; Command-line option processing. | |
1992 | ;;; | |
1993 | ||
e49951eb | 1994 | (define (show-guix-usage) |
e49951eb | 1995 | (format (current-error-port) |
69daee23 | 1996 | (G_ "Try `guix --help' for more information.~%")) |
25c93676 | 1997 | (exit 1)) |
e49951eb | 1998 | |
3794ce93 LC |
1999 | ;; Representation of a 'guix' command. |
2000 | (define-immutable-record-type <command> | |
2001 | (command name synopsis category) | |
2002 | command? | |
2003 | (name command-name) | |
2004 | (synopsis command-synopsis) | |
2005 | (category command-category)) | |
2006 | ||
2007 | (define (source-file-command file) | |
2008 | "Read FILE, a Scheme source file, and return either a <command> object based | |
2009 | on the 'define-command' top-level form found therein, or #f if FILE does not | |
2010 | contain a 'define-command' form." | |
2011 | (define command-name | |
2012 | (match (string-split file #\/) | |
2013 | ((_ ... "guix" "scripts" name) | |
2014 | (list (file-sans-extension name))) | |
2015 | ((_ ... "guix" "scripts" first second) | |
2016 | (list first (file-sans-extension second))))) | |
2017 | ||
2018 | ;; The strategy here is to parse FILE. This is much cheaper than a | |
2019 | ;; technique based on run-time introspection where we'd load FILE and all | |
2020 | ;; the modules it depends on. | |
2021 | (call-with-input-file file | |
2022 | (lambda (port) | |
2023 | (let loop () | |
2024 | (match (read port) | |
2025 | (('define-command _ ('synopsis synopsis) | |
2026 | _ ...) | |
2027 | (command command-name synopsis 'main)) | |
2028 | (('define-command _ | |
2029 | ('category category) ('synopsis synopsis) | |
2030 | _ ...) | |
2031 | (command command-name synopsis category)) | |
2032 | ((? eof-object?) | |
2033 | #f) | |
2034 | (_ | |
2035 | (loop))))))) | |
2036 | ||
e31ff8b8 LC |
2037 | (define (command-files) |
2038 | "Return the list of source files that define Guix sub-commands." | |
2039 | (define directory | |
2040 | (and=> (search-path %load-path "guix.scm") | |
2041 | (compose (cut string-append <> "/guix/scripts") | |
2042 | dirname))) | |
2043 | ||
2b8cf44f LC |
2044 | (define dot-scm? |
2045 | (cut string-suffix? ".scm" <>)) | |
2046 | ||
e31ff8b8 | 2047 | (if directory |
3794ce93 LC |
2048 | (map (cut string-append directory "/" <>) |
2049 | (scandir directory dot-scm?)) | |
e31ff8b8 LC |
2050 | '())) |
2051 | ||
2052 | (define (commands) | |
3794ce93 LC |
2053 | "Return the list of commands, alphabetically sorted." |
2054 | (filter-map source-file-command (command-files))) | |
e31ff8b8 LC |
2055 | |
2056 | (define (show-guix-help) | |
59f734f3 | 2057 | (define (internal? command) |
94d92c77 LC |
2058 | (member command '("substitute" "authenticate" "offload" |
2059 | "perform-download"))) | |
59f734f3 | 2060 | |
3794ce93 LC |
2061 | (define (display-commands commands) |
2062 | (let* ((names (map (lambda (command) | |
2063 | (string-join (command-name command))) | |
2064 | commands)) | |
2065 | (max-width (reduce max 0 (map string-length names)))) | |
2066 | (for-each (lambda (name command) | |
2067 | (format #t " ~a ~a~%" | |
2068 | (string-pad-right name max-width) | |
2069 | (G_ (command-synopsis command)))) | |
2070 | names | |
2071 | commands))) | |
2072 | ||
2073 | (define (category-predicate category) | |
2074 | (lambda (command) | |
2075 | (eq? category (command-category command)))) | |
2076 | ||
69daee23 | 2077 | (format #t (G_ "Usage: guix COMMAND ARGS... |
e31ff8b8 LC |
2078 | Run COMMAND with ARGS.\n")) |
2079 | (newline) | |
69daee23 | 2080 | (format #t (G_ "COMMAND must be one of the sub-commands listed below:\n")) |
3794ce93 LC |
2081 | |
2082 | (let ((commands (commands)) | |
2083 | (categories (module-ref (resolve-interface '(guix scripts)) | |
2084 | '%command-categories))) | |
2085 | (for-each (match-lambda | |
2086 | (('internal . _) | |
2087 | #t) ;hide internal commands | |
2088 | ((category . synopsis) | |
2089 | (format #t "~% ~a~%" (G_ synopsis)) | |
2090 | (display-commands (filter (category-predicate category) | |
2091 | commands)))) | |
2092 | categories)) | |
e31ff8b8 LC |
2093 | (show-bug-report-information)) |
2094 | ||
ec5d0a85 LC |
2095 | (define (run-guix-command command . args) |
2096 | "Run COMMAND with the given ARGS. Report an error when COMMAND is not | |
2097 | found." | |
2098 | (define module | |
2099 | (catch 'misc-error | |
2100 | (lambda () | |
2101 | (resolve-interface `(guix scripts ,command))) | |
2102 | (lambda - | |
25c93676 | 2103 | (format (current-error-port) |
69daee23 | 2104 | (G_ "guix: ~a: command not found~%") command) |
25c93676 | 2105 | (show-guix-usage)))) |
ec5d0a85 LC |
2106 | |
2107 | (let ((command-main (module-ref module | |
2108 | (symbol-append 'guix- command)))) | |
2109 | (parameterize ((program-name command)) | |
14d5ca2e LC |
2110 | ;; Disable canonicalization so we don't don't stat unreasonably. |
2111 | (with-fluids ((%file-port-name-canonicalization #f)) | |
f4453df9 LC |
2112 | (dynamic-wind |
2113 | (const #f) | |
2114 | (lambda () | |
2115 | (apply command-main args)) | |
2116 | (lambda () | |
2117 | ;; Abuse 'exit-hook' (which is normally meant to be used by the | |
2118 | ;; REPL) to run things like profiling hooks upon completion. | |
2119 | (run-hook exit-hook))))))) | |
ec5d0a85 | 2120 | |
caa6732e AK |
2121 | (define (run-guix . args) |
2122 | "Run the 'guix' command defined by command line ARGS. | |
2123 | Unlike 'guix-main', this procedure assumes that locale, i18n support, | |
694e10af | 2124 | and signal handling have already been set up." |
caa6732e AK |
2125 | (define option? (cut string-prefix? "-" <>)) |
2126 | ||
cbee9559 LC |
2127 | ;; The default %LOAD-EXTENSIONS includes the empty string, which doubles the |
2128 | ;; number of 'stat' calls per entry in %LOAD-PATH. Shamelessly remove it. | |
2129 | (set! %load-extensions '(".scm")) | |
2130 | ||
caa6732e AK |
2131 | (match args |
2132 | (() | |
2133 | (format (current-error-port) | |
69daee23 | 2134 | (G_ "guix: missing command name~%")) |
caa6732e AK |
2135 | (show-guix-usage)) |
2136 | ((or ("-h") ("--help")) | |
2137 | (show-guix-help)) | |
85c3fbf5 | 2138 | ((or ("-V") ("--version")) |
caa6732e AK |
2139 | (show-version-and-exit "guix")) |
2140 | (((? option? o) args ...) | |
2141 | (format (current-error-port) | |
69daee23 | 2142 | (G_ "guix: unrecognized option '~a'~%") o) |
caa6732e | 2143 | (show-guix-usage)) |
2ab5fdc4 LC |
2144 | (("help" command) |
2145 | (apply run-guix-command (string->symbol command) | |
2146 | '("--help"))) | |
caa6732e AK |
2147 | (("help" args ...) |
2148 | (show-guix-help)) | |
2149 | ((command args ...) | |
2150 | (apply run-guix-command | |
2151 | (string->symbol command) | |
2152 | args)))) | |
2153 | ||
e49951eb MW |
2154 | (define (guix-main arg0 . args) |
2155 | (initialize-guix) | |
caa6732e | 2156 | (apply run-guix args)) |
e49951eb | 2157 | |
a168c3e4 LC |
2158 | ;;; Local Variables: |
2159 | ;;; eval: (put 'guard* 'scheme-indent-function 2) | |
2160 | ;;; End: | |
2161 | ||
073c34d7 | 2162 | ;;; ui.scm ends here |