Skip to content

Commit

Permalink
Changed file writing to be atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
ekarulf committed Mar 29, 2009
1 parent 01d56f9 commit 9f46212
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions staticgenerator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Static file generator for Django."""
import os
import stat
import tempfile
from django.http import HttpRequest
from django.core.handlers.base import BaseHandler
from django.db.models.base import ModelBase
Expand Down Expand Up @@ -141,9 +143,12 @@ def publish_from_path(self, path, content=None):
raise StaticGeneratorException('Could not create the directory: %s' % directory)

try:
f = open(fn, 'w')
f.write(content)
f.close()
f, tmpname = tempfile.mkstemp(dir=directory)
os.write(f, content)
os.close(f)
os.chmod(tmpname, stat.S_IREAD | stat.S_IWRITE | stat.S_IWUSR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
os.rename(tmpname, fn)

except:
raise StaticGeneratorException('Could not create the file: %s' % fn)

Expand Down

0 comments on commit 9f46212

Please sign in to comment.