| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
The idea: |
| 5 |
compile a module's source text and walk recursively |
| 6 |
through the code objects. Find out which globals |
| 7 |
are used. |
| 8 |
Then examine each 'import *' by importing that module |
| 9 |
and looking for those globals. |
| 10 |
Replace the 'import *' by the list found. |
| 11 |
More advanced: If the new import has more than, say, 5 entries, |
| 12 |
rewrite the import to use module.name throughout the source. |
| 13 |
""" |
| 14 |
|
| 15 |
import dis, cStringIO, sys |
| 16 |
|
| 17 |
def disasm(code): |
| 18 |
hold = sys.stdout |
| 19 |
try: |
| 20 |
sys.stdout = cStringIO.StringIO() |
| 21 |
dis.dis(code) |
| 22 |
return sys.stdout.getvalue() |
| 23 |
finally: |
| 24 |
sys.stdout = hold |
| 25 |
|
| 26 |
def opsequence(code): |
| 27 |
for line in disasm(code).split('\n'): |
| 28 |
pieces = line.split('(', 1) |
| 29 |
if len(pieces) == 1: |
| 30 |
start, arg = pieces[0], None |
| 31 |
else: |
| 32 |
start, arg = pieces |
| 33 |
words = start.split() |
| 34 |
while words and (words[0].isdigit() or words[0] == '>>'): |
| 35 |
word = words.pop(0) |
| 36 |
if word.isdigit(): |
| 37 |
ofs = int(word) |
| 38 |
if not words: |
| 39 |
continue |
| 40 |
op = words[0] |
| 41 |
if arg: |
| 42 |
arg = arg[:-1] |
| 43 |
if op.startswith('JUMP'): |
| 44 |
arg = int(words[1]) |
| 45 |
yield ofs, op, arg |
| 46 |
|
| 47 |
def globalsof(code, globrefs=None, stars=None, globals=None): |
| 48 |
names = code.co_names |
| 49 |
vars = code.co_varnames |
| 50 |
if globrefs is None: globrefs = {} |
| 51 |
if stars is None: stars = [] |
| 52 |
if globals is None: globals = {} |
| 53 |
in_seq = False |
| 54 |
for ofs, op, arg in opsequence(code): |
| 55 |
if op == 'LOAD_GLOBAL': |
| 56 |
name = arg |
| 57 |
refs = globrefs.setdefault(name, {}) |
| 58 |
offsets = refs.setdefault(code, []) |
| 59 |
offsets.append(ofs) |
| 60 |
elif op == 'IMPORT_NAME': |
| 61 |
in_seq = True |
| 62 |
imp_module = arg |
| 63 |
imp_what = None |
| 64 |
elif op == 'IMPORT_FROM': |
| 65 |
in_seq = True |
| 66 |
imp_what = arg |
| 67 |
elif op == 'STORE_NAME': |
| 68 |
|
| 69 |
|
| 70 |
name = arg |
| 71 |
if in_seq: |
| 72 |
globals[name] = imp_what, imp_module |
| 73 |
in_seq = False |
| 74 |
else: |
| 75 |
globals[name] = None, None |
| 76 |
elif op == 'IMPORT_STAR': |
| 77 |
stars.append( (imp_module, ofs) ) |
| 78 |
in_seq = False |
| 79 |
else: |
| 80 |
in_seq = False |
| 81 |
return globrefs, stars, globals |
| 82 |
|
| 83 |
def offsetmap(c): |
| 84 |
|
| 85 |
|
| 86 |
tab = c.co_lnotab |
| 87 |
line = c.co_firstlineno - 1 |
| 88 |
addr = 0 |
| 89 |
res = { addr: line } |
| 90 |
for i in range(0, len(tab), 2): |
| 91 |
addr = addr + ord(tab[i]) |
| 92 |
line = line + ord(tab[i+1]) |
| 93 |
res[addr] = line |
| 94 |
return res |
| 95 |
|
| 96 |
|
| 97 |
class Analyser: |
| 98 |
def __init__(self, fname): |
| 99 |
self.fname = fname |
| 100 |
self.source = file(fname).read() |
| 101 |
self.starimports = [] |
| 102 |
self.codeobjects = {} |
| 103 |
self.globrefs, self.stars, self.globals = self.analyse() |
| 104 |
|
| 105 |
def analyse(self): |
| 106 |
globrefs = {} |
| 107 |
stars = [] |
| 108 |
globals = {} |
| 109 |
seen = {} |
| 110 |
code = compile(self.source, self.fname, 'exec') |
| 111 |
todo = [code] |
| 112 |
while todo: |
| 113 |
code = todo.pop(0) |
| 114 |
self.codeobjects[code] = offsetmap(code) |
| 115 |
globalsof(code, globrefs, stars, globals) |
| 116 |
seen[code] = True |
| 117 |
for const in code.co_consts: |
| 118 |
if type(const) is type(code) and const not in seen: |
| 119 |
todo.append(const) |
| 120 |
return globrefs, stars, globals |
| 121 |
|
| 122 |
def get_unknown_globals(self): |
| 123 |
from __builtin__ import __dict__ as bltin |
| 124 |
ret = [name for name in self.globrefs.keys() |
| 125 |
if name not in bltin and name not in self.globals] |
| 126 |
return ret |
| 127 |
|
| 128 |
def get_from_star(self, modname): |
| 129 |
dic = {} |
| 130 |
exec "from %s import *" % modname in dic |
| 131 |
return dic |
| 132 |
|
| 133 |
def resolve_star_imports(self): |
| 134 |
implicit = {} |
| 135 |
which = {} |
| 136 |
for star, ofs in self.stars: |
| 137 |
which[star] = [] |
| 138 |
for key in self.get_from_star(star).keys(): |
| 139 |
implicit[key] = star |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
for name in self.get_unknown_globals(): |
| 144 |
mod = implicit[name] |
| 145 |
which[mod].append(name) |
| 146 |
imps = [] |
| 147 |
for star, ofs in self.stars: |
| 148 |
imps.append( (ofs, star, which[star]) ) |
| 149 |
self.starimports = imps |
| 150 |
|
| 151 |
def find_statements(self): |
| 152 |
|
| 153 |
|
| 154 |
lineset = {} |
| 155 |
for co, ofs2line in self.codeobjects.items(): |
| 156 |
for ofs, line in ofs2line.items(): |
| 157 |
lineset[line] = True |
| 158 |
linenos = lineset.keys() |
| 159 |
if 0 not in linenos: |
| 160 |
linenos.append(0) |
| 161 |
linenos.sort() |
| 162 |
self.linenos = linenos |
| 163 |
|
| 164 |
srclines = self.source.split('\n') |
| 165 |
stmts = [] |
| 166 |
start = 0 |
| 167 |
for lno in linenos[1:] + [sys.maxint]: |
| 168 |
stmt = '\n'.join(srclines[start:lno]) |
| 169 |
stmts.append(stmt) |
| 170 |
start = lno |
| 171 |
self.statements = stmts |