Skip to content

Instantly share code, notes, and snippets.

@gkoberger
Created March 23, 2011 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkoberger/884153 to your computer and use it in GitHub Desktop.
Save gkoberger/884153 to your computer and use it in GitHub Desktop.
import re
import os
import shlex
import subprocess as sub
import stat
def walktree (top = ".", depthfirst = True):
names = os.listdir(top)
if not depthfirst:
yield top, names
for name in names:
try:
st = os.lstat(os.path.join(top, name))
except os.error:
continue
if stat.S_ISDIR(st.st_mode):
for (newtop, children) in walktree (os.path.join(top, name), depthfirst):
yield newtop, children
if depthfirst:
yield top, names
if __name__ == '__main__':
# ack for all the references to amo2009 images
p = sub.Popen(["ack", "amo2009", "media"], stdout=sub.PIPE,stderr=sub.PIPE)
output_media, errors = p.communicate()
p = sub.Popen(["ack", "amo2009", "apps"], stdout=sub.PIPE,stderr=sub.PIPE)
output_app, errors = p.communicate()
all_files = output_media + output_app
files_used = set(re.findall("amo2009/(.*?.(?:png|jpg|gif))", all_files))
files_delete = []
files_exist = []
print "FILES TO DELETE:"
for (basepath, children) in walktree("media/img/amo2009/",False):
for child in children:
filename = os.path.join('/'.join(basepath.split('/')[3:]), child)
if not filename.startswith(".git") and filename.rfind(".") > 0:
files_exist.append(filename)
if not filename in files_used:
files_delete.append(filename)
print " - %s" % filename
print "BUT WAIT! DON'T FORGET ABOUT:"
for filename in set(files_used) - set(files_exist):
print ' - %s' % filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment