From: Oleg Broytman Date: Wed, 18 Oct 2017 02:54:48 +0000 (+0300) Subject: Feat(rm.py): Ask interactively to remove read-only files or directories X-Git-Tag: 0.6.0~15 X-Git-Url: https://git.phdru.name/?p=ppu.git;a=commitdiff_plain;h=a29e92233cdbed6237e01046c56a1d3d27ab195d Feat(rm.py): Ask interactively to remove read-only files or directories --- diff --git a/TODO b/TODO index e2173e8..37859bf 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,3 @@ -Test if the file is read-only in rm.py; -ask the user if they wants to delete it. - - Add option -f for rm.py. diff --git a/docs/index.rst b/docs/index.rst index 93730f5..476a8a4 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -48,7 +48,8 @@ Options:: rm.py ~~~~~ -Remove files or directories. +Remove files or directories. Ask interactively to remove read-only files or +directories. Usage:: diff --git a/docs/news.rst b/docs/news.rst index 2503340..2a2b1bd 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -1,6 +1,11 @@ News ==== +Version 0.6.0 (2017-10-??) +-------------------------- + +* rm.py ask interactively to remove read-only files or directories. + Version 0.5.0 (2017-07-09) -------------------------- diff --git a/scripts/rm.py b/scripts/rm.py index 4f24b72..8523b43 100755 --- a/scripts/rm.py +++ b/scripts/rm.py @@ -4,6 +4,12 @@ import argparse import os import shutil +try: + raw_input +except NameError: # Python 3 + raw_input = input + + if __name__ == '__main__': parser = argparse.ArgumentParser(description='Remove files/directories') parser.add_argument('-r', '--recursive', action='store_true', @@ -13,7 +19,27 @@ if __name__ == '__main__': args = parser.parse_args() for name in args.names: - if os.path.isdir(name): + is_dir = os.path.isdir(name) + if 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) else: