<s> __author__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" <EOL> import urllib2 <EOL> import urlparse <EOL> import time <EOL> import hashlib <EOL> import hmac <EOL> import json <EOL> import yum <EOL> import yum . config <EOL> import yum . Errors <EOL> import yum . plugins <EOL> from yum . yumRepo import YumRepository <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> requires_api_version = '<STR_LIT>' <EOL> plugin_type = yum . plugins . TYPE_CORE <EOL> CONDUIT = None <EOL> def config_hook ( conduit ) : <EOL> yum . config . RepoConf . s3_enabled = yum . config . BoolOption ( False ) <EOL> yum . config . RepoConf . key_id = yum . config . Option ( ) <EOL> yum . config . RepoConf . secret_key = yum . config . Option ( ) <EOL> def prereposetup_hook ( conduit ) : <EOL> """<STR_LIT>""" <EOL> repos = conduit . getRepos ( ) <EOL> for repo in repos . listEnabled ( ) : <EOL> if isinstance ( repo , YumRepository ) and repo . s3_enabled : <EOL> new_repo = S3Repository ( repo . id , repo . baseurl ) <EOL> new_repo . name = repo . name <EOL> new_repo . mirrorlist = repo . mirrorlist <EOL> new_repo . basecachedir = repo . basecachedir <EOL> new_repo . gpgcheck = repo . gpgcheck <EOL> new_repo . gpgkey = repo . gpgkey <EOL> new_repo . key_id = repo . key_id <EOL> new_repo . secret_key = repo . secret_key <EOL> new_repo . proxy = repo . proxy <EOL> new_repo . enablegroups = repo . enablegroups <EOL> if hasattr ( repo , '<STR_LIT>' ) : <EOL> new_repo . priority = repo . priority <EOL> if hasattr ( repo , '<STR_LIT>' ) : <EOL> new_repo . base_persistdir = repo . base_persistdir <EOL> if hasattr ( repo , '<STR_LIT>' ) : <EOL> new_repo . metadata_expire = repo . metadata_expire <EOL> if hasattr ( repo , '<STR_LIT>' ) : <EOL> new_repo . skip_if_unavailable = repo . skip_if_unavailable <EOL> repos . delete ( repo . id ) <EOL> repos . add ( new_repo ) <EOL> class S3Repository ( YumRepository ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , repoid , baseurl ) : <EOL> super ( S3Repository , self ) . __init__ ( repoid ) <EOL> self . iamrole = None <EOL> self . baseurl = baseurl <EOL> self . grabber = None <EOL> self . enable ( ) <EOL> @ property <EOL> def grabfunc ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ property <EOL> def grab ( self ) : <EOL> if not self . grabber : <EOL> self . grabber = S3Grabber ( self ) <EOL> if self . key_id and self . secret_key : <EOL> self . grabber . set_credentials ( self . key_id , self . secret_key ) <EOL> else : <EOL> self . grabber . get_role ( ) <EOL> self . grabber . get_credentials ( ) <EOL> return self . grabber <EOL> class S3Grabber ( object ) : <EOL> def __init__ ( self , repo ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( repo , basestring ) : <EOL> self . baseurl = repo <EOL> else : <EOL> if len ( repo . baseurl ) != <NUM_LIT:1> : <EOL> raise yum . plugins . PluginYumExit ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % repo . id ) <EOL> else : <EOL> self . baseurl = repo . baseurl [ <NUM_LIT:0> ] <EOL> if not self . baseurl . endswith ( '<STR_LIT:/>' ) : <EOL> self . baseurl += '<STR_LIT:/>' <EOL> def get_role ( self ) : <EOL> """<STR_LIT>""" <EOL> request = urllib2 . Request ( <EOL> urlparse . urljoin ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ) ) <EOL> response = None <EOL> try : <EOL> response = urllib2 . urlopen ( request ) <EOL> self . iamrole = ( response . read ( ) ) <EOL> finally : <EOL> if response : <EOL> response . close ( ) <EOL> def get_credentials ( self ) : <EOL> """<STR_LIT>""" <EOL> request = urllib2 . Request ( <EOL> urlparse . urljoin ( <EOL> urlparse . urljoin ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) , self . iamrole ) ) <EOL> response = None <EOL> try : <EOL> response = urllib2 . urlopen ( request ) <EOL> data = json . loads ( response . read ( ) ) <EOL> finally : <EOL> if response : <EOL> response . close ( ) <EOL> self . access_key = data [ '<STR_LIT>' ] <EOL> self . secret_key = data [ '<STR_LIT>' ] <EOL> self . token = data [ '<STR_LIT>' ] <EOL> def set_credentials ( self , access_key , secret_key ) : <EOL> self . access_key = access_key <EOL> self . secret_key = secret_key <EOL> self . token = None <EOL> def _request ( self , path ) : <EOL> url = urlparse . urljoin ( self . baseurl , urllib2 . quote ( path ) ) <EOL> request = urllib2 . Request ( url ) <EOL> if self . token : <EOL> request . add_header ( '<STR_LIT>' , self . token ) <EOL> signature = self . sign ( request ) <EOL> request . add_header ( '<STR_LIT>' , "<STR_LIT>" . format ( <EOL> self . access_key , <EOL> signature ) ) <EOL> return request <EOL> def urlgrab ( self , url , filename = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> request = self . _request ( url ) <EOL> if filename is None : <EOL> filename = request . get_selector ( ) <EOL> if filename . startswith ( '<STR_LIT:/>' ) : <EOL> filename = filename [ <NUM_LIT:1> : ] <EOL> response = None <EOL> try : <EOL> out = open ( filename , '<STR_LIT>' ) <EOL> response = urllib2 . urlopen ( request ) <EOL> buff = response . read ( <NUM_LIT> ) <EOL> while buff : <EOL> out . write ( buff ) <EOL> buff = response . read ( <NUM_LIT> ) <EOL> except urllib2 . HTTPError , e : <EOL> from urlgrabber . grabber import URLGrabError <EOL> new_e = URLGrabError ( <NUM_LIT> , '<STR_LIT>' % ( e , url ) ) <EOL> new_e . code = e . code <EOL> new_e . exception = e <EOL> new_e . url = url <EOL> raise new_e <EOL> finally : <EOL> if response : <EOL> response . close ( ) <EOL> out . close ( ) <EOL> return filename <EOL> def urlopen ( self , url , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return urllib2 . urlopen ( self . _request ( url ) ) <EOL> def urlread ( self , url , limit = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return urllib2 . urlopen ( self . _request ( url ) ) . read ( ) <EOL> def sign ( self , request , timeval = None ) : <EOL> """<STR_LIT>""" <EOL> date = time . strftime ( "<STR_LIT>" , timeval or time . gmtime ( ) ) <EOL> request . add_header ( '<STR_LIT>' , date ) <EOL> host = request . get_host ( ) <EOL> try : <EOL> pos = host . find ( "<STR_LIT>" ) <EOL> assert pos != - <NUM_LIT:1> <EOL> bucket = host [ : pos ] <EOL> except AssertionError : <EOL> raise yum . plugins . PluginYumExit ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % host ) <EOL> resource = "<STR_LIT>" % ( bucket , request . get_selector ( ) , ) <EOL> if self . token : <EOL> amz_headers = '<STR_LIT>' % self . token <EOL> else : <EOL> amz_headers = '<STR_LIT>' <EOL> sigstring = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % ( { <EOL> '<STR_LIT>' : request . get_method ( ) , <EOL> '<STR_LIT:date>' : request . headers . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : amz_headers , <EOL> '<STR_LIT>' : resource } ) <EOL> digest = hmac . new ( <EOL> str ( self . secret_key ) , <EOL> str ( sigstring ) , <EOL> hashlib . sha1 ) . digest ( ) <EOL> signature = digest . encode ( '<STR_LIT>' ) <EOL> return signature . strip ( ) </s>
