<s> from __future__ import unicode_literals <EOL> from sprinter import lib <EOL> from sprinter . exceptions import SprinterException , FormulaException <EOL> from sprinter . external . pippuppet import Pip , PipException <EOL> import sys <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> LEGACY_MAPPINGS = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> class FeatureDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , environment , source_manifest , target_manifest , pip_install_path , formula_dict = None ) : <EOL> """<STR_LIT>""" <EOL> self . _environment = environment <EOL> self . _run_order = [ ] <EOL> self . _formula_dict = formula_dict or { } <EOL> self . _pip = Pip ( pip_install_path ) <EOL> self . _pip . delete_all_eggs ( ) <EOL> if target_manifest : <EOL> for feature in target_manifest . sections ( ) : <EOL> feature_key = self . _instantiate_feature ( feature , target_manifest , '<STR_LIT:target>' ) <EOL> if feature_key : <EOL> self . _run_order . append ( feature_key ) <EOL> if source_manifest : <EOL> for feature in source_manifest . sections ( ) : <EOL> feature_key = self . _instantiate_feature ( feature , source_manifest , '<STR_LIT:source>' ) <EOL> if feature_key : <EOL> self . _run_order . append ( feature_key ) <EOL> @ property <EOL> def run_order ( self ) : <EOL> return self . _run_order <EOL> def _instantiate_feature ( self , feature , manifest , kind ) : <EOL> if feature == "<STR_LIT>" : <EOL> return None <EOL> feature_config = manifest . get_feature_config ( feature ) <EOL> if feature_config . has ( '<STR_LIT>' ) : <EOL> key = ( feature , feature_config . get ( '<STR_LIT>' ) ) <EOL> if key not in self : <EOL> try : <EOL> formula_class = self . _get_formula_class ( feature_config . get ( '<STR_LIT>' ) ) <EOL> self [ key ] = formula_class ( self . _environment , feature , ** { kind : feature_config } ) <EOL> if self [ key ] . should_run ( ) : <EOL> return key <EOL> else : <EOL> del ( self [ key ] ) <EOL> except SprinterException : <EOL> logger . debug ( "<STR_LIT>" , exc_info = sys . exc_info ( ) ) <EOL> raise FormulaException ( <EOL> "<STR_LIT>" . format ( <EOL> feature_config . get ( '<STR_LIT>' ) , kind , feature ) ) <EOL> else : <EOL> setattr ( self [ key ] , kind , feature_config ) <EOL> else : <EOL> raise FormulaException ( <EOL> """<STR_LIT>""" . format ( feature ) <EOL> ) <EOL> return None <EOL> def _get_formula_class ( self , formula ) : <EOL> """<STR_LIT>""" <EOL> from sprinter . formula . base import FormulaBase <EOL> if formula in LEGACY_MAPPINGS : <EOL> formula = LEGACY_MAPPINGS [ formula ] <EOL> formula_class , formula_url = formula , None <EOL> if '<STR_LIT::>' in formula : <EOL> formula_class , formula_url = formula . split ( "<STR_LIT::>" , <NUM_LIT:1> ) <EOL> if formula_class not in self . _formula_dict : <EOL> try : <EOL> self . _formula_dict [ formula_class ] = lib . get_subclass_from_module ( formula_class , FormulaBase ) <EOL> except ( SprinterException , ImportError ) : <EOL> logger . info ( "<STR_LIT>" % formula_class ) <EOL> try : <EOL> self . _pip . install_egg ( formula_url or formula_class ) <EOL> try : <EOL> self . _formula_dict [ formula_class ] = lib . get_subclass_from_module ( formula_class , FormulaBase ) <EOL> except ImportError : <EOL> logger . debug ( "<STR_LIT>" , exc_info = sys . exc_info ( ) ) <EOL> raise SprinterException ( "<STR_LIT>" % formula_class ) <EOL> except PipException : <EOL> logger . error ( "<STR_LIT>" % formula_class ) <EOL> return self . _formula_dict [ formula_class ] </s>
