409
by Martin Pool
- New AtomicFile class |
1 |
# Copyright (C) 2004, 2005 by Canonical Ltd
|
2 |
||
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
||
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
||
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
||
18 |
||
563
by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed |
19 |
from warnings import warn |
1185.31.58
by John Arbash Meinel
Updating for new transport tests so that they pass on win32 |
20 |
from bzrlib.osutils import rename |
1185.12.80
by Aaron Bentley
Added mode preservation to AtomicFile |
21 |
import errno |
563
by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed |
22 |
|
558
by Martin Pool
- All top-level classes inherit from object |
23 |
class AtomicFile(object): |
409
by Martin Pool
- New AtomicFile class |
24 |
"""A file that does an atomic-rename to move into place.
|
25 |
||
26 |
This also causes hardlinks to break when it's written out.
|
|
27 |
||
28 |
Open this as for a regular file, then use commit() to move into
|
|
29 |
place or abort() to cancel.
|
|
30 |
||
431
by Martin Pool
- stat cache is written in utf-8 to accomodate non-ascii |
31 |
An encoding can be specified; otherwise the default is ascii.
|
409
by Martin Pool
- New AtomicFile class |
32 |
"""
|
33 |
||
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
34 |
def __init__(self, filename, mode='wb', encoding=None, new_mode=None): |
409
by Martin Pool
- New AtomicFile class |
35 |
if mode != 'wb' and mode != 'wt': |
36 |
raise ValueError("invalid AtomicFile mode %r" % mode) |
|
37 |
||
38 |
import os, socket |
|
447
by Martin Pool
- atomicfile temporaries should end in .tmp to make it |
39 |
self.tmpfilename = '%s.%d.%s.tmp' % (filename, os.getpid(), |
409
by Martin Pool
- New AtomicFile class |
40 |
socket.gethostname()) |
41 |
self.realfilename = filename |
|
42 |
||
431
by Martin Pool
- stat cache is written in utf-8 to accomodate non-ascii |
43 |
if encoding: |
44 |
import codecs |
|
563
by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed |
45 |
self.f = codecs.open(self.tmpfilename, mode, encoding) |
46 |
else: |
|
47 |
self.f = open(self.tmpfilename, mode) |
|
48 |
||
409
by Martin Pool
- New AtomicFile class |
49 |
self.write = self.f.write |
563
by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed |
50 |
self.closed = False |
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
51 |
self._new_mode = new_mode |
563
by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed |
52 |
|
53 |
||
54 |
def __repr__(self): |
|
55 |
return '%s(%r)' % (self.__class__.__name__, |
|
56 |
self.realfilename) |
|
57 |
||
409
by Martin Pool
- New AtomicFile class |
58 |
|
59 |
def commit(self): |
|
497
by Martin Pool
- new AtomicFile.close() aborts if appropriate |
60 |
"""Close the file and move to final name."""
|
410
by Martin Pool
- Fix ignore command and add tests |
61 |
import sys, os |
62 |
||
563
by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed |
63 |
if self.closed: |
64 |
raise Exception('%r is already closed' % self) |
|
65 |
||
565
by Martin Pool
- more invariant checks in AtomicFile |
66 |
self.closed = True |
409
by Martin Pool
- New AtomicFile class |
67 |
self.f.close() |
565
by Martin Pool
- more invariant checks in AtomicFile |
68 |
self.f = None |
69 |
||
1185.12.80
by Aaron Bentley
Added mode preservation to AtomicFile |
70 |
try: |
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
71 |
if self._new_mode is None: |
72 |
self._new_mode = os.lstat(self.realfilename).st_mode |
|
1185.12.80
by Aaron Bentley
Added mode preservation to AtomicFile |
73 |
except OSError, e: |
74 |
if e.errno != errno.ENOENT: |
|
75 |
raise
|
|
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
76 |
else: |
77 |
os.chmod(self.tmpfilename, self._new_mode) |
|
78 |
||
1185.1.40
by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch. |
79 |
rename(self.tmpfilename, self.realfilename) |
409
by Martin Pool
- New AtomicFile class |
80 |
|
563
by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed |
81 |
|
409
by Martin Pool
- New AtomicFile class |
82 |
def abort(self): |
497
by Martin Pool
- new AtomicFile.close() aborts if appropriate |
83 |
"""Discard temporary file without committing changes."""
|
410
by Martin Pool
- Fix ignore command and add tests |
84 |
import os |
563
by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed |
85 |
|
86 |
if self.closed: |
|
87 |
raise Exception('%r is already closed' % self) |
|
88 |
||
565
by Martin Pool
- more invariant checks in AtomicFile |
89 |
self.closed = True |
409
by Martin Pool
- New AtomicFile class |
90 |
self.f.close() |
565
by Martin Pool
- more invariant checks in AtomicFile |
91 |
self.f = None |
409
by Martin Pool
- New AtomicFile class |
92 |
os.remove(self.tmpfilename) |
497
by Martin Pool
- new AtomicFile.close() aborts if appropriate |
93 |
|
563
by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed |
94 |
|
497
by Martin Pool
- new AtomicFile.close() aborts if appropriate |
95 |
def close(self): |
96 |
"""Discard the file unless already committed."""
|
|
97 |
if not self.closed: |
|
98 |
self.abort() |
|
563
by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed |
99 |
|
100 |
||
101 |
def __del__(self): |
|
1185.11.2
by John Arbash Meinel
LocalTransport tests pass. |
102 |
if hasattr(self, 'closed') and not self.closed: |
563
by Martin Pool
- AtomicFile emits a warning if it is gc'd without being closed |
103 |
warn("%r leaked" % self) |
428
by Martin Pool
- Use AtomicFile to update statcache. |
104 |