| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 | 71 |
| 72 def copyfile(self, source, destination): | 72 def copyfile(self, source, destination): |
| 73 shutil.copyfile(source, destination) | 73 shutil.copyfile(source, destination) |
| 74 | 74 |
| 75 def dirname(self, path): | 75 def dirname(self, path): |
| 76 return os.path.dirname(path) | 76 return os.path.dirname(path) |
| 77 | 77 |
| 78 def exists(self, path): | 78 def exists(self, path): |
| 79 return os.path.exists(path) | 79 return os.path.exists(path) |
| 80 | 80 |
| 81 def files_under(self, path, dirs_to_skip=[], file_filter=None): | 81 def files_under(self, path, dirs_to_skip=None, file_filter=None): |
| 82 """Return the list of all files under the given path in topdown order. | 82 """Return the list of all files under the given path in topdown order. |
| 83 | 83 |
| 84 Args: | 84 Args: |
| 85 dirs_to_skip: a list of directories to skip over during the | 85 dirs_to_skip: a list of directories to skip over during the |
| 86 traversal (e.g., .svn, resources, etc.) | 86 traversal (e.g., .svn, resources, etc.) |
| 87 file_filter: if not None, the filter will be invoked | 87 file_filter: if not None, the filter will be invoked |
| 88 with the filesystem object and the dirname and basename of | 88 with the filesystem object and the dirname and basename of |
| 89 each file found. The file is included in the result if the | 89 each file found. The file is included in the result if the |
| 90 callback returns True. | 90 callback returns True. |
| 91 """ | 91 """ |
| 92 def filter_all(fs, dirpath, basename): | 92 dirs_to_skip = dirs_to_skip or [] |
| 93 |
| 94 def filter_all(*_): |
| 93 return True | 95 return True |
| 94 | 96 |
| 95 file_filter = file_filter or filter_all | 97 file_filter = file_filter or filter_all |
| 96 files = [] | 98 files = [] |
| 97 if self.isfile(path): | 99 if self.isfile(path): |
| 98 if file_filter(self, self.dirname(path), self.basename(path)): | 100 if file_filter(self, self.dirname(path), self.basename(path)): |
| 99 files.append(path) | 101 files.append(path) |
| 100 return files | 102 return files |
| 101 | 103 |
| 102 if self.basename(path) in dirs_to_skip: | 104 if self.basename(path) in dirs_to_skip: |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 def __init__(self, **kwargs): | 155 def __init__(self, **kwargs): |
| 154 self._kwargs = kwargs | 156 self._kwargs = kwargs |
| 155 self._directory_path = tempfile.mkdtemp(**self._kwargs) | 157 self._directory_path = tempfile.mkdtemp(**self._kwargs) |
| 156 | 158 |
| 157 def __str__(self): | 159 def __str__(self): |
| 158 return self._directory_path | 160 return self._directory_path |
| 159 | 161 |
| 160 def __enter__(self): | 162 def __enter__(self): |
| 161 return self._directory_path | 163 return self._directory_path |
| 162 | 164 |
| 163 def __exit__(self, type, value, traceback): | 165 def __exit__(self, *_): |
| 164 # Only self-delete if necessary. | 166 # Only self-delete if necessary. |
| 165 | |
| 166 # FIXME: Should we delete non-empty directories? | 167 # FIXME: Should we delete non-empty directories? |
| 167 if os.path.exists(self._directory_path): | 168 if os.path.exists(self._directory_path): |
| 168 os.rmdir(self._directory_path) | 169 os.rmdir(self._directory_path) |
| 169 | 170 |
| 170 return TemporaryDirectory(**kwargs) | 171 return TemporaryDirectory(**kwargs) |
| 171 | 172 |
| 172 def maybe_make_directory(self, *path): | 173 def maybe_make_directory(self, *path): |
| 173 """Create the specified directory if it doesn't already exist.""" | 174 """Create the specified directory if it doesn't already exist.""" |
| 174 try: | 175 try: |
| 175 os.makedirs(self.join(*path)) | 176 os.makedirs(self.join(*path)) |
| 176 except OSError as e: | 177 except OSError as e: |
| 177 if e.errno != errno.EEXIST: | 178 if e.errno != errno.EEXIST: |
| 178 raise | 179 raise |
| 179 | 180 |
| 180 def move(self, source, destination): | 181 def move(self, source, destination): |
| 181 shutil.move(source, destination) | 182 shutil.move(source, destination) |
| 182 | 183 |
| 183 def mtime(self, path): | 184 def mtime(self, path): |
| 184 return os.stat(path).st_mtime | 185 return os.stat(path).st_mtime |
| 185 | 186 |
| 186 def normpath(self, path): | 187 def normpath(self, path): |
| 187 return os.path.normpath(path) | 188 return os.path.normpath(path) |
| 188 | 189 |
| 189 def open_binary_tempfile(self, suffix): | 190 def open_binary_tempfile(self, suffix): |
| 190 """Create, open, and return a binary temp file. Returns a tuple of the f
ile and the name.""" | 191 """Create, open, and return a binary temp file. Returns a tuple of the f
ile and the name.""" |
| 191 temp_fd, temp_name = tempfile.mkstemp(suffix) | 192 temp_fd, temp_name = tempfile.mkstemp(suffix) |
| 192 f = os.fdopen(temp_fd, 'wb') | 193 fh = os.fdopen(temp_fd, 'wb') |
| 193 return f, temp_name | 194 return fh, temp_name |
| 194 | 195 |
| 195 def open_binary_file_for_reading(self, path): | 196 def open_binary_file_for_reading(self, path): |
| 196 return codecs.open(path, 'rb') | 197 return codecs.open(path, 'rb') |
| 197 | 198 |
| 198 def read_binary_file(self, path): | 199 def read_binary_file(self, path): |
| 199 """Return the contents of the file at the given path as a byte string.""
" | 200 """Return the contents of the file at the given path as a byte string.""
" |
| 200 with file(path, 'rb') as f: | 201 with file(path, 'rb') as fh: |
| 201 return f.read() | 202 return fh.read() |
| 202 | 203 |
| 203 def write_binary_file(self, path, contents): | 204 def write_binary_file(self, path, contents): |
| 204 with file(path, 'wb') as f: | 205 with file(path, 'wb') as fh: |
| 205 f.write(contents) | 206 fh.write(contents) |
| 206 | 207 |
| 207 def open_text_file_for_reading(self, path): | 208 def open_text_file_for_reading(self, path): |
| 208 # Note: There appears to be an issue with the returned file objects | 209 # Note: There appears to be an issue with the returned file objects |
| 209 # not being seekable. See | 210 # not being seekable. See |
| 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') |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 def split(self, path): | 271 def split(self, path): |
| 271 """Return (dirname, basename + '.' + ext)""" | 272 """Return (dirname, basename + '.' + ext)""" |
| 272 return os.path.split(path) | 273 return os.path.split(path) |
| 273 | 274 |
| 274 def splitext(self, path): | 275 def splitext(self, path): |
| 275 """Return (dirname + os.sep + basename, '.' + ext)""" | 276 """Return (dirname + os.sep + basename, '.' + ext)""" |
| 276 return os.path.splitext(path) | 277 return os.path.splitext(path) |
| 277 | 278 |
| 278 def make_executable(self, file_path): | 279 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) | 280 os.chmod(file_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_
IRGRP | stat.S_IXGRP) |
| OLD | NEW |