Debian Codegraph @

scripts/remove.py

raw
1 import os
2 import shutil
3 from glob import glob
4 import argparse
5 from itertools import chain
6
7 parser = argparse.ArgumentParser(
8 description="""
9 Remove full directories and files matching the provided glob patterns.
10
11 If -r/--recursive, the pattern '**' will match any files and zero or more
12 directories and subdirectories.
13 """
14 )
15 parser.add_argument('paths', nargs='+')
16 parser.add_argument('-r', '--recursive', action='store_true',
17 help="Use recursive globs, i.e. the pattern '**' will match any "
18 "files and zero or more directories and subdirectories.")
19 parser.add_argument('-d', '--dry-run', action='store_true',
20 help='Do not remove files, just print a list of them')
21
22 args = parser.parse_args()
23
24 paths = set(chain.from_iterable(
25 glob(arg, recursive=args.recursive)
26 for arg in args.paths
27 ))
28
29 if args.dry_run:
30 print('\n'.join(paths))
31 else:
32 for path in paths:
33 if os.path.isdir(path):
34 shutil.rmtree(path)
35 else:
36 os.remove(path)
37 print('Removed', path)