<s> import os <EOL> import os . path <EOL> import logging <EOL> from logging import warning as warn <EOL> import collections <EOL> import yaml <EOL> from jinja2 import Environment , DictLoader <EOL> from servi . exceptions import MasterNotFound , ServiError <EOL> '''<STR_LIT>''' <EOL> SERVI_DIR = os . path . normpath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) <EOL> TMPL_DIR_SITE = os . path . normpath ( os . path . join ( SERVI_DIR , '<STR_LIT>' ) ) <EOL> BOX_DIR = os . path . abspath ( os . path . join ( SERVI_DIR , '<STR_LIT>' ) ) <EOL> MASTER_DIR = None <EOL> MANIFEST_FILE = "<STR_LIT>" <EOL> VERSION_FILE = "<STR_LIT>" <EOL> SERVIFILE = "<STR_LIT>" <EOL> SERVIFILE_GLOBAL = "<STR_LIT>" <EOL> SERVIFILE_GLOBAL_FULL = os . path . expanduser ( os . path . join ( '<STR_LIT>' , SERVIFILE_GLOBAL ) ) <EOL> TEMPLATE = '<STR_LIT>' <EOL> MASTER = '<STR_LIT>' <EOL> MISSING_HASH = '<STR_LIT>' <EOL> SERVI_IGNORE_FILES = [ ] <EOL> DIFFTOOL = '<STR_LIT>' <EOL> DEFAULT_LOG_LEVEL = logging . INFO <EOL> HOSTS = None <EOL> MAIN_USERNAME = None <EOL> MAIN_RSA_KEY_FILE = None <EOL> LOCAL_DIR = None <EOL> SITE_SUFFIX = None <EOL> LOOKUP_FAILED_MESSAGE = '<STR_LIT>' <EOL> def lookup ( ltype , arg1 ) : <EOL> if type ( ltype ) is not str or ltype . strip ( ) . lower ( ) != '<STR_LIT>' : <EOL> raise ServiError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> retval = os . environ . get ( arg1 ) <EOL> if retval is None : <EOL> retval = LOOKUP_FAILED_MESSAGE <EOL> return retval <EOL> def setup_jinja ( env = None , template_text = None ) : <EOL> if env is None : <EOL> env = Environment ( loader = DictLoader ( { SERVIFILE : template_text } ) ) <EOL> env . globals [ '<STR_LIT>' ] = lookup <EOL> return env <EOL> def set_master_dir ( set_dir_to ) : <EOL> """<STR_LIT>""" <EOL> global MASTER_DIR <EOL> MASTER_DIR = set_dir_to <EOL> def load_user_config ( ) : <EOL> """<STR_LIT>""" <EOL> global_config = { } <EOL> if not os . path . exists ( SERVIFILE_GLOBAL_FULL ) : <EOL> warn ( '<STR_LIT>' . format ( SERVIFILE_GLOBAL_FULL ) ) <EOL> else : <EOL> with open ( SERVIFILE_GLOBAL_FULL ) as f : <EOL> servi_raw = f . read ( ) <EOL> global_config = process_config ( servi_raw ) <EOL> if MASTER_DIR : <EOL> user_config = getconfig ( <EOL> SERVIFILE , TEMPLATE , MASTER , TMPL_DIR_SITE , MASTER_DIR ) <EOL> else : <EOL> warn ( '<STR_LIT>' ) <EOL> user_config = { } <EOL> combined_config = deep_update ( global_config , user_config ) <EOL> for k , v in combined_config . items ( ) : <EOL> globals ( ) [ k ] = v <EOL> return global_config , user_config , combined_config <EOL> def find_master_dir ( start_dir , fail_ok = False ) : <EOL> """<STR_LIT>""" <EOL> master_dir = find_ancestor_servifile ( start_dir ) <EOL> if not master_dir : <EOL> if not fail_ok : <EOL> raise MasterNotFound ( ) <EOL> else : <EOL> return None <EOL> else : <EOL> return os . path . abspath ( master_dir ) <EOL> def find_ancestor_servifile ( starting_dir ) : <EOL> return find_ancestor_with ( starting_dir , SERVIFILE ) <EOL> def find_ancestor_with ( starting_dir , target ) : <EOL> """<STR_LIT>""" <EOL> cur_dir = os . path . abspath ( starting_dir ) <EOL> while cur_dir != '<STR_LIT:/>' : <EOL> if os . path . exists ( os . path . join ( cur_dir , target ) ) : <EOL> return cur_dir <EOL> cur_dir = os . path . abspath ( <EOL> os . path . normpath ( os . path . join ( cur_dir , '<STR_LIT:..>' ) ) ) <EOL> return None <EOL> def servi_file_exists_in ( path ) : <EOL> return os . path . exists ( os . path . join ( path , SERVIFILE ) ) <EOL> def global_servi_file_exists ( ) : <EOL> return os . path . exists ( SERVIFILE_GLOBAL_FULL ) <EOL> """<STR_LIT>""" <EOL> def pathfor ( fname , source , template , master , template_dir , master_dir ) : <EOL> assert source in [ template , master ] <EOL> if source == template : <EOL> path = os . path . normpath ( os . path . join ( template_dir , fname ) ) <EOL> else : <EOL> path = os . path . normpath ( os . path . join ( master_dir , fname ) ) <EOL> return path <EOL> def getconfig ( fname , template , master , template_dir , master_dir ) : <EOL> with open ( pathfor ( fname , master , <EOL> template , master , template_dir , master_dir ) ) as f : <EOL> servi_raw = f . read ( ) <EOL> return process_config ( servi_raw ) <EOL> def process_config ( raw_text ) : <EOL> env = setup_jinja ( env = None , template_text = raw_text ) <EOL> tmpl = env . get_template ( SERVIFILE ) <EOL> rendered = tmpl . render ( ) <EOL> data = yaml . load ( rendered ) <EOL> return data <EOL> def deep_update ( orig_dict , new_dict ) : <EOL> for key , val in new_dict . items ( ) : <EOL> if isinstance ( val , collections . Mapping ) : <EOL> tmp = deep_update ( orig_dict . get ( key , { } ) , val ) <EOL> orig_dict [ key ] = tmp <EOL> else : <EOL> orig_dict [ key ] = new_dict [ key ] <EOL> return orig_dict </s>
