<s> import copy <EOL> import os <EOL> import re <EOL> import six <EOL> import uuid <EOL> from fuel_bootstrap import consts <EOL> from fuel_bootstrap import errors <EOL> from fuel_bootstrap import settings <EOL> CONF = settings . Configuration ( ) <EOL> class BootstrapDataBuilder ( object ) : <EOL> def __init__ ( self , data ) : <EOL> self . uuid = six . text_type ( uuid . uuid4 ( ) ) <EOL> self . container_format = consts . CONTAINER_FORMAT <EOL> self . ubuntu_release = data . get ( '<STR_LIT>' ) or consts . UBUNTU_RELEASE <EOL> self . ubuntu_repo = data . get ( '<STR_LIT>' ) <EOL> self . mos_repo = data . get ( '<STR_LIT>' ) <EOL> self . extra_repos = data . get ( '<STR_LIT>' ) or [ ] <EOL> self . http_proxy = data . get ( '<STR_LIT>' ) or CONF . http_proxy <EOL> self . https_proxy = data . get ( '<STR_LIT>' ) or CONF . https_proxy <EOL> self . direct_repo_addr = data . get ( '<STR_LIT>' ) <EOL> self . post_script_file = data . get ( '<STR_LIT>' ) or CONF . post_script_file <EOL> self . root_ssh_authorized_file = data . get ( '<STR_LIT>' ) or CONF . root_ssh_authorized_file <EOL> self . extra_dirs = data . get ( '<STR_LIT>' ) <EOL> self . include_kernel_module = data . get ( '<STR_LIT>' ) <EOL> self . blacklist_kernel_module = data . get ( '<STR_LIT>' ) <EOL> self . packages = data . get ( '<STR_LIT>' ) <EOL> self . label = data . get ( '<STR_LIT:label>' ) or self . uuid <EOL> self . extend_kopts = data . get ( '<STR_LIT>' ) <EOL> self . kernel_flavor = data . get ( '<STR_LIT>' ) <EOL> file_name = "<STR_LIT>" . format ( self . uuid , self . container_format ) <EOL> output_dir = data . get ( '<STR_LIT>' , CONF . output_dir ) <EOL> self . output = os . path . join ( output_dir , file_name ) <EOL> def build ( self ) : <EOL> return { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . _prepare_modules ( ) , <EOL> '<STR_LIT>' : self . extend_kopts , <EOL> '<STR_LIT>' : self . post_script_file , <EOL> '<STR_LIT>' : self . uuid , <EOL> '<STR_LIT>' : self . _get_extra_dirs ( ) , <EOL> '<STR_LIT>' : self . root_ssh_authorized_file , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : consts . METADATA_FILE , <EOL> '<STR_LIT>' : self . container_format <EOL> } <EOL> } , <EOL> '<STR_LIT>' : self . _get_repos ( ) , <EOL> '<STR_LIT>' : self . _get_proxy_settings ( ) , <EOL> '<STR_LIT>' : self . ubuntu_release , <EOL> '<STR_LIT>' : self . output , <EOL> '<STR_LIT>' : self . _get_packages ( ) , <EOL> '<STR_LIT>' : self . _prepare_image_data ( ) <EOL> } <EOL> def _get_extra_dirs ( self ) : <EOL> dirs = set ( ) <EOL> if self . extra_dirs : <EOL> dirs |= set ( self . extra_dirs ) <EOL> if CONF . extra_dirs : <EOL> dirs |= set ( CONF . extra_dirs ) <EOL> return list ( dirs ) <EOL> def _prepare_modules ( self ) : <EOL> modules = copy . copy ( consts . BOOTSTRAP_MODULES ) <EOL> for module in modules : <EOL> module [ '<STR_LIT>' ] = module [ '<STR_LIT>' ] . format ( uuid = self . uuid ) <EOL> return modules <EOL> def _prepare_image_data ( self ) : <EOL> image_data = copy . copy ( consts . IMAGE_DATA ) <EOL> image_data [ '<STR_LIT:/>' ] [ '<STR_LIT>' ] = image_data [ '<STR_LIT:/>' ] [ '<STR_LIT>' ] . format ( uuid = self . uuid ) <EOL> return image_data <EOL> def _get_proxy_settings ( self ) : <EOL> if self . http_proxy or self . https_proxy : <EOL> return { '<STR_LIT>' : { '<STR_LIT:http>' : self . http_proxy , <EOL> '<STR_LIT>' : self . https_proxy } , <EOL> '<STR_LIT>' : self . _get_direct_repo_addr ( ) } <EOL> return { } <EOL> def _get_direct_repo_addr ( self ) : <EOL> addrs = set ( ) <EOL> if self . direct_repo_addr : <EOL> addrs |= set ( self . direct_repo_addr ) <EOL> addrs |= set ( CONF . direct_repo_adresses ) <EOL> return list ( addrs ) <EOL> def _get_repos ( self ) : <EOL> repos = [ ] <EOL> if self . ubuntu_repo : <EOL> repos . extend ( self . _parse_ubuntu_repos ( self . ubuntu_repo ) ) <EOL> else : <EOL> repos . extend ( CONF . ubuntu_repos ) <EOL> if self . mos_repo : <EOL> repos . extend ( self . _parse_mos_repos ( self . mos_repo ) ) <EOL> else : <EOL> repos . extend ( CONF . mos_repos ) <EOL> repo_count = <NUM_LIT:0> <EOL> for repo in self . extra_repos : <EOL> repo_count += <NUM_LIT:1> <EOL> repos . append ( self . _parse_repo ( <EOL> repo , <EOL> name = "<STR_LIT>" . format ( repo_count ) ) ) <EOL> if not self . extra_repos and CONF . extra_repos : <EOL> repos . extend ( CONF . extra_repos ) <EOL> return repos <EOL> def _get_packages ( self ) : <EOL> result = set ( CONF . packages ) <EOL> result . add ( self . kernel_flavor ) <EOL> if self . packages : <EOL> result |= set ( self . packages ) <EOL> return list ( result ) <EOL> def _parse_ubuntu_repos ( self , repo ) : <EOL> uri , suite = self . _parse_not_extra_repo ( repo ) <EOL> return self . _generate_repos_from_uri ( <EOL> uri = uri , <EOL> codename = self . ubuntu_release , <EOL> name = '<STR_LIT>' , <EOL> components = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> section = '<STR_LIT>' <EOL> ) <EOL> @ classmethod <EOL> def _parse_not_extra_repo ( cls , repo ) : <EOL> regexp = r"<STR_LIT>" <EOL> match = re . match ( regexp , repo ) <EOL> if not match : <EOL> raise errors . IncorrectRepository ( <EOL> "<STR_LIT>" . <EOL> format ( repo ) <EOL> ) <EOL> return match . group ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ classmethod <EOL> def _parse_mos_repos ( cls , repo ) : <EOL> uri , suite = cls . _parse_not_extra_repo ( repo ) <EOL> result = cls . _generate_repos_from_uri ( <EOL> uri = uri , <EOL> codename = suite , <EOL> name = '<STR_LIT>' , <EOL> components = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> section = '<STR_LIT>' , <EOL> priority = '<STR_LIT>' <EOL> ) <EOL> result += cls . _generate_repos_from_uri ( <EOL> uri = uri , <EOL> codename = suite , <EOL> name = '<STR_LIT>' , <EOL> components = [ '<STR_LIT>' ] , <EOL> section = '<STR_LIT>' , <EOL> priority = '<STR_LIT>' <EOL> ) <EOL> return result <EOL> @ classmethod <EOL> def _generate_repos_from_uri ( cls , uri , codename , name , components = None , <EOL> section = None , type_ = None , priority = None ) : <EOL> if not components : <EOL> components = [ '<STR_LIT>' ] <EOL> result = [ ] <EOL> for component in components : <EOL> result . append ( { <EOL> "<STR_LIT:name>" : "<STR_LIT>" . format ( name , component ) , <EOL> "<STR_LIT:type>" : type_ or "<STR_LIT>" , <EOL> "<STR_LIT>" : uri , <EOL> "<STR_LIT>" : priority , <EOL> "<STR_LIT>" : section , <EOL> "<STR_LIT>" : "<STR_LIT>" . format ( codename , component ) <EOL> } ) <EOL> return result <EOL> @ classmethod <EOL> def _parse_repo ( cls , repo , name = None ) : <EOL> regexp = r"<STR_LIT>" r"<STR_LIT>" <EOL> match = re . match ( regexp , repo ) <EOL> if not match : <EOL> raise errors . IncorrectRepository ( "<STR_LIT>" <EOL> . format ( repo ) ) <EOL> repo_type = match . group ( '<STR_LIT:type>' ) <EOL> repo_suite = match . group ( '<STR_LIT>' ) <EOL> repo_section = match . group ( '<STR_LIT>' ) <EOL> repo_uri = match . group ( '<STR_LIT>' ) <EOL> repo_priority = match . group ( '<STR_LIT>' ) <EOL> return { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:type>' : repo_type , <EOL> '<STR_LIT>' : repo_uri , <EOL> '<STR_LIT>' : repo_priority , <EOL> '<STR_LIT>' : repo_suite , <EOL> '<STR_LIT>' : repo_section or '<STR_LIT>' } </s>
