Commit | Line | Data |
---|---|---|
233e7676 | 1 | ;;; GNU Guix --- Functional package management for GNU |
8fa3e6b3 | 2 | ;;; Copyright © 2012, 2013, 2014 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> |
073c34d7 | 5 | ;;; |
233e7676 | 6 | ;;; This file is part of GNU Guix. |
073c34d7 | 7 | ;;; |
233e7676 | 8 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
073c34d7 LC |
9 | ;;; under the terms of the GNU General Public License as published by |
10 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
11 | ;;; your option) any later version. | |
12 | ;;; | |
233e7676 | 13 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
073c34d7 LC |
14 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;;; GNU General Public License for more details. | |
17 | ;;; | |
18 | ;;; You should have received a copy of the GNU General Public License | |
233e7676 | 19 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
073c34d7 LC |
20 | |
21 | (define-module (guix ui) | |
22 | #:use-module (guix utils) | |
23 | #:use-module (guix store) | |
cdd5d6f9 | 24 | #:use-module (guix config) |
073c34d7 | 25 | #:use-module (guix packages) |
9b222abe | 26 | #:use-module (guix build-system) |
9bb2b96a | 27 | #:use-module (guix derivations) |
299112d3 LC |
28 | #:use-module ((guix licenses) #:select (license? license-name)) |
29 | #:use-module (srfi srfi-1) | |
30 | #:use-module (srfi srfi-11) | |
2cd09108 | 31 | #:use-module (srfi srfi-19) |
073c34d7 LC |
32 | #:use-module (srfi srfi-26) |
33 | #:use-module (srfi srfi-34) | |
c1d52c71 | 34 | #:use-module (srfi srfi-35) |
a5975ced | 35 | #:use-module (srfi srfi-37) |
e31ff8b8 | 36 | #:autoload (ice-9 ftw) (scandir) |
64fc89b6 | 37 | #:use-module (ice-9 match) |
9bb2b96a | 38 | #:use-module (ice-9 format) |
2cd09108 | 39 | #:use-module (ice-9 regex) |
073c34d7 LC |
40 | #:export (_ |
41 | N_ | |
ee764179 | 42 | P_ |
073c34d7 | 43 | leave |
cdd5d6f9 | 44 | show-version-and-exit |
3441e164 | 45 | show-bug-report-information |
969e678e | 46 | string->number* |
1d6243cf | 47 | size->number |
9bb2b96a | 48 | show-what-to-build |
073c34d7 | 49 | call-with-error-handling |
64fc89b6 | 50 | with-error-handling |
ac5de156 | 51 | read/eval |
eb0880e7 | 52 | read/eval-package-expression |
299112d3 | 53 | location->string |
c61b026e | 54 | switch-symlinks |
f651b477 | 55 | config-directory |
299112d3 LC |
56 | fill-paragraph |
57 | string->recutils | |
e49951eb | 58 | package->recutils |
2876b989 | 59 | package-specification->name+version+output |
2cd09108 NK |
60 | string->generations |
61 | string->duration | |
a5975ced | 62 | args-fold* |
e49951eb | 63 | run-guix-command |
a2011be5 LC |
64 | program-name |
65 | guix-warning-port | |
66 | warning | |
e49951eb | 67 | guix-main)) |
073c34d7 LC |
68 | |
69 | ;;; Commentary: | |
70 | ;;; | |
71 | ;;; User interface facilities for command-line tools. | |
72 | ;;; | |
73 | ;;; Code: | |
74 | ||
75 | (define %gettext-domain | |
ee764179 | 76 | ;; Text domain for strings used in the tools. |
073c34d7 LC |
77 | "guix") |
78 | ||
ee764179 LC |
79 | (define %package-text-domain |
80 | ;; Text domain for package synopses and descriptions. | |
81 | "guix-packages") | |
82 | ||
073c34d7 LC |
83 | (define _ (cut gettext <> %gettext-domain)) |
84 | (define N_ (cut ngettext <> <> <> %gettext-domain)) | |
ee764179 | 85 | (define P_ (cut gettext <> %package-text-domain)) |
073c34d7 | 86 | |
b2a886f6 LC |
87 | (define-syntax-rule (define-diagnostic name prefix) |
88 | "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all | |
89 | messages." | |
90 | (define-syntax name | |
91 | (lambda (x) | |
92 | (define (augmented-format-string fmt) | |
93 | (string-append "~:[~*~;guix ~a: ~]~a" (syntax->datum fmt))) | |
94 | ||
89d02b98 LC |
95 | (syntax-case x () |
96 | ((name (underscore fmt) args (... ...)) | |
97 | (and (string? (syntax->datum #'fmt)) | |
98 | (free-identifier=? #'underscore #'_)) | |
b2a886f6 LC |
99 | (with-syntax ((fmt* (augmented-format-string #'fmt)) |
100 | (prefix (datum->syntax x prefix))) | |
101 | #'(format (guix-warning-port) (gettext fmt*) | |
102 | (program-name) (program-name) prefix | |
103 | args (... ...)))) | |
89d02b98 | 104 | ((name (N-underscore singular plural n) args (... ...)) |
b2a886f6 | 105 | (and (string? (syntax->datum #'singular)) |
89d02b98 LC |
106 | (string? (syntax->datum #'plural)) |
107 | (free-identifier=? #'N-underscore #'N_)) | |
b2a886f6 LC |
108 | (with-syntax ((s (augmented-format-string #'singular)) |
109 | (p (augmented-format-string #'plural)) | |
110 | (prefix (datum->syntax x prefix))) | |
111 | #'(format (guix-warning-port) | |
112 | (ngettext s p n %gettext-domain) | |
113 | (program-name) (program-name) prefix | |
114 | args (... ...)))))))) | |
115 | ||
116 | (define-diagnostic warning "warning: ") ; emit a warning | |
117 | ||
118 | (define-diagnostic report-error "error: ") | |
119 | (define-syntax-rule (leave args ...) | |
120 | "Emit an error message and exit." | |
121 | (begin | |
122 | (report-error args ...) | |
123 | (exit 1))) | |
124 | ||
125 | (define (install-locale) | |
126 | "Install the current locale settings." | |
127 | (catch 'system-error | |
128 | (lambda _ | |
129 | (setlocale LC_ALL "")) | |
130 | (lambda args | |
131 | (warning (_ "failed to install locale: ~a~%") | |
132 | (strerror (system-error-errno args)))))) | |
133 | ||
e49951eb | 134 | (define (initialize-guix) |
633f045f | 135 | "Perform the usual initialization for stand-alone Guix commands." |
e49951eb | 136 | (install-locale) |
39e9f95d | 137 | (textdomain %gettext-domain) |
e14c3929 LC |
138 | |
139 | ;; Ignore SIGPIPE. If the daemon closes the connection, we prefer to be | |
140 | ;; notified via an EPIPE later. | |
141 | (sigaction SIGPIPE SIG_IGN) | |
142 | ||
e49951eb MW |
143 | (setvbuf (current-output-port) _IOLBF) |
144 | (setvbuf (current-error-port) _IOLBF)) | |
145 | ||
cdd5d6f9 LC |
146 | (define* (show-version-and-exit #:optional (command (car (command-line)))) |
147 | "Display version information for COMMAND and `(exit 0)'." | |
148 | (simple-format #t "~a (~a) ~a~%" | |
149 | command %guix-package-name %guix-version) | |
c5e0eb28 | 150 | (display (_ "Copyright (C) 2014 the Guix authors |
64a967cc LC |
151 | License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> |
152 | This is free software: you are free to change and redistribute it. | |
153 | There is NO WARRANTY, to the extent permitted by law. | |
154 | ")) | |
cdd5d6f9 LC |
155 | (exit 0)) |
156 | ||
3441e164 LC |
157 | (define (show-bug-report-information) |
158 | (format #t (_ " | |
159 | Report bugs to: ~a.") %guix-bug-report-address) | |
160 | (format #t (_ " | |
161 | ~a home page: <~a>") %guix-package-name %guix-home-page-url) | |
162 | (display (_ " | |
163 | General help using GNU software: <http://www.gnu.org/gethelp/>")) | |
164 | (newline)) | |
165 | ||
969e678e LC |
166 | (define (string->number* str) |
167 | "Like `string->number', but error out with an error message on failure." | |
168 | (or (string->number str) | |
169 | (leave (_ "~a: invalid number~%") str))) | |
170 | ||
1d6243cf LC |
171 | (define (size->number str) |
172 | "Convert STR, a storage measurement representation such as \"1024\" or | |
173 | \"1MiB\", to a number of bytes. Raise an error if STR could not be | |
174 | interpreted." | |
175 | (define unit-pos | |
176 | (string-rindex str char-set:digit)) | |
177 | ||
178 | (define unit | |
179 | (and unit-pos (substring str (+ 1 unit-pos)))) | |
180 | ||
181 | (let* ((numstr (if unit-pos | |
182 | (substring str 0 (+ 1 unit-pos)) | |
183 | str)) | |
184 | (num (string->number numstr))) | |
185 | (unless num | |
186 | (leave (_ "invalid number: ~a~%") numstr)) | |
187 | ||
188 | ((compose inexact->exact round) | |
189 | (* num | |
190 | (match unit | |
191 | ("KiB" (expt 2 10)) | |
192 | ("MiB" (expt 2 20)) | |
193 | ("GiB" (expt 2 30)) | |
194 | ("TiB" (expt 2 40)) | |
195 | ("KB" (expt 10 3)) | |
196 | ("MB" (expt 10 6)) | |
197 | ("GB" (expt 10 9)) | |
198 | ("TB" (expt 10 12)) | |
199 | ("" 1) | |
200 | (_ | |
201 | (leave (_ "unknown unit: ~a~%") unit))))))) | |
202 | ||
073c34d7 LC |
203 | (define (call-with-error-handling thunk) |
204 | "Call THUNK within a user-friendly error handler." | |
205 | (guard (c ((package-input-error? c) | |
206 | (let* ((package (package-error-package c)) | |
207 | (input (package-error-invalid-input c)) | |
208 | (location (package-location package)) | |
209 | (file (location-file location)) | |
210 | (line (location-line location)) | |
211 | (column (location-column location))) | |
98eb8cbe | 212 | (leave (_ "~a:~a:~a: package `~a' has an invalid input: ~s~%") |
073c34d7 LC |
213 | file line column |
214 | (package-full-name package) input))) | |
9b222abe LC |
215 | ((package-cross-build-system-error? c) |
216 | (let* ((package (package-error-package c)) | |
217 | (loc (package-location package)) | |
218 | (system (package-build-system package))) | |
219 | (leave (_ "~a: ~a: build system `~a' does not support cross builds~%") | |
220 | (location->string loc) | |
221 | (package-full-name package) | |
222 | (build-system-name system)))) | |
ef86c39f | 223 | ((nix-connection-error? c) |
98eb8cbe | 224 | (leave (_ "failed to connect to `~a': ~a~%") |
ef86c39f LC |
225 | (nix-connection-error-file c) |
226 | (strerror (nix-connection-error-code c)))) | |
073c34d7 LC |
227 | ((nix-protocol-error? c) |
228 | ;; FIXME: Server-provided error messages aren't i18n'd. | |
98eb8cbe | 229 | (leave (_ "build failed: ~a~%") |
c1d52c71 LC |
230 | (nix-protocol-error-message c))) |
231 | ((message-condition? c) | |
232 | ;; Normally '&message' error conditions have an i18n'd message. | |
233 | (leave (_ "~a~%") (gettext (condition-message c))))) | |
e14c3929 LC |
234 | ;; Catch EPIPE and the likes. |
235 | (catch 'system-error | |
236 | thunk | |
bde8c0e6 LC |
237 | (lambda (key proc format-string format-args . rest) |
238 | (leave (_ "~a: ~a~%") proc | |
239 | (apply format #f format-string format-args)))))) | |
073c34d7 | 240 | |
56b82106 LC |
241 | (define %guix-user-module |
242 | ;; Module in which user expressions are evaluated. | |
243 | (let ((module (make-module))) | |
244 | (beautify-user-module! module) | |
245 | ;; Use (guix gexp) so that one can use #~ & co. | |
246 | (module-use! module (resolve-interface '(guix gexp))) | |
247 | module)) | |
248 | ||
ac5de156 LC |
249 | (define (read/eval str) |
250 | "Read and evaluate STR, raising an error if something goes wrong." | |
eb0880e7 LC |
251 | (let ((exp (catch #t |
252 | (lambda () | |
253 | (call-with-input-string str read)) | |
254 | (lambda args | |
255 | (leave (_ "failed to read expression ~s: ~s~%") | |
256 | str args))))) | |
ac5de156 LC |
257 | (catch #t |
258 | (lambda () | |
56b82106 | 259 | (eval exp %guix-user-module)) |
ac5de156 LC |
260 | (lambda args |
261 | (leave (_ "failed to evaluate expression `~a': ~s~%") | |
262 | exp args))))) | |
263 | ||
264 | (define (read/eval-package-expression str) | |
265 | "Read and evaluate STR and return the package it refers to, or exit an | |
266 | error." | |
267 | (match (read/eval str) | |
268 | ((? package? p) p) | |
269 | (_ | |
270 | (leave (_ "expression ~s does not evaluate to a package~%") | |
271 | str)))) | |
eb0880e7 | 272 | |
dd36b51b LC |
273 | (define* (show-what-to-build store drv |
274 | #:key dry-run? (use-substitutes? #t)) | |
9bb2b96a | 275 | "Show what will or would (depending on DRY-RUN?) be built in realizing the |
4d60610a | 276 | derivations listed in DRV. Return #t if there's something to build, #f |
dd36b51b LC |
277 | otherwise. When USE-SUBSTITUTES?, check and report what is prerequisites are |
278 | available for download." | |
52ddf2ae LC |
279 | (define (built-or-substitutable? drv) |
280 | (let ((out (derivation->output-path drv))) | |
281 | ;; If DRV has zero outputs, OUT is #f. | |
282 | (or (not out) | |
283 | (or (valid-path? store out) | |
284 | (and use-substitutes? | |
285 | (has-substitutes? store out)))))) | |
286 | ||
dd36b51b | 287 | (let*-values (((build download) |
59688fc4 LC |
288 | (fold2 (lambda (drv build download) |
289 | (let-values (((b d) | |
290 | (derivation-prerequisites-to-build | |
291 | store drv | |
292 | #:use-substitutes? | |
293 | use-substitutes?))) | |
294 | (values (append b build) | |
295 | (append d download)))) | |
dd36b51b LC |
296 | '() '() |
297 | drv)) | |
298 | ((build) ; add the DRV themselves | |
299 | (delete-duplicates | |
59688fc4 | 300 | (append (map derivation-file-name |
52ddf2ae | 301 | (remove built-or-substitutable? drv)) |
dd36b51b LC |
302 | (map derivation-input-path build)))) |
303 | ((download) ; add the references of DOWNLOAD | |
1a8b7834 LC |
304 | (if use-substitutes? |
305 | (delete-duplicates | |
306 | (append download | |
307 | (remove (cut valid-path? store <>) | |
308 | (append-map | |
309 | substitutable-references | |
310 | (substitutable-path-info store | |
311 | download))))) | |
312 | download))) | |
00554b2a | 313 | ;; TODO: Show the installed size of DOWNLOAD. |
9bb2b96a | 314 | (if dry-run? |
dd36b51b LC |
315 | (begin |
316 | (format (current-error-port) | |
83e61a73 LC |
317 | (N_ "~:[The following derivation would be built:~%~{ ~a~%~}~;~]" |
318 | "~:[The following derivations would be built:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
319 | (length build)) |
320 | (null? build) build) | |
321 | (format (current-error-port) | |
83e61a73 LC |
322 | (N_ "~:[The following file would be downloaded:~%~{ ~a~%~}~;~]" |
323 | "~:[The following files would be downloaded:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
324 | (length download)) |
325 | (null? download) download)) | |
326 | (begin | |
327 | (format (current-error-port) | |
83e61a73 LC |
328 | (N_ "~:[The following derivation will be built:~%~{ ~a~%~}~;~]" |
329 | "~:[The following derivations will be built:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
330 | (length build)) |
331 | (null? build) build) | |
332 | (format (current-error-port) | |
83e61a73 LC |
333 | (N_ "~:[The following file will be downloaded:~%~{ ~a~%~}~;~]" |
334 | "~:[The following files will be downloaded:~%~{ ~a~%~}~;~]" | |
dd36b51b LC |
335 | (length download)) |
336 | (null? download) download))) | |
337 | (pair? build))) | |
9bb2b96a | 338 | |
073c34d7 LC |
339 | (define-syntax with-error-handling |
340 | (syntax-rules () | |
341 | "Run BODY within a user-friendly error condition handler." | |
342 | ((_ body ...) | |
343 | (call-with-error-handling | |
344 | (lambda () | |
345 | body ...))))) | |
346 | ||
64fc89b6 LC |
347 | (define (location->string loc) |
348 | "Return a human-friendly, GNU-standard representation of LOC." | |
349 | (match loc | |
350 | (#f (_ "<unknown location>")) | |
351 | (($ <location> file line column) | |
352 | (format #f "~a:~a:~a" file line column)))) | |
353 | ||
c61b026e LC |
354 | (define (switch-symlinks link target) |
355 | "Atomically switch LINK, a symbolic link, to point to TARGET. Works | |
356 | both when LINK already exists and when it does not." | |
357 | (let ((pivot (string-append link ".new"))) | |
358 | (symlink target pivot) | |
359 | (rename-file pivot link))) | |
360 | ||
f651b477 LC |
361 | (define (config-directory) |
362 | "Return the name of the configuration directory, after making sure that it | |
363 | exists. Honor the XDG specs, | |
364 | <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>." | |
365 | (let ((dir (and=> (or (getenv "XDG_CONFIG_HOME") | |
366 | (and=> (getenv "HOME") | |
367 | (cut string-append <> "/.config"))) | |
368 | (cut string-append <> "/guix")))) | |
369 | (catch 'system-error | |
370 | (lambda () | |
371 | (mkdir dir) | |
372 | dir) | |
373 | (lambda args | |
374 | (match (system-error-errno args) | |
375 | ((or EEXIST 0) | |
376 | dir) | |
377 | (err | |
378 | (leave (_ "failed to create configuration directory `~a': ~a~%") | |
379 | dir (strerror err)))))))) | |
380 | ||
299112d3 LC |
381 | (define* (fill-paragraph str width #:optional (column 0)) |
382 | "Fill STR such that each line contains at most WIDTH characters, assuming | |
383 | that the first character is at COLUMN. | |
384 | ||
385 | When STR contains a single line break surrounded by other characters, it is | |
386 | converted to a space; sequences of more than one line break are preserved." | |
387 | (define (maybe-break chr result) | |
388 | (match result | |
389 | ((column newlines chars) | |
390 | (case chr | |
391 | ((#\newline) | |
392 | `(,column ,(+ 1 newlines) ,chars)) | |
393 | (else | |
394 | (let ((chars (case newlines | |
395 | ((0) chars) | |
396 | ((1) (cons #\space chars)) | |
397 | (else | |
398 | (append (make-list newlines #\newline) chars)))) | |
399 | (column (case newlines | |
400 | ((0) column) | |
401 | ((1) (+ 1 column)) | |
402 | (else 0)))) | |
403 | (let ((chars (cons chr chars)) | |
404 | (column (+ 1 column))) | |
405 | (if (> column width) | |
406 | (let*-values (((before after) | |
407 | (break (cut eqv? #\space <>) chars)) | |
408 | ((len) | |
409 | (length before))) | |
410 | (if (<= len width) | |
411 | `(,len | |
412 | 0 | |
413 | ,(if (null? after) | |
414 | before | |
415 | (append before (cons #\newline (cdr after))))) | |
416 | `(,column 0 ,chars))) ; unbreakable | |
417 | `(,column 0 ,chars))))))))) | |
418 | ||
419 | (match (string-fold maybe-break | |
420 | `(,column 0 ()) | |
421 | str) | |
422 | ((_ _ chars) | |
423 | (list->string (reverse chars))))) | |
424 | ||
2876b989 LC |
425 | \f |
426 | ;;; | |
427 | ;;; Packages. | |
428 | ;;; | |
429 | ||
299112d3 LC |
430 | (define (string->recutils str) |
431 | "Return a version of STR where newlines have been replaced by newlines | |
432 | followed by \"+ \", which makes for a valid multi-line field value in the | |
433 | `recutils' syntax." | |
434 | (list->string | |
435 | (string-fold-right (lambda (chr result) | |
436 | (if (eqv? chr #\newline) | |
437 | (cons* chr #\+ #\space result) | |
438 | (cons chr result))) | |
439 | '() | |
440 | str))) | |
441 | ||
442 | (define* (package->recutils p port | |
443 | #:optional (width (or (and=> (getenv "WIDTH") | |
444 | string->number) | |
445 | 80))) | |
446 | "Write to PORT a `recutils' record of package P, arranging to fit within | |
447 | WIDTH columns." | |
448 | (define (description->recutils str) | |
ee764179 | 449 | (let ((str (P_ str))) |
299112d3 LC |
450 | (string->recutils |
451 | (fill-paragraph str width | |
452 | (string-length "description: "))))) | |
453 | ||
454 | ;; Note: Don't i18n field names so that people can post-process it. | |
455 | (format port "name: ~a~%" (package-name p)) | |
456 | (format port "version: ~a~%" (package-version p)) | |
457 | (format port "location: ~a~%" | |
458 | (or (and=> (package-location p) location->string) | |
459 | (_ "unknown"))) | |
8fa3e6b3 LC |
460 | |
461 | ;; Note: Starting from version 1.6 or recutils, hyphens are not allowed in | |
462 | ;; field identifiers. | |
463 | (format port "homepage: ~a~%" (package-home-page p)) | |
464 | ||
299112d3 LC |
465 | (format port "license: ~a~%" |
466 | (match (package-license p) | |
467 | (((? license? licenses) ...) | |
468 | (string-join (map license-name licenses) | |
469 | ", ")) | |
470 | ((? license? license) | |
471 | (license-name license)) | |
472 | (x | |
473 | (_ "unknown")))) | |
474 | (format port "synopsis: ~a~%" | |
475 | (string-map (match-lambda | |
476 | (#\newline #\space) | |
477 | (chr chr)) | |
ee764179 | 478 | (or (and=> (package-synopsis p) P_) |
299112d3 LC |
479 | ""))) |
480 | (format port "description: ~a~%" | |
481 | (and=> (package-description p) description->recutils)) | |
482 | (newline port)) | |
483 | ||
2cd09108 NK |
484 | (define (string->generations str) |
485 | "Return the list of generations matching a pattern in STR. This function | |
486 | accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", \"..9\"." | |
487 | (define (maybe-integer) | |
488 | (let ((x (string->number str))) | |
489 | (and (integer? x) | |
490 | x))) | |
491 | ||
492 | (define (maybe-comma-separated-integers) | |
493 | (let ((lst (delete-duplicates | |
494 | (map string->number | |
495 | (string-split str #\,))))) | |
496 | (and (every integer? lst) | |
497 | lst))) | |
498 | ||
499 | (cond ((maybe-integer) | |
500 | => | |
501 | list) | |
502 | ((maybe-comma-separated-integers) | |
503 | => | |
504 | identity) | |
505 | ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str) | |
506 | => | |
507 | (lambda (match) | |
508 | (let ((s (string->number (match:substring match 1))) | |
509 | (e (string->number (match:substring match 2)))) | |
510 | (and (every integer? (list s e)) | |
511 | (<= s e) | |
512 | (iota (1+ (- e s)) s))))) | |
513 | ((string-match "^([0-9]+)\\.\\.$" str) | |
514 | => | |
515 | (lambda (match) | |
516 | (let ((s (string->number (match:substring match 1)))) | |
517 | (and (integer? s) | |
518 | `(>= ,s))))) | |
519 | ((string-match "^\\.\\.([0-9]+)$" str) | |
520 | => | |
521 | (lambda (match) | |
522 | (let ((e (string->number (match:substring match 1)))) | |
523 | (and (integer? e) | |
524 | `(<= ,e))))) | |
525 | (else #f))) | |
526 | ||
527 | (define (string->duration str) | |
528 | "Return the duration matching a pattern in STR. This function accepts the | |
529 | following patterns: \"1d\", \"1w\", \"1m\"." | |
530 | (define (hours->duration hours match) | |
531 | (make-time time-duration 0 | |
532 | (* 3600 hours (string->number (match:substring match 1))))) | |
533 | ||
534 | (cond ((string-match "^([0-9]+)d$" str) | |
535 | => | |
536 | (lambda (match) | |
537 | (hours->duration 24 match))) | |
538 | ((string-match "^([0-9]+)w$" str) | |
539 | => | |
540 | (lambda (match) | |
541 | (hours->duration (* 24 7) match))) | |
542 | ((string-match "^([0-9]+)m$" str) | |
543 | => | |
544 | (lambda (match) | |
545 | (hours->duration (* 24 30) match))) | |
546 | (else #f))) | |
547 | ||
2876b989 LC |
548 | (define* (package-specification->name+version+output spec |
549 | #:optional (output "out")) | |
550 | "Parse package specification SPEC and return three value: the specified | |
551 | package name, version number (or #f), and output name (or OUTPUT). SPEC may | |
552 | optionally contain a version number and an output name, as in these examples: | |
553 | ||
554 | guile | |
555 | guile-2.0.9 | |
556 | guile:debug | |
557 | guile-2.0.9:debug | |
558 | " | |
559 | (let*-values (((name sub-drv) | |
560 | (match (string-rindex spec #\:) | |
561 | (#f (values spec output)) | |
562 | (colon (values (substring spec 0 colon) | |
563 | (substring spec (+ 1 colon)))))) | |
564 | ((name version) | |
565 | (package-name->name+version name))) | |
566 | (values name version sub-drv))) | |
567 | ||
568 | \f | |
569 | ;;; | |
570 | ;;; Command-line option processing. | |
571 | ;;; | |
572 | ||
a5975ced LC |
573 | (define (args-fold* options unrecognized-option-proc operand-proc . seeds) |
574 | "A wrapper on top of `args-fold' that does proper user-facing error | |
575 | reporting." | |
576 | (catch 'misc-error | |
577 | (lambda () | |
578 | (apply args-fold options unrecognized-option-proc | |
579 | operand-proc seeds)) | |
580 | (lambda (key proc msg args . rest) | |
581 | ;; XXX: MSG is not i18n'd. | |
582 | (leave (_ "invalid argument: ~a~%") | |
583 | (apply format #f msg args))))) | |
584 | ||
e49951eb | 585 | (define (show-guix-usage) |
e49951eb | 586 | (format (current-error-port) |
25c93676 LC |
587 | (_ "Try `guix --help' for more information.~%")) |
588 | (exit 1)) | |
e49951eb | 589 | |
e31ff8b8 LC |
590 | (define (command-files) |
591 | "Return the list of source files that define Guix sub-commands." | |
592 | (define directory | |
593 | (and=> (search-path %load-path "guix.scm") | |
594 | (compose (cut string-append <> "/guix/scripts") | |
595 | dirname))) | |
596 | ||
2b8cf44f LC |
597 | (define dot-scm? |
598 | (cut string-suffix? ".scm" <>)) | |
599 | ||
600 | ;; In Guile 2.0.5 `scandir' would return "." and ".." regardless even though | |
601 | ;; they don't match `dot-scm?'. Work around it by doing additional | |
602 | ;; filtering. | |
e31ff8b8 | 603 | (if directory |
2b8cf44f | 604 | (filter dot-scm? (scandir directory dot-scm?)) |
e31ff8b8 LC |
605 | '())) |
606 | ||
607 | (define (commands) | |
608 | "Return the list of Guix command names." | |
609 | (map (compose (cut string-drop-right <> 4) | |
610 | basename) | |
611 | (command-files))) | |
612 | ||
613 | (define (show-guix-help) | |
59f734f3 | 614 | (define (internal? command) |
49e6291a | 615 | (member command '("substitute-binary" "authenticate" "offload"))) |
59f734f3 | 616 | |
e31ff8b8 LC |
617 | (format #t (_ "Usage: guix COMMAND ARGS... |
618 | Run COMMAND with ARGS.\n")) | |
619 | (newline) | |
620 | (format #t (_ "COMMAND must be one of the sub-commands listed below:\n")) | |
621 | (newline) | |
622 | ;; TODO: Display a synopsis of each command. | |
59f734f3 LC |
623 | (format #t "~{ ~a~%~}" (sort (remove internal? (commands)) |
624 | string<?)) | |
e31ff8b8 LC |
625 | (show-bug-report-information)) |
626 | ||
a2011be5 LC |
627 | (define program-name |
628 | ;; Name of the command-line program currently executing, or #f. | |
629 | (make-parameter #f)) | |
630 | ||
ec5d0a85 LC |
631 | (define (run-guix-command command . args) |
632 | "Run COMMAND with the given ARGS. Report an error when COMMAND is not | |
633 | found." | |
634 | (define module | |
635 | (catch 'misc-error | |
636 | (lambda () | |
637 | (resolve-interface `(guix scripts ,command))) | |
638 | (lambda - | |
25c93676 LC |
639 | (format (current-error-port) |
640 | (_ "guix: ~a: command not found~%") command) | |
641 | (show-guix-usage)))) | |
ec5d0a85 LC |
642 | |
643 | (let ((command-main (module-ref module | |
644 | (symbol-append 'guix- command)))) | |
645 | (parameterize ((program-name command)) | |
646 | (apply command-main args)))) | |
647 | ||
a2011be5 LC |
648 | (define guix-warning-port |
649 | (make-parameter (current-warning-port))) | |
650 | ||
e49951eb MW |
651 | (define (guix-main arg0 . args) |
652 | (initialize-guix) | |
653 | (let () | |
654 | (define (option? str) (string-prefix? "-" str)) | |
655 | (match args | |
25c93676 LC |
656 | (() |
657 | (format (current-error-port) | |
658 | (_ "guix: missing command name~%")) | |
659 | (show-guix-usage)) | |
e12b3eb9 | 660 | ((or ("-h") ("--help")) |
25c93676 LC |
661 | (show-guix-help)) |
662 | (("--version") | |
663 | (show-version-and-exit "guix")) | |
664 | (((? option? o) args ...) | |
665 | (format (current-error-port) | |
666 | (_ "guix: unrecognized option '~a'~%") o) | |
667 | (show-guix-usage)) | |
e49951eb | 668 | ((command args ...) |
ec5d0a85 LC |
669 | (apply run-guix-command |
670 | (string->symbol command) | |
671 | args))))) | |
e49951eb | 672 | |
073c34d7 | 673 | ;;; ui.scm ends here |