<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import logging <EOL> import sys <EOL> from functools import partial <EOL> from tornado import httpclient , options , web , ioloop <EOL> from viewfinder . backend . base import util <EOL> options . define ( "<STR_LIT>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> _metadata = dict ( ) <EOL> """<STR_LIT>""" <EOL> def GetAMIMetadata ( ) : <EOL> return _metadata <EOL> def SetAMIMetadata ( metadata ) : <EOL> global _metadata <EOL> _metadata = metadata <EOL> class Metadata ( object ) : <EOL> """<STR_LIT>""" <EOL> _QUERY_IP = "<STR_LIT>" <EOL> _QUERY_VERSION = "<STR_LIT>" <EOL> def __init__ ( self , callback = None , query_ip = _QUERY_IP , query_version = _QUERY_VERSION ) : <EOL> """<STR_LIT>""" <EOL> self . _query_ip = query_ip <EOL> self . _query_version = query_version <EOL> if callback : <EOL> self . _FetchCommonMetadata ( callback ) <EOL> def FetchMetadata ( self , paths , callback ) : <EOL> """<STR_LIT>""" <EOL> metadata = { } <EOL> def _OnFetch ( path , callback , response ) : <EOL> if response . code == <NUM_LIT:200> : <EOL> metadata [ path ] = response . body . strip ( ) <EOL> else : <EOL> logging . error ( "<STR_LIT>" , path , response . error ) <EOL> callback ( ) <EOL> if type ( paths ) in ( unicode , str ) : <EOL> paths = [ paths ] <EOL> with util . Barrier ( partial ( callback , metadata ) ) as b : <EOL> for path in paths : <EOL> logging . info ( '<STR_LIT>' % self . _GetQueryURL ( path ) ) <EOL> http_client = httpclient . AsyncHTTPClient ( ) <EOL> http_client . fetch ( self . _GetQueryURL ( path ) , partial ( _OnFetch , path , b . Callback ( ) ) , <EOL> connect_timeout = <NUM_LIT:1> , request_timeout = <NUM_LIT:5> ) <EOL> def _GetQueryURL ( self , path ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" . format ( <EOL> self . _query_ip , self . _query_version , path ) <EOL> def _FetchCommonMetadata ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> paths = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . FetchMetadata ( paths , callback ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> query_ip = Metadata . _QUERY_IP <EOL> options . parse_command_line ( ) <EOL> if options . options . mock : <EOL> from tornado import testing <EOL> port = testing . get_unused_port ( ) <EOL> class Handler ( web . RequestHandler ) : <EOL> def get ( self , path ) : <EOL> self . write ( path . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:1> ] ) <EOL> application = web . Application ( [ ( r"<STR_LIT>" , Handler ) , ] ) <EOL> application . listen ( port ) <EOL> query_ip = "<STR_LIT>" . format ( port ) <EOL> def _MetadataCallback ( metadata ) : <EOL> print metadata <EOL> ioloop . IOLoop . current ( ) . stop ( ) <EOL> Metadata ( callback = _MetadataCallback , query_ip = query_ip ) <EOL> ioloop . IOLoop . current ( ) . start ( ) <EOL> return <NUM_LIT:0> <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( ) ) </s>
