<s> import sys <EOL> import imp <EOL> import struct <EOL> import marshal <EOL> import imputil <EOL> import os <EOL> import pyjd <EOL> _TupleType = type ( ( ) ) <EOL> _StringType = type ( '<STR_LIT>' ) <EOL> _suffix_char = __debug__ and '<STR_LIT:c>' or '<STR_LIT:o>' <EOL> _suffix = '<STR_LIT>' + _suffix_char <EOL> _c_suffixes = filter ( lambda x : x [ <NUM_LIT:2> ] == imp . C_EXTENSION , imp . get_suffixes ( ) ) <EOL> from modcompile import PlatformParser , Module <EOL> pp = PlatformParser ( verbose = False ) <EOL> pp . platform = pyjd . engine <EOL> parser = PlatformParser ( verbose = False , chain_plat = pp ) <EOL> def _timestamp ( pathname ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> s = os . stat ( pathname ) <EOL> except OSError : <EOL> return None <EOL> return long ( s [ <NUM_LIT:8> ] ) <EOL> def _fs_import ( dir , modname , fqname ) : <EOL> "<STR_LIT>" <EOL> pathname = os . path . join ( dir , modname ) <EOL> if os . path . isdir ( pathname ) : <EOL> values = { '<STR_LIT>' : pathname , '<STR_LIT>' : [ pathname ] } <EOL> ispkg = <NUM_LIT:1> <EOL> pathname = os . path . join ( pathname , '<STR_LIT>' ) <EOL> else : <EOL> values = { } <EOL> ispkg = <NUM_LIT:0> <EOL> for desc in _c_suffixes : <EOL> fname = pathname + desc [ <NUM_LIT:0> ] <EOL> try : <EOL> fp = open ( fname , desc [ <NUM_LIT:1> ] ) <EOL> except IOError : <EOL> pass <EOL> else : <EOL> module = imp . load_module ( fqname , fp , fname , desc ) <EOL> values [ '<STR_LIT>' ] = fname <EOL> return <NUM_LIT:0> , module , values <EOL> filename = pathname + '<STR_LIT>' <EOL> filenamec = pathname + _suffix <EOL> t_py = _timestamp ( filename ) <EOL> t_pyc = _timestamp ( filenamec ) <EOL> if t_py is None and t_pyc is None : <EOL> return None <EOL> code = None <EOL> out_t_py = t_py <EOL> out_filename = filename <EOL> platform_file = parser . checkOverridePlatformFile ( filename ) <EOL> if platform_file : <EOL> platform_filec = platform_file [ : - <NUM_LIT:3> ] + _suffix <EOL> t_p_py = _timestamp ( platform_file ) <EOL> t_p_pyc = _timestamp ( platform_filec ) <EOL> if t_p_py is not None or t_p_pyc is not None : <EOL> ok = True <EOL> if t_py is not None and t_pyc is not None and t_py > t_pyc : <EOL> ok = False <EOL> if t_py is not None and t_p_py is not None and t_py > t_p_py : <EOL> ok = False <EOL> if t_py is not None and t_p_pyc is not None and t_py > t_p_pyc : <EOL> ok = False <EOL> if ok and ( t_p_py is None or ( t_p_pyc is not None and t_p_pyc >= t_p_py ) ) : <EOL> f = open ( platform_filec , '<STR_LIT:rb>' ) <EOL> magic = f . read ( <NUM_LIT:4> ) <EOL> if magic == imp . get_magic ( ) : <EOL> t = struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:4> ) ) [ <NUM_LIT:0> ] <EOL> if t == t_p_py : <EOL> code = marshal . load ( f ) <EOL> f . close ( ) <EOL> if code is None and ( t_py is None or ( t_pyc is not None and t_pyc >= t_py ) ) : <EOL> f = open ( filenamec , '<STR_LIT:rb>' ) <EOL> magic = f . read ( <NUM_LIT:4> ) <EOL> if magic == imp . get_magic ( ) : <EOL> t = struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:4> ) ) [ <NUM_LIT:0> ] <EOL> if t == t_py : <EOL> code = marshal . load ( f ) <EOL> f . close ( ) <EOL> if code is None : <EOL> filename = pathname + '<STR_LIT>' <EOL> "<STR_LIT>" , filename <EOL> mod , filename = parser . parseModule ( modname , filename ) <EOL> code = Module ( mod , filename ) <EOL> code . compile ( ) <EOL> code = code . getCode ( ) <EOL> if platform_file and t_p_py : <EOL> out_t_py = t_p_py <EOL> out_filename = platform_file <EOL> try : <EOL> f = open ( out_filename + _suffix_char , '<STR_LIT:wb>' ) <EOL> except IOError , e : <EOL> pass <EOL> else : <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( struct . pack ( '<STR_LIT>' , out_t_py ) ) <EOL> marshal . dump ( code , f ) <EOL> f . flush ( ) <EOL> f . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> f . write ( imp . get_magic ( ) ) <EOL> f . close ( ) <EOL> values [ '<STR_LIT>' ] = filename <EOL> return ispkg , code , values <EOL> class FuncImporter ( imputil . Importer ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , func ) : <EOL> self . func = func <EOL> def get_code ( self , parent , modname , fqname ) : <EOL> return self . func ( parent , modname , fqname ) <EOL> def install_with ( func ) : <EOL> FuncImporter ( func ) . install ( ) <EOL> class PackageArchiveImporter ( imputil . Importer ) : <EOL> """<STR_LIT>""" <EOL> def get_code ( self , parent , modname , fqname ) : <EOL> if parent : <EOL> assert parent . __importer__ == self <EOL> result = self . get_subfile ( parent . __archive__ , modname ) <EOL> if result is None : <EOL> return None <EOL> if isinstance ( result , _TupleType ) : <EOL> assert len ( result ) == <NUM_LIT:2> <EOL> return ( <NUM_LIT:0> , ) + result <EOL> return <NUM_LIT:0> , result , { } <EOL> archive = self . get_archive ( modname ) <EOL> if archive is None : <EOL> return None <EOL> return <NUM_LIT:1> , "<STR_LIT>" , { '<STR_LIT>' : archive } <EOL> def get_archive ( self , modname ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError , "<STR_LIT>" <EOL> def get_subfile ( self , archive , modname ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError , "<STR_LIT>" <EOL> class PackageArchive ( PackageArchiveImporter ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , modname , archive_pathname ) : <EOL> self . __modname = modname <EOL> self . __path = archive_pathname <EOL> def get_archive ( self , modname ) : <EOL> if modname == self . __modname : <EOL> return self . __path <EOL> return None <EOL> class DirectoryImporter ( imputil . Importer ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , dir ) : <EOL> self . dir = dir <EOL> def get_code ( self , parent , modname , fqname ) : <EOL> if parent : <EOL> dir = parent . __pkgdir__ <EOL> else : <EOL> dir = self . dir <EOL> return _fs_import ( dir , modname , fqname ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __module__ , <EOL> self . __class__ . __name__ , <EOL> self . dir , <EOL> id ( self ) ) <EOL> class PathImporter ( imputil . Importer ) : <EOL> def __init__ ( self , path = sys . path ) : <EOL> self . path = path <EOL> def get_code ( self , parent , modname , fqname ) : <EOL> if parent : <EOL> return _fs_import ( parent . __pkgdir__ , modname , fqname ) <EOL> for dir in self . path : <EOL> if isinstance ( dir , _StringType ) : <EOL> result = _fs_import ( dir , modname , fqname ) <EOL> if result : <EOL> return result <EOL> return None <EOL> def _test_dir ( ) : <EOL> "<STR_LIT>" <EOL> imputil . ImportManager ( ) . install ( ) <EOL> path = sys . path [ : ] <EOL> path . reverse ( ) <EOL> for d in path : <EOL> sys . path . insert ( <NUM_LIT:0> , DirectoryImporter ( d ) ) <EOL> sys . path . insert ( <NUM_LIT:0> , imputil . BuiltinImporter ( ) ) <EOL> def _test_revamp ( ) : <EOL> "<STR_LIT>" <EOL> imputil . ImportManager ( ) . install ( ) <EOL> sys . path . insert ( <NUM_LIT:0> , PathImporter ( ) ) <EOL> sys . path . insert ( <NUM_LIT:0> , imputil . BuiltinImporter ( ) ) </s>
