changeset 855:258f60678791

pickle_atomic: use atomictempfile to avoid duplicating hg functionality atomictempfile takes care of a whole bunch of things we need, including atomic renaming, putting the tempfile in a reasonable location (with a reasonable name), and file mode copying. Thanks to mg for pointing out that we could use this instead of rolling our own file mode handling.
author Augie Fackler <durin42@gmail.com>
date Tue, 17 Apr 2012 07:46:43 -0500
parents 50a09999dce3
children 2a034bd52d14
files hgsubversion/svnmeta.py
diffstat 1 files changed, 6 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/svnmeta.py
+++ b/hgsubversion/svnmeta.py
@@ -13,21 +13,15 @@ import maps
 import editor
 
 
-def pickle_atomic(data, file_path, dir=None):
+def pickle_atomic(data, file_path):
     """pickle some data to a path atomically.
 
     This is present because I kept corrupting my revmap by managing to hit ^C
     during the pickle of that file.
     """
-    try:
-        f, path = tempfile.mkstemp(prefix='pickling', dir=dir)
-        f = os.fdopen(f, 'w')
-        pickle.dump(data, f)
-        f.close()
-    except: # pragma: no cover
-        raise
-    else:
-        hgutil.rename(path, file_path)
+    f = hgutil.atomictempfile(file_path, createmode=0644)
+    pickle.dump(data, f)
+    f.close()
 
 
 class SVNMeta(object):
@@ -77,8 +71,7 @@ class SVNMeta(object):
             self.repo.ui.setconfig('hgsubversion', 'layout', self._layout)
         else:
             self._layout = None
-        pickle_atomic(self.tag_locations, self.tag_locations_file,
-                      self.meta_data_dir)
+        pickle_atomic(self.tag_locations, self.tag_locations_file)
         # ensure nested paths are handled properly
         self.tag_locations.sort()
         self.tag_locations.reverse()
@@ -225,7 +218,7 @@ class SVNMeta(object):
         '''Save the Subversion metadata. This should really be called after
         every revision is created.
         '''
-        pickle_atomic(self.branches, self.branch_info_file, self.meta_data_dir)
+        pickle_atomic(self.branches, self.branch_info_file)
 
     def localname(self, path):
         """Compute the local name for a branch located at path.