Modified: trunk/Tools/Scripts/webkitpy/common/system/filesystem.py (90701 => 90702)
--- trunk/Tools/Scripts/webkitpy/common/system/filesystem.py 2011-07-11 01:47:21 UTC (rev 90701)
+++ trunk/Tools/Scripts/webkitpy/common/system/filesystem.py 2011-07-11 01:59:57 UTC (rev 90702)
@@ -191,12 +191,6 @@
f = os.fdopen(temp_fd, 'wb')
return f, temp_name
- def open_text_file_for_writing(self, path, append=False):
- mode = 'w'
- if append:
- mode = 'a'
- return codecs.open(path, mode, 'utf8')
-
def open_binary_file_for_reading(self, path):
return codecs.open(path, 'rb')
@@ -205,6 +199,16 @@
with file(path, 'rb') as f:
return f.read()
+ def write_binary_file(self, path, contents):
+ with file(path, 'wb') as f:
+ f.write(contents)
+
+ def open_text_file_for_reading(self, path):
+ return codecs.open(path, 'r', 'utf8')
+
+ def open_text_file_for_writing(self, path):
+ return codecs.open(path, 'w', 'utf8')
+
def read_text_file(self, path):
"""Return the contents of the file at the given path as a Unicode string.
@@ -212,6 +216,13 @@
with codecs.open(path, 'r', 'utf8') as f:
return f.read()
+ def write_text_file(self, path, contents):
+ """Write the contents to the file at the given location.
+
+ The file is written encoded as UTF-8 with no BOM."""
+ with codecs.open(path, 'w', 'utf8') as f:
+ f.write(contents)
+
def relpath(self, path, start='.'):
return ospath.relpath(path, start)
@@ -252,14 +263,3 @@
def splitext(self, path):
"""Return (dirname + os.sep + basename, '.' + ext)"""
return os.path.splitext(path)
-
- def write_binary_file(self, path, contents):
- with file(path, 'wb') as f:
- f.write(contents)
-
- def write_text_file(self, path, contents):
- """Write the contents to the file at the given location.
-
- The file is written encoded as UTF-8 with no BOM."""
- with codecs.open(path, 'w', 'utf8') as f:
- f.write(contents)
Modified: trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py (90701 => 90702)
--- trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py 2011-07-11 01:47:21 UTC (rev 90701)
+++ trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py 2011-07-11 01:59:57 UTC (rev 90702)
@@ -253,18 +253,12 @@
def open_binary_tempfile(self, suffix=''):
path = self._mktemp(suffix)
- return (WritableFileObject(self, path), path)
+ return (WritableBinaryFileObject(self, path), path)
- def open_text_file_for_writing(self, path, append=False):
- return WritableFileObject(self, path, append)
-
- def read_text_file(self, path):
- return self.read_binary_file(path).decode('utf-8')
-
def open_binary_file_for_reading(self, path):
if self.files[path] is None:
self._raise_not_found(path)
- return ReadableFileObject(self, path, self.files[path])
+ return ReadableBinaryFileObject(self, path, self.files[path])
def read_binary_file(self, path):
# Intentionally raises KeyError if we don't recognize the path.
@@ -272,6 +266,24 @@
self._raise_not_found(path)
return self.files[path]
+ def write_binary_file(self, path, contents):
+ self.files[path] = contents
+ self.written_files[path] = contents
+
+ def open_text_file_for_reading(self, path):
+ if self.files[path] is None:
+ self._raise_not_found(path)
+ return ReadableTextFileObject(self, path)
+
+ def open_text_file_for_writing(self, path):
+ return WritableTextFileObject(self, path)
+
+ def read_text_file(self, path):
+ return self.read_binary_file(path).decode('utf-8')
+
+ def write_text_file(self, path, contents):
+ return self.write_binary_file(path, contents.encode('utf-8'))
+
def relpath(self, path, start='.'):
return ospath.relpath(path, start, self.abspath, self.sep)
@@ -302,21 +314,13 @@
idx = 0
return (path[0:idx], path[idx:])
- def write_text_file(self, path, contents):
- return self.write_binary_file(path, contents.encode('utf-8'))
- def write_binary_file(self, path, contents):
- self.files[path] = contents
- self.written_files[path] = contents
-
-
-class WritableFileObject(object):
- def __init__(self, fs, path, append=False, encoding=None):
+class WritableBinaryFileObject(object):
+ def __init__(self, fs, path):
self.fs = fs
self.path = path
self.closed = False
- if path not in self.fs.files or not append:
- self.fs.files[path] = ""
+ self.fs.files[path] = ""
def __enter__(self):
return self
@@ -332,7 +336,12 @@
self.fs.written_files[self.path] = self.fs.files[self.path]
-class ReadableFileObject(object):
+class WritableTextFileObject(WritableBinaryFileObject):
+ def write(self, str):
+ WritableBinaryFileObject.write(self, str.encode('utf-8'))
+
+
+class ReadableBinaryFileObject(object):
def __init__(self, fs, path, data=""
self.fs = fs
self.path = path
@@ -355,3 +364,8 @@
start = self.offset
self.offset += bytes
return self.data[start:self.offset]
+
+
+class ReadableTextFileObject(ReadableBinaryFileObject):
+ def read(self, bytes=None):
+ return ReadableBinaryFileObject.read(self, bytes).decode('utf-8')