<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import logging <EOL> import os <EOL> import re <EOL> import sys <EOL> from io import StringIO <EOL> from six . moves import configparser <EOL> from six import string_types <EOL> import requests <EOL> import sprinter . lib as lib <EOL> from sprinter . next . compat import create_configparser <EOL> from sprinter . lib . dependencytree import DependencyTree , DependencyTreeException <EOL> from . featureconfig import FeatureConfig <EOL> from . inputs import Inputs <EOL> CONFIG_RESERVED = [ '<STR_LIT:source>' , '<STR_LIT>' ] <EOL> FEATURE_RESERVED = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> NAMESPACE_REGEX = re . compile ( '<STR_LIT>' ) <EOL> MANIFEST_NULL_KEY = object ( ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> def load_manifest ( raw_manifest , namespace = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( raw_manifest , configparser . RawConfigParser ) : <EOL> return Manifest ( raw_manifest ) <EOL> manifest = create_configparser ( ) <EOL> if not manifest . has_section ( '<STR_LIT>' ) : <EOL> manifest . add_section ( '<STR_LIT>' ) <EOL> _load_manifest_interpret_source ( manifest , <EOL> raw_manifest , <EOL> ** kwargs ) <EOL> return Manifest ( manifest , namespace = namespace ) <EOL> def _load_manifest_interpret_source ( manifest , source , username = None , password = None , verify_certificate = True , do_inherit = True ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if isinstance ( source , string_types ) : <EOL> if source . startswith ( "<STR_LIT:http>" ) : <EOL> _load_manifest_from_url ( manifest , source , <EOL> verify_certificate = verify_certificate , <EOL> username = username , password = password ) <EOL> else : <EOL> _load_manifest_from_file ( manifest , source ) <EOL> if not manifest . has_option ( '<STR_LIT>' , '<STR_LIT:source>' ) : <EOL> manifest . set ( '<STR_LIT>' , '<STR_LIT:source>' , str ( source ) ) <EOL> else : <EOL> manifest . readfp ( source ) <EOL> if manifest . has_option ( '<STR_LIT>' , '<STR_LIT>' ) and do_inherit : <EOL> parent_manifest = configparser . RawConfigParser ( ) <EOL> _load_manifest_interpret_source ( parent_manifest , <EOL> manifest . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> username = username , <EOL> password = password , <EOL> verify_certificate = verify_certificate ) <EOL> for s in parent_manifest . sections ( ) : <EOL> for k , v in parent_manifest . items ( s ) : <EOL> if not manifest . has_option ( s , k ) : <EOL> manifest . set ( s , k , v ) <EOL> except configparser . Error : <EOL> logger . debug ( "<STR_LIT>" , exc_info = True ) <EOL> error_message = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> raise ManifestException ( "<STR_LIT>" . format ( error_message ) ) <EOL> def _load_manifest_from_url ( manifest , url , verify_certificate = True , username = None , password = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if username and password : <EOL> manifest_file_handler = StringIO ( lib . authenticated_get ( username , password , url , <EOL> verify = verify_certificate ) . decode ( "<STR_LIT:utf-8>" ) ) <EOL> else : <EOL> manifest_file_handler = StringIO ( lib . cleaned_request ( <EOL> '<STR_LIT>' , url , verify = verify_certificate <EOL> ) . text ) <EOL> manifest . readfp ( manifest_file_handler ) <EOL> except requests . exceptions . RequestException : <EOL> logger . debug ( "<STR_LIT>" , exc_info = True ) <EOL> error_message = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> raise ManifestException ( "<STR_LIT>" . format ( url , str ( error_message ) ) ) <EOL> def _load_manifest_from_file ( manifest , path ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . abspath ( os . path . expanduser ( path ) ) <EOL> if not os . path . exists ( path ) : <EOL> raise ManifestException ( "<STR_LIT>" . format ( path ) ) <EOL> manifest . read ( path ) <EOL> if not manifest . has_option ( '<STR_LIT>' , '<STR_LIT:source>' ) : <EOL> manifest . set ( '<STR_LIT>' , '<STR_LIT:source>' , str ( path ) ) <EOL> class ManifestException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class Manifest ( object ) : <EOL> """<STR_LIT>""" <EOL> dtree = None <EOL> additional_context_variables = { } <EOL> def __init__ ( self , raw_manifest , namespace = None ) : <EOL> self . manifest = raw_manifest <EOL> if not self . manifest . has_section ( '<STR_LIT>' ) : <EOL> self . manifest . add_section ( '<STR_LIT>' ) <EOL> self . inputs = self . __setup_inputs ( ) <EOL> self . namespace = namespace or self . __parse_namespace ( ) <EOL> self . dtree = self . __generate_dependency_tree ( ) <EOL> def formula_sections ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . dtree is not None : <EOL> return self . dtree . order <EOL> else : <EOL> return [ s for s in self . manifest . sections ( ) if s != "<STR_LIT>" ] <EOL> def source ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get ( '<STR_LIT>' , '<STR_LIT:source>' ) if self . has_option ( '<STR_LIT>' , '<STR_LIT:source>' ) else None <EOL> def set_source ( self , source ) : <EOL> """<STR_LIT>""" <EOL> self . set ( '<STR_LIT>' , '<STR_LIT:source>' , source ) <EOL> def is_affirmative ( self , section , option ) : <EOL> """<STR_LIT>""" <EOL> return self . has_option ( section , option ) and lib . is_affirmative ( self . get ( section , option ) ) <EOL> def set_input ( self , key , value ) : <EOL> if self . inputs . is_input ( key ) : <EOL> self . inputs . set_input ( key , value ) <EOL> self . set ( '<STR_LIT>' , key , value ) <EOL> def write ( self , file_handle ) : <EOL> """<STR_LIT>""" <EOL> for k , v in self . inputs . write_values ( ) . items ( ) : <EOL> self . set ( '<STR_LIT>' , k , v ) <EOL> self . set ( '<STR_LIT>' , '<STR_LIT>' , self . namespace ) <EOL> self . manifest . write ( file_handle ) <EOL> def grab_inputs ( self , force = False ) : <EOL> self . inputs . prompt_unset_inputs ( force = force ) <EOL> def get_feature_config ( self , feature_name ) : <EOL> """<STR_LIT>""" <EOL> return FeatureConfig ( self , feature_name ) <EOL> def get_context_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> context_dict = { } <EOL> for s in self . sections ( ) : <EOL> for k , v in self . manifest . items ( s ) : <EOL> context_dict [ "<STR_LIT>" % ( s , k ) ] = v <EOL> for k , v in self . inputs . values ( ) . items ( ) : <EOL> context_dict [ "<STR_LIT>" . format ( k ) ] = v <EOL> context_dict . update ( self . additional_context_variables . items ( ) ) <EOL> context_dict . update ( dict ( [ ( "<STR_LIT>" % k , re . escape ( str ( v ) or "<STR_LIT>" ) ) for k , v in context_dict . items ( ) ] ) ) <EOL> return context_dict <EOL> def add_additional_context ( self , additional_context ) : <EOL> """<STR_LIT>""" <EOL> self . additional_context_variables . update ( additional_context ) <EOL> def get ( self , section , key , default = MANIFEST_NULL_KEY ) : <EOL> """<STR_LIT>""" <EOL> if not self . manifest . has_option ( section , key ) and default is not MANIFEST_NULL_KEY : <EOL> return default <EOL> return self . manifest . get ( section , key ) <EOL> def __parse_namespace ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . manifest . has_option ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return self . manifest . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif self . manifest . has_option ( '<STR_LIT>' , '<STR_LIT:source>' ) : <EOL> return NAMESPACE_REGEX . search ( self . manifest . get ( '<STR_LIT>' , '<STR_LIT:source>' ) ) . groups ( ) [ <NUM_LIT:0> ] <EOL> else : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> return None <EOL> def __generate_dependency_tree ( self ) : <EOL> """<STR_LIT>""" <EOL> dependency_dict = { } <EOL> for s in self . manifest . sections ( ) : <EOL> if s != "<STR_LIT>" : <EOL> if self . manifest . has_option ( s , '<STR_LIT>' ) : <EOL> dependency_list = [ d . strip ( ) for d in re . split ( '<STR_LIT>' , self . manifest . get ( s , '<STR_LIT>' ) ) ] <EOL> dependency_dict [ s ] = dependency_list <EOL> else : <EOL> dependency_dict [ s ] = [ ] <EOL> try : <EOL> return DependencyTree ( dependency_dict ) <EOL> except DependencyTreeException : <EOL> dte = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> raise ManifestException ( "<STR_LIT>" % str ( dte ) ) <EOL> def __substitute_objects ( self , value , context_dict ) : <EOL> """<STR_LIT>""" <EOL> if type ( value ) == dict : <EOL> return dict ( [ ( k , self . __substitute_objects ( v , context_dict ) ) for k , v in value . items ( ) ] ) <EOL> elif type ( value ) == str : <EOL> try : <EOL> return value % context_dict <EOL> except KeyError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> logger . warn ( "<STR_LIT>" % ( value , e ) ) <EOL> return value <EOL> else : <EOL> return value <EOL> def __setup_inputs ( self ) : <EOL> """<STR_LIT>""" <EOL> input_object = Inputs ( ) <EOL> for s in self . manifest . sections ( ) : <EOL> if self . has_option ( s , '<STR_LIT>' ) : <EOL> input_object . add_inputs_from_inputstring ( self . get ( s , '<STR_LIT>' ) ) <EOL> for k , v in self . items ( '<STR_LIT>' ) : <EOL> if input_object . is_input ( s ) : <EOL> input_object . set_input ( k , v ) <EOL> return input_object <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , Manifest ) : <EOL> return False <EOL> for s in self . manifest . sections ( ) : <EOL> if s not in other . manifest . sections ( ) : <EOL> return False <EOL> for option in self . manifest . options ( s ) : <EOL> if ( not other . manifest . has_option ( s , option ) or <EOL> self . manifest . get ( s , option ) != other . manifest . get ( s , option ) ) : <EOL> return False <EOL> return True <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . manifest , name ) </s>
