Created
April 1, 2015 02:42
-
-
Save tritium21/dd616d939678d73296a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import logging | |
import os | |
import sys | |
def kill_it( | |
base_path=os.getcwd(), | |
thumbsdb='Thumbs.db', | |
unlink=True, | |
): | |
for dirpath, dirnames, filenames in os.walk(base_path): | |
if thumbsdb not in filenames: | |
continue | |
killable = os.path.join(dirpath, thumbsdb) | |
logging.info("rm: %s", killable) | |
if unlink: | |
os.unlink(killable) | |
def main(argv=None): | |
CWD = os.getcwd() | |
parser = argparse.ArgumentParser( | |
description="Kill evil files, recursively" | |
) | |
parser.add_argument( | |
'base_path', | |
nargs='?', | |
default=CWD, | |
metavar="PATH", | |
help=( | |
"Root directory in which to search for evil files." | |
" Default: '%s'" % (CWD,) | |
), | |
) | |
parser.add_argument( | |
'-f', | |
'--file', | |
dest='thumbsdb', | |
default='Thumbs.db', | |
metavar='FILE', | |
help="File name to search for. Default: 'Thumbs.db'" | |
) | |
me = parser.add_mutually_exclusive_group() | |
me.add_argument( | |
'-n', | |
'--noop', | |
action='store_false', | |
dest='unlink', | |
help="Do nothing, just search." | |
) | |
me.add_argument( | |
'-q', | |
'--quiet', | |
action='store_const', | |
dest='loglevel', | |
const=logging.ERROR, | |
default=logging.INFO, | |
help="Be quiet." | |
) | |
if argv is None: | |
args = parser.parse_args() | |
else: | |
args = parser.parse_args(argv) | |
logging.basicConfig( | |
format='%(message)s', | |
level=args.loglevel | |
) | |
del args.loglevel | |
kill_it(**vars(args)) | |
return 0 | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment