better file writing/fix crontab -e

--HG--
branch : pmacs2
This commit is contained in:
moculus 2009-02-23 08:19:06 +00:00
parent f89d94867f
commit c5db9456ef
1 changed files with 19 additions and 18 deletions

View File

@ -580,10 +580,10 @@ class FileBuffer(Buffer):
raise Exception, "Path '%s' cannot be read" % (path) raise Exception, "Path '%s' cannot be read" % (path)
f = open(path, 'r') f = open(path, 'r')
return f return f
def _open_file_w(self, path=None): def _open_file_w(self, path=None, preserve=True):
if path is None: if path is None:
path = self.path path = self.path
if os.path.isfile(path): if preserve and os.path.isfile(path):
raise Exception, "Path '%s' already exists" % (path) raise Exception, "Path '%s' already exists" % (path)
d = os.path.dirname(path) d = os.path.dirname(path)
if not os.access(d, os.R_OK): if not os.access(d, os.R_OK):
@ -660,23 +660,24 @@ class FileBuffer(Buffer):
raise Exception, "oh no! %r has changed on-disk!" % self.path raise Exception, "oh no! %r has changed on-disk!" % self.path
temp_path = self._temp_path() temp_path = self._temp_path()
data = self.make_string() shutil.copyfile(self.path, temp_path)
if self.windows[0].mode.savetabs:
data = data.replace(" ", "\t")
data = self.write_filter(data)
f2 = self._open_file_w(temp_path) try:
f2.write(self.bytemark + data) data = self.make_string()
f2.close() if self.windows[0].mode.savetabs:
data = data.replace(" ", "\t")
if self.path_exists(): data = self.write_filter(data)
mode = os.stat(self.path)[0]
os.chmod(temp_path, mode) f2 = self._open_file_w(self.path, preserve=False)
f2.write(self.bytemark + data)
shutil.move(temp_path, self.path) f2.close()
self.store_checksum(data) except:
self.modified = False shutil.copyfile(temp_path, self.path)
else:
self.store_checksum(data)
self.modified = False
finally:
os.unlink(temp_path)
def save_as(self, path): def save_as(self, path):
self.path = path self.path = path
self.save() self.save()