File: remove.py

package info (click to toggle)
python-cloup 3.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 936 kB
  • sloc: python: 5,371; makefile: 120
file content (37 lines) | stat: -rw-r--r-- 1,094 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
import shutil
from glob import glob
import argparse
from itertools import chain

parser = argparse.ArgumentParser(
    description="""
    Remove full directories and files matching the provided glob patterns.

    If -r/--recursive, the pattern '**' will match any files and zero or more
    directories and subdirectories.
    """
)
parser.add_argument('paths', nargs='+')
parser.add_argument('-r', '--recursive', action='store_true',
                    help="Use recursive globs, i.e. the pattern '**' will match any "
                         "files and zero or more directories and subdirectories.")
parser.add_argument('-d', '--dry-run', action='store_true',
                    help='Do not remove files, just print a list of them')

args = parser.parse_args()

paths = set(chain.from_iterable(
    glob(arg, recursive=args.recursive)
    for arg in args.paths
))

if args.dry_run:
    print('\n'.join(paths))
else:
    for path in paths:
        if os.path.isdir(path):
            shutil.rmtree(path)
        else:
            os.remove(path)
        print('Removed', path)