<s> import ConfigParser <EOL> import os <EOL> import subprocess <EOL> from string import Template <EOL> from minos_config import Log <EOL> MINOS_ROOT = os . getenv ( "<STR_LIT>" ) <EOL> ENV_PIP = os . getenv ( "<STR_LIT>" ) <EOL> BUILD_INFO_FILE = os . getenv ( "<STR_LIT>" ) <EOL> BUILD_OFFLINE_REQUIREMENTS_FILE = os . getenv ( "<STR_LIT>" ) <EOL> def execute_command ( cmd , log_message = "<STR_LIT>" , error_message = "<STR_LIT>" ) : <EOL> if log_message : <EOL> Log . print_info ( log_message ) <EOL> try : <EOL> subprocess . check_call ( cmd ) <EOL> except BaseException , e : <EOL> Log . print_critical ( '<STR_LIT>' % error_message if error_message else str ( e ) ) <EOL> def check_command_output ( cmd , error_message = "<STR_LIT>" , skip_error = False ) : <EOL> try : <EOL> out = subprocess . check_output ( cmd , shell = True , stderr = subprocess . STDOUT ) <EOL> except BaseException , e : <EOL> if skip_error : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> Log . print_critical ( '<STR_LIT>' % error_message if error_message else str ( e ) ) <EOL> return <NUM_LIT:1> <EOL> def get_command_variable ( cmd ) : <EOL> child = subprocess . Popen ( cmd , shell = True , stdout = subprocess . PIPE ) <EOL> out = child . communicate ( ) <EOL> return out [ <NUM_LIT:0> ] . strip ( ) <EOL> def get_process_running_pid ( pid_file ) : <EOL> try : <EOL> with open ( pid_file ) as fp : <EOL> return int ( fp . readline ( ) ) <EOL> except ValueError , e : <EOL> Log . print_critical ( "<STR_LIT>" % pid_file ) <EOL> def check_process_is_running ( pid_file ) : <EOL> if not os . path . exists ( pid_file ) : <EOL> return False <EOL> process_pid = get_process_running_pid ( pid_file ) <EOL> try : <EOL> os . kill ( process_pid , <NUM_LIT:0> ) <EOL> return True <EOL> except OSError : <EOL> return False <EOL> def exec_daemon_script ( dest_path , script , * extra_para ) : <EOL> os . chdir ( dest_path ) <EOL> cmd = [ "<STR_LIT:%s>" % script ] <EOL> cmd . extend ( list ( extra_para ) ) <EOL> execute_command ( cmd ) <EOL> os . chdir ( MINOS_ROOT ) <EOL> def start_daemon_process ( process_name , pid_file , dest_path , script , * extra_para ) : <EOL> if check_process_is_running ( pid_file ) : <EOL> Log . print_warning ( "<STR_LIT>" % process_name ) <EOL> return <EOL> Log . print_info ( "<STR_LIT>" % process_name ) <EOL> exec_daemon_script ( dest_path , script , * extra_para ) <EOL> Log . print_success ( "<STR_LIT>" % process_name ) <EOL> def stop_daemon_process ( process_name , pid_file , dest_path , script ) : <EOL> if not check_process_is_running ( pid_file ) : <EOL> Log . print_warning ( "<STR_LIT>" % process_name ) <EOL> return <EOL> Log . print_info ( "<STR_LIT>" % process_name ) <EOL> exec_daemon_script ( dest_path , script , str ( get_process_running_pid ( pid_file ) ) ) <EOL> Log . print_success ( "<STR_LIT>" % process_name ) <EOL> def generate_config_file ( template_file , dest_file , config_dict ) : <EOL> config_template = Template ( open ( template_file ) . read ( ) ) <EOL> config_file = config_template . safe_substitute ( config_dict ) <EOL> with open ( dest_file , '<STR_LIT:w>' ) as output : <EOL> output . write ( config_file ) <EOL> def output_build_info ( component , info_key , info_val ) : <EOL> build_info_parser = ConfigParser . SafeConfigParser ( ) <EOL> build_info_parser . read ( [ BUILD_INFO_FILE ] ) <EOL> if not build_info_parser . has_section ( component ) : <EOL> build_info_parser . add_section ( component ) <EOL> build_info_parser . set ( component , info_key , str ( info_val ) ) <EOL> with open ( BUILD_INFO_FILE , '<STR_LIT:wb>' ) as build_info : <EOL> build_info_parser . write ( build_info ) <EOL> def get_build_info_option ( component , option ) : <EOL> build_info_parser = ConfigParser . SafeConfigParser ( ) <EOL> build_info_parser . read ( [ BUILD_INFO_FILE ] ) <EOL> if build_info_parser . has_option ( component , option ) : <EOL> return build_info_parser . get ( component , option ) <EOL> return None <EOL> def check_module_installed ( module ) : <EOL> try : <EOL> __import__ ( module ) <EOL> except ImportError : <EOL> return <NUM_LIT:0> <EOL> return <NUM_LIT:1> <EOL> def pip_install_offline ( offline_package_dir ) : <EOL> cmd = [ ENV_PIP , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> offline_package_dir , "<STR_LIT>" , BUILD_OFFLINE_REQUIREMENTS_FILE ] <EOL> execute_command ( cmd ) <EOL> def pip_install ( module , module_version ) : <EOL> log_message = "<STR_LIT>" % module <EOL> cmd = [ ENV_PIP , "<STR_LIT>" , "<STR_LIT>" % ( module , module_version ) ] <EOL> execute_command ( cmd , log_message = log_message ) <EOL> def check_and_install_modules ( modules_list ) : <EOL> for module_key , module_val , module_version in modules_list : <EOL> if not check_module_installed ( module_key ) : <EOL> pip_install ( module_val , module_version ) </s>
