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.
--- 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."""