<s> import base64 <EOL> import json <EOL> import logging <EOL> import os <EOL> import struct <EOL> from google . appengine . ext import webapp <EOL> from google . appengine . ext . webapp import util <EOL> import member <EOL> class SyncSignatures ( webapp . RequestHandler ) : <EOL> isJson = False <EOL> def post ( self ) : <EOL> self . response . headers . add_header ( "<STR_LIT>" , "<STR_LIT:*>" ) <EOL> header = self . request . headers [ '<STR_LIT:Content-Type>' ] <EOL> logging . debug ( "<STR_LIT>" % header ) <EOL> if ( str ( header ) . startswith ( '<STR_LIT>' ) ) : <EOL> self . isJson = True <EOL> self . response . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> data_dict = json . loads ( self . request . body ) <EOL> else : <EOL> self . response . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> STR_VERSERVER = '<STR_LIT>' <EOL> INT_VERCLIENT = <NUM_LIT> <EOL> STR_VERCLIENT = '<STR_LIT>' <EOL> if not os . environ . has_key ( '<STR_LIT>' ) : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> if not os . environ . has_key ( '<STR_LIT>' ) : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> HTTPS = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> CURRENT_VERSION_ID = os . environ . get ( '<STR_LIT>' , STR_VERSERVER ) <EOL> if HTTPS . __str__ ( ) != '<STR_LIT>' : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> minlen = <NUM_LIT:4> + <NUM_LIT:4> + <NUM_LIT:4> + <NUM_LIT:4> <EOL> data = self . request . body <EOL> logging . debug ( "<STR_LIT>" % data ) <EOL> size = str . __len__ ( data ) <EOL> logging . debug ( "<STR_LIT>" % size ) <EOL> if size < minlen : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> server = int ( CURRENT_VERSION_ID [ <NUM_LIT:0> : <NUM_LIT:8> ] , <NUM_LIT:16> ) <EOL> if self . isJson : <EOL> client = int ( data_dict [ '<STR_LIT>' ] , <NUM_LIT:10> ) <EOL> else : <EOL> client = ( struct . unpack ( "<STR_LIT>" , data [ <NUM_LIT:0> : <NUM_LIT:4> ] ) ) [ <NUM_LIT:0> ] <EOL> logging . debug ( "<STR_LIT>" % client ) <EOL> if self . isJson : <EOL> usrid = int ( data_dict [ '<STR_LIT>' ] , <NUM_LIT:10> ) <EOL> else : <EOL> usrid = ( struct . unpack ( "<STR_LIT>" , data [ <NUM_LIT:4> : <NUM_LIT:8> ] ) ) [ <NUM_LIT:0> ] <EOL> logging . debug ( "<STR_LIT>" % usrid ) <EOL> if self . isJson : <EOL> numEntry = data_dict [ '<STR_LIT>' ] . __len__ ( ) <EOL> else : <EOL> numEntry = ( struct . unpack ( "<STR_LIT>" , data [ <NUM_LIT:8> : <NUM_LIT:12> ] ) ) [ <NUM_LIT:0> ] <EOL> logging . debug ( "<STR_LIT>" % numEntry ) <EOL> pos = <NUM_LIT:12> <EOL> expectedsize = <NUM_LIT:4> + <NUM_LIT:4> + <NUM_LIT:4> + ( <NUM_LIT:4> * numEntry ) <EOL> usrids = [ ] <EOL> i = <NUM_LIT:0> <EOL> while numEntry > len ( usrids ) : <EOL> if self . isJson : <EOL> otherid = int ( data_dict [ '<STR_LIT>' ] [ i ] , <NUM_LIT:10> ) <EOL> i += <NUM_LIT:1> <EOL> else : <EOL> otherid = struct . unpack ( "<STR_LIT>" , data [ pos : pos + <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] <EOL> pos += <NUM_LIT:4> <EOL> usrids . append ( otherid ) <EOL> logging . debug ( "<STR_LIT>" % otherid ) <EOL> postSelf = False <EOL> if self . isJson : <EOL> if '<STR_LIT>' in data_dict : <EOL> newVal = base64 . decodestring ( data_dict [ '<STR_LIT>' ] ) <EOL> postSelf = True <EOL> else : <EOL> if size > expectedsize : <EOL> newVal = data [ pos : ] <EOL> postSelf = True <EOL> if postSelf : <EOL> logging . debug ( "<STR_LIT>" % newVal ) <EOL> if client < INT_VERCLIENT : <EOL> self . resp_simple ( <NUM_LIT:0> , ( '<STR_LIT>' % STR_VERCLIENT ) ) <EOL> return <EOL> query = member . Member . all ( ) <EOL> query . filter ( '<STR_LIT>' , usrid ) <EOL> num = query . count ( ) <EOL> if num == <NUM_LIT:1> : <EOL> mem = query . get ( ) <EOL> usridlink = mem . usr_id_link <EOL> if postSelf : <EOL> mem . signature = newVal <EOL> mem . put ( ) <EOL> key = mem . key ( ) <EOL> if not key . has_id_or_name ( ) : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> else : <EOL> if mem . signature == None : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> query = member . Member . all ( ) <EOL> query . filter ( '<STR_LIT>' , usridlink ) <EOL> mems = query . fetch ( <NUM_LIT:1000> ) <EOL> if not self . isJson : <EOL> self . response . out . write ( '<STR_LIT:%s>' % struct . pack ( '<STR_LIT>' , server ) ) <EOL> logging . debug ( "<STR_LIT>" % server ) <EOL> total = <NUM_LIT:0> <EOL> for mem in mems : <EOL> if mem . signature != None : <EOL> total = total + <NUM_LIT:1> <EOL> if not self . isJson : <EOL> self . response . out . write ( '<STR_LIT:%s>' % struct . pack ( '<STR_LIT>' , total ) ) <EOL> logging . debug ( "<STR_LIT>" % total ) <EOL> delta = <NUM_LIT:0> <EOL> for mem in mems : <EOL> posted = False <EOL> for known in usrids : <EOL> if known == mem . usr_id : <EOL> posted = True <EOL> if ( not posted ) & ( mem . signature != None ) : <EOL> delta = delta + <NUM_LIT:1> <EOL> if not self . isJson : <EOL> self . response . out . write ( '<STR_LIT:%s>' % struct . pack ( '<STR_LIT>' , delta ) ) <EOL> logging . debug ( "<STR_LIT>" % delta ) <EOL> deltas = [ ] <EOL> for mem in mems : <EOL> posted = False <EOL> for known in usrids : <EOL> if known == mem . usr_id : <EOL> posted = True <EOL> if ( not posted ) & ( mem . signature != None ) : <EOL> length = str . __len__ ( mem . signature ) <EOL> if self . isJson : <EOL> deltas . append ( { '<STR_LIT>' : str ( mem . usr_id ) , '<STR_LIT>' : base64 . encodestring ( mem . signature ) } ) <EOL> else : <EOL> self . response . out . write ( '<STR_LIT>' % ( struct . pack ( '<STR_LIT>' , mem . usr_id , length ) , mem . signature ) ) <EOL> logging . debug ( "<STR_LIT>" % mem . usr_id ) <EOL> logging . debug ( "<STR_LIT>" % length ) <EOL> logging . debug ( "<STR_LIT>" % mem . signature ) <EOL> else : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' % ( usrid ) ) <EOL> return <EOL> if self . isJson : <EOL> json . dump ( { "<STR_LIT>" : str ( server ) , "<STR_LIT>" : str ( total ) , "<STR_LIT>" : deltas } , self . response . out ) <EOL> def resp_simple ( self , code , msg ) : <EOL> if self . isJson : <EOL> json . dump ( { "<STR_LIT>" : str ( code ) , "<STR_LIT>" : str ( msg ) } , self . response . out ) <EOL> else : <EOL> self . response . out . write ( '<STR_LIT>' % ( struct . pack ( '<STR_LIT>' , code ) , msg ) ) <EOL> if code == <NUM_LIT:0> : <EOL> logging . error ( msg ) <EOL> def main ( ) : <EOL> STR_VERSERVER = '<STR_LIT>' <EOL> CURRENT_VERSION_ID = os . environ . get ( '<STR_LIT>' , STR_VERSERVER ) <EOL> isProd = CURRENT_VERSION_ID [ <NUM_LIT:8> : <NUM_LIT:9> ] == '<STR_LIT:p>' <EOL> if isProd : <EOL> logging . getLogger ( ) . setLevel ( logging . INFO ) <EOL> else : <EOL> logging . getLogger ( ) . setLevel ( logging . DEBUG ) <EOL> application = webapp . WSGIApplication ( [ ( '<STR_LIT>' , SyncSignatures ) , <EOL> ] , <EOL> debug = True ) <EOL> util . run_wsgi_app ( application ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
