This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author milko.krachounov
Recipients eric.araujo, eric.smith, exarkun, giampaolo.rodola, meatballhat, milko.krachounov, olemis, pitrou, tarek, vstinner
Date 2011-05-12.18:26:25
SpamBayes Score 1.8374646e-10
Marked as misclassified No
Message-id <1305224786.83.0.480514790076.issue8604@psf.upfronthosting.co.za>
In-reply-to
Content
I have a class for overwriting files "properly" that I use in three of my projects. It aims to be atomic as possible, supports creating backups, but it doesn't have functions to set or retain permissions when requested (which might be desirable if such a function is added to stdlib). I'd give it here for reference and ideas.

- It's a context manager acting like a normal file object so you can use it with e.g. json.dump. In CM mode, if an error is caught, you end up with the old file automatically. If you use it as a file, the 'close' method has a 'cancel' argument to achieve the same.
- Normal overwrite on POSIX uses flush, fsync, rename as it should.
- Since fsync doesn't work on Mac OS X, it takes care of calling the Mac OS X specific F_FULLFSYNC fcntl.
- On POSIX, if a backup is requested, an attempt is made to do it with a hardlink, otherwise do two renames (killing the atomicity). Maybe a copy with shutil would be a better choice though.
- On Windows it uses two renames - the old file is backed up to a temporary name, and then the new file is renamed over it. If a backup wasn't requested, the temporary name is deleted.

I also have a simple unit test for it, but I've ran it on POSIX only.

Here's the part of the code that does the open/close part:
http://bazaar.launchpad.net/~exabyte/blackherd/async-refactor/view/61/blackherd/misc.py#L498

And the unit test:
http://bazaar.launchpad.net/~exabyte/blackherd/async-refactor/view/61/tests/test_misc.py#L473

I hope that's useful.
History
Date User Action Args
2011-05-12 18:26:27milko.krachounovsetrecipients: + milko.krachounov, exarkun, pitrou, vstinner, eric.smith, giampaolo.rodola, tarek, eric.araujo, olemis, meatballhat
2011-05-12 18:26:26milko.krachounovsetmessageid: <1305224786.83.0.480514790076.issue8604@psf.upfronthosting.co.za>
2011-05-12 18:26:26milko.krachounovlinkissue8604 messages
2011-05-12 18:26:25milko.krachounovcreate