<s> import os <EOL> import sys <EOL> import zipfile <EOL> import tempfile <EOL> import struct <EOL> import base64 <EOL> import shutil <EOL> from cStringIO import StringIO <EOL> import bento <EOL> from bento . installed_package_description import InstalledPkgDescription <EOL> from bento . commands . build_egg import build_egg <EOL> def read_ipkg ( wininst ) : <EOL> eof_cdir_n = <NUM_LIT> <EOL> eof_cdir_tag = <NUM_LIT> <EOL> meta_n = <NUM_LIT:12> <EOL> stat_info = os . stat ( WININST ) <EOL> inst_size = stat_info . st_size <EOL> with open ( WININST , "<STR_LIT:rb>" ) as fid : <EOL> fid . seek ( - eof_cdir_n , <NUM_LIT:2> ) <EOL> s = fid . read ( <NUM_LIT:4> ) <EOL> tag = struct . unpack ( "<STR_LIT>" , s ) [ <NUM_LIT:0> ] <EOL> if not tag == eof_cdir_tag : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> fid . read ( <NUM_LIT:2> * <NUM_LIT:4> ) <EOL> s = fid . read ( <NUM_LIT:4> ) <EOL> nBytesCDir = struct . unpack ( "<STR_LIT>" , s ) [ <NUM_LIT:0> ] <EOL> s = fid . read ( <NUM_LIT:4> ) <EOL> ofsCDir = struct . unpack ( "<STR_LIT>" , s ) [ <NUM_LIT:0> ] <EOL> arc_start = inst_size - eof_cdir_n - nBytesCDir - ofsCDir <EOL> ofs = arc_start - meta_n <EOL> fid . seek ( ofs , <NUM_LIT:0> ) <EOL> s = fid . read ( <NUM_LIT:4> ) <EOL> tag = struct . unpack ( "<STR_LIT>" , s ) [ <NUM_LIT:0> ] <EOL> if not tag == <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> s = fid . read ( <NUM_LIT:4> ) <EOL> uncomp_size = struct . unpack ( "<STR_LIT>" , s ) [ <NUM_LIT:0> ] <EOL> s = fid . read ( <NUM_LIT:4> ) <EOL> bitmap_size = struct . unpack ( "<STR_LIT>" , s ) [ <NUM_LIT:0> ] <EOL> pexe_size = ofs - uncomp_size - bitmap_size <EOL> fid . seek ( pexe_size , <NUM_LIT:0> ) <EOL> data = fid . read ( uncomp_size ) <EOL> from ConfigParser import ConfigParser <EOL> parser = ConfigParser ( ) <EOL> sdata = StringIO ( data ) <EOL> def truncate_null ( sdata ) : <EOL> cur = sdata . tell ( ) <EOL> sdata . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> nbytes = sdata . tell ( ) <EOL> try : <EOL> n = <NUM_LIT:10> <EOL> sdata . seek ( - n , <NUM_LIT:2> ) <EOL> null_ind = sdata . read ( ) . find ( "<STR_LIT>" ) <EOL> sdata . truncate ( nbytes - ( n - null_ind ) ) <EOL> finally : <EOL> sdata . seek ( cur , <NUM_LIT:0> ) <EOL> truncate_null ( sdata ) <EOL> parser . readfp ( sdata ) <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> ipkg_str = base64 . b64decode ( parser . get ( "<STR_LIT>" , "<STR_LIT:value>" ) ) <EOL> ipkg = InstalledPkgDescription . from_string ( ipkg_str ) <EOL> return ipkg <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> from bento . commands . wininst_utils import get_exe_bytes <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> WININST = "<STR_LIT>" <EOL> else : <EOL> WININST = sys . argv [ <NUM_LIT:1> ] <EOL> ipkg = read_ipkg ( WININST ) <EOL> tmpdir = tempfile . mkdtemp ( ) <EOL> z = zipfile . ZipFile ( WININST ) <EOL> try : <EOL> z . extractall ( path = tmpdir ) <EOL> build_egg ( ipkg , source_root = os . path . join ( tmpdir , "<STR_LIT>" ) , path = "<STR_LIT:.>" ) <EOL> finally : <EOL> z . close ( ) <EOL> shutil . rmtree ( tmpdir ) </s>
