Changeset 90702 in webkit
- Timestamp:
- Jul 10, 2011 6:59:57 PM (13 years ago)
- Location:
- trunk/Tools
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Tools/ChangeLog
r90697 r90702 1 2011-07-11 Roland Steiner <rolandsteiner@chromium.org> 2 3 Reviewed by Tony Chang. 4 5 Complete functions in filesystem.py 6 https://bugs.webkit.org/show_bug.cgi?id=63528 7 8 - missing text file functions added 9 - functions sorted 10 - removed 'append' optional parameters (were unused) 11 - adapted filesystem_mock in the same way 12 13 * Scripts/webkitpy/common/system/filesystem.py: 14 * Scripts/webkitpy/common/system/filesystem_mock.py: 15 * Scripts/webkitpy/common/system/filesystem_unittest.py: 16 1 17 2011-07-10 Adam Barth <abarth@webkit.org> 2 18 -
trunk/Tools/Scripts/webkitpy/common/system/filesystem.py
r90050 r90702 192 192 return f, temp_name 193 193 194 def open_text_file_for_writing(self, path, append=False):195 mode = 'w'196 if append:197 mode = 'a'198 return codecs.open(path, mode, 'utf8')199 200 194 def open_binary_file_for_reading(self, path): 201 195 return codecs.open(path, 'rb') … … 206 200 return f.read() 207 201 202 def write_binary_file(self, path, contents): 203 with file(path, 'wb') as f: 204 f.write(contents) 205 206 def open_text_file_for_reading(self, path): 207 return codecs.open(path, 'r', 'utf8') 208 209 def open_text_file_for_writing(self, path): 210 return codecs.open(path, 'w', 'utf8') 211 208 212 def read_text_file(self, path): 209 213 """Return the contents of the file at the given path as a Unicode string. … … 212 216 with codecs.open(path, 'r', 'utf8') as f: 213 217 return f.read() 218 219 def write_text_file(self, path, contents): 220 """Write the contents to the file at the given location. 221 222 The file is written encoded as UTF-8 with no BOM.""" 223 with codecs.open(path, 'w', 'utf8') as f: 224 f.write(contents) 214 225 215 226 def relpath(self, path, start='.'): … … 253 264 """Return (dirname + os.sep + basename, '.' + ext)""" 254 265 return os.path.splitext(path) 255 256 def write_binary_file(self, path, contents):257 with file(path, 'wb') as f:258 f.write(contents)259 260 def write_text_file(self, path, contents):261 """Write the contents to the file at the given location.262 263 The file is written encoded as UTF-8 with no BOM."""264 with codecs.open(path, 'w', 'utf8') as f:265 f.write(contents) -
trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py
r90543 r90702 254 254 def open_binary_tempfile(self, suffix=''): 255 255 path = self._mktemp(suffix) 256 return (WritableFileObject(self, path), path) 257 258 def open_text_file_for_writing(self, path, append=False): 259 return WritableFileObject(self, path, append) 260 261 def read_text_file(self, path): 262 return self.read_binary_file(path).decode('utf-8') 256 return (WritableBinaryFileObject(self, path), path) 263 257 264 258 def open_binary_file_for_reading(self, path): 265 259 if self.files[path] is None: 266 260 self._raise_not_found(path) 267 return Readable FileObject(self, path, self.files[path])261 return ReadableBinaryFileObject(self, path, self.files[path]) 268 262 269 263 def read_binary_file(self, path): … … 273 267 return self.files[path] 274 268 269 def write_binary_file(self, path, contents): 270 self.files[path] = contents 271 self.written_files[path] = contents 272 273 def open_text_file_for_reading(self, path): 274 if self.files[path] is None: 275 self._raise_not_found(path) 276 return ReadableTextFileObject(self, path) 277 278 def open_text_file_for_writing(self, path): 279 return WritableTextFileObject(self, path) 280 281 def read_text_file(self, path): 282 return self.read_binary_file(path).decode('utf-8') 283 284 def write_text_file(self, path, contents): 285 return self.write_binary_file(path, contents.encode('utf-8')) 286 275 287 def relpath(self, path, start='.'): 276 288 return ospath.relpath(path, start, self.abspath, self.sep) … … 303 315 return (path[0:idx], path[idx:]) 304 316 305 def write_text_file(self, path, contents): 306 return self.write_binary_file(path, contents.encode('utf-8')) 307 308 def write_binary_file(self, path, contents): 309 self.files[path] = contents 310 self.written_files[path] = contents 311 312 313 class WritableFileObject(object): 314 def __init__(self, fs, path, append=False, encoding=None): 317 318 class WritableBinaryFileObject(object): 319 def __init__(self, fs, path): 315 320 self.fs = fs 316 321 self.path = path 317 322 self.closed = False 318 if path not in self.fs.files or not append: 319 self.fs.files[path] = "" 323 self.fs.files[path] = "" 320 324 321 325 def __enter__(self): … … 333 337 334 338 335 class ReadableFileObject(object): 339 class WritableTextFileObject(WritableBinaryFileObject): 340 def write(self, str): 341 WritableBinaryFileObject.write(self, str.encode('utf-8')) 342 343 344 class ReadableBinaryFileObject(object): 336 345 def __init__(self, fs, path, data=""): 337 346 self.fs = fs … … 356 365 self.offset += bytes 357 366 return self.data[start:self.offset] 367 368 369 class ReadableTextFileObject(ReadableBinaryFileObject): 370 def read(self, bytes=None): 371 return ReadableBinaryFileObject.read(self, bytes).decode('utf-8') -
trunk/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py
r89944 r90702 143 143 os.rmdir(sub_dir) 144 144 145 def test_read_and_write_text_file(self): 146 fs = FileSystem() 147 text_path = None 148 149 unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D' 150 hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD' 151 try: 152 text_path = tempfile.mktemp(prefix='tree_unittest_') 153 file = fs.open_text_file_for_writing(text_path) 154 file.write(unicode_text_string) 155 file.close() 156 157 file = fs.open_text_file_for_reading(text_path) 158 read_text = file.read() 159 file.close() 160 161 self.assertEqual(read_text, unicode_text_string) 162 finally: 163 if text_path and fs.isfile(text_path): 164 os.remove(text_path) 165 145 166 def test_read_and_write_file(self): 146 167 fs = FileSystem() … … 148 169 binary_path = None 149 170 150 unicode_text_string = u' Ūnĭcōde̽'171 unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D' 151 172 hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD' 152 173 try:
Note: See TracChangeset
for help on using the changeset viewer.