Create helper functions for I/O to files in the working directory
authormpm@selenic.com
Wed, 24 Aug 2005 00:32:10 -0700
changeset 1019 a9cca981c423
parent 1018 bb3f23fe59f0
child 1020 f1b052db3515
Create helper functions for I/O to files in the working directory This simplifies some code and gives us a single place to add I/O filters.
mercurial/hg.py
--- a/mercurial/hg.py	Wed Aug 24 00:12:04 2005 -0700
+++ b/mercurial/hg.py	Wed Aug 24 00:32:10 2005 -0700
@@ -773,6 +773,14 @@
     def wfile(self, f, mode='r'):
         return self.wopener(f, mode)
 
+    def wread(self, filename):
+        return self.wopener(filename, 'r').read()
+
+    def wwrite(self, filename, data, fd=None):
+        if fd:
+            return fd.write(data)
+        return self.wopener(filename, 'w').write(data)
+
     def transaction(self):
         # save dirstate for undo
         try:
@@ -839,7 +847,7 @@
         linkrev = self.changelog.count()
         for f in files:
             try:
-                t = self.wfile(f).read()
+                t = self.wread(f)
                 tm = util.is_exec(self.wjoin(f), mfm.get(f, False))
                 r = self.file(f)
                 mfm[f] = tm
@@ -932,7 +940,7 @@
             self.ui.note(f + "\n")
             try:
                 mf1[f] = util.is_exec(self.wjoin(f), mf1.get(f, False))
-                t = self.wfile(f).read()
+                t = self.wread(f)
             except IOError:
                 self.ui.warn("trouble committing %s!\n" % f)
                 raise
@@ -1023,7 +1031,7 @@
         mf2, u = None, []
 
         def fcmp(fn, mf):
-            t1 = self.wfile(fn).read()
+            t1 = self.wread(fn)
             t2 = self.file(fn).read(mf.get(fn, nullid))
             return cmp(t1, t2)
 
@@ -1660,7 +1668,7 @@
 
                 # is the wfile new since m1, and match m2?
                 if f not in m1:
-                    t1 = self.wfile(f).read()
+                    t1 = self.wread(f)
                     t2 = self.file(f).read(m2[f])
                     if cmp(t1, t2) == 0:
                         n = m2[f]
@@ -1780,10 +1788,10 @@
             self.ui.note("getting %s\n" % f)
             t = self.file(f).read(get[f])
             try:
-                self.wfile(f, "w").write(t)
+                self.wwrite(f, t)
             except IOError:
                 os.makedirs(os.path.dirname(self.wjoin(f)))
-                self.wfile(f, "w").write(t)
+                self.wwrite(f, t)
             util.set_exec(self.wjoin(f), mf2[f])
             if moddirstate:
                 if branch_merge:
@@ -1836,7 +1844,7 @@
             pre = "%s~%s." % (os.path.basename(fn), prefix)
             (fd, name) = tempfile.mkstemp("", pre)
             f = os.fdopen(fd, "wb")
-            f.write(fl.read(node))
+            self.wwrite(fn, fl.read(node), f)
             f.close()
             return name