Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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