Skip to content

Instantly share code, notes, and snippets.

@llimllib
Created February 25, 2009 17:44
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 llimllib/70312 to your computer and use it in GitHub Desktop.
Save llimllib/70312 to your computer and use it in GitHub Desktop.
run with "python as3_unused_imports.py <directory to search recursively>"
#!/usr/bin/python
import sys, re
from os import walk
from os.path import join as pathjoin
from pprint import pprint as pp
def parsef(f):
imports = []
notimports = []
for line in file(f):
r = re.search("^\s*import\s*(.*)\s*;\s*$", line)
if r: imports.append(r.groups()[0])
else: notimports.append(line)
return imports, notimports
#be super conservative and don't list any imports whose name is mentioned anywhere in the file
def find_unused(notimports, imports):
return [i for i in imports if "".join(notimports).find(i) == -1]
root = sys.argv[-1]
for dirpath, dirnames, filenames in walk(root):
for f in (f_ for f_ in filenames if f_.endswith(".as")):
f = pathjoin(dirpath, f)
imports, notimports = parsef(f)
classnames = [x[x.rfind(".")+1:] for x in imports]
unused = find_unused(notimports, classnames)
if unused:
print f + " :"
pp(unused)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment