]> git.phdru.name Git - ppu.git/blobdiff - scripts/rm.py
Feat(rm.py): Ask interactively to remove read-only files or directories
[ppu.git] / scripts / rm.py
index 4f24b7200ab39a4ef92d57ca4a881633736097c5..8523b4397ed3b67b9b19b5c833d7b3c761a7729c 100755 (executable)
@@ -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: