<s> """<STR_LIT>""" <EOL> from __future__ import generators <EOL> import dis <EOL> import imp <EOL> import marshal <EOL> import os <EOL> import sys <EOL> import types <EOL> import struct <EOL> if hasattr ( sys . __stdout__ , "<STR_LIT>" ) : <EOL> READ_MODE = "<STR_LIT>" <EOL> else : <EOL> READ_MODE = "<STR_LIT:r>" <EOL> LOAD_CONST = chr ( dis . opname . index ( '<STR_LIT>' ) ) <EOL> IMPORT_NAME = chr ( dis . opname . index ( '<STR_LIT>' ) ) <EOL> STORE_NAME = chr ( dis . opname . index ( '<STR_LIT>' ) ) <EOL> STORE_GLOBAL = chr ( dis . opname . index ( '<STR_LIT>' ) ) <EOL> STORE_OPS = [ STORE_NAME , STORE_GLOBAL ] <EOL> HAVE_ARGUMENT = chr ( dis . HAVE_ARGUMENT ) <EOL> from modulefinder import packagePathMap <EOL> def AddPackagePath ( packagename , path ) : <EOL> paths = packagePathMap . get ( packagename , [ ] ) <EOL> paths . append ( path ) <EOL> packagePathMap [ packagename ] = paths <EOL> from modulefinder import replacePackageMap <EOL> def ReplacePackage ( oldname , newname ) : <EOL> replacePackageMap [ oldname ] = newname <EOL> class Module : <EOL> def __init__ ( self , name , file = None , path = None ) : <EOL> self . __name__ = name <EOL> self . __file__ = file <EOL> self . __path__ = path <EOL> self . __code__ = None <EOL> self . globalnames = { } <EOL> self . starimports = { } <EOL> def __repr__ ( self ) : <EOL> s = "<STR_LIT>" % ( self . __name__ , ) <EOL> if self . __file__ is not None : <EOL> s = s + "<STR_LIT>" % ( self . __file__ , ) <EOL> if self . __path__ is not None : <EOL> s = s + "<STR_LIT>" % ( self . __path__ , ) <EOL> s = s + "<STR_LIT:)>" <EOL> return s <EOL> class ModuleFinder : <EOL> def __init__ ( self , path = None , debug = <NUM_LIT:0> , excludes = [ ] , replace_paths = [ ] , skip_scan = [ ] ) : <EOL> if path is None : <EOL> path = sys . path <EOL> self . path = path <EOL> self . modules = { } <EOL> self . badmodules = { } <EOL> self . debug = debug <EOL> self . indent = <NUM_LIT:0> <EOL> self . excludes = excludes <EOL> self . replace_paths = replace_paths <EOL> self . skip_scan = skip_scan <EOL> self . processed_paths = [ ] <EOL> def msg ( self , level , str , * args ) : <EOL> if level <= self . debug : <EOL> for i in range ( self . indent ) : <EOL> print "<STR_LIT:U+0020>" , <EOL> print str , <EOL> for arg in args : <EOL> print repr ( arg ) , <EOL> print <EOL> def msgin ( self , * args ) : <EOL> level = args [ <NUM_LIT:0> ] <EOL> if level <= self . debug : <EOL> self . indent = self . indent + <NUM_LIT:1> <EOL> self . msg ( * args ) <EOL> def msgout ( self , * args ) : <EOL> level = args [ <NUM_LIT:0> ] <EOL> if level <= self . debug : <EOL> self . indent = self . indent - <NUM_LIT:1> <EOL> self . msg ( * args ) <EOL> def run_script ( self , pathname ) : <EOL> self . msg ( <NUM_LIT:2> , "<STR_LIT>" , pathname ) <EOL> fp = open ( pathname , READ_MODE ) <EOL> stuff = ( "<STR_LIT>" , "<STR_LIT:r>" , imp . PY_SOURCE ) <EOL> self . load_module ( '<STR_LIT:__main__>' , fp , pathname , stuff ) <EOL> def load_file ( self , pathname ) : <EOL> dir , name = os . path . split ( pathname ) <EOL> name , ext = os . path . splitext ( name ) <EOL> fp = open ( pathname , READ_MODE ) <EOL> stuff = ( ext , "<STR_LIT:r>" , imp . PY_SOURCE ) <EOL> self . load_module ( name , fp , pathname , stuff ) <EOL> def import_hook ( self , name , caller = None , fromlist = None , level = - <NUM_LIT:1> ) : <EOL> self . msg ( <NUM_LIT:3> , "<STR_LIT>" , name , caller , fromlist , level ) <EOL> parent = self . determine_parent ( caller , level = level ) <EOL> q , tail = self . find_head_package ( parent , name ) <EOL> m = self . load_tail ( q , tail ) <EOL> if not fromlist : <EOL> return q <EOL> if m . __path__ : <EOL> self . ensure_fromlist ( m , fromlist ) <EOL> return None <EOL> def determine_parent ( self , caller , level = - <NUM_LIT:1> ) : <EOL> self . msgin ( <NUM_LIT:4> , "<STR_LIT>" , caller , level ) <EOL> if not caller or level == <NUM_LIT:0> : <EOL> self . msgout ( <NUM_LIT:4> , "<STR_LIT>" ) <EOL> return None <EOL> pname = caller . __name__ <EOL> if level >= <NUM_LIT:1> : <EOL> if caller . __path__ : <EOL> level -= <NUM_LIT:1> <EOL> if level == <NUM_LIT:0> : <EOL> parent = self . modules [ pname ] <EOL> assert parent is caller <EOL> self . msgout ( <NUM_LIT:4> , "<STR_LIT>" , parent ) <EOL> return parent <EOL> if pname . count ( "<STR_LIT:.>" ) < level : <EOL> raise ImportError , "<STR_LIT>" <EOL> pname = "<STR_LIT:.>" . join ( pname . split ( "<STR_LIT:.>" ) [ : - level ] ) <EOL> parent = self . modules [ pname ] <EOL> self . msgout ( <NUM_LIT:4> , "<STR_LIT>" , parent ) <EOL> return parent <EOL> if caller . __path__ : <EOL> parent = self . modules [ pname ] <EOL> assert caller is parent <EOL> self . msgout ( <NUM_LIT:4> , "<STR_LIT>" , parent ) <EOL> return parent <EOL> if '<STR_LIT:.>' in pname : <EOL> i = pname . rfind ( '<STR_LIT:.>' ) <EOL> pname = pname [ : i ] <EOL> parent = self . modules [ pname ] <EOL> assert parent . __name__ == pname <EOL> self . msgout ( <NUM_LIT:4> , "<STR_LIT>" , parent ) <EOL> return parent <EOL> self . msgout ( <NUM_LIT:4> , "<STR_LIT>" ) <EOL> return None <EOL> def find_head_package ( self , parent , name ) : <EOL> self . msgin ( <NUM_LIT:4> , "<STR_LIT>" , parent , name ) <EOL> if '<STR_LIT:.>' in name : <EOL> i = name . find ( '<STR_LIT:.>' ) <EOL> head = name [ : i ] <EOL> tail = name [ i + <NUM_LIT:1> : ] <EOL> else : <EOL> head = name <EOL> tail = "<STR_LIT>" <EOL> if parent : <EOL> qname = "<STR_LIT>" % ( parent . __name__ , head ) <EOL> else : <EOL> qname = head <EOL> q = self . import_module ( head , qname , parent ) <EOL> if q : <EOL> self . msgout ( <NUM_LIT:4> , "<STR_LIT>" , ( q , tail ) ) <EOL> return q , tail <EOL> if parent : <EOL> qname = head <EOL> parent = None <EOL> q = self . import_module ( head , qname , parent ) <EOL> if q : <EOL> self . msgout ( <NUM_LIT:4> , "<STR_LIT>" , ( q , tail ) ) <EOL> return q , tail <EOL> self . msgout ( <NUM_LIT:4> , "<STR_LIT>" , qname ) <EOL> raise ImportError , "<STR_LIT>" + qname <EOL> def load_tail ( self , q , tail ) : <EOL> self . msgin ( <NUM_LIT:4> , "<STR_LIT>" , q , tail ) <EOL> m = q <EOL> while tail : <EOL> i = tail . find ( '<STR_LIT:.>' ) <EOL> if i < <NUM_LIT:0> : i = len ( tail ) <EOL> head , tail = tail [ : i ] , tail [ i + <NUM_LIT:1> : ] <EOL> mname = "<STR_LIT>" % ( m . __name__ , head ) <EOL> m = self . import_module ( head , mname , m ) <EOL> if not m : <EOL> self . msgout ( <NUM_LIT:4> , "<STR_LIT>" , mname ) <EOL> raise ImportError , "<STR_LIT>" + mname <EOL> self . msgout ( <NUM_LIT:4> , "<STR_LIT>" , m ) <EOL> return m <EOL> def ensure_fromlist ( self , m , fromlist , recursive = <NUM_LIT:0> ) : <EOL> self . msg ( <NUM_LIT:4> , "<STR_LIT>" , m , fromlist , recursive ) <EOL> for sub in fromlist : <EOL> if sub == "<STR_LIT:*>" : <EOL> if not recursive : <EOL> all = self . find_all_submodules ( m ) <EOL> if all : <EOL> self . ensure_fromlist ( m , all , <NUM_LIT:1> ) <EOL> elif not hasattr ( m , sub ) : <EOL> subname = "<STR_LIT>" % ( m . __name__ , sub ) <EOL> submod = self . import_module ( sub , subname , m ) <EOL> if not submod : <EOL> raise ImportError , "<STR_LIT>" + subname <EOL> def find_all_submodules ( self , m ) : <EOL> if not m . __path__ : <EOL> return <EOL> modules = { } <EOL> suffixes = [ ] <EOL> for triple in imp . get_suffixes ( ) : <EOL> suffixes . append ( triple [ <NUM_LIT:0> ] ) <EOL> for dir in m . __path__ : <EOL> try : <EOL> names = os . listdir ( dir ) <EOL> except os . error : <EOL> self . msg ( <NUM_LIT:2> , "<STR_LIT>" , dir ) <EOL> continue <EOL> for name in names : <EOL> mod = None <EOL> for suff in suffixes : <EOL> n = len ( suff ) <EOL> if name [ - n : ] == suff : <EOL> mod = name [ : - n ] <EOL> break <EOL> if mod and mod != "<STR_LIT>" : <EOL> modules [ mod ] = mod <EOL> return modules . keys ( ) <EOL> def import_module ( self , partname , fqname , parent ) : <EOL> self . msgin ( <NUM_LIT:3> , "<STR_LIT>" , partname , fqname , parent ) <EOL> try : <EOL> m = self . modules [ fqname ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> self . msgout ( <NUM_LIT:3> , "<STR_LIT>" , m ) <EOL> return m <EOL> if self . badmodules . has_key ( fqname ) : <EOL> self . msgout ( <NUM_LIT:3> , "<STR_LIT>" ) <EOL> return None <EOL> if parent and parent . __path__ is None : <EOL> self . msgout ( <NUM_LIT:3> , "<STR_LIT>" ) <EOL> return None <EOL> try : <EOL> fp , pathname , stuff = self . find_module ( partname , <EOL> parent and parent . __path__ , parent ) <EOL> except ImportError : <EOL> self . msgout ( <NUM_LIT:3> , "<STR_LIT>" , None ) <EOL> return None <EOL> try : <EOL> m = self . load_module ( fqname , fp , pathname , stuff ) <EOL> finally : <EOL> if fp : fp . close ( ) <EOL> if parent : <EOL> setattr ( parent , partname , m ) <EOL> self . msgout ( <NUM_LIT:3> , "<STR_LIT>" , m ) <EOL> return m <EOL> def load_module ( self , fqname , fp , pathname , ( suffix , mode , type ) ) : <EOL> self . msgin ( <NUM_LIT:2> , "<STR_LIT>" , fqname , fp and "<STR_LIT>" , pathname ) <EOL> if type == imp . PKG_DIRECTORY : <EOL> m = self . load_package ( fqname , pathname ) <EOL> self . msgout ( <NUM_LIT:2> , "<STR_LIT>" , m ) <EOL> return m <EOL> if type == imp . PY_SOURCE : <EOL> co = compile ( fp . read ( ) + '<STR_LIT:\n>' , pathname , '<STR_LIT>' ) <EOL> elif type == imp . PY_COMPILED : <EOL> if fp . read ( <NUM_LIT:4> ) != imp . get_magic ( ) : <EOL> self . msgout ( <NUM_LIT:2> , "<STR_LIT>" , pathname ) <EOL> raise ImportError , "<STR_LIT>" % pathname <EOL> fp . read ( <NUM_LIT:4> ) <EOL> co = marshal . load ( fp ) <EOL> else : <EOL> co = None <EOL> m = self . add_module ( fqname ) <EOL> m . __file__ = pathname <EOL> if co : <EOL> if self . replace_paths : <EOL> co = self . replace_paths_in_code ( co ) <EOL> m . __code__ = co <EOL> self . scan_code ( co , m ) <EOL> self . msgout ( <NUM_LIT:2> , "<STR_LIT>" , m ) <EOL> return m <EOL> def _add_badmodule ( self , name , caller ) : <EOL> if name not in self . badmodules : <EOL> self . badmodules [ name ] = { } <EOL> if caller : <EOL> self . badmodules [ name ] [ caller . __name__ ] = <NUM_LIT:1> <EOL> else : <EOL> self . badmodules [ name ] [ "<STR_LIT:->" ] = <NUM_LIT:1> <EOL> def _safe_import_hook ( self , name , caller , fromlist , level = - <NUM_LIT:1> ) : <EOL> if name in self . badmodules : <EOL> self . _add_badmodule ( name , caller ) <EOL> return <EOL> try : <EOL> self . import_hook ( name , caller , level = level ) <EOL> except ImportError , msg : <EOL> self . msg ( <NUM_LIT:2> , "<STR_LIT>" , str ( msg ) ) <EOL> self . _add_badmodule ( name , caller ) <EOL> else : <EOL> if fromlist : <EOL> for sub in fromlist : <EOL> if sub in self . badmodules : <EOL> self . _add_badmodule ( sub , caller ) <EOL> continue <EOL> try : <EOL> self . import_hook ( name , caller , [ sub ] , level = level ) <EOL> except ImportError , msg : <EOL> self . msg ( <NUM_LIT:2> , "<STR_LIT>" , str ( msg ) ) <EOL> fullname = name + "<STR_LIT:.>" + sub <EOL> self . _add_badmodule ( fullname , caller ) <EOL> def scan_opcodes ( self , co , <EOL> unpack = struct . unpack ) : <EOL> code = co . co_code <EOL> names = co . co_names <EOL> consts = co . co_consts <EOL> while code : <EOL> c = code [ <NUM_LIT:0> ] <EOL> if c in STORE_OPS : <EOL> oparg , = unpack ( '<STR_LIT>' , code [ <NUM_LIT:1> : <NUM_LIT:3> ] ) <EOL> yield "<STR_LIT:store>" , ( names [ oparg ] , ) <EOL> code = code [ <NUM_LIT:3> : ] <EOL> continue <EOL> if c == LOAD_CONST and code [ <NUM_LIT:3> ] == IMPORT_NAME : <EOL> oparg_1 , oparg_2 = unpack ( '<STR_LIT>' , code [ : <NUM_LIT:6> ] ) <EOL> yield "<STR_LIT>" , ( consts [ oparg_1 ] , names [ oparg_2 ] ) <EOL> code = code [ <NUM_LIT:6> : ] <EOL> continue <EOL> if c >= HAVE_ARGUMENT : <EOL> code = code [ <NUM_LIT:3> : ] <EOL> else : <EOL> code = code [ <NUM_LIT:1> : ] <EOL> def scan_opcodes_25 ( self , co , <EOL> unpack = struct . unpack ) : <EOL> code = co . co_code <EOL> names = co . co_names <EOL> consts = co . co_consts <EOL> LOAD_LOAD_AND_IMPORT = LOAD_CONST + LOAD_CONST + IMPORT_NAME <EOL> while code : <EOL> c = code [ <NUM_LIT:0> ] <EOL> if c in STORE_OPS : <EOL> oparg , = unpack ( '<STR_LIT>' , code [ <NUM_LIT:1> : <NUM_LIT:3> ] ) <EOL> yield "<STR_LIT:store>" , ( names [ oparg ] , ) <EOL> code = code [ <NUM_LIT:3> : ] <EOL> continue <EOL> if code [ : <NUM_LIT:9> : <NUM_LIT:3> ] == LOAD_LOAD_AND_IMPORT : <EOL> oparg_1 , oparg_2 , oparg_3 = unpack ( '<STR_LIT>' , code [ : <NUM_LIT:9> ] ) <EOL> level = consts [ oparg_1 ] <EOL> if level == - <NUM_LIT:1> : <EOL> yield "<STR_LIT>" , ( consts [ oparg_2 ] , names [ oparg_3 ] ) <EOL> elif level == <NUM_LIT:0> : <EOL> yield "<STR_LIT>" , ( consts [ oparg_2 ] , names [ oparg_3 ] ) <EOL> else : <EOL> yield "<STR_LIT>" , ( level , consts [ oparg_2 ] , names [ oparg_3 ] ) <EOL> code = code [ <NUM_LIT:9> : ] <EOL> continue <EOL> if c >= HAVE_ARGUMENT : <EOL> code = code [ <NUM_LIT:3> : ] <EOL> else : <EOL> code = code [ <NUM_LIT:1> : ] <EOL> def scan_code ( self , co , m ) : <EOL> if m . __name__ in self . skip_scan : <EOL> return <EOL> code = co . co_code <EOL> if sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:5> ) : <EOL> scanner = self . scan_opcodes_25 <EOL> else : <EOL> scanner = self . scan_opcodes <EOL> for what , args in scanner ( co ) : <EOL> if what == "<STR_LIT:store>" : <EOL> name , = args <EOL> m . globalnames [ name ] = <NUM_LIT:1> <EOL> elif what in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> fromlist , name = args <EOL> have_star = <NUM_LIT:0> <EOL> if fromlist is not None : <EOL> if "<STR_LIT:*>" in fromlist : <EOL> have_star = <NUM_LIT:1> <EOL> fromlist = [ f for f in fromlist if f != "<STR_LIT:*>" ] <EOL> if what == "<STR_LIT>" : level = <NUM_LIT:0> <EOL> else : level = - <NUM_LIT:1> <EOL> self . _safe_import_hook ( name , m , fromlist , level = level ) <EOL> if have_star : <EOL> mm = None <EOL> if m . __path__ : <EOL> mm = self . modules . get ( m . __name__ + "<STR_LIT:.>" + name ) <EOL> if mm is None : <EOL> mm = self . modules . get ( name ) <EOL> if mm is not None : <EOL> m . globalnames . update ( mm . globalnames ) <EOL> m . starimports . update ( mm . starimports ) <EOL> if mm . __code__ is None : <EOL> m . starimports [ name ] = <NUM_LIT:1> <EOL> else : <EOL> m . starimports [ name ] = <NUM_LIT:1> <EOL> elif what == "<STR_LIT>" : <EOL> level , fromlist , name = args <EOL> if name : <EOL> self . _safe_import_hook ( name , m , fromlist , level = level ) <EOL> else : <EOL> parent = self . determine_parent ( m , level = level ) <EOL> self . _safe_import_hook ( parent . __name__ , None , fromlist , level = <NUM_LIT:0> ) <EOL> else : <EOL> raise RuntimeError ( what ) <EOL> for c in co . co_consts : <EOL> if isinstance ( c , type ( co ) ) : <EOL> self . scan_code ( c , m ) <EOL> def load_package ( self , fqname , pathname ) : <EOL> self . msgin ( <NUM_LIT:2> , "<STR_LIT>" , fqname , pathname ) <EOL> newname = replacePackageMap . get ( fqname ) <EOL> if newname : <EOL> fqname = newname <EOL> m = self . add_module ( fqname ) <EOL> m . __file__ = pathname <EOL> m . __path__ = [ pathname ] <EOL> m . __path__ = m . __path__ + packagePathMap . get ( fqname , [ ] ) <EOL> fp , buf , stuff = self . find_module ( "<STR_LIT>" , m . __path__ ) <EOL> self . load_module ( fqname , fp , buf , stuff ) <EOL> self . msgout ( <NUM_LIT:2> , "<STR_LIT>" , m ) <EOL> return m <EOL> def add_module ( self , fqname ) : <EOL> if self . modules . has_key ( fqname ) : <EOL> return self . modules [ fqname ] <EOL> self . modules [ fqname ] = m = Module ( fqname ) <EOL> return m <EOL> def find_module ( self , name , path , parent = None ) : <EOL> if parent is not None : <EOL> fullname = parent . __name__ + '<STR_LIT:.>' + name <EOL> else : <EOL> fullname = name <EOL> if fullname in self . excludes : <EOL> self . msgout ( <NUM_LIT:3> , "<STR_LIT>" , fullname ) <EOL> raise ImportError , name <EOL> if path is None : <EOL> if name in sys . builtin_module_names : <EOL> return ( None , None , ( "<STR_LIT>" , "<STR_LIT>" , imp . C_BUILTIN ) ) <EOL> path = self . path <EOL> return imp . find_module ( name , path ) <EOL> def report ( self ) : <EOL> """<STR_LIT>""" <EOL> print <EOL> print "<STR_LIT>" % ( "<STR_LIT:Name>" , "<STR_LIT>" ) <EOL> print "<STR_LIT>" % ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> keys = self . modules . keys ( ) <EOL> keys . sort ( ) <EOL> for key in keys : <EOL> m = self . modules [ key ] <EOL> if m . __path__ : <EOL> print "<STR_LIT:P>" , <EOL> else : <EOL> print "<STR_LIT:m>" , <EOL> print "<STR_LIT>" % key , m . __file__ or "<STR_LIT>" <EOL> missing , maybe = self . any_missing_maybe ( ) <EOL> if missing : <EOL> print <EOL> print "<STR_LIT>" <EOL> for name in missing : <EOL> mods = self . badmodules [ name ] . keys ( ) <EOL> mods . sort ( ) <EOL> print "<STR_LIT:?>" , name , "<STR_LIT>" , '<STR_LIT:U+002CU+0020>' . join ( mods ) <EOL> if maybe : <EOL> print <EOL> print "<STR_LIT>" , <EOL> print "<STR_LIT>" <EOL> for name in maybe : <EOL> mods = self . badmodules [ name ] . keys ( ) <EOL> mods . sort ( ) <EOL> print "<STR_LIT:?>" , name , "<STR_LIT>" , '<STR_LIT:U+002CU+0020>' . join ( mods ) <EOL> def any_missing ( self ) : <EOL> """<STR_LIT>""" <EOL> missing , maybe = self . any_missing_maybe ( ) <EOL> return missing + maybe <EOL> def any_missing_maybe ( self ) : <EOL> """<STR_LIT>""" <EOL> missing = [ ] <EOL> maybe = [ ] <EOL> for name in self . badmodules : <EOL> if name in self . excludes : <EOL> continue <EOL> i = name . rfind ( "<STR_LIT:.>" ) <EOL> if i < <NUM_LIT:0> : <EOL> missing . append ( name ) <EOL> continue <EOL> subname = name [ i + <NUM_LIT:1> : ] <EOL> pkgname = name [ : i ] <EOL> pkg = self . modules . get ( pkgname ) <EOL> if pkg is not None : <EOL> if pkgname in self . badmodules [ name ] : <EOL> missing . append ( name ) <EOL> elif subname in pkg . globalnames : <EOL> pass <EOL> elif pkg . starimports : <EOL> maybe . append ( name ) <EOL> else : <EOL> missing . append ( name ) <EOL> else : <EOL> missing . append ( name ) <EOL> missing . sort ( ) <EOL> maybe . sort ( ) <EOL> return missing , maybe <EOL> def replace_paths_in_code ( self , co ) : <EOL> new_filename = original_filename = os . path . normpath ( co . co_filename ) <EOL> for f , r in self . replace_paths : <EOL> if original_filename . startswith ( f ) : <EOL> new_filename = r + original_filename [ len ( f ) : ] <EOL> break <EOL> if self . debug and original_filename not in self . processed_paths : <EOL> if new_filename != original_filename : <EOL> self . msgout ( <NUM_LIT:2> , "<STR_LIT>" % ( original_filename , new_filename , ) ) <EOL> else : <EOL> self . msgout ( <NUM_LIT:2> , "<STR_LIT>" % ( original_filename , ) ) <EOL> self . processed_paths . append ( original_filename ) <EOL> consts = list ( co . co_consts ) <EOL> for i in range ( len ( consts ) ) : <EOL> if isinstance ( consts [ i ] , type ( co ) ) : <EOL> consts [ i ] = self . replace_paths_in_code ( consts [ i ] ) <EOL> return types . CodeType ( co . co_argcount , co . co_nlocals , co . co_stacksize , <EOL> co . co_flags , co . co_code , tuple ( consts ) , co . co_names , <EOL> co . co_varnames , new_filename , co . co_name , <EOL> co . co_firstlineno , co . co_lnotab , <EOL> co . co_freevars , co . co_cellvars ) <EOL> def test ( ) : <EOL> import getopt <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , "<STR_LIT>" ) <EOL> except getopt . error , msg : <EOL> print msg <EOL> return <EOL> debug = <NUM_LIT:1> <EOL> domods = <NUM_LIT:0> <EOL> addpath = [ ] <EOL> exclude = [ ] <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : <EOL> debug = debug + <NUM_LIT:1> <EOL> if o == '<STR_LIT>' : <EOL> domods = <NUM_LIT:1> <EOL> if o == '<STR_LIT>' : <EOL> addpath = addpath + a . split ( os . pathsep ) <EOL> if o == '<STR_LIT>' : <EOL> debug = <NUM_LIT:0> <EOL> if o == '<STR_LIT>' : <EOL> exclude . append ( a ) <EOL> if not args : <EOL> script = "<STR_LIT>" <EOL> else : <EOL> script = args [ <NUM_LIT:0> ] <EOL> path = sys . path [ : ] <EOL> path [ <NUM_LIT:0> ] = os . path . dirname ( script ) <EOL> path = addpath + path <EOL> if debug > <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> for item in path : <EOL> print "<STR_LIT:U+0020>" , repr ( item ) <EOL> mf = ModuleFinder ( path , debug , exclude ) <EOL> for arg in args [ <NUM_LIT:1> : ] : <EOL> if arg == '<STR_LIT>' : <EOL> domods = <NUM_LIT:1> <EOL> continue <EOL> if domods : <EOL> if arg [ - <NUM_LIT:2> : ] == '<STR_LIT>' : <EOL> mf . import_hook ( arg [ : - <NUM_LIT:2> ] , None , [ "<STR_LIT:*>" ] ) <EOL> else : <EOL> mf . import_hook ( arg ) <EOL> else : <EOL> mf . load_file ( arg ) <EOL> mf . run_script ( script ) <EOL> mf . report ( ) <EOL> return mf <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> try : <EOL> mf = test ( ) <EOL> except KeyboardInterrupt : <EOL> print "<STR_LIT>" <EOL> import tempfile <EOL> import urllib <EOL> try : <EOL> set <EOL> except NameError : <EOL> from sets import Set as set <EOL> Base = ModuleFinder <EOL> del ModuleFinder <EOL> class ModuleFinder ( Base ) : <EOL> def __init__ ( self , * args , ** kw ) : <EOL> self . _depgraph = { } <EOL> self . _types = { } <EOL> self . _last_caller = None <EOL> self . _scripts = set ( ) <EOL> Base . __init__ ( self , * args , ** kw ) <EOL> def run_script ( self , pathname ) : <EOL> self . _scripts . add ( pathname ) <EOL> Base . run_script ( self , pathname ) <EOL> def import_hook ( self , name , caller = None , fromlist = None , level = - <NUM_LIT:1> ) : <EOL> old_last_caller = self . _last_caller <EOL> try : <EOL> self . _last_caller = caller <EOL> return Base . import_hook ( self , name , caller , fromlist , level ) <EOL> finally : <EOL> self . _last_caller = old_last_caller <EOL> def import_module ( self , partnam , fqname , parent ) : <EOL> r = Base . import_module ( self , partnam , fqname , parent ) <EOL> if r is not None and self . _last_caller : <EOL> self . _depgraph . setdefault ( self . _last_caller . __name__ , set ( ) ) . add ( r . __name__ ) <EOL> return r <EOL> def load_module ( self , fqname , fp , pathname , ( suffix , mode , typ ) ) : <EOL> r = Base . load_module ( self , fqname , fp , pathname , ( suffix , mode , typ ) ) <EOL> if r is not None : <EOL> self . _types [ r . __name__ ] = typ <EOL> return r <EOL> def create_xref ( self ) : <EOL> depgraph = { } <EOL> importedby = { } <EOL> for name , value in self . _depgraph . items ( ) : <EOL> depgraph [ name ] = list ( value ) <EOL> for needs in value : <EOL> importedby . setdefault ( needs , set ( ) ) . add ( name ) <EOL> names = self . _types . keys ( ) <EOL> names . sort ( ) <EOL> fd , htmlfile = tempfile . mkstemp ( "<STR_LIT>" ) <EOL> ofi = open ( htmlfile , "<STR_LIT:w>" ) <EOL> os . close ( fd ) <EOL> print >> ofi , "<STR_LIT>" % sys . argv [ <NUM_LIT:0> ] <EOL> print >> ofi , "<STR_LIT>" % sys . argv [ <NUM_LIT:0> ] <EOL> for name in names : <EOL> if self . _types [ name ] in ( imp . PY_SOURCE , imp . PKG_DIRECTORY ) : <EOL> print >> ofi , '<STR_LIT>' % ( name , name ) <EOL> if name == "<STR_LIT:__main__>" : <EOL> for fname in self . _scripts : <EOL> path = urllib . pathname2url ( os . path . abspath ( fname ) ) <EOL> print >> ofi , '<STR_LIT>' % ( path , fname ) <EOL> print >> ofi , '<STR_LIT>' <EOL> else : <EOL> fname = urllib . pathname2url ( self . modules [ name ] . __file__ ) <EOL> print >> ofi , '<STR_LIT>' % ( fname , self . modules [ name ] . __file__ ) <EOL> else : <EOL> fname = self . modules [ name ] . __file__ <EOL> if fname : <EOL> print >> ofi , '<STR_LIT>' % ( name , name , fname ) <EOL> else : <EOL> print >> ofi , '<STR_LIT>' % ( name , name , TYPES [ self . _types [ name ] ] ) <EOL> if name in depgraph : <EOL> needs = depgraph [ name ] <EOL> for n in needs : <EOL> print >> ofi , '<STR_LIT>' % ( n , n ) <EOL> print >> ofi , "<STR_LIT>" <EOL> print >> ofi , '<STR_LIT>' <EOL> if name in importedby : <EOL> for i in importedby [ name ] : <EOL> print >> ofi , '<STR_LIT>' % ( i , i ) <EOL> print >> ofi , "<STR_LIT>" <EOL> print >> ofi , "<STR_LIT>" <EOL> print >> ofi , "<STR_LIT>" <EOL> ofi . close ( ) <EOL> os . startfile ( htmlfile ) <EOL> import threading <EOL> threading . Timer ( <NUM_LIT:5> , os . remove , args = [ htmlfile ] ) <EOL> TYPES = { imp . C_BUILTIN : "<STR_LIT>" , <EOL> imp . C_EXTENSION : "<STR_LIT>" , <EOL> imp . IMP_HOOK : "<STR_LIT>" , <EOL> imp . PKG_DIRECTORY : "<STR_LIT>" , <EOL> imp . PY_CODERESOURCE : "<STR_LIT>" , <EOL> imp . PY_COMPILED : "<STR_LIT>" , <EOL> imp . PY_FROZEN : "<STR_LIT>" , <EOL> imp . PY_RESOURCE : "<STR_LIT>" , <EOL> imp . PY_SOURCE : "<STR_LIT>" , <EOL> imp . SEARCH_ERROR : "<STR_LIT>" <EOL> } </s>
