<s> import urllib2 <EOL> import base64 <EOL> import logging <EOL> log = logging . getLogger ( __name__ ) <EOL> class PreemptiveBasicAuthHandler ( urllib2 . BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , password_mgr = None ) : <EOL> if password_mgr is None : <EOL> password_mgr = urllib2 . HTTPPasswordMgrWithDefaultRealm ( ) <EOL> self . passwd = password_mgr <EOL> self . add_password = self . passwd . add_password <EOL> def http_request ( self , req ) : <EOL> uri = req . get_full_url ( ) <EOL> user , pw = self . passwd . find_user_password ( None , uri ) <EOL> log . debug ( '<STR_LIT>' % ( uri , user , pw ) ) <EOL> if pw is None : return req <EOL> raw = "<STR_LIT>" % ( user , pw ) <EOL> auth = '<STR_LIT>' % base64 . b64encode ( raw ) . strip ( ) <EOL> req . add_unredirected_header ( '<STR_LIT>' , auth ) <EOL> return req <EOL> def mkurlopener ( jenkinsuser , jenkinspass , jenkinsurl , proxyhost , proxyport , proxyuser , proxypass ) : <EOL> """<STR_LIT>""" <EOL> handlers = [ ] <EOL> for handler in get_jenkins_auth_handler ( jenkinsuser = jenkinsuser , jenkinspass = jenkinspass , jenkinsurl = jenkinsurl ) : <EOL> handlers . append ( handler ) <EOL> for handler in get_proxy_handler ( proxyhost , proxyport , proxyuser , proxypass ) : <EOL> handlers . append ( handler ) <EOL> opener = urllib2 . build_opener ( * handlers ) <EOL> return opener . open <EOL> def mkopener ( * handlers ) : <EOL> opener = urllib2 . build_opener ( * handlers ) <EOL> return opener . open <EOL> def get_jenkins_auth_handler ( jenkinsuser , jenkinspass , jenkinsurl ) : <EOL> """<STR_LIT>""" <EOL> for param in jenkinsuser , jenkinspass , jenkinsurl : <EOL> if param is None : <EOL> return [ ] <EOL> assert type ( jenkinsuser ) == str , "<STR_LIT>" % repr ( jenkinsuser ) <EOL> assert type ( jenkinspass ) == str , "<STR_LIT>" % repr ( jenkinspass ) <EOL> handler = PreemptiveBasicAuthHandler ( ) <EOL> handler . add_password ( None , jenkinsurl , jenkinsuser , jenkinspass ) <EOL> log . debug ( '<STR_LIT>' % ( jenkinsurl , jenkinsuser ) ) <EOL> return [ handler ] <EOL> def get_proxy_handler ( proxyhost , proxyport , proxyuser , proxypass ) : <EOL> """<STR_LIT>""" <EOL> for param in proxyhost , proxyport , proxyuser , proxypass : <EOL> if param is None : <EOL> return [ ] <EOL> assert type ( proxyport ) == int , "<STR_LIT>" % repr ( proxyport ) <EOL> assert type ( proxypass ) == str , "<STR_LIT>" % repr ( proxypass ) <EOL> assert type ( proxyuser ) == str , "<STR_LIT>" % repr ( proxyuser ) <EOL> proxy_spec = { '<STR_LIT:http>' : '<STR_LIT>' % ( proxyhost , proxyport ) , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( proxyhost , proxyport ) } <EOL> proxy_handler = urllib2 . ProxyHandler ( proxy_spec ) <EOL> proxy_auth_handler = urllib2 . HTTPBasicAuthHandler ( ) <EOL> proxy_auth_handler . add_password ( None , proxyhost , proxyuser , proxypass ) <EOL> return [ proxy_handler , proxy_auth_handler ] <EOL> class NoAuto302Handler ( urllib2 . HTTPRedirectHandler ) : <EOL> def http_error_302 ( self , req , fp , code , msg , hdrs ) : <EOL> return fp </s>
