<s> from MurkyWaters import MurkyWaters <EOL> import time , json , sys <EOL> import BaseHTTPServer <EOL> import os <EOL> import zlib <EOL> def save_file ( config , resid , data ) : <EOL> path = os . path . join ( config [ '<STR_LIT>' ] , "<STR_LIT>" % resid ) <EOL> with open ( path , '<STR_LIT:wb>' ) as handle : <EOL> handle . write ( zlib . compress ( data ) ) <EOL> murky = None <EOL> files = { } <EOL> fn = "<STR_LIT>" <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> fn = sys . argv [ <NUM_LIT:1> ] <EOL> config = json . load ( open ( fn ) ) <EOL> class MurkyProxy ( BaseHTTPServer . BaseHTTPRequestHandler ) : <EOL> def do_GET ( self ) : <EOL> global murky , files , config <EOL> if len ( self . path ) > <NUM_LIT:1> : <EOL> try : <EOL> resid = int ( self . path [ <NUM_LIT:1> : ] ) <EOL> if resid in files : <EOL> content = files [ resid ] <EOL> else : <EOL> content = murky . fetch ( resid ) <EOL> if content : <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( content ) <EOL> else : <EOL> self . send_response ( <NUM_LIT> ) <EOL> self . end_headers ( ) <EOL> except ValueError : <EOL> self . send_response ( <NUM_LIT> ) <EOL> self . end_headers ( ) <EOL> return <EOL> else : <EOL> self . send_response ( <NUM_LIT> ) <EOL> self . end_headers ( ) <EOL> return <EOL> def do_PUT ( self ) : <EOL> global murky , files , config <EOL> if len ( self . path ) > <NUM_LIT:1> : <EOL> try : <EOL> resid = int ( self . path [ <NUM_LIT:1> : ] ) <EOL> L = int ( self . headers [ '<STR_LIT>' ] ) <EOL> data = self . rfile . read ( L ) <EOL> murky . add ( resid , data ) <EOL> murky . propagate ( resid ) <EOL> save_file ( config , resid , data ) <EOL> files [ resid ] = data <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( json . dumps ( { '<STR_LIT>' : '<STR_LIT>' } ) + "<STR_LIT:\n>" ) <EOL> except ValueError : <EOL> self . send_response ( <NUM_LIT> ) <EOL> self . end_headers ( ) <EOL> return <EOL> else : <EOL> self . send_response ( <NUM_LIT> ) <EOL> self . end_headers ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> server = BaseHTTPServer . HTTPServer ( ( config [ '<STR_LIT:http>' ] [ '<STR_LIT:host>' ] , config [ '<STR_LIT:http>' ] [ '<STR_LIT:port>' ] ) , MurkyProxy ) <EOL> murky = MurkyWaters ( save_file , config ) <EOL> murky . start ( ) <EOL> servers = json . load ( open ( config [ '<STR_LIT>' ] ) ) <EOL> for entry in servers : <EOL> murky . server ( entry [ '<STR_LIT:host>' ] , entry [ '<STR_LIT:port>' ] ) <EOL> local_files = os . listdir ( config [ '<STR_LIT>' ] ) <EOL> for fn in local_files : <EOL> try : <EOL> resid = int ( fn , <NUM_LIT:16> ) <EOL> path = os . path . join ( config [ '<STR_LIT>' ] , fn ) <EOL> data = zlib . decompress ( open ( path , '<STR_LIT:rb>' ) . read ( ) ) <EOL> files [ resid ] = data <EOL> murky . add ( resid , data ) <EOL> if config [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> murky . propagate ( resid ) <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> print "<STR_LIT>" % time . strftime ( "<STR_LIT>" ) <EOL> server . serve_forever ( ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> server . shutdown ( ) <EOL> murky . stop ( ) </s>
