<s> """<STR_LIT>""" <EOL> from atomic_reactor . constants import YUM_REPOS_DIR <EOL> from atomic_reactor . plugin import PreBuildPlugin <EOL> import os <EOL> import os . path <EOL> import requests <EOL> try : <EOL> from urlparse import unquote , urlsplit <EOL> import ConfigParser as configparser <EOL> from io import BytesIO , BytesIO as StringIO <EOL> except ImportError : <EOL> from urllib . parse import unquote , urlsplit <EOL> import configparser <EOL> from io import BytesIO , StringIO <EOL> class YumRepo ( object ) : <EOL> def __init__ ( self , repourl , dst_repos_dir = YUM_REPOS_DIR ) : <EOL> self . repourl = repourl <EOL> self . dst_repos_dir = dst_repos_dir <EOL> self . content = None <EOL> @ property <EOL> def filename ( self ) : <EOL> urlpath = unquote ( urlsplit ( self . repourl , allow_fragments = False ) . path ) <EOL> return os . path . basename ( urlpath ) <EOL> @ property <EOL> def dst_filename ( self ) : <EOL> return os . path . join ( self . dst_repos_dir , self . filename ) <EOL> def fetch ( self ) : <EOL> response = requests . get ( self . repourl ) <EOL> response . raise_for_status ( ) <EOL> self . content = response . content <EOL> def is_valid ( self ) : <EOL> with BytesIO ( self . content ) as buf : <EOL> self . config = configparser . ConfigParser ( ) <EOL> try : <EOL> try : <EOL> self . config . read_string ( self . content . decode ( '<STR_LIT>' ) ) <EOL> except AttributeError : <EOL> self . config . readfp ( buf ) <EOL> except configparser . Error : <EOL> self . log . warn ( "<STR_LIT>" , self . content ) <EOL> return False <EOL> else : <EOL> return True <EOL> def set_proxy_for_all_repos ( self , proxy_name ) : <EOL> for section in self . config . sections ( ) : <EOL> self . config . set ( section , '<STR_LIT>' , proxy_name ) <EOL> with StringIO ( ) as output : <EOL> self . config . write ( output ) <EOL> self . content = output . getvalue ( ) <EOL> class AddYumRepoByUrlPlugin ( PreBuildPlugin ) : <EOL> key = "<STR_LIT>" <EOL> is_allowed_to_fail = False <EOL> def __init__ ( self , tasker , workflow , repourls , inject_proxy = None ) : <EOL> """<STR_LIT>""" <EOL> super ( AddYumRepoByUrlPlugin , self ) . __init__ ( tasker , workflow ) <EOL> self . repourls = repourls <EOL> self . inject_proxy = inject_proxy <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . repourls : <EOL> for repourl in self . repourls : <EOL> yumrepo = YumRepo ( repourl ) <EOL> yumrepo . fetch ( ) <EOL> self . log . info ( "<STR_LIT>" , yumrepo . repourl ) <EOL> if self . inject_proxy : <EOL> if yumrepo . is_valid ( ) : <EOL> yumrepo . set_proxy_for_all_repos ( self . inject_proxy ) <EOL> self . workflow . files [ yumrepo . dst_filename ] = yumrepo . content <EOL> self . log . debug ( "<STR_LIT>" , yumrepo . dst_filename , len ( yumrepo . content ) ) </s>
