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