OLD | NEW |
1 # Copyright (C) 2010 Google Inc. All rights reserved. | 1 # Copyright (C) 2010 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 27 matching lines...) Expand all Loading... |
38 import shutil | 38 import shutil |
39 import sys | 39 import sys |
40 import tempfile | 40 import tempfile |
41 import time | 41 import time |
42 | 42 |
43 | 43 |
44 class FileSystem(object): | 44 class FileSystem(object): |
45 """FileSystem interface for webkitpy. | 45 """FileSystem interface for webkitpy. |
46 | 46 |
47 Unless otherwise noted, all paths are allowed to be either absolute | 47 Unless otherwise noted, all paths are allowed to be either absolute |
48 or relative.""" | 48 or relative. |
| 49 """ |
49 sep = os.sep | 50 sep = os.sep |
50 pardir = os.pardir | 51 pardir = os.pardir |
51 | 52 |
52 def abspath(self, path): | 53 def abspath(self, path): |
53 return os.path.abspath(path) | 54 return os.path.abspath(path) |
54 | 55 |
55 def realpath(self, path): | 56 def realpath(self, path): |
56 return os.path.realpath(path) | 57 return os.path.realpath(path) |
57 | 58 |
58 def path_to_module(self, module_name): | 59 def path_to_module(self, module_name): |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 # http://stackoverflow.com/questions/1510188/can-seek-and-tell-work-with
-utf-8-encoded-documents-in-python | 211 # http://stackoverflow.com/questions/1510188/can-seek-and-tell-work-with
-utf-8-encoded-documents-in-python |
211 # . | 212 # . |
212 return codecs.open(path, 'r', 'utf8') | 213 return codecs.open(path, 'r', 'utf8') |
213 | 214 |
214 def open_text_file_for_writing(self, path): | 215 def open_text_file_for_writing(self, path): |
215 return codecs.open(path, 'w', 'utf8') | 216 return codecs.open(path, 'w', 'utf8') |
216 | 217 |
217 def read_text_file(self, path): | 218 def read_text_file(self, path): |
218 """Return the contents of the file at the given path as a Unicode string
. | 219 """Return the contents of the file at the given path as a Unicode string
. |
219 | 220 |
220 The file is read assuming it is a UTF-8 encoded file with no BOM.""" | 221 The file is read assuming it is a UTF-8 encoded file with no BOM. |
| 222 """ |
221 with codecs.open(path, 'r', 'utf8') as f: | 223 with codecs.open(path, 'r', 'utf8') as f: |
222 return f.read() | 224 return f.read() |
223 | 225 |
224 def write_text_file(self, path, contents): | 226 def write_text_file(self, path, contents): |
225 """Write the contents to the file at the given location. | 227 """Write the contents to the file at the given location. |
226 | 228 |
227 The file is written encoded as UTF-8 with no BOM.""" | 229 The file is written encoded as UTF-8 with no BOM. |
| 230 """ |
228 with codecs.open(path, 'w', 'utf8') as f: | 231 with codecs.open(path, 'w', 'utf8') as f: |
229 f.write(contents) | 232 f.write(contents) |
230 | 233 |
231 def sha1(self, path): | 234 def sha1(self, path): |
232 contents = self.read_binary_file(path) | 235 contents = self.read_binary_file(path) |
233 return hashlib.sha1(contents).hexdigest() | 236 return hashlib.sha1(contents).hexdigest() |
234 | 237 |
235 def relpath(self, path, start='.'): | 238 def relpath(self, path, start='.'): |
236 return os.path.relpath(path, start) | 239 return os.path.relpath(path, start) |
237 | 240 |
238 class _WindowsError(exceptions.OSError): | 241 class _WindowsError(exceptions.OSError): |
239 """Fake exception for Linux and Mac.""" | 242 """Fake exception for Linux and Mac.""" |
240 | 243 |
241 def remove(self, path, osremove=os.remove): | 244 def remove(self, path, osremove=os.remove): |
242 """On Windows, if a process was recently killed and it held on to a | 245 """On Windows, if a process was recently killed and it held on to a |
243 file, the OS will hold on to the file for a short while. This makes | 246 file, the OS will hold on to the file for a short while. This makes |
244 attempts to delete the file fail. To work around that, this method | 247 attempts to delete the file fail. To work around that, this method |
245 will retry for a few seconds until Windows is done with the file.""" | 248 will retry for a few seconds until Windows is done with the file. |
| 249 """ |
246 try: | 250 try: |
247 exceptions.WindowsError | 251 exceptions.WindowsError |
248 except AttributeError: | 252 except AttributeError: |
249 exceptions.WindowsError = FileSystem._WindowsError | 253 exceptions.WindowsError = FileSystem._WindowsError |
250 | 254 |
251 retry_timeout_sec = 3.0 | 255 retry_timeout_sec = 3.0 |
252 sleep_interval = 0.1 | 256 sleep_interval = 0.1 |
253 while True: | 257 while True: |
254 try: | 258 try: |
255 osremove(path) | 259 osremove(path) |
(...skipping 14 matching lines...) Expand all Loading... |
270 def split(self, path): | 274 def split(self, path): |
271 """Return (dirname, basename + '.' + ext)""" | 275 """Return (dirname, basename + '.' + ext)""" |
272 return os.path.split(path) | 276 return os.path.split(path) |
273 | 277 |
274 def splitext(self, path): | 278 def splitext(self, path): |
275 """Return (dirname + os.sep + basename, '.' + ext)""" | 279 """Return (dirname + os.sep + basename, '.' + ext)""" |
276 return os.path.splitext(path) | 280 return os.path.splitext(path) |
277 | 281 |
278 def make_executable(self, file_path): | 282 def make_executable(self, file_path): |
279 os.chmod(file_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_
IRGRP | stat.S_IXGRP) | 283 os.chmod(file_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_
IRGRP | stat.S_IXGRP) |
OLD | NEW |