Skip to content

Instantly share code, notes, and snippets.

@jwoschitz
Created February 17, 2012 20:29
Show Gist options
  • Save jwoschitz/1855273 to your computer and use it in GitHub Desktop.
Save jwoschitz/1855273 to your computer and use it in GitHub Desktop.
directory cleanup script
'''
/* @Cleanup\Delete("SomeRandomString-123") */
// @Cleanup\Delete("asd")
'''
import os, re, argparse
class Filter():
def matches(self):
raise NotImplementedError()
class EndsWithFilter(Filter):
def __init__(self, pattern_to_test):
self.pattern = pattern_to_test
def matches(self, value):
return value.endswith(self.pattern)
class Cleaner():
def __init__(self, target_dir, tokens_to_delete, is_dry_run = False):
self.target_dir = target_dir
self.tokens_to_delete = tokens_to_delete
self.files_to_delete = []
self.is_dry_run = is_dry_run
def doCleanup(self, exclude_dir_filters, include_file_filters):
for current_dir, sub_dirs, files in os.walk(self.target_dir):
for sub_dir in sub_dirs:
for filter in exclude_dir_filters:
if(filter.matches(sub_dir)):
sub_dirs.remove(sub_dir)
break
for file in files:
ignore_file = True
for filter in include_file_filters:
if(filter.matches(file)):
ignore_file = False
break
if(ignore_file):
continue
filePath = os.path.join(current_dir, file)
self.processFile(filePath)
for filePath in self.files_to_delete:
print filePath
if not self.is_dry_run:
os.remove(filePath)
def processFile(self, filePath, maxLinesToRead = 10):
file = open(filePath)
counter = 0
line = file.readline()
text = ""
while line and counter <= maxLinesToRead:
counter += 1
text += line
line = file.readline()
matches = []
matches.extend(re.findall('/\*[^\@]+\@Cleanup.Delete\("([^\)]+)"\)', text))
matches.extend(re.findall('//[^\@]+\@Cleanup.Delete\("([^\)]+)"\)', text))
if matches:
for match in matches:
self.processMatch(match, filePath)
def processMatch(self, match, filePath):
if match in self.tokens_to_delete:
self.files_to_delete.append(filePath)
def getTokensFromFile(filePath):
tokens = []
file = open(filePath)
line = file.readline()
while line:
tokens.append(line.rstrip('\n'))
line = file.readline()
return tokens
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--path',help="the path to the directory which should be cleaned")
parser.add_argument('-t', '--tokenfile',help="the file which contains the tokens which should be deleted")
parser.add_argument('-D', '--dryrun', action='store_true', help="if this option is set, the script will only list the files instead of deleting them")
args = parser.parse_args()
if not (args.path):
parser.error('No path is set. Aborting.')
if not (args.tokenfile):
parser.error('No token file defined. Aborting.')
target_dir = args.path
tokens_to_delete = getTokensFromFile(args.tokenfile)
is_dry_run = args.dryrun
cleaner = Cleaner(target_dir, tokens_to_delete, is_dry_run)
exclude_dir_filters = [EndsWithFilter('.svn')]
include_file_filters = [EndsWithFilter('.js'), EndsWithFilter('.php'), EndsWithFilter('.tpl')]
cleaner.doCleanup(exclude_dir_filters, include_file_filters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment