<s> import json <EOL> import sys <EOL> from twisted . internet import reactor , defer <EOL> from twisted . web . client import getPage , HTTPClientFactory <EOL> try : <EOL> from twisted . internet import ssl <EOL> except ImportError : <EOL> ssl = None <EOL> class Jar : <EOL> def __init__ ( self , name_long , name_short , url ) : <EOL> self . name_long = list ( name_long ) <EOL> self . name_short = list ( name_short ) <EOL> self . url = str ( url ) <EOL> for i , l in enumerate ( self . name_long ) : <EOL> l = l . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:->' ) . lower ( ) <EOL> if i > len ( self . name_short ) : <EOL> self . name_short . append ( l ) <EOL> elif self . name_short [ i ] is None : <EOL> self . name_short [ i ] = l <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT:->' . join ( self . name_short ) <EOL> class JarProvider : <EOL> major = None <EOL> def __init__ ( self , deferred ) : <EOL> self . deferred = deferred <EOL> self . response = [ ] <EOL> self . work ( ) <EOL> def get ( self , url , callback ) : <EOL> d = getPage ( str ( url ) ) <EOL> d . addCallback ( callback ) <EOL> d . addErrback ( self . error ) <EOL> return d <EOL> def add ( self , * a , ** k ) : <EOL> self . response . append ( Jar ( * a , ** k ) ) <EOL> def commit ( self , d = None ) : <EOL> self . deferred . callback ( self . response ) <EOL> def error ( self , d = None ) : <EOL> self . deferred . errback ( d ) <EOL> def work ( self ) : <EOL> raise NotImplementedError <EOL> class JenkinsJarProvider ( JarProvider ) : <EOL> base = None <EOL> project = None <EOL> name = None <EOL> def work ( self ) : <EOL> self . get ( '<STR_LIT>' . format ( self . base , self . project ) , self . handle_data ) <EOL> def handle_data ( self , data ) : <EOL> data = json . loads ( data ) <EOL> url = '<STR_LIT>' . format ( self . base , self . project , data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> self . add ( ( self . name , '<STR_LIT>' ) , ( None , None ) , url ) <EOL> self . commit ( ) <EOL> modules = [ ] <EOL> for m in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> try : <EOL> name = "<STR_LIT>" . format ( m ) <EOL> __import__ ( name ) <EOL> modules . append ( sys . modules [ name ] ) <EOL> except ImportError : <EOL> pass <EOL> def get_raw ( ) : <EOL> d_results = defer . Deferred ( ) <EOL> dd = [ defer . succeed ( [ ] ) ] <EOL> for mod in modules : <EOL> d = defer . Deferred ( ) <EOL> mod . ref ( d ) <EOL> dd . append ( d ) <EOL> dd = defer . DeferredList ( dd , consumeErrors = True ) <EOL> def callback2 ( raw ) : <EOL> results = [ ] <EOL> for ok , data in raw : <EOL> if ok : <EOL> results . extend ( data ) <EOL> else : <EOL> print "<STR_LIT>" % data . value <EOL> d_results . callback ( results ) <EOL> dd . addCallback ( callback2 ) <EOL> return d_results <EOL> def jar_list ( ) : <EOL> d_result = defer . Deferred ( ) <EOL> def got_results ( results ) : <EOL> listing = "<STR_LIT>" <EOL> o = [ ] <EOL> m = <NUM_LIT:0> <EOL> for r in results : <EOL> left = '<STR_LIT:->' . join ( r . name_short ) <EOL> right = '<STR_LIT:U+0020>' . join ( r . name_long ) <EOL> m = max ( m , len ( left ) ) <EOL> o . append ( ( left , right ) ) <EOL> for left , right in sorted ( o ) : <EOL> listing += "<STR_LIT>" % ( left . ljust ( m ) , right ) <EOL> d_result . callback ( listing . rstrip ( ) ) <EOL> d = get_raw ( ) <EOL> d . addCallbacks ( got_results , d_result . errback ) <EOL> return d_result <EOL> def jar_get ( name ) : <EOL> d_result = defer . Deferred ( ) <EOL> def got_data ( factory , data ) : <EOL> filename = factory . path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> dis = factory . response_headers . get ( '<STR_LIT>' , None ) <EOL> if dis : <EOL> dis = dis [ <NUM_LIT:0> ] . split ( '<STR_LIT:;>' ) <EOL> if dis [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> for param in dis [ <NUM_LIT:1> : ] : <EOL> key , value = param . strip ( ) . split ( '<STR_LIT:=>' ) <EOL> if key == '<STR_LIT:filename>' : <EOL> filename = value . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> d_result . callback ( ( filename , data ) ) <EOL> def got_results ( results ) : <EOL> for r in results : <EOL> if name == '<STR_LIT:->' . join ( r . name_short ) : <EOL> factory = HTTPClientFactory ( r . url ) <EOL> if factory . scheme == '<STR_LIT>' : <EOL> if ssl : <EOL> reactor . connectSSL ( factory . host , factory . port , factory , ssl . ClientContextFactory ( ) ) <EOL> else : <EOL> d_result . errback ( Exception ( "<STR_LIT>" . format ( name ) ) ) <EOL> else : <EOL> reactor . connectTCP ( factory . host , factory . port , factory ) <EOL> factory . deferred . addCallback ( lambda d : got_data ( factory , d ) ) <EOL> factory . deferred . addErrback ( d_result . errback ) <EOL> return <EOL> d_result . errback ( Exception ( "<STR_LIT>" . format ( name ) ) ) <EOL> d = get_raw ( ) <EOL> d . addCallbacks ( got_results , d_result . errback ) <EOL> return d_result </s>
