utils.scm 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ;;; utils.scm --- General utilities for Guile-Daemon
  2. ;; Copyright © 2016 Alex Kost <alezost@gmail.com>
  3. ;; This file is part of Guile-Daemon.
  4. ;; Guile-Daemon is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; Guile-Daemon is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with Guile-Daemon. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This module provides miscellaneous utilities for guile-daemon.
  18. ;;; Code:
  19. (define-module (daemon utils)
  20. #:use-module (ice-9 match)
  21. #:use-module (srfi srfi-1)
  22. #:export (alist-replace
  23. delete-file-maybe
  24. report-error
  25. mkdir-with-parents
  26. ensure-directory
  27. print-output
  28. print-error))
  29. (define-syntax-rule (print-output format-string args ...)
  30. "Write some text and a newline to stdout using 'format'."
  31. (begin
  32. (format #t format-string args ...)
  33. (newline)))
  34. (define-syntax-rule (print-error format-string args ...)
  35. "Write some text and a newline to stderr using 'format'."
  36. (begin
  37. (format (current-error-port) format-string args ...)
  38. (newline)))
  39. (define (report-error error . args)
  40. "Call 'display-error' if ARGS are specified.
  41. Otherwise, display ERROR key."
  42. (if (null? args)
  43. (print-error "ERROR: ~a" error)
  44. (apply display-error #f (current-error-port) args)))
  45. (define* (alist-replace key value alist #:optional (key-eq? eq?))
  46. "Remove KEY elements from alist and add KEY/VALUE pair instead."
  47. (alist-cons key value
  48. (alist-delete key alist key-eq?)))
  49. ;; Originates from Guix: 'mkdir-p' from (guix build utils) module.
  50. (define (mkdir-with-parents directory)
  51. "Create DIRECTORY and all its ancestors."
  52. (let ((not-slash (char-set-complement (char-set #\/))))
  53. (let loop ((components (string-tokenize directory not-slash))
  54. (root (if (string-prefix? "/" directory) "" ".")))
  55. (match components
  56. ((head tail ...)
  57. (let ((file (string-append root "/" head)))
  58. (unless (file-exists? file)
  59. (mkdir file))
  60. (loop tail file)))
  61. (_ #t)))))
  62. (define (ensure-directory directory)
  63. "Create DIRECTORY if it does not exist."
  64. (unless (file-exists? directory)
  65. (mkdir-with-parents directory)))
  66. (define (delete-file-maybe file-name)
  67. "Delete file with FILE-NAME if it exists."
  68. (when (file-exists? file-name)
  69. (delete-file file-name)))
  70. ;;; utils.scm ends here