]> git.phdru.name Git - ppu.git/commitdiff
Feat(rm): Add option -f
authorOleg Broytman <phd@phdru.name>
Mon, 23 Oct 2017 20:51:31 +0000 (23:51 +0300)
committerOleg Broytman <phd@phdru.name>
Mon, 23 Oct 2017 20:51:31 +0000 (23:51 +0300)
TODO
docs/news.rst
scripts/rm.py
tests/test_rm.py

diff --git a/TODO b/TODO
index 37859bff3730f26595f0596dae36eb0309e679a0..36bf37fc1c75f7ef466c16d2abb4014c185bf214 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,6 +1,3 @@
-Add option -f for rm.py.
-
-
 Add option -s for cmp.py.
 
 
index 2a2b1bdbcee1d2c7777c6a166facab78f1f11644..807f4ba83dbeda95fbba596b5103a85ad481dcc2 100644 (file)
@@ -6,6 +6,8 @@ Version 0.6.0 (2017-10-??)
 
 * rm.py ask interactively to remove read-only files or directories.
 
+* Add option -f for rm.py.
+
 Version 0.5.0 (2017-07-09)
 --------------------------
 
index 8523b4397ed3b67b9b19b5c833d7b3c761a7729c..17d4164dcb35f14e2da2f692d09732876be5f962 100755 (executable)
@@ -12,6 +12,8 @@ except NameError:  # Python 3
 
 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='+',
@@ -19,6 +21,8 @@ if __name__ == '__main__':
     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 os.access(name, os.W_OK):
             if is_dir:
@@ -41,7 +45,7 @@ if __name__ == '__main__':
                 continue
         if is_dir:
             if args.recursive:
-                shutil.rmtree(name)
+                shutil.rmtree(name, args.force)
             else:
                 os.rmdir(name)
         else:
index b9ad1b8b2a9ebeeba0cf124cb48c92c54d0e7d44..76937e516245f8d8633cab4321d341fc6e46c4a6 100755 (executable)
@@ -24,6 +24,11 @@ def test_rm():
     assert_files_exist('test1')
     assert_files_not_exist('test2')
 
+    assert subprocess.call(
+        [sys.executable, test_prog_path, "test3"]) == 1  # not exists
+    assert subprocess.call(
+        [sys.executable, test_prog_path, "-f", "test3"]) == 0
+
 
 def test_rm_recursive():
     create_files(['test'])
@@ -35,3 +40,8 @@ def test_rm_recursive():
         [sys.executable, test_prog_path, "-r", "subdir"]) == 0
     assert_files_exist('test')
     assert_files_not_exist(['subdir/subd2/test'])
+
+    assert subprocess.call(
+        [sys.executable, test_prog_path, "-r", "test3"]) == 1  # not exists
+    assert subprocess.call(
+        [sys.executable, test_prog_path, "-rf", "test3"]) == 0