2008-08-21 18:18:58 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""\
|
|
|
|
py.cleanup [PATH]
|
|
|
|
|
|
|
|
Delete pyc file recursively, starting from PATH (which defaults to the current
|
|
|
|
working directory). Don't follow links and don't recurse into directories with
|
|
|
|
a ".".
|
|
|
|
"""
|
|
|
|
import py
|
|
|
|
|
|
|
|
def main():
|
2009-08-28 03:12:55 +08:00
|
|
|
parser = py.std.optparse.OptionParser(usage=__doc__)
|
2009-04-29 05:00:57 +08:00
|
|
|
parser.add_option("-e", "--remove", dest="ext", default=".pyc", action="store",
|
|
|
|
help="remove files with the given comma-separated list of extensions"
|
|
|
|
)
|
|
|
|
parser.add_option("-n", "--dryrun", dest="dryrun", default=False,
|
|
|
|
action="store_true",
|
|
|
|
help="display would-be-removed filenames"
|
|
|
|
)
|
2008-08-21 18:18:58 +08:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
if not args:
|
|
|
|
args = ["."]
|
2009-04-29 05:00:57 +08:00
|
|
|
ext = options.ext.split(",")
|
|
|
|
def shouldremove(p):
|
|
|
|
return p.ext in ext
|
|
|
|
|
2008-08-21 18:18:58 +08:00
|
|
|
for arg in args:
|
|
|
|
path = py.path.local(arg)
|
2009-08-30 02:04:48 +08:00
|
|
|
py.builtin.print_("cleaning path", path, "of extensions", ext)
|
2009-04-29 05:00:57 +08:00
|
|
|
for x in path.visit(shouldremove, lambda x: x.check(dotfile=0, link=0)):
|
|
|
|
if options.dryrun:
|
2009-08-30 02:04:48 +08:00
|
|
|
py.builtin.print_("would remove", x)
|
2009-04-29 05:00:57 +08:00
|
|
|
else:
|
2009-08-30 02:04:48 +08:00
|
|
|
py.builtin.print_("removing", x)
|
2009-04-29 05:00:57 +08:00
|
|
|
x.remove()
|