<s> import os <EOL> import sys <EOL> import zipfile <EOL> from six . moves import cStringIO <EOL> from bento . _config import BUILD_MANIFEST_PATH <EOL> from bento . conv import to_distutils_meta <EOL> from bento . core import PackageMetadata <EOL> from bento . installed_package_description import iter_source_files , BuildManifest <EOL> def egg_filename ( fullname , pyver = None ) : <EOL> if not pyver : <EOL> pyver = "<STR_LIT:.>" . join ( [ str ( i ) for i in sys . version_info [ : <NUM_LIT:2> ] ] ) <EOL> return "<STR_LIT>" % ( fullname , pyver ) <EOL> def egg_info_dirname ( fullname , pyver = None ) : <EOL> if not pyver : <EOL> pyver = "<STR_LIT:.>" . join ( [ str ( i ) for i in sys . version_info [ : <NUM_LIT:2> ] ] ) <EOL> return "<STR_LIT>" % ( fullname , pyver ) <EOL> class EggInfo ( object ) : <EOL> @ classmethod <EOL> def from_build_manifest ( cls , build_manifest , src_node ) : <EOL> meta = PackageMetadata . from_build_manifest ( build_manifest ) <EOL> executables = build_manifest . executables <EOL> file_sections = build_manifest . resolve_paths ( src_node ) <EOL> sources = list ( [ n . abspath ( ) for n in iter_source_files ( file_sections ) ] ) <EOL> ret = cls ( meta , executables , sources ) <EOL> ret . build_manifest = build_manifest <EOL> return ret <EOL> def __init__ ( self , meta , executables , sources ) : <EOL> self . _dist_meta = to_distutils_meta ( meta ) <EOL> self . sources = sources <EOL> self . meta = meta <EOL> self . executables = executables <EOL> self . build_manifest = None <EOL> def get_pkg_info ( self ) : <EOL> tmp = cStringIO ( ) <EOL> self . _dist_meta . write_pkg_file ( tmp ) <EOL> ret = tmp . getvalue ( ) <EOL> tmp . close ( ) <EOL> return ret <EOL> def get_sources ( self ) : <EOL> return "<STR_LIT:\n>" . join ( [ os . path . normpath ( f ) for f in self . sources ] ) <EOL> def get_install_requires ( self ) : <EOL> return "<STR_LIT:\n>" . join ( self . meta . install_requires ) <EOL> def get_top_levels ( self ) : <EOL> return "<STR_LIT:\n>" . join ( self . meta . top_levels + [ '<STR_LIT>' ] ) <EOL> def get_not_zip_safe ( self ) : <EOL> return "<STR_LIT:\n>" <EOL> def get_dependency_links ( self ) : <EOL> return "<STR_LIT:\n>" <EOL> def get_entry_points ( self ) : <EOL> ret = [ ] <EOL> ret . append ( "<STR_LIT>" ) <EOL> ret . extend ( [ exe . full_representation ( ) for exe in self . executables . values ( ) ] ) <EOL> ret . append ( '<STR_LIT>' ) <EOL> return "<STR_LIT:\n>" . join ( ret ) <EOL> def get_build_manifest_info ( self , build_manifest_node ) : <EOL> if self . build_manifest is None : <EOL> return build_manifest_node . read ( ) <EOL> else : <EOL> tmp = cStringIO ( ) <EOL> self . build_manifest . _write ( tmp ) <EOL> ret = tmp . getvalue ( ) <EOL> tmp . close ( ) <EOL> return ret <EOL> def iter_meta ( self , build_node ) : <EOL> build_manifest_node = build_node . make_node ( BUILD_MANIFEST_PATH ) <EOL> func_table = { <EOL> "<STR_LIT>" : self . get_pkg_info , <EOL> "<STR_LIT>" : self . get_sources , <EOL> "<STR_LIT>" : self . get_install_requires , <EOL> "<STR_LIT>" : self . get_top_levels , <EOL> "<STR_LIT>" : self . get_not_zip_safe , <EOL> "<STR_LIT>" : self . get_dependency_links , <EOL> "<STR_LIT>" : self . get_entry_points , <EOL> "<STR_LIT>" : lambda : self . get_build_manifest_info ( build_manifest_node ) , <EOL> } <EOL> file_table = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> for k in func_table : <EOL> yield file_table [ k ] , func_table [ k ] ( ) <EOL> def extract_egg ( egg , extract_dir ) : <EOL> build_manifest = BuildManifest . from_egg ( egg ) <EOL> build_manifest . update_paths ( { "<STR_LIT>" : "<STR_LIT:.>" , "<STR_LIT>" : "<STR_LIT:.>" , "<STR_LIT>" : "<STR_LIT:.>" } ) <EOL> zid = zipfile . ZipFile ( egg ) <EOL> try : <EOL> for type , sections in build_manifest . files . items ( ) : <EOL> for name , section in sections . items ( ) : <EOL> target_dir = build_manifest . resolve_path ( section . target_dir ) <EOL> section . source_dir = os . path . join ( extract_dir , target_dir ) <EOL> for source , target in section . files : <EOL> g = os . path . join ( target_dir , target ) <EOL> g = os . path . normpath ( g ) <EOL> zid . extract ( g , extract_dir ) <EOL> finally : <EOL> zid . close ( ) <EOL> return build_manifest </s>
