OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Delete files in directories matching a pattern. |
| 7 """ |
| 8 |
| 9 import glob |
| 10 import optparse |
| 11 import os |
| 12 import shutil |
| 13 import sys |
| 14 |
| 15 def main(): |
| 16 parser = optparse.OptionParser() |
| 17 parser.add_option( |
| 18 '--pattern', |
| 19 help='Pattern for matching Files to delete.') |
| 20 parser.add_option( |
| 21 '--keep', |
| 22 help='Files to keep even if they matches the pattern.') |
| 23 |
| 24 options, args = parser.parse_args() |
| 25 |
| 26 if not options.pattern or not args: |
| 27 print 'No --pattern or target directories given' |
| 28 return |
| 29 |
| 30 for target_dir in args: |
| 31 target_pattern = os.path.join(target_dir, options.pattern) |
| 32 matching_files = glob.glob(target_pattern) |
| 33 |
| 34 keep_pattern = os.path.join(target_dir, options.keep) |
| 35 files_to_keep = glob.glob(keep_pattern) |
| 36 |
| 37 for target_file in matching_files: |
| 38 if target_file in files_to_keep: |
| 39 continue |
| 40 |
| 41 if os.path.isfile(target_file): |
| 42 os.remove(target_file) |
| 43 |
| 44 if __name__ == '__main__': |
| 45 sys.exit(main()) |
| 46 |
OLD | NEW |