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