changeset 23368:bf8c3172255c

vfs: add "readlines" and "tryreadlines" This patch allows "readlines" and "tryreadlines" to take "mode" argument, because "subrepo" requires to read files not in "rb" (binary, default for vfs) but in "r" (text) mode in subsequent patch.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Wed, 19 Nov 2014 18:35:14 +0900
parents 115af8de76a4
children 22e00674d17e
files mercurial/scmutil.py
diffstat 1 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/scmutil.py	Wed Nov 19 18:35:14 2014 +0900
+++ b/mercurial/scmutil.py	Wed Nov 19 18:35:14 2014 +0900
@@ -188,6 +188,15 @@
                 raise
         return ""
 
+    def tryreadlines(self, path, mode='rb'):
+        '''gracefully return an empty array for missing files'''
+        try:
+            return self.readlines(path, mode=mode)
+        except IOError, inst:
+            if inst.errno != errno.ENOENT:
+                raise
+        return []
+
     def open(self, path, mode="r", text=False, atomictemp=False):
         self.open = self.__call__
         return self.__call__(path, mode, text, atomictemp)
@@ -199,6 +208,13 @@
         finally:
             fp.close()
 
+    def readlines(self, path, mode='rb'):
+        fp = self(path, mode=mode)
+        try:
+            return fp.readlines()
+        finally:
+            fp.close()
+
     def write(self, path, data):
         fp = self(path, 'wb')
         try: