From 1bdc0896ca8b6aa0e784f98785a5091be04d5316 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 12 Oct 2009 11:24:41 +0200 Subject: [PATCH] introduce "-d" to py.cleanup --HG-- branch : trunk --- _py/cmdline/pycleanup.py | 20 +++++++++++++++----- doc/changelog.txt | 2 ++ testing/cmdline/test_cmdline.py | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/_py/cmdline/pycleanup.py b/_py/cmdline/pycleanup.py index 701fc25fd..8a933484c 100755 --- a/_py/cmdline/pycleanup.py +++ b/_py/cmdline/pycleanup.py @@ -18,6 +18,8 @@ def main(): action="store_true", help="display would-be-removed filenames" ) + parser.add_option("-d", action="store_true", dest="removedir", + help="remove empty directories") (options, args) = parser.parse_args() if not args: args = ["."] @@ -29,8 +31,16 @@ def main(): path = py.path.local(arg) py.builtin.print_("cleaning path", path, "of extensions", ext) for x in path.visit(shouldremove, lambda x: x.check(dotfile=0, link=0)): - if options.dryrun: - py.builtin.print_("would remove", x) - else: - py.builtin.print_("removing", x) - x.remove() + remove(x, options) + if options.removedir: + for x in path.visit(lambda x: x.check(dir=1), + lambda x: x.check(dotfile=0, link=0)): + remove(x, options) + +def remove(path, options): + if options.dryrun: + py.builtin.print_("would remove", path) + else: + py.builtin.print_("removing", path) + path.remove() + diff --git a/doc/changelog.txt b/doc/changelog.txt index bb8d53097..4c4f0d116 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -1,6 +1,8 @@ Changes between 1.0.2 and '1.1.0b1' ===================================== +* introduce and test "py.cleanup -d" to remove empty directories + * fix issue #59 - robustify unittest test collection * make bpython/help interaction work by adding an __all__ attribute diff --git a/testing/cmdline/test_cmdline.py b/testing/cmdline/test_cmdline.py index 6616a9d9f..73d1fcfa4 100644 --- a/testing/cmdline/test_cmdline.py +++ b/testing/cmdline/test_cmdline.py @@ -26,3 +26,24 @@ class TestPyLookup: result.stdout.fnmatch_lines( ["%s:1: stuff = x" % (searched.basename,)] ) + +class TestPyCleanup: + def test_basic(self, testdir, tmpdir): + p = tmpdir.ensure("hello.py") + result = testdir.runpybin("py.cleanup", tmpdir) + assert result.ret == 0 + assert p.check() + pyc = p.new(ext='pyc') + pyc.ensure() + result = testdir.runpybin("py.cleanup", tmpdir) + assert not pyc.check() + + def test_dir_remove(self, testdir, tmpdir): + p = tmpdir.mkdir("a") + result = testdir.runpybin("py.cleanup", tmpdir) + assert result.ret == 0 + assert p.check() + result = testdir.runpybin("py.cleanup", tmpdir, '-d') + assert result.ret == 0 + assert not p.check() +