aboutsummaryrefslogtreecommitdiff
path: root/ravanan/utils.scm
blob: b2c3c947fabed0afe899f43d2045edd2ce3cb097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
;;; ravanan --- High-reproducibility CWL runner powered by Guix
;;; Copyright © 2024 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of ravanan.
;;;
;;; ravanan is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; ravanan is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with ravanan.  If not, see <https://www.gnu.org/licenses/>.

(define-module (ravanan utils)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (ice-9 match)
  #:export (string-trim-prefix
            call-with-temporary-file))

(define (string-trim-prefix prefix str)
  "Remove @var{prefix} from @var{str} if it exists. Else, return @var{str} as is."
  (if (string-prefix? prefix str)
      (substring str (string-length prefix))
      str))

(define* (call-with-temporary-file proc #:optional (parent-directory (getcwd)))
  "Call @var{proc} with an output port to a new temporary file in
@var{parent-directory}, and delete it when @var{proc} returns or exits
non-locally."
  (let ((temporary-file-port (mkstemp (string-append parent-directory "/XXXXXX"))))
    (dynamic-wind (const #t)
                  (cut proc temporary-file-port)
                  (lambda ()
                    (delete-file (port-filename temporary-file-port))))))