utils.scm 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  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.
  10. ;;;
  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.
  15. ;;;
  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/>.
  18. (define-module (gnu installer utils)
  19. #:use-module (guix utils)
  20. #:use-module (guix build utils)
  21. #:use-module (ice-9 rdelim)
  22. #:use-module (ice-9 regex)
  23. #:use-module (ice-9 textual-ports)
  24. #:export (read-lines
  25. read-all
  26. nearest-exact-integer
  27. read-percentage
  28. run-shell-command))
  29. (define* (read-lines #:optional (port (current-input-port)))
  30. "Read lines from PORT and return them as a list."
  31. (let loop ((line (read-line port))
  32. (lines '()))
  33. (if (eof-object? line)
  34. (reverse lines)
  35. (loop (read-line port)
  36. (cons line lines)))))
  37. (define (read-all file)
  38. "Return the content of the given FILE as a string."
  39. (call-with-input-file file
  40. get-string-all))
  41. (define (nearest-exact-integer x)
  42. "Given a real number X, return the nearest exact integer, with ties going to
  43. the nearest exact even integer."
  44. (inexact->exact (round x)))
  45. (define (read-percentage percentage)
  46. "Read PERCENTAGE string and return the corresponding percentage as a
  47. number. If no percentage is found, return #f"
  48. (let ((result (string-match "^([0-9]+)%$" percentage)))
  49. (and result
  50. (string->number (match:substring result 1)))))
  51. (define (run-shell-command command)
  52. (call-with-temporary-output-file
  53. (lambda (file port)
  54. (format port "~a~%" command)
  55. ;; (format port "exit~%")
  56. (close port)
  57. (invoke "bash" "--init-file" file))))