<s> import base64 <EOL> import bz2 <EOL> import contextlib <EOL> import itertools <EOL> import json <EOL> import mimetypes <EOL> import optparse <EOL> import os <EOL> import re <EOL> import tarfile <EOL> def list_files ( fn ) : <EOL> if os . path . isdir ( fn ) : <EOL> return listFilesystem ( fn ) <EOL> else : <EOL> return listTar ( fn ) <EOL> def listFilesystem ( root ) : <EOL> """<STR_LIT>""" <EOL> visit = [ ( root , '<STR_LIT>' ) ] <EOL> while len ( visit ) > <NUM_LIT:0> : <EOL> r , v = visit . pop ( <NUM_LIT:0> ) <EOL> children = os . listdir ( r ) <EOL> yield ( v , children , None ) <EOL> for direntry in children : <EOL> rfn = os . path . join ( r , direntry ) <EOL> vfn = v + '<STR_LIT:/>' + direntry <EOL> if os . path . isdir ( rfn ) : <EOL> visit . append ( ( rfn , vfn ) ) <EOL> else : <EOL> yield ( vfn , None , open ( rfn , '<STR_LIT:rb>' ) ) <EOL> def listTar ( tarfilename ) : <EOL> tf = tarfile . open ( tarfilename ) <EOL> members = tf . getmembers ( ) <EOL> rootChildren = [ ] <EOL> for entry in members : <EOL> ename = '<STR_LIT:/>' + entry . name . strip ( '<STR_LIT:/>' ) <EOL> if '<STR_LIT:/>' not in ename : <EOL> rootChildren . append ( ename ) <EOL> if entry . isdir ( ) : <EOL> children = [ <EOL> m . name . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] for m in members <EOL> if ( m . name . startswith ( ename [ <NUM_LIT:1> : ] + '<STR_LIT:/>' ) and <EOL> '<STR_LIT:/>' not in m . name [ len ( ename [ <NUM_LIT:1> : ] + '<STR_LIT:/>' ) ] ) ] <EOL> yield ( ename , children , None ) <EOL> else : <EOL> f = tf . extractfile ( entry ) <EOL> yield ( ename , None , f ) <EOL> yield ( '<STR_LIT>' , rootChildren , None ) <EOL> def parseFiles ( iterFiles ) : <EOL> res = { } <EOL> for vfn , children , fileh in iterFiles : <EOL> assert ( children is None ) != ( fileh is None ) <EOL> fdata = { } <EOL> if fileh is None : <EOL> fdata [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> fdata [ '<STR_LIT:content>' ] = list ( sorted ( children ) ) <EOL> else : <EOL> fdata [ '<STR_LIT:type>' ] = mimetypes . guess_type ( vfn ) [ <NUM_LIT:0> ] <EOL> with contextlib . closing ( fileh ) as f : <EOL> content = f . read ( ) <EOL> fdata [ '<STR_LIT>' ] = base64 . b64encode ( content ) . decode ( '<STR_LIT:ascii>' ) <EOL> res [ vfn ] = fdata <EOL> return res <EOL> def genFilesRaw ( iterFiles ) : <EOL> fs = parseFiles ( iterFiles ) <EOL> if '<STR_LIT>' not in fs : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if len ( fs [ '<STR_LIT>' ] [ '<STR_LIT:content>' ] ) == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in fs : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> raw = json . dumps ( fs ) . encode ( '<STR_LIT:utf-8>' ) <EOL> raw = bz2 . compress ( raw ) <EOL> return raw <EOL> def genFaviconRaw ( source ) : <EOL> with contextlib . closing ( open ( source , '<STR_LIT:rb>' ) ) as f : <EOL> return f . read ( ) <EOL> def genDbDataRawFromFile ( source ) : <EOL> with contextlib . closing ( open ( source , '<STR_LIT:rb>' ) ) as f : <EOL> raw = base64 . b16decode ( bz2 . decompress ( f . read ( ) ) ) <EOL> raw = bz2 . compress ( raw ) <EOL> return raw <EOL> def replace_constant ( code , constant , rawVal ) : <EOL> strVal = base64 . b64encode ( rawVal ) . decode ( '<STR_LIT:ascii>' ) <EOL> p = re . compile ( '<STR_LIT:(>' + re . escape ( constant ) + '<STR_LIT>' ) <EOL> code = p . sub ( lambda m : m . group ( <NUM_LIT:1> ) + '<STR_LIT>' + strVal + '<STR_LIT:">' , code ) <EOL> return code <EOL> def main ( ) : <EOL> op = optparse . OptionParser ( ) <EOL> op . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> op . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> op . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> options , args = op . parse_args ( ) <EOL> if len ( args ) != <NUM_LIT:0> : <EOL> op . error ( '<STR_LIT>' ) <EOL> if options . traversalroots is None and options . favicon is None and options . dbdata_file is None : <EOL> op . error ( '<STR_LIT>' ) <EOL> scriptfn = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> with contextlib . closing ( open ( scriptfn , '<STR_LIT>' ) ) as scriptf : <EOL> code = scriptf . read ( ) <EOL> if options . traversalroots is not None : <EOL> fileList = itertools . chain ( * map ( list_files , options . traversalroots ) ) <EOL> filesRaw = genFilesRaw ( fileList ) <EOL> code = replace_constant ( code , '<STR_LIT>' , filesRaw ) <EOL> if options . favicon is not None : <EOL> faviconRaw = genFaviconRaw ( options . favicon ) <EOL> code = replace_constant ( code , '<STR_LIT>' , faviconRaw ) <EOL> if options . dbdata_file is not None : <EOL> dbDataRaw = genDbDataRawFromFile ( options . dbdata_file ) <EOL> code = replace_constant ( code , '<STR_LIT>' , dbDataRaw ) <EOL> scriptf . seek ( <NUM_LIT:0> ) <EOL> scriptf . truncate ( <NUM_LIT:0> ) <EOL> scriptf . write ( code ) <EOL> scriptf . flush ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
