Patchwork [06,of,11] vfs: add "readlines" and "tryreadlines"

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Nov. 19, 2014, 9:41 a.m.
Message ID <875a3e5eb588b732c42e.1416390111@juju>
Download mbox | patch
Permalink /patch/6793/
State Accepted
Commit bf8c3172255ccd83af6a283c58592783a9aaf3aa
Headers show

Comments

Katsunori FUJIWARA - Nov. 19, 2014, 9:41 a.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1416389714 -32400
#      Wed Nov 19 18:35:14 2014 +0900
# Node ID 875a3e5eb588b732c42eee4cbbd8e5e653297fd8
# Parent  911845cbabc2d44a434b3974b2534d8593615b35
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.

Patch

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -188,6 +188,15 @@  class abstractvfs(object):
                 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 @@  class abstractvfs(object):
         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: