1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017, 2018 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix status)
20 #:use-module (guix records)
21 #:use-module (guix i18n)
22 #:use-module ((guix ui) #:select (colorize-string))
23 #:use-module (guix progress)
24 #:autoload (guix build syscalls) (terminal-columns)
25 #:use-module ((guix build download)
26 #:select (nar-uri-abbreviation))
27 #:use-module (guix store)
28 #:use-module (guix derivations)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-9)
31 #:use-module (srfi srfi-19)
32 #:use-module (srfi srfi-26)
33 #:use-module (ice-9 regex)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 format)
36 #:use-module (ice-9 binary-ports)
37 #:use-module (rnrs bytevectors)
38 #:use-module ((system foreign)
39 #:select (bytevector->pointer pointer->bytevector))
40 #:export (build-event-output-port
46 build-status-downloading
47 build-status-builds-completed
48 build-status-downloads-completed
61 print-build-event/quiet
68 ;;; This module provides facilities to track the status of ongoing builds and
69 ;;; downloads in a given session, as well as tools to report about the current
70 ;;; status to user interfaces. It does so by analyzing the output of
71 ;;; 'current-build-output-port'. The build status is maintained in a
72 ;;; <build-status> record.
78 ;;; Build status tracking.
81 ;; Builds and substitutions performed by the daemon.
82 (define-record-type* <build-status> build-status make-build-status
84 (building build-status-building ;list of drv
86 (downloading build-status-downloading ;list of <download>
88 (builds-completed build-status-builds-completed ;list of drv
90 (downloads-completed build-status-downloads-completed ;list of store items
93 ;; On-going or completed downloads. Downloads can be stem from substitutes
94 ;; and from "builtin:download" fixed-output derivations.
95 (define-record-type <download>
96 (%download item uri size start end transferred)
98 (item download-item) ;store item
99 (uri download-uri) ;string | #f
100 (size download-size) ;integer | #f
101 (start download-start) ;<time>
102 (end download-end) ;#f | <time>
103 (transferred download-transferred)) ;integer
105 (define* (download item uri
107 (start (current-time time-monotonic)) end
109 "Return a new download."
110 (%download item uri size start end transferred))
112 (define (matching-download item)
113 "Return a predicate that matches downloads of ITEM."
115 (string=? item (download-item download))))
117 (define* (compute-status event status
118 #:key (current-time current-time))
119 "Given EVENT, a tuple like (build-started \"/gnu/store/...-foo.drv\" ...),
120 compute a new status based on STATUS."
122 (('build-started drv _ ...)
125 (building (cons drv (build-status-building status)))))
126 (((or 'build-succeeded 'build-failed) drv _ ...)
129 (building (delete drv (build-status-building status)))
130 (builds-completed (cons drv (build-status-builds-completed status)))))
132 ;; Note: Ignore 'substituter-started' and 'substituter-succeeded' because
133 ;; they're not as informative as 'download-started' and
134 ;; 'download-succeeded'.
136 (('download-started item uri (= string->number size))
137 ;; This is presumably a fixed-output derivation so move it from
138 ;; 'building' to 'downloading'. XXX: This doesn't work in 'check' mode
139 ;; because ITEM is different from DRV's output.
142 (building (remove (lambda (drv)
143 (equal? (false-if-exception
144 (derivation->output-path
145 (read-derivation-from-file drv)))
147 (build-status-building status)))
148 (downloading (cons (download item uri #:size size
149 #:start (current-time time-monotonic))
150 (build-status-downloading status)))))
151 (('download-succeeded item uri (= string->number size))
152 (let ((current (find (matching-download item)
153 (build-status-downloading status))))
156 (downloading (delq current (build-status-downloading status)))
158 (cons (download item uri
160 #:start (download-start current)
162 #:end (current-time time-monotonic))
163 (build-status-downloads-completed status))))))
164 (('substituter-succeeded item _ ...)
165 (match (find (matching-download item)
166 (build-status-downloading status))
168 ;; Presumably we already got a 'download-succeeded' event for ITEM,
169 ;; everything is fine.
172 ;; Maybe the build process didn't emit a 'download-succeeded' event
173 ;; for ITEM, so remove CURRENT from the queue now.
176 (downloading (delq current (build-status-downloading status)))
178 (cons (download item (download-uri current)
179 #:size (download-size current)
180 #:start (download-start current)
181 #:transferred (download-size current)
182 #:end (current-time time-monotonic))
183 (build-status-downloads-completed status)))))))
184 (('download-progress item uri
185 (= string->number size)
186 (= string->number transferred))
187 (let ((downloads (remove (matching-download item)
188 (build-status-downloading status)))
189 (current (find (matching-download item)
190 (build-status-downloading status))))
193 (downloading (cons (download item uri
197 (download-start current))
198 (current-time time-monotonic))
199 #:transferred transferred)
204 (define (simultaneous-jobs status)
205 "Return the number of on-going builds and downloads for STATUS."
206 (+ (length (build-status-building status))
207 (length (build-status-downloading status))))
214 (define (extended-build-trace-supported?)
215 "Return true if the currently used store is known to support \"extended
216 build traces\" such as \"@ download-progress\" traces."
217 ;; Support for extended build traces was added in protocol version #x162.
218 (and (current-store-protocol-version)
219 (>= (current-store-protocol-version) #x162)))
222 (let ((steps (circular-list "\\" "|" "/" "-")))
224 "Display a spinner on PORT."
228 (display "\r\x1b[K" port)
230 (force-output port))))))
232 (define (color-output? port)
233 "Return true if we should write colored output to PORT."
234 (and (not (getenv "INSIDE_EMACS"))
235 (not (getenv "NO_COLOR"))
238 (define-syntax color-rules
240 "Return a procedure that colorizes the string it is passed according to
241 the given rules. Each rule has the form:
243 (REGEXP COLOR1 COLOR2 ...)
245 where COLOR1 specifies how to colorize the first submatch of REGEXP, and so
247 ((_ (regexp colors ...) rest ...)
248 (let ((next (color-rules rest ...))
249 (rx (make-regexp regexp)))
251 (if (string-index str #\nul)
253 (match (regexp-exec rx str)
260 (string-concatenate-reverse result))
263 (cons (colorize-string (match:substring m n)
270 (define colorize-log-line
271 ;; Take a string and return a possibly colorized string according to the
274 ("^(phase)(.*)(succeeded after)(.*)(seconds)(.*)"
275 GREEN BOLD GREEN RESET GREEN BLUE)
276 ("^(phase)(.*)(failed after)(.*)(seconds)(.*)"
277 RED BLUE RED BLUE RED BLUE)
278 ("^(.*)(error|fail|failed|\\<FAIL|FAILED)([[:blank:]]*)(:)(.*)"
279 RESET RED BOLD BOLD BOLD)
280 ("^(.*)(warning)([[:blank:]]*)(:)(.*)"
281 RESET MAGENTA BOLD BOLD BOLD)))
283 (define* (print-build-event event old-status status
284 #:optional (port (current-error-port))
286 (colorize? (color-output? port))
288 "Print information about EVENT and STATUS to PORT. When COLORIZE? is true,
289 produce colorful output. When PRINT-LOG? is true, display the build log in
290 addition to build events."
293 (cut colorize-string <> 'BOLD)
298 (cut colorize-string <> 'GREEN 'BOLD)
303 (cut colorize-string <> 'RED 'BOLD)
306 (define print-log-line
310 (display (colorize-log-line line) port))
311 (cut display <> port))
315 (display "\r" port) ;erase the spinner
317 (('build-started drv . _)
318 (format port (info (G_ "building ~a...")) drv)
320 (('build-succeeded drv . _)
321 (format port (success (G_ "successfully built ~a")) drv)
323 (match (build-status-building status)
325 (ongoing ;when max-jobs > 1
327 (N_ "The following build is still in progress:~%~{ ~a~%~}~%"
328 "The following builds are still in progress:~%~{ ~a~%~}~%"
331 (('build-failed drv . _)
332 (format port (failure (G_ "build of ~a failed")) drv)
334 (match (derivation-log-file drv)
336 (format port (failure (G_ "Could not find build log for '~a'."))
339 (format port (info (G_ "View build log at '~a'.")) log)))
341 (('substituter-started item _ ...)
342 (when (or print-log? (not (extended-build-trace-supported?)))
343 (format port (info (G_ "substituting ~a...")) item)
345 (('download-started item uri _ ...)
346 (format port (info (G_ "downloading from ~a...")) uri)
348 (('download-progress item uri
349 (= string->number size)
350 (= string->number transferred))
351 ;; Print a progress bar, but only if there's only one on-going
352 ;; job--otherwise the output would be intermingled.
353 (when (= 1 (simultaneous-jobs status))
354 (match (find (matching-download item)
355 (build-status-downloading status))
356 (#f #f) ;shouldn't happen!
358 ;; XXX: It would be nice to memoize the abbreviation.
359 (let ((uri (if (string-contains uri "/nar/")
360 (nar-uri-abbreviation uri)
362 (display-download-progress uri size
364 (download-start download)
365 #:transferred transferred))))))
366 (('substituter-succeeded item _ ...)
367 ;; If there are no jobs running, we already reported download completion
368 ;; so there's nothing left to do.
369 (unless (and (zero? (simultaneous-jobs status))
370 (extended-build-trace-supported?))
371 (format port (success (G_ "substitution of ~a complete")) item)
373 (('substituter-failed item _ ...)
374 (format port (failure (G_ "substitution of ~a failed")) item)
376 (('hash-mismatch item algo expected actual _ ...)
377 ;; TRANSLATORS: The final string looks like "sha256 hash mismatch for
378 ;; /gnu/store/…-sth:", where "sha256" is the hash algorithm.
379 (format port (failure (G_ "~a hash mismatch for ~a:")) algo item)
381 (format port (info (G_ "\
386 ;; The daemon prefixes early messages coming with 'guix substitute' with
387 ;; "substitute:". These are useful ("updating substitutes from URL"), so
389 (if (string-prefix? "substitute: " line)
393 (print-log-line line)))
397 (define* (print-build-event/quiet event old-status status
399 (port (current-error-port))
401 (colorize? (color-output? port)))
402 (print-build-event event old-status status port
403 #:colorize? colorize?
406 (define* (build-status-updater #:optional (on-change (const #t)))
407 "Return a procedure that can be passed to 'build-event-output-port'. That
408 procedure computes the new build status upon each event and calls ON-CHANGE:
410 (ON-CHANGE event status new-status)
412 ON-CHANGE can display the build status, build events, etc."
413 (lambda (event status)
414 (let ((new (compute-status event status)))
415 (on-change event status new)
424 (char-set #\return #\newline))
426 (define* (build-event-output-port proc #:optional (seed (build-status)))
427 "Return an output port for use as 'current-build-output-port' that calls
428 PROC with its current state value, initialized with SEED, on every build
429 event. Build events passed to PROC are tuples corresponding to the \"build
430 traces\" produced by the daemon:
432 (build-started \"/gnu/store/...-foo.drv\" ...)
433 (substituter-started \"/gnu/store/...-foo\" ...)
437 The second return value is a thunk to retrieve the current state."
439 ;; Line fragments received so far.
443 ;; Current state for PROC.
446 (define (process-line line)
447 (if (string-prefix? "@ " line)
448 (match (string-tokenize (string-drop line 2))
449 (((= string->symbol event-name) args ...)
451 (proc (cons event-name args)
453 (set! %state (proc (list 'build-log line)
456 (define (bytevector-range bv offset count)
457 (let ((ptr (bytevector->pointer bv offset)))
458 (pointer->bytevector ptr count)))
460 (define (write! bv offset count)
461 (let loop ((str (utf8->string (bytevector-range bv offset count))))
462 (match (string-index str %newline)
464 (let ((tail (string-take str (+ 1 cr))))
465 (process-line (string-concatenate-reverse
466 (cons tail %fragments)))
467 (set! %fragments '())
468 (loop (string-drop str (+ 1 cr)))))
470 (unless (string-null? str)
471 (set! %fragments (cons str %fragments)))
475 (make-custom-binary-output-port "filtering-input-port"
480 ;; The build port actually receives Unicode strings.
481 (set-port-encoding! port "UTF-8")
482 (setvbuf port (cond-expand (guile-2.2 'line) (else _IOLBF)))
484 (values port (lambda () %state)))
486 (define (call-with-status-report on-event thunk)
487 (parameterize ((current-terminal-columns (terminal-columns))
488 (current-build-output-port
489 (build-event-output-port (build-status-updater on-event))))
492 (define-syntax-rule (with-status-report on-event exp ...)
493 "Set up build status reporting to the user using the ON-EVENT procedure;
494 evaluate EXP... in that context."
495 (call-with-status-report on-event (lambda () exp ...)))