| 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 """Delete files in directories matching a pattern. | 6 """Delete files in directories matching a pattern. |
| 7 """ | 7 """ |
| 8 | 8 |
| 9 import glob | 9 import glob |
| 10 import optparse | 10 import optparse |
| 11 import os | 11 import os |
| 12 import sys | 12 import sys |
| 13 | 13 |
| 14 from util import build_utils |
| 15 |
| 14 def main(): | 16 def main(): |
| 15 parser = optparse.OptionParser() | 17 parser = optparse.OptionParser() |
| 16 parser.add_option( | 18 parser.add_option( |
| 17 '--pattern', | 19 '--pattern', |
| 18 help='Pattern for matching Files to delete.') | 20 help='Pattern for matching Files to delete.') |
| 19 parser.add_option( | 21 parser.add_option( |
| 20 '--keep', | 22 '--keep', |
| 21 help='Files to keep even if they matches the pattern.') | 23 help='Files to keep even if they matches the pattern.') |
| 24 parser.add_option( |
| 25 '--stamp', |
| 26 help='Path to touch on success') |
| 22 | 27 |
| 23 options, args = parser.parse_args() | 28 options, args = parser.parse_args() |
| 24 | 29 |
| 25 if not options.pattern or not args: | 30 if not options.pattern or not args: |
| 26 print 'No --pattern or target directories given' | 31 print 'No --pattern or target directories given' |
| 27 return | 32 return |
| 28 | 33 |
| 29 for target_dir in args: | 34 for target_dir in args: |
| 30 target_pattern = os.path.join(target_dir, options.pattern) | 35 target_pattern = os.path.join(target_dir, options.pattern) |
| 31 matching_files = glob.glob(target_pattern) | 36 matching_files = glob.glob(target_pattern) |
| 32 | 37 |
| 33 keep_pattern = os.path.join(target_dir, options.keep) | 38 keep_pattern = os.path.join(target_dir, options.keep) |
| 34 files_to_keep = glob.glob(keep_pattern) | 39 files_to_keep = glob.glob(keep_pattern) |
| 35 | 40 |
| 36 for target_file in matching_files: | 41 for target_file in matching_files: |
| 37 if target_file in files_to_keep: | 42 if target_file in files_to_keep: |
| 38 continue | 43 continue |
| 39 | 44 |
| 40 if os.path.isfile(target_file): | 45 if os.path.isfile(target_file): |
| 41 os.remove(target_file) | 46 os.remove(target_file) |
| 42 | 47 |
| 48 if options.stamp: |
| 49 build_utils.Touch(options.stamp) |
| 50 |
| 43 if __name__ == '__main__': | 51 if __name__ == '__main__': |
| 44 sys.exit(main()) | 52 sys.exit(main()) |
| 45 | 53 |
| OLD | NEW |