lilypond-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: race condition in writing lilypond-book filenames


From: Matthias Kilian
Subject: Re: race condition in writing lilypond-book filenames
Date: Sun, 13 Dec 2009 18:53:57 +0100
User-agent: Mutt/1.4.2.3i

On Sun, Dec 13, 2009 at 05:49:13PM +0100, Matthias Kilian wrote:
> If the a file with the same name always has the same content,
> regardless from which document it has been generated, would something
> like the following help (no real code, i'm not good at all if it
> comes to python)?

Don't laugh at me -- I can't even test this here on OpenBSD, because
I don't have all prerequisites available in the necessery versions.


diff --git a/scripts/lilypond-book.py b/scripts/lilypond-book.py
index 392ddd0..35a1d8a 100644
--- a/scripts/lilypond-book.py
+++ b/scripts/lilypond-book.py
@@ -1273,8 +1273,17 @@ left-margin-default right-margin-default)"
         directory = os.path.split(path)[0]
         if not os.path.isdir (directory):
             os.makedirs (directory)
-        out = file (path + '.ly', 'w')
+        out = tempfile.NamedTemporaryFile(mode = 'w', suffix = '.lytmp',
+                                          dir = directory, delete = False)
         out.write (self.full_ly ())
+        out.close()
+        filename = path + '.ly'
+        if os.path.exists(filename):
+            # What else? Print a warning? Compare both files and error
+            # out if they differ?
+            os.remove(out.name)
+        else:
+            os.rename(out.name, filename)
         file (path + '.txt', 'w').write ('image of music')
 
     def relevant_contents (self, ly):




reply via email to

[Prev in Thread] Current Thread [Next in Thread]