X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=scripts%2Frm.py;h=fca4dca6dc4c2ac84689f31dea4e3ad228fdcab8;hb=dbc98b293abfbea2105efde4c066223e73ae63a6;hp=b7219f6d20268d3fe8f74ba8077f3fc01a12ebfe;hpb=129068b91b9f880672fe17c248c8085d8589fce7;p=ppu.git diff --git a/scripts/rm.py b/scripts/rm.py index b7219f6..fca4dca 100755 --- a/scripts/rm.py +++ b/scripts/rm.py @@ -4,8 +4,16 @@ import argparse import os import shutil +try: + raw_input +except NameError: # Python 3 + raw_input = input + + if __name__ == '__main__': - parser = argparse.ArgumentParser(description='Remove old files') + 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='+', @@ -13,9 +21,31 @@ if __name__ == '__main__': args = parser.parse_args() for name in args.names: - if os.path.isdir(name): + 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) + shutil.rmtree(name, args.force) else: os.rmdir(name) else: