<s> import sys <EOL> import json <EOL> import os <EOL> import copy <EOL> import re <EOL> try : <EOL> import unittest2 as unittest <EOL> except ImportError : <EOL> import unittest <EOL> import salt . loader <EOL> from salt . utils . odict import OrderedDict <EOL> from contextlib import nested <EOL> import contextlib <EOL> import mc_states <EOL> import mc_states . tests <EOL> import mc_states . modules <EOL> import mc_states . modules . mc_utils <EOL> from mock import patch <EOL> A = os . path . abspath <EOL> J = os . path . join <EOL> D = os . path . dirname <EOL> sys . path . append ( D ( D ( mc_states . __file__ ) ) ) <EOL> import mc_states . tests . utils <EOL> import mc_states . saltapi <EOL> import mc_states . api <EOL> six = mc_states . saltapi . six <EOL> _NO_ATTR = object ( ) <EOL> _KIND = '<STR_LIT>' <EOL> re_cache = { } <EOL> class _ModuleCase ( unittest . TestCase ) : <EOL> '''<STR_LIT>''' <EOL> maxDiff = None <EOL> _context_mods = ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> kind = None <EOL> contextual = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _ ( self , fun , * args , ** kwargs ) : <EOL> func = self . salt [ fun ] <EOL> if args and kwargs : <EOL> ret = func ( * args , ** kwargs ) <EOL> elif kwargs and not self . args : <EOL> ret = func ( ** kwargs ) <EOL> elif args and not kwargs : <EOL> ret = func ( * args ) <EOL> else : <EOL> ret = func <EOL> return ret <EOL> def reset ( self ) : <EOL> for i in [ a for a in mc_states . api . _LOCAL_CACHES ] : <EOL> mc_states . api . _LOCAL_CACHES . pop ( i , { } ) <EOL> for i in [ a for a in self . dunders_ ] : <EOL> val = self . dunders_ . pop ( i ) <EOL> del val <EOL> for i in ( '<STR_LIT>' , ) + self . contextual : <EOL> k = i + '<STR_LIT:_>' <EOL> dval = getattr ( self , '<STR_LIT>' . format ( i ) , { } ) <EOL> if isinstance ( dval , dict ) : <EOL> dval = copy . deepcopy ( dval ) <EOL> setattr ( self , k , dval ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( _ModuleCase , self ) . __init__ ( * args , ** kwargs ) <EOL> self . states_dir = A ( D ( mc_states . __file__ ) ) <EOL> self . ms = A ( D ( self . states_dir ) ) <EOL> self . salt_root = A ( D ( self . ms ) ) <EOL> self . root = '<STR_LIT:/>' <EOL> self . dunders_ = { } <EOL> self . opts_ = { } <EOL> self . context_ = { } <EOL> self . pillar_ = { } <EOL> self . grains_ = { } <EOL> self . salt = { } <EOL> self . reset ( ) <EOL> def get_opts ( self ) : <EOL> opts = { '<STR_LIT:root>' : self . root , <EOL> '<STR_LIT>' : self . root , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . root , <EOL> '<STR_LIT>' : self . root , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . salt_root , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ self . salt_root ] } , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> with open ( J ( self . states_dir , '<STR_LIT>' ) ) as fic : <EOL> content = json . loads ( fic . read ( ) ) <EOL> opts . update ( content ) <EOL> opts = mc_states . modules . mc_utils . format_resolve ( opts ) <EOL> if not os . path . exists ( opts [ '<STR_LIT>' ] ) : <EOL> os . makedirs ( opts [ '<STR_LIT>' ] ) <EOL> return opts <EOL> def __ ( self , fun , * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ret = self . _ ( fun , * args , ** kwargs ) <EOL> if not args and not kwargs : <EOL> ret = ret ( ) <EOL> return ret <EOL> @ property <EOL> def copts ( self ) : <EOL> return copy . deepcopy ( self . opts_ ) <EOL> def setUp ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . root = mc_states . tests . utils . test_setup ( ) <EOL> self . reset ( ) <EOL> if not self . kind : <EOL> self . kind = _KIND <EOL> self . opts_ . update ( self . get_opts ( ) ) <EOL> for i in [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> d = self . opts_ [ i ] <EOL> if i in [ '<STR_LIT>' ] : <EOL> d = d [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if not os . path . exists ( d ) : <EOL> os . makedirs ( d ) <EOL> if not os . path . exists ( self . opts_ [ '<STR_LIT>' ] ) : <EOL> with open ( self . opts_ [ '<STR_LIT>' ] , '<STR_LIT:w>' ) as fic : <EOL> fic . write ( '<STR_LIT>' ) <EOL> self . grains_ . update ( salt . loader . grains ( self . opts_ ) ) <EOL> self . opts_ [ '<STR_LIT>' ] = self . grains_ <EOL> self . opts_ [ '<STR_LIT>' ] = self . pillar_ <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . LazyLoader ( <EOL> salt . loader . _module_dirs ( <EOL> self . opts_ , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ext_type_dirs = '<STR_LIT>' ) , <EOL> self . opts_ , <EOL> tag = '<STR_LIT>' ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . utils ( self . opts_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . minion_mods ( <EOL> self . opts_ , <EOL> context = self . context_ , <EOL> utils = self . dunders_ [ '<STR_LIT>' ] ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . roster ( self . opts_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . fileserver ( self . opts_ , { } ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . auth ( self . opts_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . log_handlers ( self . opts_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . outputters ( self . opts_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . wheels ( self . opts_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . ssh_wrapper ( <EOL> self . opts_ , <EOL> functions = self . dunders_ [ '<STR_LIT>' ] , <EOL> context = self . context_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . tops ( self . opts_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . runner ( self . opts_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . engines ( <EOL> self . opts_ , self . dunders_ [ '<STR_LIT>' ] , self . dunders_ [ '<STR_LIT>' ] ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . proxy ( <EOL> self . opts_ , self . dunders_ [ '<STR_LIT>' ] ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . returners ( <EOL> self . opts_ , self . dunders_ [ '<STR_LIT>' ] , context = self . context_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . states ( <EOL> self . opts_ , self . dunders_ [ '<STR_LIT>' ] ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . beacons ( <EOL> self . opts_ , self . dunders_ [ '<STR_LIT>' ] , context = self . context_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . search ( <EOL> self . opts_ , self . dunders_ [ '<STR_LIT>' ] ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . sdb ( <EOL> self . opts_ , self . dunders_ [ '<STR_LIT>' ] ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . queues ( self . opts_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . clouds ( self . opts_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . netapi ( self . opts_ ) <EOL> self . dunders_ [ '<STR_LIT>' ] = salt . loader . render ( <EOL> self . opts_ , <EOL> self . dunders_ [ '<STR_LIT>' ] , <EOL> states = self . dunders_ [ '<STR_LIT>' ] ) <EOL> self . salt = self . dunders_ [ self . kind ] <EOL> def tearDown ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . reset ( ) <EOL> mc_states . tests . utils . test_teardown ( ) <EOL> def patch ( self , force_load = True , kinds = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> tpatchs = OrderedDict ( ) <EOL> globs = kwargs . get ( '<STR_LIT>' , { } ) <EOL> mod_globs = { } <EOL> funcs = kwargs . get ( '<STR_LIT>' , { } ) <EOL> for module , funcsdata in funcs . items ( ) : <EOL> for fun , callback in funcsdata . items ( ) : <EOL> if '<STR_LIT:.>' in fun : <EOL> module , sfun = fun . split ( '<STR_LIT:.>' ) <EOL> mglobs = mod_globs . setdefault ( module , copy . deepcopy ( globs ) ) <EOL> mglobs [ sfun ] = callback <EOL> k = '<STR_LIT>' . format ( module , fun ) <EOL> tpatchs [ k ] = patch . dict ( self . salt , { fun : callback } ) <EOL> for opt in self . contextual : <EOL> overriden = kwargs . get ( opt , { } ) <EOL> tpatchs [ opt ] = patch . dict ( getattr ( self , opt + '<STR_LIT:_>' ) , overriden ) <EOL> if not kinds : <EOL> kinds = [ a for a in self . dunders_ ] <EOL> for kind in kinds : <EOL> mods = self . dunders_ [ kind ] <EOL> if force_load : <EOL> mods = [ a for a in self . dunders_ [ kind ] ] <EOL> filtered = kwargs . get ( '<STR_LIT>' , None ) <EOL> if filtered is None : <EOL> filtered = mods [ : ] <EOL> if not filtered : <EOL> continue <EOL> for mod in mods : <EOL> matched = False <EOL> for match in filtered : <EOL> rmatch = re_cache . get ( match ) <EOL> if not rmatch : <EOL> rmatch = re_cache [ match ] = re . compile ( match ) <EOL> if rmatch . search ( mod ) : <EOL> matched = True <EOL> break <EOL> if not matched : <EOL> continue <EOL> func = self . dunders_ [ kind ] [ mod ] <EOL> if not isinstance ( func , dict ) : <EOL> try : <EOL> _globals = func . __globals__ <EOL> except AttributeError : <EOL> continue <EOL> thismod = mod . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> globspatch = mod_globs . setdefault ( thismod , { } ) <EOL> for i in globs : <EOL> globspatch [ i ] = globs [ i ] <EOL> k = '<STR_LIT>' . format ( kind , mod ) <EOL> for opt in self . contextual : <EOL> gopt = '<STR_LIT>' . format ( opt ) <EOL> if gopt in _globals : <EOL> globspatch [ gopt ] = getattr ( self , opt + '<STR_LIT:_>' ) <EOL> tpatchs [ k ] = patch . dict ( func . __globals__ , globspatch ) <EOL> return contextlib . nested ( * tuple ( tpatchs . values ( ) ) ) <EOL> def get_private ( self , private_name , public_name = None , kind = None ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT:.>' not in private_name : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> mod , sfun = private_name . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if not kind : <EOL> kind = self . kind <EOL> func = None <EOL> if public_name : <EOL> try : <EOL> func = self . dunders_ [ kind ] [ <EOL> public_name ] . __globals__ [ sfun ] <EOL> except ( KeyError , AttributeError ) : <EOL> pass <EOL> if func is None : <EOL> for fname , f_ in six . iteritems ( self . dunders_ [ kind ] ) : <EOL> if fname . startswith ( mod + '<STR_LIT:.>' ) : <EOL> try : <EOL> func = f_ . __globals__ [ sfun ] <EOL> break <EOL> except ( KeyError , AttributeError ) : <EOL> continue <EOL> if func is None : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( private_name ) ) <EOL> return func <EOL> class GrainsCase ( _ModuleCase ) : <EOL> kind = '<STR_LIT>' <EOL> class ModuleCase ( _ModuleCase ) : <EOL> kind = '<STR_LIT>' <EOL> class RunnerCase ( _ModuleCase ) : <EOL> kind = '<STR_LIT>' </s>
