<s> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> import os <EOL> from kokki . environment import Environment <EOL> from kokki . exceptions import Fail <EOL> from kokki . system import System <EOL> from kokki . utils import AttributeDictionary <EOL> class Cookbook ( object ) : <EOL> def __init__ ( self , name , path , config = None ) : <EOL> self . name = name <EOL> self . path = path <EOL> self . _meta = None <EOL> self . _library = None <EOL> @ property <EOL> def config ( self ) : <EOL> return self . meta . get ( '<STR_LIT>' , { } ) <EOL> @ property <EOL> def loader ( self ) : <EOL> return self . meta . get ( '<STR_LIT>' , lambda kit : None ) <EOL> @ property <EOL> def meta ( self ) : <EOL> if self . _meta is None : <EOL> metapath = os . path . join ( self . path , "<STR_LIT>" ) <EOL> with open ( metapath , "<STR_LIT:rb>" ) as fp : <EOL> source = fp . read ( ) <EOL> meta = { '<STR_LIT>' : System . get_instance ( ) } <EOL> exec compile ( source , metapath , "<STR_LIT>" ) in meta <EOL> self . _meta = meta <EOL> return self . _meta <EOL> @ property <EOL> def library ( self ) : <EOL> if self . _library is None : <EOL> libpath = os . path . join ( self . path , "<STR_LIT>" ) <EOL> globs = { } <EOL> if os . path . exists ( libpath ) : <EOL> for f in sorted ( os . listdir ( libpath ) ) : <EOL> if not f . endswith ( '<STR_LIT>' ) : <EOL> continue <EOL> path = os . path . join ( libpath , f ) <EOL> with open ( path , "<STR_LIT:rb>" ) as fp : <EOL> source = fp . read ( ) <EOL> exec compile ( source , libpath , "<STR_LIT>" ) in globs <EOL> self . _library = AttributeDictionary ( globs ) <EOL> return self . _library <EOL> def get_recipe ( self , name ) : <EOL> path = os . path . join ( self . path , "<STR_LIT>" , name + "<STR_LIT>" ) <EOL> if not os . path . exists ( path ) : <EOL> raise Fail ( "<STR_LIT>" % ( name , self . name ) ) <EOL> with open ( path , "<STR_LIT:rb>" ) as fp : <EOL> return fp . read ( ) <EOL> def __getattr__ ( self , name ) : <EOL> return self . library [ name ] <EOL> @ classmethod <EOL> def load_from_path ( cls , name , path ) : <EOL> return cls ( name , path ) <EOL> def __repr__ ( self ) : <EOL> return str ( self ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % self . name <EOL> class Kitchen ( Environment ) : <EOL> def __init__ ( self ) : <EOL> super ( Kitchen , self ) . __init__ ( ) <EOL> self . included_recipes_order = [ ] <EOL> self . included_recipes = { } <EOL> self . sourced_recipes = set ( ) <EOL> self . cookbooks = AttributeDictionary ( ) <EOL> self . cookbook_paths = [ ] <EOL> self . running = False <EOL> def add_cookbook_path ( self , * args ) : <EOL> for path in args : <EOL> origpath = path <EOL> if "<STR_LIT:.>" in path and not os . path . exists ( path ) : <EOL> pkg = __import__ ( path , { } , { } , path ) <EOL> path = os . path . dirname ( os . path . abspath ( pkg . __file__ ) ) <EOL> self . cookbook_paths . append ( ( origpath , os . path . abspath ( path ) ) ) <EOL> def register_cookbook ( self , cb ) : <EOL> self . update_config ( dict ( ( k , v . get ( '<STR_LIT:default>' ) ) for k , v in cb . config . items ( ) ) , False ) <EOL> self . cookbooks [ cb . name ] = cb <EOL> def load_cookbook ( self , * args , ** kwargs ) : <EOL> for name in args : <EOL> cb = None <EOL> for origpath , path in reversed ( self . cookbook_paths ) : <EOL> fullpath = os . path . join ( path , name ) <EOL> if not os . path . exists ( fullpath ) : <EOL> continue <EOL> cb = Cookbook . load_from_path ( name , fullpath ) <EOL> if not cb : <EOL> raise ImportError ( "<STR_LIT>" % name ) <EOL> self . register_cookbook ( cb ) <EOL> def include_recipe ( self , * args ) : <EOL> for name in args : <EOL> if name in self . included_recipes : <EOL> continue <EOL> self . included_recipes_order . append ( name ) <EOL> try : <EOL> cookbook , recipe = name . split ( '<STR_LIT:.>' ) <EOL> except ValueError : <EOL> cookbook , recipe = name , "<STR_LIT:default>" <EOL> try : <EOL> cb = self . cookbooks [ cookbook ] <EOL> except KeyError : <EOL> self . load_cookbook ( cookbook ) <EOL> cb = self . cookbooks [ cookbook ] <EOL> self . included_recipes [ name ] = ( cb , recipe ) <EOL> if self . running : <EOL> self . source_recipe ( cb , recipe ) <EOL> def source_recipe ( self , cookbook , recipe ) : <EOL> name = "<STR_LIT>" % ( cookbook . name , recipe ) <EOL> if name in self . sourced_recipes : <EOL> return <EOL> self . sourced_recipes . add ( name ) <EOL> cookbook . loader ( self ) <EOL> rc = cookbook . get_recipe ( recipe ) <EOL> globs = { '<STR_LIT>' : self } <EOL> with self : <EOL> exec compile ( rc , name , '<STR_LIT>' ) in globs <EOL> def prerun ( self ) : <EOL> for name in self . included_recipes_order : <EOL> cookbook , recipe = self . included_recipes [ name ] <EOL> self . source_recipe ( cookbook , recipe ) <EOL> def run ( self ) : <EOL> self . running = True <EOL> self . prerun ( ) <EOL> super ( Kitchen , self ) . run ( ) <EOL> self . running = False <EOL> def __getstate__ ( self ) : <EOL> state = super ( Kitchen , self ) . __getstate__ ( ) <EOL> state . update ( <EOL> cookbook_paths = [ x [ <NUM_LIT:0> ] for x in self . cookbook_paths ] , <EOL> included_recipes = self . included_recipes_order , <EOL> ) <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> super ( Kitchen , self ) . __setstate__ ( state ) <EOL> for path in state [ '<STR_LIT>' ] : <EOL> self . add_cookbook_path ( path ) <EOL> for recipe in state [ '<STR_LIT>' ] : <EOL> self . include_recipe ( recipe ) </s>
