| 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=None, file_filter=None): | 81 def files_under(self, path, dirs_to_skip=[], 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 dirs_to_skip = dirs_to_skip or [] | 92 def filter_all(fs, dirpath, basename): |
| 93 | |
| 94 def filter_all(*_): | |
| 95 return True | 93 return True |
| 96 | 94 |
| 97 file_filter = file_filter or filter_all | 95 file_filter = file_filter or filter_all |
| 98 files = [] | 96 files = [] |
| 99 if self.isfile(path): | 97 if self.isfile(path): |
| 100 if file_filter(self, self.dirname(path), self.basename(path)): | 98 if file_filter(self, self.dirname(path), self.basename(path)): |
| 101 files.append(path) | 99 files.append(path) |
| 102 return files | 100 return files |
| 103 | 101 |
| 104 if self.basename(path) in dirs_to_skip: | 102 if self.basename(path) in dirs_to_skip: |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 def __init__(self, **kwargs): | 153 def __init__(self, **kwargs): |
| 156 self._kwargs = kwargs | 154 self._kwargs = kwargs |
| 157 self._directory_path = tempfile.mkdtemp(**self._kwargs) | 155 self._directory_path = tempfile.mkdtemp(**self._kwargs) |
| 158 | 156 |
| 159 def __str__(self): | 157 def __str__(self): |
| 160 return self._directory_path | 158 return self._directory_path |
| 161 | 159 |
| 162 def __enter__(self): | 160 def __enter__(self): |
| 163 return self._directory_path | 161 return self._directory_path |
| 164 | 162 |
| 165 def __exit__(self, *_): | 163 def __exit__(self, type, value, traceback): |
| 166 # Only self-delete if necessary. | 164 # Only self-delete if necessary. |
| 165 |
| 167 # FIXME: Should we delete non-empty directories? | 166 # FIXME: Should we delete non-empty directories? |
| 168 if os.path.exists(self._directory_path): | 167 if os.path.exists(self._directory_path): |
| 169 os.rmdir(self._directory_path) | 168 os.rmdir(self._directory_path) |
| 170 | 169 |
| 171 return TemporaryDirectory(**kwargs) | 170 return TemporaryDirectory(**kwargs) |
| 172 | 171 |
| 173 def maybe_make_directory(self, *path): | 172 def maybe_make_directory(self, *path): |
| 174 """Create the specified directory if it doesn't already exist.""" | 173 """Create the specified directory if it doesn't already exist.""" |
| 175 try: | 174 try: |
| 176 os.makedirs(self.join(*path)) | 175 os.makedirs(self.join(*path)) |
| 177 except OSError as e: | 176 except OSError as e: |
| 178 if e.errno != errno.EEXIST: | 177 if e.errno != errno.EEXIST: |
| 179 raise | 178 raise |
| 180 | 179 |
| 181 def move(self, source, destination): | 180 def move(self, source, destination): |
| 182 shutil.move(source, destination) | 181 shutil.move(source, destination) |
| 183 | 182 |
| 184 def mtime(self, path): | 183 def mtime(self, path): |
| 185 return os.stat(path).st_mtime | 184 return os.stat(path).st_mtime |
| 186 | 185 |
| 187 def normpath(self, path): | 186 def normpath(self, path): |
| 188 return os.path.normpath(path) | 187 return os.path.normpath(path) |
| 189 | 188 |
| 190 def open_binary_tempfile(self, suffix): | 189 def open_binary_tempfile(self, suffix): |
| 191 """Create, open, and return a binary temp file. Returns a tuple of the f
ile and the name.""" | 190 """Create, open, and return a binary temp file. Returns a tuple of the f
ile and the name.""" |
| 192 temp_fd, temp_name = tempfile.mkstemp(suffix) | 191 temp_fd, temp_name = tempfile.mkstemp(suffix) |
| 193 fh = os.fdopen(temp_fd, 'wb') | 192 f = os.fdopen(temp_fd, 'wb') |
| 194 return fh, temp_name | 193 return f, temp_name |
| 195 | 194 |
| 196 def open_binary_file_for_reading(self, path): | 195 def open_binary_file_for_reading(self, path): |
| 197 return codecs.open(path, 'rb') | 196 return codecs.open(path, 'rb') |
| 198 | 197 |
| 199 def read_binary_file(self, path): | 198 def read_binary_file(self, path): |
| 200 """Return the contents of the file at the given path as a byte string.""
" | 199 """Return the contents of the file at the given path as a byte string.""
" |
| 201 with file(path, 'rb') as fh: | 200 with file(path, 'rb') as f: |
| 202 return fh.read() | 201 return f.read() |
| 203 | 202 |
| 204 def write_binary_file(self, path, contents): | 203 def write_binary_file(self, path, contents): |
| 205 with file(path, 'wb') as fh: | 204 with file(path, 'wb') as f: |
| 206 fh.write(contents) | 205 f.write(contents) |
| 207 | 206 |
| 208 def open_text_file_for_reading(self, path): | 207 def open_text_file_for_reading(self, path): |
| 209 # Note: There appears to be an issue with the returned file objects | 208 # Note: There appears to be an issue with the returned file objects |
| 210 # not being seekable. See | 209 # not being seekable. See |
| 211 # http://stackoverflow.com/questions/1510188/can-seek-and-tell-work-with
-utf-8-encoded-documents-in-python | 210 # http://stackoverflow.com/questions/1510188/can-seek-and-tell-work-with
-utf-8-encoded-documents-in-python |
| 212 # . | 211 # . |
| 213 return codecs.open(path, 'r', 'utf8') | 212 return codecs.open(path, 'r', 'utf8') |
| 214 | 213 |
| 215 def open_text_file_for_writing(self, path): | 214 def open_text_file_for_writing(self, path): |
| 216 return codecs.open(path, 'w', 'utf8') | 215 return codecs.open(path, 'w', 'utf8') |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 def split(self, path): | 270 def split(self, path): |
| 272 """Return (dirname, basename + '.' + ext)""" | 271 """Return (dirname, basename + '.' + ext)""" |
| 273 return os.path.split(path) | 272 return os.path.split(path) |
| 274 | 273 |
| 275 def splitext(self, path): | 274 def splitext(self, path): |
| 276 """Return (dirname + os.sep + basename, '.' + ext)""" | 275 """Return (dirname + os.sep + basename, '.' + ext)""" |
| 277 return os.path.splitext(path) | 276 return os.path.splitext(path) |
| 278 | 277 |
| 279 def make_executable(self, file_path): | 278 def make_executable(self, file_path): |
| 280 os.chmod(file_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_
IRGRP | stat.S_IXGRP) | 279 os.chmod(file_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_
IRGRP | stat.S_IXGRP) |
| OLD | NEW |