changeset 14099:0824a0a3cefc

util: add readfile() & writefile() helper functions These two functions allow quickly reading or writing a file, without relying on reference counting to close the file handle afterwards.
author Dan Villiom Podlaski Christiansen <danchr@gmail.com>
date Sun, 01 May 2011 11:46:49 +0200
parents 9f5a0acb0056
children 3e9e02a41dfb
files mercurial/util.py
diffstat 1 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Sat Apr 30 18:30:14 2011 +0200
+++ b/mercurial/util.py	Sun May 01 11:46:49 2011 +0200
@@ -771,6 +771,20 @@
     makedirs(parent, mode)
     makedirs(name, mode)
 
+def readfile(path):
+    fp = open(path)
+    try:
+        return fp.read(size)
+    finally:
+        fp.close()
+
+def writefile(path, mode, text):
+    fp = open(path, mode)
+    try:
+        fp.write(text)
+    finally:
+        fp.close()
+
 class chunkbuffer(object):
     """Allow arbitrary sized chunks of data to be efficiently read from an
     iterator over chunks of arbitrary size."""