| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import contextlib |
| 6 import fnmatch | 7 import fnmatch |
| 7 import os | 8 import os |
| 8 import subprocess | 9 import subprocess |
| 10 import shutil |
| 11 import tempfile |
| 9 | 12 |
| 10 mojo_root_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), | 13 mojo_root_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 11 os.pardir, os.pardir, os.pardir) | 14 os.pardir, os.pardir, os.pardir) |
| 12 | 15 |
| 13 def commit(message, cwd=None): | 16 def commit(message, cwd=None): |
| 14 subprocess.call(['git', 'commit', '-a', '-m', message], cwd=cwd) | 17 subprocess.call(['git', 'commit', '-a', '-m', message], cwd=cwd) |
| 15 | 18 |
| 16 def system(command, cwd=None): | 19 def system(command, cwd=None): |
| 17 return subprocess.check_output(command, cwd=cwd) | 20 return subprocess.check_output(command, cwd=cwd) |
| 18 | 21 |
| 19 def find(patterns, start='.'): | 22 def find(patterns, start='.'): |
| 20 for path, dirs, files in os.walk(start): | 23 for path, dirs, files in os.walk(start): |
| 21 for basename in files + dirs: | 24 for basename in files + dirs: |
| 22 if any([fnmatch.fnmatch(basename, pattern) for pattern in patterns]): | 25 if any([fnmatch.fnmatch(basename, pattern) for pattern in patterns]): |
| 23 filename = os.path.join(path, basename) | 26 filename = os.path.join(path, basename) |
| 24 yield filename | 27 yield filename |
| 25 | 28 |
| 29 # From |
| 30 # http://stackoverflow.com/questions/13379742/right-way-to-clean-up-a-temporary-
folder-in-python-class |
| 31 @contextlib.contextmanager |
| 32 def temp_dir(*args, **kwargs): |
| 33 d = tempfile.mkdtemp(*args, **kwargs) |
| 34 try: |
| 35 yield d |
| 36 finally: |
| 37 shutil.rmtree(d) |
| 38 |
| 26 def filter_file(path, predicate): | 39 def filter_file(path, predicate): |
| 27 with open(path, 'r+') as f: | 40 with open(path, 'r+') as f: |
| 28 lines = f.readlines() | 41 lines = f.readlines() |
| 29 new_lines = [line for line in lines if predicate(line)] | 42 new_lines = [line for line in lines if predicate(line)] |
| 30 f.seek(0) | 43 f.seek(0) |
| 31 f.truncate() | 44 f.truncate() |
| 32 f.write(''.join(new_lines)) | 45 f.write(''.join(new_lines)) |
| OLD | NEW |