evaluate.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017 Jan Nieuwenhuizen <janneke@gnu.org>
  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. ;;; This program replicates the behavior of Hydra's 'hydra-eval-guile-job'.
  20. ;;; It evaluates the Hydra job defined by the program passed as its first
  21. ;;; arguments and outputs an sexp of the jobs on standard output.
  22. (use-modules (guix store)
  23. (srfi srfi-19)
  24. (ice-9 match)
  25. (ice-9 pretty-print)
  26. (ice-9 format))
  27. (define %user-module
  28. ;; Hydra user module.
  29. (let ((m (make-module)))
  30. (beautify-user-module! m)
  31. m))
  32. (cond-expand
  33. (guile-2.2
  34. ;; Guile 2.2.2 has a bug whereby 'time-monotonic' objects have seconds and
  35. ;; nanoseconds swapped (fixed in Guile commit 886ac3e). Work around it.
  36. (define time-monotonic time-tai))
  37. (else #t))
  38. (define (call-with-time thunk kont)
  39. "Call THUNK and pass KONT the elapsed time followed by THUNK's return
  40. values."
  41. (let* ((start (current-time time-monotonic))
  42. (result (call-with-values thunk list))
  43. (end (current-time time-monotonic)))
  44. (apply kont (time-difference end start) result)))
  45. (define (call-with-time-display thunk)
  46. "Call THUNK and write to the current output port its duration."
  47. (call-with-time thunk
  48. (lambda (time . results)
  49. (format #t "~,3f seconds~%"
  50. (+ (time-second time)
  51. (/ (time-nanosecond time) 1e9)))
  52. (apply values results))))
  53. (define (assert-valid-job job thing)
  54. "Raise an error if THING is not an alist with a valid 'derivation' entry.
  55. Otherwise return THING."
  56. (unless (and (list? thing)
  57. (and=> (assoc-ref thing 'derivation)
  58. (lambda (value)
  59. (and (string? value)
  60. (string-suffix? ".drv" value)))))
  61. (error "job did not produce a valid alist" job thing))
  62. thing)
  63. ;; Without further ado...
  64. (match (command-line)
  65. ((command file cuirass? ...)
  66. ;; Load FILE, a Scheme file that defines Hydra jobs.
  67. (let ((port (current-output-port)))
  68. (save-module-excursion
  69. (lambda ()
  70. (set-current-module %user-module)
  71. (primitive-load file)))
  72. (with-store store
  73. ;; Make sure we don't resort to substitutes.
  74. (set-build-options store
  75. #:use-substitutes? #f
  76. #:substitute-urls '())
  77. ;; Grafts can trigger early builds. We do not want that to happen
  78. ;; during evaluation, so use a sledgehammer to catch such problems.
  79. (set! build-things
  80. (lambda (store . args)
  81. (format (current-error-port)
  82. "error: trying to build things during evaluation!~%")
  83. (format (current-error-port)
  84. "'build-things' arguments: ~s~%" args)
  85. (exit 1)))
  86. ;; Call the entry point of FILE and print the resulting job sexp.
  87. (pretty-print
  88. (match ((module-ref %user-module
  89. (if (equal? cuirass? "cuirass")
  90. 'cuirass-jobs
  91. 'hydra-jobs))
  92. store '())
  93. (((names . thunks) ...)
  94. (map (lambda (job thunk)
  95. (format (current-error-port) "evaluating '~a'... " job)
  96. (force-output (current-error-port))
  97. (cons job
  98. (assert-valid-job job
  99. (call-with-time-display thunk))))
  100. names thunks)))
  101. port))))
  102. ((command _ ...)
  103. (format (current-error-port) "Usage: ~a FILE [cuirass]
  104. Evaluate the Hydra or Cuirass jobs defined in FILE.~%"
  105. command)
  106. (exit 1)))
  107. ;;; Local Variables:
  108. ;;; eval: (put 'call-with-time 'scheme-indent-function 1)
  109. ;;; End: