From: Oleg Broytman Date: Sun, 9 Jul 2017 13:12:07 +0000 (+0300) Subject: Add option -r for rm.py X-Git-Tag: 0.5.0~1 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=129068b91b9f880672fe17c248c8085d8589fce7;p=ppu.git Add option -r for rm.py --- diff --git a/README.rst b/README.rst index ed42598..b99b8f3 100644 --- a/README.rst +++ b/README.rst @@ -11,7 +11,7 @@ cmp.py - compare two files. remove-old-files.py - remove old files. It's a portable replacement for `find start_dir -type f -mtime +31 -delete`. -rm.py - remove files. +rm.py - remove files and directories. which.py - find a program in PATH and print full path to it. diff --git a/TODO b/TODO index 0afd4eb..27ffec3 100644 --- a/TODO +++ b/TODO @@ -7,7 +7,4 @@ Rename remove-old-files.py to find.py. Make options more GNU find-like. Add option -s for cmp.py. -Add option -r for rm.py. - - Extend find.py: add options to find files by name, extension, path. diff --git a/docs/news.rst b/docs/news.rst index 8ed59f0..0685da8 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -1,9 +1,11 @@ News ==== -Version 0.4.1 (2017-06-??) +Version 0.5.0 (2017-07-??) -------------------------- +* Add option -r for rm.py. + * Use remove-old-files.py to cleanup pip cache. Version 0.4.0 (2017-06-04) diff --git a/scripts/rm.py b/scripts/rm.py index 4278fef..b7219f6 100755 --- a/scripts/rm.py +++ b/scripts/rm.py @@ -1,7 +1,22 @@ #! /usr/bin/env python +import argparse import os -import sys +import shutil -for filename in sys.argv[1:]: - os.unlink(filename) +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Remove old files') + parser.add_argument('-r', '--recursive', action='store_true', + help='remove directories recursively') + parser.add_argument('names', nargs='+', + help='files/directories names to remove') + args = parser.parse_args() + + for name in args.names: + if os.path.isdir(name): + if args.recursive: + shutil.rmtree(name) + else: + os.rmdir(name) + else: + os.unlink(name) diff --git a/tests/test_rm.py b/tests/test_rm.py index 631502c..4e4cd76 100755 --- a/tests/test_rm.py +++ b/tests/test_rm.py @@ -21,3 +21,22 @@ def test_rm(): [sys.executable, test_prog_path, "test2"]) == 0 assert_files_exist('test1') assert_files_not_exist('test2') + + create_files(['test1', 'test2']) + assert_files_exist(['test1', 'test2']) + assert subprocess.call( + [sys.executable, test_prog_path, "-r", "test2"]) == 0 + assert_files_exist('test1') + assert_files_not_exist('test2') + + +def test_rm_recursive(): + create_files(['test']) + create_files(['test'], 'subdir/subd2') + assert_files_exist(['test', 'subdir/subd2/test']) + assert subprocess.call( + [sys.executable, test_prog_path, "subdir"]) == 1 + assert subprocess.call( + [sys.executable, test_prog_path, "-r", "subdir"]) == 0 + assert_files_exist('test') + assert_files_not_exist(['subdir/subd2/test'])