Hi,

On Sun, Dec 13, 2009 at 03:55:00PM +0000, Graham Percival wrote:
> diff --git a/scripts/lilypond-book.py b/scripts/lilypond-book.py
> index 392ddd0..b9731f1 100644
> --- a/scripts/lilypond-book.py
> +++ b/scripts/lilypond-book.py
> @@ -1273,7 +1273,11 @@ 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')
> +        filename = path + '.ly'
> +        if os.path.exists(filename):
> +            print "PANIC!  ", filename, "already exists!"
> +            sys.exit(1)
> +        out = file (filename, 'w')
>          out.write (self.full_ly ())
>          file (path + '.txt', 'w').write ('image of music')

It's still possible that a file gets overwritten, for example if
one process creates the output file *after* another process checked
wether it exists, but *before* that other process actually creates
the file.

Anyway, I had an idea after seeing that diff...

> In any case, we definitely have the same filename being used multiple
> times when building lilypond.  Is this a problem?  I can't say for
> certainty, but I *do* know that unpredictable race conditions occur in
> the docs when building with more than one process.

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)?


- write the output to a temporary file (with a unique filename, but
  within the same directory)

- after writing the temporary file, test wether a file with the final
  name (path + '.ly') already exists

- if not, just rename the tempory file to the final name

- if it already existed, remove the temporary file (here one could
  additionally compare the two files and error out if they differ)

There's still a race here (again, between the check wether the final
file exists and the actual renaming). However, if equally named
files are expected to have the same content, this race doesn't
matter, because renaming a file is an atomic operation, and because
the final file will always be complete.

Ciao,
        Kili


_______________________________________________
lilypond-devel mailing list
lilypond-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/lilypond-devel

Reply via email to