changeset 1019:a9cca981c423

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.
author mpm@selenic.com
date Wed, 24 Aug 2005 00:32:10 -0700
parents bb3f23fe59f0
children f1b052db3515
files mercurial/hg.py
diffstat 1 files changed, 15 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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