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