From 4adfe52d186866729845c4457ec4d3b8e5fbd58f Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Mon, 23 Oct 2017 23:51:31 +0300 Subject: [PATCH] Feat(rm): Add option -f --- TODO | 3 --- docs/news.rst | 2 ++ scripts/rm.py | 6 +++++- tests/test_rm.py | 10 ++++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/TODO b/TODO index 37859bf..36bf37f 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,3 @@ -Add option -f for rm.py. - - Add option -s for cmp.py. diff --git a/docs/news.rst b/docs/news.rst index 2a2b1bd..807f4ba 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -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) -------------------------- diff --git a/scripts/rm.py b/scripts/rm.py index 8523b43..17d4164 100755 --- a/scripts/rm.py +++ b/scripts/rm.py @@ -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: diff --git a/tests/test_rm.py b/tests/test_rm.py index b9ad1b8..76937e5 100755 --- a/tests/test_rm.py +++ b/tests/test_rm.py @@ -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 -- 2.39.5