What about this patch?

Is there a way to compose the following part somehow to prevent a
possible race condition?

    (rename-file prog prog-real)

    (with-output-to-file prog (lambda ()
                                (format #t
                                        "#!~a~%~a~%exec ~a~%"
                                        (which "bash")
                                        (string-join (map export-variable vars)
                                                     "\n")
                                        (canonicalize-path prog-real))))
    (chmod prog #o755)))

From 6bd8c169c320ffe9dd13a85a6f63a60b8508bc3c Mon Sep 17 00:00:00 2001
From: Nikita Karetnikov <nik...@karetnikov.org>
Date: Sat, 2 Mar 2013 20:08:39 +0000
Subject: [PATCH] utils: Add 'wrap-program'.

* guix/build/utils.scm (wrap-program): New procedure.
---
 guix/build/utils.scm |   40 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 39 insertions(+), 1 deletions(-)

diff --git a/guix/build/utils.scm b/guix/build/utils.scm
index 6921e31..2bff789 100644
--- a/guix/build/utils.scm
+++ b/guix/build/utils.scm
@@ -1,5 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2012, 2013 Ludovic Courtès <l...@gnu.org>
+;;; Copyright © 2013 Nikita Karetnikov <nik...@karetnikov.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -49,7 +50,8 @@
             patch-shebang
             patch-makefile-SHELL
             fold-port-matches
-            remove-store-references))
+            remove-store-references
+            wrap-program))
 

 ;;;
@@ -605,6 +607,42 @@ known as `nuke-refs' in Nixpkgs."
                              (put-u8 out (char->integer char))
                              result))))))
 
+(define* (wrap-program prog #:rest vars)
+  "Rename PROG to .PROG-real and make PROG a wrapper."
+  (let ((prog-real (string-append "." prog "-real")))
+    (define (export-variable lst)
+      ;; Return a string that exports an environment variable.
+      (match lst
+        ((var sep '= rest)
+         (format #f "export ~a=\"~a\""
+                 var (string-join rest sep)))
+        ((var sep 'prefix rest)
+         (format #f "export ~a=\"~a${~a~a+~a}$~a\""
+                 var (string-join rest sep) var sep sep var))
+        ((var sep 'suffix rest)
+         (format #f "export ~a=\"$~a${~a~a+~a}~a\""
+                 var var var sep sep (string-join rest sep)))
+        ((var '= rest)
+         (format #f "export ~a=\"~a\""
+                 var (string-join rest ":")))
+        ((var 'prefix rest)
+         (format #f "export ~a=\"~a${~a:+:}$~a\""
+                 var (string-join rest ":") var var))
+        ((var 'suffix rest)
+         (format #f "export ~a=\"$~a${~a:+:}~a\""
+                 var var var (string-join rest ":")))))
+
+    (rename-file prog prog-real)
+
+    (with-output-to-file prog (lambda ()
+                                (format #t
+                                        "#!~a~%~a~%exec ~a~%"
+                                        (which "bash")
+                                        (string-join (map export-variable vars)
+                                                     "\n")
+                                        (canonicalize-path prog-real))))
+    (chmod prog #o755)))
+
 ;;; Local Variables:
 ;;; eval: (put 'call-with-output-file/atomic 'scheme-indent-function 1)
 ;;; eval: (put 'with-throw-handler 'scheme-indent-function 1)
-- 
1.7.5.4

Attachment: pgpmr73YE4upi.pgp
Description: PGP signature

Reply via email to