status.scm 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017-2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; 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 colors)
  23. #:use-module (guix progress)
  24. #:autoload (guix build syscalls) (terminal-columns)
  25. #:autoload (guix build download) (nar-uri-abbreviation)
  26. #:use-module (guix store)
  27. #:use-module (guix derivations)
  28. #:use-module (guix memoization)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-9)
  31. #:use-module (srfi srfi-9 gnu)
  32. #:use-module (srfi srfi-19)
  33. #:use-module (srfi srfi-26)
  34. #:use-module (ice-9 regex)
  35. #:use-module (ice-9 match)
  36. #:use-module (ice-9 format)
  37. #:use-module (ice-9 binary-ports)
  38. #:autoload (ice-9 rdelim) (read-string)
  39. #:use-module (rnrs bytevectors)
  40. #:use-module ((system foreign)
  41. #:select (bytevector->pointer pointer->bytevector))
  42. #:export (build-event-output-port
  43. compute-status
  44. build-status
  45. build-status?
  46. build-status-building
  47. build-status-downloading
  48. build-status-builds-completed
  49. build-status-downloads-completed
  50. build?
  51. build
  52. build-derivation
  53. build-system
  54. build-log-file
  55. build-phase
  56. build-completion
  57. download?
  58. download
  59. download-item
  60. download-uri
  61. download-size
  62. download-start
  63. download-end
  64. download-transferred
  65. build-status-updater
  66. print-build-event
  67. print-build-event/quiet
  68. print-build-status
  69. with-status-report
  70. with-status-verbosity))
  71. ;;; Commentary:
  72. ;;;
  73. ;;; This module provides facilities to track the status of ongoing builds and
  74. ;;; downloads in a given session, as well as tools to report about the current
  75. ;;; status to user interfaces. It does so by analyzing the output of
  76. ;;; 'current-build-output-port'. The build status is maintained in a
  77. ;;; <build-status> record.
  78. ;;;
  79. ;;; Code:
  80. ;;;
  81. ;;; Build status tracking.
  82. ;;;
  83. ;; Builds and substitutions performed by the daemon.
  84. (define-record-type* <build-status> build-status make-build-status
  85. build-status?
  86. (building build-status-building ;list of <build>
  87. (default '()))
  88. (downloading build-status-downloading ;list of <download>
  89. (default '()))
  90. (builds-completed build-status-builds-completed ;list of <build>
  91. (default '()))
  92. (downloads-completed build-status-downloads-completed ;list of <download>
  93. (default '())))
  94. ;; On-going or completed build.
  95. (define-immutable-record-type <build>
  96. (%build derivation id system log-file phase completion)
  97. build?
  98. (derivation build-derivation) ;string (.drv file name)
  99. (id build-id) ;#f | integer
  100. (system build-system) ;string
  101. (log-file build-log-file) ;#f | string
  102. (phase build-phase ;#f | symbol
  103. set-build-phase)
  104. (completion build-completion ;#f | integer (percentage)
  105. set-build-completion))
  106. (define* (build derivation system #:key id log-file phase completion)
  107. "Return a new build."
  108. (%build derivation id system log-file phase completion))
  109. ;; On-going or completed downloads. Downloads can be stem from substitutes
  110. ;; and from "builtin:download" fixed-output derivations.
  111. (define-record-type <download>
  112. (%download item uri size start end transferred)
  113. download?
  114. (item download-item) ;store item
  115. (uri download-uri) ;string | #f
  116. (size download-size) ;integer | #f
  117. (start download-start) ;<time>
  118. (end download-end) ;#f | <time>
  119. (transferred download-transferred)) ;integer
  120. (define* (download item uri
  121. #:key size
  122. (start (current-time time-monotonic)) end
  123. (transferred 0))
  124. "Return a new download."
  125. (%download item uri size start end transferred))
  126. (define (matching-build drv)
  127. "Return a predicate that matches builds of DRV."
  128. (lambda (build)
  129. (string=? drv (build-derivation build))))
  130. (define (matching-download item)
  131. "Return a predicate that matches downloads of ITEM."
  132. (lambda (download)
  133. (string=? item (download-item download))))
  134. (define %phase-start-rx
  135. ;; Match the "starting phase" message emitted by 'gnu-build-system'.
  136. (make-regexp "^starting phase [`']([^']+)'"))
  137. (define %percentage-line-rx
  138. ;; Things like CMake write lines like "[ 10%] gcc -c …". This regexp
  139. ;; matches them.
  140. (make-regexp "^[[:space:]]*\\[ *([0-9]+)%\\]"))
  141. (define %fraction-line-rx
  142. ;; The 'compiled-modules' derivations and Ninja produce reports like
  143. ;; "[ 1/32]" at the beginning of each line, while GHC prints "[ 6 of 45]".
  144. ;; This regexp matches these.
  145. (make-regexp "^[[:space:]]*\\[ *([0-9]+) *(/|of) *([0-9]+)\\]"))
  146. (define (update-build status id line)
  147. "Update STATUS based on LINE, a build output line for ID that might contain
  148. a completion indication."
  149. (define (find-build)
  150. (find (lambda (build)
  151. (and (build-id build)
  152. (= (build-id build) id)))
  153. (build-status-building status)))
  154. (define (update %)
  155. (let ((build (find-build)))
  156. (build-status
  157. (inherit status)
  158. (building (cons (set-build-completion build %)
  159. (delq build (build-status-building status)))))))
  160. (cond ((string-any #\nul line)
  161. ;; Don't try to match a regexp here.
  162. status)
  163. ((regexp-exec %percentage-line-rx line)
  164. =>
  165. (lambda (match)
  166. (let ((% (string->number (match:substring match 1))))
  167. (update %))))
  168. ((regexp-exec %fraction-line-rx line)
  169. =>
  170. (lambda (match)
  171. (let ((done (string->number (match:substring match 1)))
  172. (total (string->number (match:substring match 3))))
  173. (update (* 100. (/ done total))))))
  174. ((regexp-exec %phase-start-rx line)
  175. =>
  176. (lambda (match)
  177. (let ((phase (match:substring match 1))
  178. (build (find-build)))
  179. (if build
  180. (build-status
  181. (inherit status)
  182. (building
  183. (cons (set-build-phase (set-build-completion build #f)
  184. (string->symbol phase))
  185. (delq build (build-status-building status)))))
  186. status))))
  187. (else
  188. status)))
  189. (define* (compute-status event status
  190. #:key
  191. (current-time current-time)
  192. (derivation-path->output-path
  193. derivation-path->output-path))
  194. "Given EVENT, a tuple like (build-started \"/gnu/store/...-foo.drv\" ...),
  195. compute a new status based on STATUS."
  196. (match event
  197. (('build-started drv "-" system log-file . rest)
  198. (let ((build (build drv system
  199. #:id (match rest
  200. ((pid . _) (string->number pid))
  201. (_ #f))
  202. #:log-file (if (string-null? log-file)
  203. #f
  204. log-file))))
  205. (build-status
  206. (inherit status)
  207. (building (cons build (build-status-building status))))))
  208. (((or 'build-succeeded 'build-failed) drv _ ...)
  209. (let ((build (find (matching-build drv)
  210. (build-status-building status))))
  211. ;; If BUILD is #f, this may be because DRV corresponds to a
  212. ;; fixed-output derivation that is listed as a download.
  213. (if build
  214. (build-status
  215. (inherit status)
  216. (building (delq build (build-status-building status)))
  217. (builds-completed
  218. (cons build (build-status-builds-completed status))))
  219. status)))
  220. ;; Note: Ignore 'substituter-started' and 'substituter-succeeded' because
  221. ;; they're not as informative as 'download-started' and
  222. ;; 'download-succeeded'.
  223. (('download-started item uri (= string->number size))
  224. ;; This is presumably a fixed-output derivation so move it from
  225. ;; 'building' to 'downloading'. XXX: This doesn't work in 'check' mode
  226. ;; because ITEM is different from DRV's output.
  227. (build-status
  228. (inherit status)
  229. (building (remove (lambda (build)
  230. (let ((drv (build-derivation build)))
  231. (equal? (false-if-exception
  232. (derivation-path->output-path drv))
  233. item)))
  234. (build-status-building status)))
  235. (downloading (cons (download item uri #:size size
  236. #:start (current-time time-monotonic))
  237. (build-status-downloading status)))))
  238. (('download-succeeded item uri (= string->number size))
  239. (let ((current (find (matching-download item)
  240. (build-status-downloading status))))
  241. (build-status
  242. (inherit status)
  243. (downloading (delq current (build-status-downloading status)))
  244. (downloads-completed
  245. (cons (download item uri
  246. #:size size
  247. #:start (download-start current)
  248. #:transferred size
  249. #:end (current-time time-monotonic))
  250. (build-status-downloads-completed status))))))
  251. (('substituter-succeeded item _ ...)
  252. (match (find (matching-download item)
  253. (build-status-downloading status))
  254. (#f
  255. ;; Presumably we already got a 'download-succeeded' event for ITEM,
  256. ;; everything is fine.
  257. status)
  258. (current
  259. ;; Maybe the build process didn't emit a 'download-succeeded' event
  260. ;; for ITEM, so remove CURRENT from the queue now.
  261. (build-status
  262. (inherit status)
  263. (downloading (delq current (build-status-downloading status)))
  264. (downloads-completed
  265. (cons (download item (download-uri current)
  266. #:size (download-size current)
  267. #:start (download-start current)
  268. #:transferred (download-size current)
  269. #:end (current-time time-monotonic))
  270. (build-status-downloads-completed status)))))))
  271. (('download-progress item uri
  272. (= string->number size)
  273. (= string->number transferred))
  274. (let ((downloads (remove (matching-download item)
  275. (build-status-downloading status)))
  276. (current (find (matching-download item)
  277. (build-status-downloading status))))
  278. (build-status
  279. (inherit status)
  280. (downloading (cons (download item uri
  281. #:size size
  282. #:start
  283. (or (and current
  284. (download-start current))
  285. (current-time time-monotonic))
  286. #:transferred transferred)
  287. downloads)))))
  288. (('build-log (? integer? pid) line)
  289. (update-build status pid line))
  290. (_
  291. status)))
  292. (define (simultaneous-jobs status)
  293. "Return the number of on-going builds and downloads for STATUS."
  294. (+ (length (build-status-building status))
  295. (length (build-status-downloading status))))
  296. ;;;
  297. ;;; Rendering.
  298. ;;;
  299. (define (extended-build-trace-supported?)
  300. "Return true if the currently used store is known to support \"extended
  301. build traces\" such as \"@ download-progress\" traces."
  302. ;; Support for extended build traces was added in protocol version #x162.
  303. (and (current-store-protocol-version)
  304. (>= (current-store-protocol-version) #x162)))
  305. (define (multiplexed-output-supported?)
  306. "Return true if the daemon supports \"multiplexed output\"--i.e., \"@
  307. build-log\" traces."
  308. (and (current-store-protocol-version)
  309. (>= (current-store-protocol-version) #x163)))
  310. (define spin!
  311. (let ((steps (circular-list "\\" "|" "/" "-")))
  312. (lambda (phase port)
  313. "Display a spinner on PORT. If PHASE is true, display it as a hint of
  314. the current build phase."
  315. (when (isatty?* port)
  316. (match steps
  317. ((first . rest)
  318. (set! steps rest)
  319. (display "\r\x1b[K" port)
  320. (display first port)
  321. (when phase
  322. (display " " port)
  323. ;; TRANSLATORS: The word "phase" here denotes a "build phase";
  324. ;; "~a" is a placeholder for the untranslated name of the current
  325. ;; build phase--e.g., 'configure' or 'build'.
  326. (format port (G_ "'~a' phase") phase))
  327. (force-output port)))))))
  328. (define colorize-log-line
  329. ;; Take a string and return a possibly colorized string according to the
  330. ;; rules below.
  331. (color-rules
  332. ("^(phase)(.*)(succeeded after)(.*)(seconds)(.*)"
  333. GREEN BOLD GREEN RESET GREEN BLUE)
  334. ("^(phase)(.*)(failed after)(.*)(seconds)(.*)"
  335. RED BLUE RED BLUE RED BLUE)
  336. ("^(.*)(error|fail|failed|\\<FAIL|FAILED)([[:blank:]]*)(:)(.*)"
  337. RESET RED BOLD BOLD BOLD)
  338. ("^(.*)(warning)([[:blank:]]*)(:)(.*)"
  339. RESET MAGENTA BOLD BOLD BOLD)))
  340. (define (hook-message hook-type)
  341. "Return a human-readable string for the profile hook type HOOK-TYPE."
  342. (match hook-type
  343. ('info-dir
  344. (G_ "building directory of Info manuals..."))
  345. ('ghc-package-cache
  346. (G_ "building GHC package cache..."))
  347. ('ca-certificate-bundle
  348. (G_ "building CA certificate bundle..."))
  349. ('emacs-subdirs
  350. (G_ "listing Emacs sub-directories..."))
  351. ('gdk-pixbuf-loaders-cache-file
  352. (G_ "generating GdkPixbuf loaders cache..."))
  353. ('glib-schemas
  354. (G_ "generating GLib schema cache..."))
  355. ('gtk-icon-themes
  356. (G_ "creating GTK+ icon theme cache..."))
  357. ('gtk-im-modules
  358. (G_ "building cache files for GTK+ input methods..."))
  359. ('xdg-desktop-database
  360. (G_ "building XDG desktop file cache..."))
  361. ('xdg-mime-database
  362. (G_ "building XDG MIME database..."))
  363. ('fonts-dir
  364. (G_ "building fonts directory..."))
  365. ('texlive-font-maps
  366. (G_ "building TeX Live font maps..."))
  367. ('manual-database
  368. (G_ "building database for manual pages..."))
  369. ('package-cache ;package cache generated by 'guix pull'
  370. (G_ "building package cache..."))
  371. (_ #f)))
  372. (define* (print-build-event event old-status status
  373. #:optional (port (current-error-port))
  374. #:key
  375. (colorize? (color-output? port))
  376. (print-urls? #t)
  377. (print-log? #t))
  378. "Print information about EVENT and STATUS to PORT. When COLORIZE? is true,
  379. produce colorful output. When PRINT-LOG? is true, display the build log in
  380. addition to build events. When PRINT-URLS? is true, display the URL of
  381. substitutes being downloaded."
  382. (define info
  383. (if (and colorize? (or print-urls? print-log?))
  384. (cute colorize-string <> (color BOLD))
  385. identity))
  386. (define emph
  387. (if colorize?
  388. (cute colorize-string <> (color BOLD))
  389. identity))
  390. (define success
  391. (if colorize?
  392. (cute colorize-string <> (color GREEN BOLD))
  393. identity))
  394. (define failure
  395. (if colorize?
  396. (cute colorize-string <> (color RED BOLD))
  397. identity))
  398. (define tty?
  399. (isatty?* port))
  400. (define (report-build-progress phase %)
  401. (let ((% (min (max % 0) 100))) ;sanitize
  402. (erase-current-line port)
  403. (let* ((prefix (format #f "~3d% ~@['~a' ~]"
  404. (inexact->exact (round %))
  405. (case phase
  406. ((build) #f) ;not useful to display it
  407. (else phase))))
  408. (length (string-length prefix)))
  409. (display prefix port)
  410. (display (progress-bar % (- (current-terminal-columns) length))
  411. port))
  412. (force-output port)))
  413. (define print-log-line
  414. (if print-log?
  415. (if colorize?
  416. (lambda (id line)
  417. (display (colorize-log-line line) port))
  418. (lambda (id line)
  419. (display line port)))
  420. (lambda (id line)
  421. (match (build-status-building status)
  422. ((build) ;single job
  423. (match (build-completion build)
  424. ((? number? %)
  425. (report-build-progress (build-phase build) %))
  426. (_
  427. (spin! (build-phase build) port))))
  428. (_
  429. (spin! #f port))))))
  430. (define erase-current-line*
  431. (if (and (not print-log?) (isatty?* port))
  432. (lambda ()
  433. (erase-current-line port)
  434. (force-output port))
  435. (const #t)))
  436. (match event
  437. (('build-started drv . _)
  438. (erase-current-line*)
  439. (let ((properties (derivation-properties
  440. (read-derivation-from-file drv))))
  441. (match (assq-ref properties 'type)
  442. ('graft
  443. (let ((count (match (assq-ref properties 'graft)
  444. (#f 0)
  445. (lst (or (assq-ref lst 'count) 0)))))
  446. (format port (info (N_ "applying ~a graft for ~a ..."
  447. "applying ~a grafts for ~a ..."
  448. count))
  449. count
  450. (string-drop-right (store-path-package-name drv)
  451. (string-length ".drv")))))
  452. ('profile
  453. (let ((count (match (assq-ref properties 'profile)
  454. (#f 0)
  455. (lst (or (assq-ref lst 'count) 0)))))
  456. (format port (info (N_ "building profile with ~a package..."
  457. "building profile with ~a packages..."
  458. count))
  459. count)))
  460. ('profile-hook
  461. (let ((hook-type (assq-ref properties 'hook)))
  462. (or (and=> (hook-message hook-type)
  463. (lambda (msg)
  464. (display (info msg) port)))
  465. (format port (info (G_ "running profile hook of type '~a'..."))
  466. hook-type))))
  467. (_
  468. (format port (info (G_ "building ~a...")) drv))))
  469. (newline port))
  470. (('build-succeeded drv . _)
  471. (erase-current-line*) ;erase spinner or progress bar
  472. (when (or print-log? (not (extended-build-trace-supported?)))
  473. (format port (success (G_ "successfully built ~a")) drv)
  474. (newline port))
  475. (match (build-status-building status)
  476. (() #t)
  477. (ongoing ;when max-jobs > 1
  478. (format port
  479. (N_ "The following build is still in progress:~%~{ ~a~%~}~%"
  480. "The following builds are still in progress:~%~{ ~a~%~}~%"
  481. (length ongoing))
  482. (map build-derivation ongoing)))))
  483. (('build-failed drv . _)
  484. (erase-current-line*) ;erase spinner or progress bar
  485. (format port (failure (G_ "build of ~a failed")) drv)
  486. (newline port)
  487. (match (derivation-log-file drv)
  488. (#f
  489. (format port (failure (G_ "Could not find build log for '~a'."))
  490. drv))
  491. (log
  492. (format port (emph (G_ "View build log at '~a'.")) log)))
  493. (newline port))
  494. (('substituter-started item _ ...)
  495. (erase-current-line*)
  496. (when (or print-log? (not (extended-build-trace-supported?)))
  497. (format port (info (G_ "substituting ~a...")) item)
  498. (newline port)))
  499. (('download-started item uri _ ...)
  500. (when print-urls?
  501. (erase-current-line*)
  502. (format port (info (G_ "downloading from ~a ...")) uri)
  503. (newline port)))
  504. (('download-progress item uri
  505. (= string->number size)
  506. (= string->number transferred))
  507. ;; Print a progress bar, but only if there's only one on-going
  508. ;; job--otherwise the output would be intermingled.
  509. (when (= 1 (simultaneous-jobs status))
  510. (match (find (matching-download item)
  511. (build-status-downloading status))
  512. (#f #f) ;shouldn't happen!
  513. (download
  514. ;; XXX: It would be nice to memoize the abbreviation.
  515. (let ((uri (if (string-contains uri "/nar/")
  516. (nar-uri-abbreviation uri)
  517. (basename uri))))
  518. (display-download-progress uri size
  519. #:tty? tty?
  520. #:start-time
  521. (download-start download)
  522. #:transferred transferred))))))
  523. (('substituter-succeeded item _ ...)
  524. (when (extended-build-trace-supported?)
  525. ;; If there are no jobs running, we already reported download completion
  526. ;; so there's nothing left to do.
  527. (unless (zero? (simultaneous-jobs status))
  528. (format port (success (G_ "substitution of ~a complete")) item)
  529. (newline port))
  530. (when (and print-urls? (zero? (simultaneous-jobs status)))
  531. ;; Leave a blank line after the "downloading ..." line and the
  532. ;; progress bar (that's three lines in total).
  533. (newline port))))
  534. (('substituter-failed item _ ...)
  535. (format port (failure (G_ "substitution of ~a failed")) item)
  536. (newline port))
  537. (('hash-mismatch item algo expected actual _ ...)
  538. ;; TRANSLATORS: The final string looks like "sha256 hash mismatch for
  539. ;; /gnu/store/…-sth:", where "sha256" is the hash algorithm.
  540. (format port (failure (G_ "~a hash mismatch for ~a:")) algo item)
  541. (newline port)
  542. (format port (emph (G_ "\
  543. expected hash: ~a
  544. actual hash: ~a~%"))
  545. expected actual))
  546. (('build-remote drv host _ ...)
  547. (format port (emph (G_ "offloading build of ~a to '~a'")) drv host)
  548. (newline port))
  549. (('build-log pid line)
  550. (if (multiplexed-output-supported?)
  551. (if (not pid)
  552. (begin
  553. ;; LINE comes from the daemon, not from builders. Let it
  554. ;; through.
  555. (display line port)
  556. (force-output port))
  557. (print-log-line pid line))
  558. (cond ((string-prefix? "substitute: " line)
  559. ;; The daemon prefixes early messages coming with 'guix
  560. ;; substitute' with "substitute:". These are useful ("updating
  561. ;; substitutes from URL"), so let them through.
  562. (display line port)
  563. (force-output port))
  564. ((string-prefix? "waiting for locks" line)
  565. ;; This is when a derivation is already being built and we're just
  566. ;; waiting for the build to complete.
  567. (display (info (string-trim-right line)) port)
  568. (newline))
  569. (else
  570. (print-log-line pid line)))))
  571. (_
  572. event)))
  573. (define* (print-build-event/quiet event old-status status
  574. #:optional
  575. (port (current-error-port))
  576. #:key
  577. (colorize? (color-output? port)))
  578. (print-build-event event old-status status port
  579. #:colorize? colorize?
  580. #:print-urls? #f
  581. #:print-log? #f))
  582. (define* (print-build-event/quiet-with-urls event old-status status
  583. #:optional
  584. (port (current-error-port))
  585. #:key
  586. (colorize? (color-output? port)))
  587. (print-build-event event old-status status port
  588. #:colorize? colorize?
  589. #:print-urls? #t ;show download URLs
  590. #:print-log? #f))
  591. (define* (build-status-updater #:optional (on-change (const #t)))
  592. "Return a procedure that can be passed to 'build-event-output-port'. That
  593. procedure computes the new build status upon each event and calls ON-CHANGE:
  594. (ON-CHANGE event status new-status)
  595. ON-CHANGE can display the build status, build events, etc."
  596. (lambda (event status)
  597. (let ((new (compute-status event status)))
  598. (on-change event status new)
  599. new)))
  600. ;;;
  601. ;;; Build port.
  602. ;;;
  603. (define (maybe-utf8->string bv)
  604. "Attempt to decode BV as UTF-8 string and return it. Gracefully handle the
  605. case where BV does not contain only valid UTF-8."
  606. (catch 'decoding-error
  607. (lambda ()
  608. (utf8->string bv))
  609. (lambda _
  610. ;; This is the sledgehammer but it's the only safe way we have to
  611. ;; properly handle this. It's expensive but it's rarely needed.
  612. (let ((port (open-bytevector-input-port bv)))
  613. (set-port-encoding! port "UTF-8")
  614. (set-port-conversion-strategy! port 'substitute)
  615. (let ((str (read-string port)))
  616. (close-port port)
  617. str)))))
  618. (define (bytevector-index bv numbers offset count)
  619. "Search for NUMBERS in BV starting from OFFSET and reading up to COUNT bytes;
  620. return the offset where one of NUMBERS first occurs or #f if they could not be
  621. found."
  622. (let loop ((offset offset)
  623. (count count))
  624. (cond ((zero? count) #f)
  625. ((memv (bytevector-u8-ref bv offset) numbers) offset)
  626. (else (loop (+ 1 offset) (- count 1))))))
  627. (define (split-lines str)
  628. "Split STR into lines in a way that preserves newline characters."
  629. (let loop ((str str)
  630. (result '()))
  631. (if (string-null? str)
  632. (reverse result)
  633. (match (string-index str #\newline)
  634. (#f
  635. (loop "" (cons str result)))
  636. (index
  637. (loop (string-drop str (+ index 1))
  638. (cons (string-take str (+ index 1)) result)))))))
  639. (define* (build-event-output-port proc #:optional (seed (build-status)))
  640. "Return an output port for use as 'current-build-output-port' that calls
  641. PROC with its current state value, initialized with SEED, on every build
  642. event. Build events passed to PROC are tuples corresponding to the \"build
  643. traces\" produced by the daemon:
  644. (build-started \"/gnu/store/...-foo.drv\" ...)
  645. (substituter-started \"/gnu/store/...-foo\" ...)
  646. and so on.
  647. The second return value is a thunk to retrieve the current state."
  648. (define %fragments
  649. ;; Line fragments received so far.
  650. '())
  651. (define %state
  652. ;; Current state for PROC.
  653. seed)
  654. ;; When true, this represents the current state while reading a
  655. ;; "@ build-log" trace: the current builder PID, the previously-read
  656. ;; bytevectors, and the number of bytes that remain to be read.
  657. (define %build-output-pid #f)
  658. (define %build-output '())
  659. (define %build-output-left #f)
  660. (define (process-line line)
  661. (cond ((string-prefix? "@ " line)
  662. ;; Note: Drop the trailing \n, and use 'string-split' to preserve
  663. ;; spaces (the log file part of 'build-started' events can be the
  664. ;; empty string.)
  665. (match (string-split (string-drop (string-drop-right line 1) 2)
  666. #\space)
  667. (("build-log" (= string->number pid) (= string->number len))
  668. (set! %build-output-pid pid)
  669. (set! %build-output '())
  670. (set! %build-output-left len))
  671. (((= string->symbol event-name) args ...)
  672. (set! %state
  673. (proc (cons event-name args)
  674. %state)))))
  675. (else
  676. (set! %state (proc (list 'build-log #f line)
  677. %state)))))
  678. (define (process-build-output pid output)
  679. ;; Transform OUTPUT in 'build-log' events or download events as generated
  680. ;; by extended build traces.
  681. (define (line->event line)
  682. (match (and (string-prefix? "@ " line)
  683. (string-tokenize (string-drop line 2)))
  684. ((type . args)
  685. (if (or (string-prefix? "download-" type)
  686. (string=? "build-remote" type))
  687. (cons (string->symbol type) args)
  688. `(build-log ,pid ,line)))
  689. (_
  690. `(build-log ,pid ,line))))
  691. (let* ((lines (split-lines output))
  692. (events (map line->event lines)))
  693. (set! %state (fold proc %state events))))
  694. (define (bytevector-range bv offset count)
  695. (let ((ptr (bytevector->pointer bv offset)))
  696. (pointer->bytevector ptr count)))
  697. (define (write! bv offset count)
  698. (if %build-output-pid
  699. (let ((keep (min count %build-output-left)))
  700. (set! %build-output
  701. (let ((bv* (make-bytevector keep)))
  702. (bytevector-copy! bv offset bv* 0 keep)
  703. (cons bv* %build-output)))
  704. (set! %build-output-left
  705. (- %build-output-left keep))
  706. (when (zero? %build-output-left)
  707. (process-build-output %build-output-pid
  708. (string-concatenate-reverse
  709. (map maybe-utf8->string %build-output))) ;XXX
  710. (set! %build-output '())
  711. (set! %build-output-pid #f))
  712. keep)
  713. ;; Search for both '\n' and '\r'; the latter is appears in progress
  714. ;; messages sent by 'guix substitute' through the daemon.
  715. (match (bytevector-index bv
  716. (list (char->integer #\newline)
  717. (char->integer #\return))
  718. offset count)
  719. ((? integer? cr)
  720. (let* ((tail (maybe-utf8->string
  721. (bytevector-range bv offset (- cr -1 offset))))
  722. (line (string-concatenate-reverse
  723. (cons tail %fragments))))
  724. (process-line line)
  725. (set! %fragments '())
  726. (- cr -1 offset)))
  727. (#f
  728. (unless (zero? count)
  729. (let ((str (maybe-utf8->string
  730. (bytevector-range bv offset count))))
  731. (set! %fragments (cons str %fragments))))
  732. count))))
  733. (define port
  734. (make-custom-binary-output-port "filtering-input-port"
  735. write!
  736. #f #f
  737. #f))
  738. ;; The build port actually receives Unicode strings.
  739. (set-port-encoding! port "UTF-8")
  740. (setvbuf port 'line)
  741. (values port (lambda () %state)))
  742. (define (call-with-status-report on-event thunk)
  743. (parameterize ((current-terminal-columns (terminal-columns))
  744. (current-build-output-port
  745. (build-event-output-port (build-status-updater on-event))))
  746. (thunk)))
  747. (define-syntax-rule (with-status-report on-event exp ...)
  748. "Set up build status reporting to the user using the ON-EVENT procedure;
  749. evaluate EXP... in that context."
  750. (call-with-status-report on-event (lambda () exp ...)))
  751. (define (logger-for-level level)
  752. "Return the logging procedure that corresponds to LEVEL."
  753. (cond ((<= level 0) (const #t))
  754. ((= level 1) print-build-event/quiet)
  755. ((= level 2) print-build-event/quiet-with-urls)
  756. (else print-build-event)))
  757. (define (call-with-status-verbosity level thunk)
  758. (call-with-status-report (logger-for-level level) thunk))
  759. (define-syntax-rule (with-status-verbosity level exp ...)
  760. "Set up build status reporting to the user at the given LEVEL: 0 means
  761. silent, 1 means quiet, 2 means verbose. Evaluate EXP... in that context."
  762. (call-with-status-verbosity level (lambda () exp ...)))