[PATCH 2 of 4] Save a few opens on the map file

Edouard Gomez ed.gomez at free.fr
Tue Feb 6 21:14:07 UTC 2007


# HG changeset patch
# User Edouard Gomez <ed.gomez at free.fr>
# Date 1170773210 -3600
# Node ID d9d0d9f3db675624e4f4308a0d732592eec703f7
# Parent  01c8007ecedc200615702f81d837cea3fd0f2edc
Save a few opens on the map file

Save a few opens on the map file adding a class attribute

diff -r 01c8007ecedc -r d9d0d9f3db67 contrib/convert-repo
--- a/contrib/convert-repo	Tue Feb 06 00:36:20 2007 +0100
+++ b/contrib/convert-repo	Tue Feb 06 15:46:50 2007 +0100
@@ -471,15 +471,18 @@ class convert:
 
         self.source = source
         self.dest = dest
-        self.mapfile = mapfile
         self.opts = opts
         self.commitcache = {}
+        self.mapfile = mapfile
+        self.mapfilefd = None
 
         self.map = {}
         try:
-            for l in file(self.mapfile):
+            origmapfile = open(self.mapfile, 'r')
+            for l in origmapfile:
                 sv, dv = l[:-1].split()
                 self.map[sv] = dv
+            origmapfile.close()
         except IOError:
             pass
 
@@ -555,6 +558,16 @@ class convert:
 
         return s
 
+    def mapentry(self, src, dst):
+        if self.mapfilefd is None:
+            try:
+                self.mapfilefd = open(self.mapfile, "a")
+            except IOError, (errno, strerror):
+                abort("Could not open map file %s: %s, %s\n" % (self.mapfile, errno, strerror))
+        self.map[src] = dst
+        self.mapfilefd.write("%s %s\n" % (src, dst))
+        self.mapfilefd.flush()
+
     def copy(self, rev):
         c = self.commitcache[rev]
         files = self.source.getchanges(rev)
@@ -570,8 +583,8 @@ class convert:
 
         r = [self.map[v] for v in c.parents]
         f = [f for f,v in files]
-        self.map[rev] = self.dest.putcommit(f, r, c)
-        file(self.mapfile, "a").write("%s %s\n" % (rev, self.map[rev]))
+        newnode = self.dest.putcommit(f, r, c)
+        self.mapentry(rev, newnode)
 
     def convert(self):
         status("scanning source...\n")
@@ -603,7 +616,11 @@ class convert:
             # write another hash correspondence to override the previous
             # one so we don't end up with extra tag heads
             if nrev:
-                file(self.mapfile, "a").write("%s %s\n" % (c, nrev))
+                self.mapentry(c, nrev)
+
+    def cleanup(self):
+       if self.mapfilefd:
+           self.mapfilefd.close()
 
 def command(src, dest=None, mapfile=None, **opts):
     srcc = converter(src)
@@ -627,7 +644,10 @@ def command(src, dest=None, mapfile=None
             mapfile = os.path.join(destc, "map")
 
     c = convert(srcc, destc, mapfile, opts)
-    c.convert()
+    try:
+        c.convert()
+    finally:
+        c.cleanup()  
 
 options = [('q', 'quiet', None, 'suppress output'),
            ('', 'datesort', None, 'try to sort changesets by date')]



More information about the Mercurial-devel mailing list