<s> """<STR_LIT>""" <EOL> import os <EOL> import tempfile <EOL> from Bcfg2 . Compat import wraps , ConfigParser <EOL> from Bcfg2 . Options import Parser , PathOption <EOL> from testsuite . common import Bcfg2TestCase <EOL> class make_config ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config_data = None ) : <EOL> self . config_data = config_data or { } <EOL> def __call__ ( self , func ) : <EOL> @ wraps ( func ) <EOL> def inner ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> cfp = ConfigParser . ConfigParser ( ) <EOL> for section , options in self . config_data . items ( ) : <EOL> cfp . add_section ( section ) <EOL> for key , val in options . items ( ) : <EOL> cfp . set ( section , key , val ) <EOL> fd , name = tempfile . mkstemp ( ) <EOL> config_file = os . fdopen ( fd , '<STR_LIT:w>' ) <EOL> cfp . write ( config_file ) <EOL> config_file . close ( ) <EOL> args = list ( args ) + [ name ] <EOL> try : <EOL> rv = func ( * args , ** kwargs ) <EOL> finally : <EOL> os . unlink ( name ) <EOL> return rv <EOL> return inner <EOL> def clean_environment ( func ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( func ) <EOL> def inner ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> envvars = { } <EOL> for opt in self . options : <EOL> if opt . env is not None : <EOL> envvars [ opt . env ] = os . environ . get ( opt . env ) <EOL> if opt . env in os . environ : <EOL> del os . environ [ opt . env ] <EOL> rv = func ( self , * args , ** kwargs ) <EOL> for name , val in envvars . items ( ) : <EOL> if val is None and name in os . environ : <EOL> del os . environ [ name ] <EOL> elif val is not None : <EOL> os . environ [ name ] = val <EOL> return rv <EOL> return inner <EOL> class OptionTestCase ( Bcfg2TestCase ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> Parser . unit_test = False <EOL> Bcfg2TestCase . setUpClass ( ) <EOL> def setUp ( self ) : <EOL> Bcfg2TestCase . setUp ( self ) <EOL> PathOption . repository = None <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> Parser . unit_test = True </s>
