X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=scripts%2Frm.py;h=fca4dca6dc4c2ac84689f31dea4e3ad228fdcab8;hb=dbc98b293abfbea2105efde4c066223e73ae63a6;hp=4278fef5beba8752cabcccb60f7649c762db7bed;hpb=f8dcc3f8ab3d1487c0e9042e26e437db77c4e311;p=ppu.git diff --git a/scripts/rm.py b/scripts/rm.py index 4278fef..fca4dca 100755 --- a/scripts/rm.py +++ b/scripts/rm.py @@ -1,7 +1,52 @@ #! /usr/bin/env python +import argparse import os -import sys +import shutil -for filename in sys.argv[1:]: - os.unlink(filename) +try: + raw_input +except NameError: # Python 3 + raw_input = input + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Remove files/directories') + parser.add_argument('-f', '--force', action='store_true', + help='force (ignore non-existing files and errors)') + parser.add_argument('-r', '--recursive', action='store_true', + help='remove directories recursively') + parser.add_argument('names', nargs='+', + help='files/directories names to remove') + args = parser.parse_args() + + for name in args.names: + if args.force and not os.path.exists(name): + continue + is_dir = os.path.isdir(name) + if not args.force and not os.access(name, os.W_OK): + if is_dir: + ftype = 'directory' + else: + ftype = 'file' + try: + while True: + rmw = raw_input( + "rm.py: remove write-protected %s '%s'? [y/n] " + % (ftype, name)) + answer = rmw[:1].lower() + if answer == 'y': + break + elif answer == 'n': + raise StopIteration + else: + continue + except StopIteration: + continue + if is_dir: + if args.recursive: + shutil.rmtree(name, args.force) + else: + os.rmdir(name) + else: + os.unlink(name)