status.scm 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018, 2019, 2020, 2021 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. ('glib-schemas
  352. (G_ "generating GLib schema cache..."))
  353. ('gtk-icon-themes
  354. (G_ "creating GTK+ icon theme cache..."))
  355. ('gtk-im-modules
  356. (G_ "building cache files for GTK+ input methods..."))
  357. ('xdg-desktop-database
  358. (G_ "building XDG desktop file cache..."))
  359. ('xdg-mime-database
  360. (G_ "building XDG MIME database..."))
  361. ('fonts-dir
  362. (G_ "building fonts directory..."))
  363. ('texlive-configuration
  364. (G_ "building TeX Live configuration..."))
  365. ('manual-database
  366. (G_ "building database for manual pages..."))
  367. ('package-cache ;package cache generated by 'guix pull'
  368. (G_ "building package cache..."))
  369. (_ #f)))
  370. (define* (print-build-event event old-status status
  371. #:optional (port (current-error-port))
  372. #:key
  373. (colorize? (color-output? port))
  374. (print-urls? #t)
  375. (print-log? #t))
  376. "Print information about EVENT and STATUS to PORT. When COLORIZE? is true,
  377. produce colorful output. When PRINT-LOG? is true, display the build log in
  378. addition to build events. When PRINT-URLS? is true, display the URL of
  379. substitutes being downloaded."
  380. (define info
  381. (if colorize?
  382. (cute colorize-string <> (color BOLD))
  383. identity))
  384. (define success
  385. (if colorize?
  386. (cute colorize-string <> (color GREEN BOLD))
  387. identity))
  388. (define failure
  389. (if colorize?
  390. (cute colorize-string <> (color RED BOLD))
  391. identity))
  392. (define tty?
  393. (isatty?* port))
  394. (define (report-build-progress phase %)
  395. (let ((% (min (max % 0) 100))) ;sanitize
  396. (erase-current-line port)
  397. (let* ((prefix (format #f "~3d% ~@['~a' ~]"
  398. (inexact->exact (round %))
  399. (case phase
  400. ((build) #f) ;not useful to display it
  401. (else phase))))
  402. (length (string-length prefix)))
  403. (display prefix port)
  404. (display (progress-bar % (- (current-terminal-columns) length))
  405. port))
  406. (force-output port)))
  407. (define print-log-line
  408. (if print-log?
  409. (if colorize?
  410. (lambda (id line)
  411. (display (colorize-log-line line) port))
  412. (lambda (id line)
  413. (display line port)))
  414. (lambda (id line)
  415. (match (build-status-building status)
  416. ((build) ;single job
  417. (match (build-completion build)
  418. ((? number? %)
  419. (report-build-progress (build-phase build) %))
  420. (_
  421. (spin! (build-phase build) port))))
  422. (_
  423. (spin! #f port))))))
  424. (define erase-current-line*
  425. (if (and (not print-log?) (isatty?* port))
  426. (lambda ()
  427. (erase-current-line port)
  428. (force-output port))
  429. (const #t)))
  430. (match event
  431. (('build-started drv . _)
  432. (erase-current-line*)
  433. (let ((properties (derivation-properties
  434. (read-derivation-from-file drv))))
  435. (match (assq-ref properties 'type)
  436. ('graft
  437. (let ((count (match (assq-ref properties 'graft)
  438. (#f 0)
  439. (lst (or (assq-ref lst 'count) 0)))))
  440. (format port (info (N_ "applying ~a graft for ~a ..."
  441. "applying ~a grafts for ~a ..."
  442. count))
  443. count drv)))
  444. ('profile
  445. (let ((count (match (assq-ref properties 'profile)
  446. (#f 0)
  447. (lst (or (assq-ref lst 'count) 0)))))
  448. (format port (info (N_ "building profile with ~a package..."
  449. "building profile with ~a packages..."
  450. count))
  451. count)))
  452. ('profile-hook
  453. (let ((hook-type (assq-ref properties 'hook)))
  454. (or (and=> (hook-message hook-type)
  455. (lambda (msg)
  456. (format port (info msg))))
  457. (format port (info (G_ "running profile hook of type '~a'..."))
  458. hook-type))))
  459. (_
  460. (format port (info (G_ "building ~a...")) drv))))
  461. (newline port))
  462. (('build-succeeded drv . _)
  463. (erase-current-line*) ;erase spinner or progress bar
  464. (when (or print-log? (not (extended-build-trace-supported?)))
  465. (format port (success (G_ "successfully built ~a")) drv)
  466. (newline port))
  467. (match (build-status-building status)
  468. (() #t)
  469. (ongoing ;when max-jobs > 1
  470. (format port
  471. (N_ "The following build is still in progress:~%~{ ~a~%~}~%"
  472. "The following builds are still in progress:~%~{ ~a~%~}~%"
  473. (length ongoing))
  474. (map build-derivation ongoing)))))
  475. (('build-failed drv . _)
  476. (erase-current-line*) ;erase spinner or progress bar
  477. (format port (failure (G_ "build of ~a failed")) drv)
  478. (newline port)
  479. (match (derivation-log-file drv)
  480. (#f
  481. (format port (failure (G_ "Could not find build log for '~a'."))
  482. drv))
  483. (log
  484. (format port (info (G_ "View build log at '~a'.")) log)))
  485. (newline port))
  486. (('substituter-started item _ ...)
  487. (erase-current-line*)
  488. (when (or print-log? (not (extended-build-trace-supported?)))
  489. (format port (info (G_ "substituting ~a...")) item)
  490. (newline port)))
  491. (('download-started item uri _ ...)
  492. (when print-urls?
  493. (erase-current-line*)
  494. (format port (info (G_ "downloading from ~a ...")) uri)
  495. (newline port)))
  496. (('download-progress item uri
  497. (= string->number size)
  498. (= string->number transferred))
  499. ;; Print a progress bar, but only if there's only one on-going
  500. ;; job--otherwise the output would be intermingled.
  501. (when (= 1 (simultaneous-jobs status))
  502. (match (find (matching-download item)
  503. (build-status-downloading status))
  504. (#f #f) ;shouldn't happen!
  505. (download
  506. ;; XXX: It would be nice to memoize the abbreviation.
  507. (let ((uri (if (string-contains uri "/nar/")
  508. (nar-uri-abbreviation uri)
  509. (basename uri))))
  510. (display-download-progress uri size
  511. #:tty? tty?
  512. #:start-time
  513. (download-start download)
  514. #:transferred transferred))))))
  515. (('substituter-succeeded item _ ...)
  516. (when (extended-build-trace-supported?)
  517. ;; If there are no jobs running, we already reported download completion
  518. ;; so there's nothing left to do.
  519. (unless (zero? (simultaneous-jobs status))
  520. (format port (success (G_ "substitution of ~a complete")) item))
  521. (when (and print-urls? (zero? (simultaneous-jobs status)))
  522. ;; Leave a blank line after the "downloading ..." line and the
  523. ;; progress bar (that's three lines in total).
  524. (newline port))))
  525. (('substituter-failed item _ ...)
  526. (format port (failure (G_ "substitution of ~a failed")) item)
  527. (newline port))
  528. (('hash-mismatch item algo expected actual _ ...)
  529. ;; TRANSLATORS: The final string looks like "sha256 hash mismatch for
  530. ;; /gnu/store/…-sth:", where "sha256" is the hash algorithm.
  531. (format port (failure (G_ "~a hash mismatch for ~a:")) algo item)
  532. (newline port)
  533. (format port (info (G_ "\
  534. expected hash: ~a
  535. actual hash: ~a~%"))
  536. expected actual))
  537. (('build-remote drv host _ ...)
  538. (format port (info (G_ "offloading build of ~a to '~a'")) drv host)
  539. (newline port))
  540. (('build-log pid line)
  541. (if (multiplexed-output-supported?)
  542. (if (not pid)
  543. (begin
  544. ;; LINE comes from the daemon, not from builders. Let it
  545. ;; through.
  546. (display line port)
  547. (force-output port))
  548. (print-log-line pid line))
  549. (cond ((string-prefix? "substitute: " line)
  550. ;; The daemon prefixes early messages coming with 'guix
  551. ;; substitute' with "substitute:". These are useful ("updating
  552. ;; substitutes from URL"), so let them through.
  553. (display line port)
  554. (force-output port))
  555. ((string-prefix? "waiting for locks" line)
  556. ;; This is when a derivation is already being built and we're just
  557. ;; waiting for the build to complete.
  558. (display (info (string-trim-right line)) port)
  559. (newline))
  560. (else
  561. (print-log-line pid line)))))
  562. (_
  563. event)))
  564. (define* (print-build-event/quiet event old-status status
  565. #:optional
  566. (port (current-error-port))
  567. #:key
  568. (colorize? (color-output? port)))
  569. (print-build-event event old-status status port
  570. #:colorize? colorize?
  571. #:print-urls? #f
  572. #:print-log? #f))
  573. (define* (print-build-event/quiet-with-urls 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? #t ;show download URLs
  581. #:print-log? #f))
  582. (define* (build-status-updater #:optional (on-change (const #t)))
  583. "Return a procedure that can be passed to 'build-event-output-port'. That
  584. procedure computes the new build status upon each event and calls ON-CHANGE:
  585. (ON-CHANGE event status new-status)
  586. ON-CHANGE can display the build status, build events, etc."
  587. (lambda (event status)
  588. (let ((new (compute-status event status)))
  589. (on-change event status new)
  590. new)))
  591. ;;;
  592. ;;; Build port.
  593. ;;;
  594. (define (maybe-utf8->string bv)
  595. "Attempt to decode BV as UTF-8 string and return it. Gracefully handle the
  596. case where BV does not contain only valid UTF-8."
  597. (catch 'decoding-error
  598. (lambda ()
  599. (utf8->string bv))
  600. (lambda _
  601. ;; This is the sledgehammer but it's the only safe way we have to
  602. ;; properly handle this. It's expensive but it's rarely needed.
  603. (let ((port (open-bytevector-input-port bv)))
  604. (set-port-encoding! port "UTF-8")
  605. (set-port-conversion-strategy! port 'substitute)
  606. (let ((str (read-string port)))
  607. (close-port port)
  608. str)))))
  609. (define (bytevector-index bv number offset count)
  610. "Search for NUMBER in BV starting from OFFSET and reading up to COUNT bytes;
  611. return the offset where NUMBER first occurs or #f if it could not be found."
  612. (let loop ((offset offset)
  613. (count count))
  614. (cond ((zero? count) #f)
  615. ((= (bytevector-u8-ref bv offset) number) offset)
  616. (else (loop (+ 1 offset) (- count 1))))))
  617. (define (split-lines str)
  618. "Split STR into lines in a way that preserves newline characters."
  619. (let loop ((str str)
  620. (result '()))
  621. (if (string-null? str)
  622. (reverse result)
  623. (match (string-index str #\newline)
  624. (#f
  625. (loop "" (cons str result)))
  626. (index
  627. (loop (string-drop str (+ index 1))
  628. (cons (string-take str (+ index 1)) result)))))))
  629. (define* (build-event-output-port proc #:optional (seed (build-status)))
  630. "Return an output port for use as 'current-build-output-port' that calls
  631. PROC with its current state value, initialized with SEED, on every build
  632. event. Build events passed to PROC are tuples corresponding to the \"build
  633. traces\" produced by the daemon:
  634. (build-started \"/gnu/store/...-foo.drv\" ...)
  635. (substituter-started \"/gnu/store/...-foo\" ...)
  636. and so on.
  637. The second return value is a thunk to retrieve the current state."
  638. (define %fragments
  639. ;; Line fragments received so far.
  640. '())
  641. (define %state
  642. ;; Current state for PROC.
  643. seed)
  644. ;; When true, this represents the current state while reading a
  645. ;; "@ build-log" trace: the current builder PID, the previously-read
  646. ;; bytevectors, and the number of bytes that remain to be read.
  647. (define %build-output-pid #f)
  648. (define %build-output '())
  649. (define %build-output-left #f)
  650. (define (process-line line)
  651. (cond ((string-prefix? "@ " line)
  652. ;; Note: Drop the trailing \n, and use 'string-split' to preserve
  653. ;; spaces (the log file part of 'build-started' events can be the
  654. ;; empty string.)
  655. (match (string-split (string-drop (string-drop-right line 1) 2)
  656. #\space)
  657. (("build-log" (= string->number pid) (= string->number len))
  658. (set! %build-output-pid pid)
  659. (set! %build-output '())
  660. (set! %build-output-left len))
  661. (((= string->symbol event-name) args ...)
  662. (set! %state
  663. (proc (cons event-name args)
  664. %state)))))
  665. (else
  666. (set! %state (proc (list 'build-log #f line)
  667. %state)))))
  668. (define (process-build-output pid output)
  669. ;; Transform OUTPUT in 'build-log' events or download events as generated
  670. ;; by extended build traces.
  671. (define (line->event line)
  672. (match (and (string-prefix? "@ " line)
  673. (string-tokenize (string-drop line 2)))
  674. ((type . args)
  675. (if (or (string-prefix? "download-" type)
  676. (string=? "build-remote" type))
  677. (cons (string->symbol type) args)
  678. `(build-log ,pid ,line)))
  679. (_
  680. `(build-log ,pid ,line))))
  681. (let* ((lines (split-lines output))
  682. (events (map line->event lines)))
  683. (set! %state (fold proc %state events))))
  684. (define (bytevector-range bv offset count)
  685. (let ((ptr (bytevector->pointer bv offset)))
  686. (pointer->bytevector ptr count)))
  687. (define (write! bv offset count)
  688. (if %build-output-pid
  689. (let ((keep (min count %build-output-left)))
  690. (set! %build-output
  691. (let ((bv* (make-bytevector keep)))
  692. (bytevector-copy! bv offset bv* 0 keep)
  693. (cons bv* %build-output)))
  694. (set! %build-output-left
  695. (- %build-output-left keep))
  696. (when (zero? %build-output-left)
  697. (process-build-output %build-output-pid
  698. (string-concatenate-reverse
  699. (map maybe-utf8->string %build-output))) ;XXX
  700. (set! %build-output '())
  701. (set! %build-output-pid #f))
  702. keep)
  703. (match (bytevector-index bv (char->integer #\newline)
  704. offset count)
  705. ((? integer? cr)
  706. (let* ((tail (maybe-utf8->string
  707. (bytevector-range bv offset (- cr -1 offset))))
  708. (line (string-concatenate-reverse
  709. (cons tail %fragments))))
  710. (process-line line)
  711. (set! %fragments '())
  712. (- cr -1 offset)))
  713. (#f
  714. (unless (zero? count)
  715. (let ((str (maybe-utf8->string
  716. (bytevector-range bv offset count))))
  717. (set! %fragments (cons str %fragments))))
  718. count))))
  719. (define port
  720. (make-custom-binary-output-port "filtering-input-port"
  721. write!
  722. #f #f
  723. #f))
  724. ;; The build port actually receives Unicode strings.
  725. (set-port-encoding! port "UTF-8")
  726. (setvbuf port 'line)
  727. (values port (lambda () %state)))
  728. (define (call-with-status-report on-event thunk)
  729. (parameterize ((current-terminal-columns (terminal-columns))
  730. (current-build-output-port
  731. (build-event-output-port (build-status-updater on-event))))
  732. (thunk)))
  733. (define-syntax-rule (with-status-report on-event exp ...)
  734. "Set up build status reporting to the user using the ON-EVENT procedure;
  735. evaluate EXP... in that context."
  736. (call-with-status-report on-event (lambda () exp ...)))
  737. (define (logger-for-level level)
  738. "Return the logging procedure that corresponds to LEVEL."
  739. (cond ((<= level 0) (const #t))
  740. ((= level 1) print-build-event/quiet)
  741. ((= level 2) print-build-event/quiet-with-urls)
  742. (else print-build-event)))
  743. (define (call-with-status-verbosity level thunk)
  744. (call-with-status-report (logger-for-level level) thunk))
  745. (define-syntax-rule (with-status-verbosity level exp ...)
  746. "Set up build status reporting to the user at the given LEVEL: 0 means
  747. silent, 1 means quiet, 2 means verbose. Evaluate EXP... in that context."
  748. (call-with-status-verbosity level (lambda () exp ...)))