<s> import os , sys <EOL> from subprocess import PIPE , Popen <EOL> class UtilsMeta ( type ) : <EOL> _instances = { } <EOL> def __call__ ( cls , * args , ** kwargs ) : <EOL> if cls not in cls . _instances : <EOL> cls . _instances [ cls ] = super ( UtilsMeta , cls ) . __call__ ( * args , ** kwargs ) <EOL> return cls . _instances [ cls ] <EOL> class Utils ( object ) : <EOL> __metaclass__ = UtilsMeta <EOL> @ staticmethod <EOL> def env_with_default ( varname , default ) : <EOL> return os . getenv ( varname , default ) <EOL> @ staticmethod <EOL> def cmd ( command ) : <EOL> from config import Config <EOL> log = None if Config . cmd_log ( ) == None else open ( Config . cmd_log ( ) , '<STR_LIT:a>' , <NUM_LIT:0> ) <EOL> output = list ( ) <EOL> process = Popen ( args = command , stdout = PIPE , stderr = PIPE , shell = True ) <EOL> for line in process . stdout : <EOL> if log != None : <EOL> log . write ( line ) <EOL> output . append ( line ) <EOL> process . wait ( ) <EOL> if log != None : log . close ( ) <EOL> return { '<STR_LIT>' : process . returncode , '<STR_LIT>' : "<STR_LIT>" . join ( output ) , '<STR_LIT>' : process . stderr . read ( ) } <EOL> @ staticmethod <EOL> def exit_with_cmd_error ( file , error ) : <EOL> from config import Config <EOL> Config . print_help ( ) <EOL> print >> sys . stderr , "<STR_LIT>" . format ( os . path . basename ( file ) , error ) <EOL> exit ( <NUM_LIT> ) <EOL> @ staticmethod <EOL> def confirm ( message ) : <EOL> from config import Config <EOL> if Config . args ( ) . auto_accept == True : <EOL> return True <EOL> response = raw_input ( "<STR_LIT>" . format ( message ) ) <EOL> if response == "<STR_LIT:yes>" or response == "<STR_LIT:y>" : <EOL> return True <EOL> return False <EOL> @ staticmethod <EOL> def print_result_error ( LOG , message , result ) : <EOL> LOG . error ( "<STR_LIT>" . format ( message , result [ '<STR_LIT>' ] ) ) <EOL> LOG . debug ( result [ '<STR_LIT>' ] ) <EOL> LOG . debug ( result [ '<STR_LIT>' ] ) <EOL> @ staticmethod <EOL> def platform ( ) : <EOL> return sys . platform <EOL> @ staticmethod <EOL> def list_builds ( LOG , directory ) : <EOL> for name in [ name for name in os . listdir ( directory ) if os . path . isdir ( os . path . join ( directory , name ) ) ] : <EOL> print name <EOL> @ staticmethod <EOL> def list_releases ( LOG , repo_dir , master_branch ) : <EOL> result = Utils . cmd ( "<STR_LIT>" . format ( repo_dir ) ) <EOL> if result [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> releases = result [ '<STR_LIT>' ] . split ( "<STR_LIT:\n>" ) <EOL> releases . append ( master_branch ) <EOL> return releases <EOL> else : <EOL> Utils . print_result_error ( LOG , "<STR_LIT>" , result ) <EOL> return [ ] <EOL> @ staticmethod <EOL> def list_sources ( sources_dir , kind ) : <EOL> path = "<STR_LIT>" . format ( sources_dir , kind ) <EOL> for name in os . listdir ( path ) : <EOL> full_path = os . path . join ( path , name ) <EOL> git_config = "<STR_LIT>" . format ( full_path ) <EOL> if os . path . isfile ( git_config ) : <EOL> file = open ( git_config , '<STR_LIT:r>' ) <EOL> data = Utils . parse_git_config ( file . readlines ( ) ) <EOL> file . close ( ) <EOL> if '<STR_LIT>' in data : <EOL> if '<STR_LIT:url>' in data [ '<STR_LIT>' ] : <EOL> print "<STR_LIT>" . format ( <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT:url>' ] , <EOL> full_path ) <EOL> @ staticmethod <EOL> def parse_git_config ( lines ) : <EOL> response = dict ( ) <EOL> last_section = None <EOL> for line in lines : <EOL> line = line . strip ( ) <EOL> if line . startswith ( "<STR_LIT:[>" ) and line . endswith ( "<STR_LIT:]>" ) : <EOL> last_section = line [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> response [ last_section ] = dict ( ) <EOL> else : <EOL> if last_section != None : <EOL> prop , value = line . split ( "<STR_LIT>" ) <EOL> response [ last_section ] [ prop . strip ( ) ] = value . strip ( ) <EOL> return response <EOL> @ staticmethod <EOL> def ensure_sources ( LOG , repo_dir , repo_addr ) : <EOL> if os . path . isdir ( "<STR_LIT>" . format ( repo_dir ) ) : <EOL> LOG . info ( "<STR_LIT>" . format ( repo_addr ) ) <EOL> result = Utils . cmd ( "<STR_LIT>" . format ( repo_dir ) ) <EOL> if result [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> return True <EOL> else : <EOL> Utils . print_result_error ( LOG , "<STR_LIT>" , result ) <EOL> return False <EOL> else : <EOL> LOG . info ( "<STR_LIT>" . format ( repo_addr ) ) <EOL> result = Utils . cmd ( "<STR_LIT>" . format ( repo_dir , repo_addr ) ) <EOL> if result [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> return True <EOL> else : <EOL> Utils . print_result_error ( LOG , "<STR_LIT>" , result ) <EOL> return False <EOL> @ staticmethod <EOL> def exit_if_git_release_not_set ( LOG , build_dir , release , master_branch , git_repo ) : <EOL> LOG . info ( "<STR_LIT>" . format ( release ) ) <EOL> result = Utils . cmd ( "<STR_LIT>" . format ( build_dir , release ) ) <EOL> if result [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> if release == master_branch : <EOL> LOG . info ( "<STR_LIT>" . format ( release ) ) <EOL> result = Utils . cmd ( "<STR_LIT>" . format ( build_dir , release ) ) <EOL> if result [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> Utils . print_result_error ( LOG , <EOL> "<STR_LIT>" . format ( release , git_repo ) , <EOL> result ) <EOL> exit ( <NUM_LIT> ) <EOL> else : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> else : <EOL> Utils . print_result_error ( LOG , "<STR_LIT>" . format ( <EOL> release , git_repo ) , result ) <EOL> exit ( <NUM_LIT> ) <EOL> @ staticmethod <EOL> def is_docker_available ( ) : <EOL> result = Utils . cmd ( "<STR_LIT>" ) <EOL> return result [ '<STR_LIT>' ] == <NUM_LIT:0> <EOL> @ staticmethod <EOL> def is_git_available ( ) : <EOL> result = Utils . cmd ( "<STR_LIT>" ) <EOL> return result [ '<STR_LIT>' ] == <NUM_LIT:0> <EOL> @ staticmethod <EOL> def is_brew_available ( ) : <EOL> result = Utils . cmd ( "<STR_LIT>" ) <EOL> return result [ '<STR_LIT>' ] == <NUM_LIT:0> <EOL> @ staticmethod <EOL> def is_autoconf_available ( ) : <EOL> result = Utils . cmd ( "<STR_LIT>" ) <EOL> return result [ '<STR_LIT>' ] == <NUM_LIT:0> <EOL> @ staticmethod <EOL> def is_automake_available ( ) : <EOL> result = Utils . cmd ( "<STR_LIT>" ) <EOL> return result [ '<STR_LIT>' ] == <NUM_LIT:0> <EOL> @ staticmethod <EOL> def is_libtool_available ( ) : <EOL> result = Utils . cmd ( "<STR_LIT>" ) <EOL> return result [ '<STR_LIT>' ] == <NUM_LIT:0> <EOL> @ staticmethod <EOL> def is_xcode_available ( ) : <EOL> result = Utils . cmd ( "<STR_LIT>" ) <EOL> return result [ '<STR_LIT>' ] == <NUM_LIT:0> <EOL> @ staticmethod <EOL> def is_apr_available ( ) : <EOL> if Utils . is_brew_available ( ) : <EOL> result = Utils . cmd ( "<STR_LIT>" ) <EOL> return result [ '<STR_LIT>' ] == <NUM_LIT:0> <EOL> else : <EOL> return False <EOL> @ staticmethod <EOL> def is_svn_available ( ) : <EOL> result = Utils . cmd ( "<STR_LIT>" ) <EOL> if result [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> result = Utils . cmd ( "<STR_LIT>" ) <EOL> return result [ '<STR_LIT>' ] . startswith ( "<STR_LIT>" ) <EOL> else : <EOL> return False <EOL> @ staticmethod <EOL> def is_java_available ( ) : <EOL> result = Utils . cmd ( "<STR_LIT>" ) <EOL> if result [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> result = Utils . cmd ( "<STR_LIT>" ) <EOL> return "<STR_LIT>" in result [ '<STR_LIT>' ] <EOL> else : <EOL> return False <EOL> @ staticmethod <EOL> def is_docker_image ( LOG , image_name ) : <EOL> Utils . exit_if_docker_unavailable ( LOG ) <EOL> LOG . info ( "<STR_LIT>" ) <EOL> result = Utils . cmd ( "<STR_LIT>" . format ( image_name ) ) <EOL> return result [ '<STR_LIT>' ] != "<STR_LIT>" <EOL> @ staticmethod <EOL> def exit_if_docker_unavailable ( LOG ) : <EOL> if not Utils . is_docker_available ( ) : <EOL> Utils . print_result_error ( LOG , "<STR_LIT>" , result ) <EOL> exit ( <NUM_LIT> ) <EOL> @ staticmethod <EOL> def exit_if_docker_image_not_built ( LOG , templates_dir , item , image_name ) : <EOL> result = Utils . cmd ( "<STR_LIT>" . format ( templates_dir , item , image_name ) ) <EOL> if result [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> Utils . print_result_error ( LOG , "<STR_LIT>" , result ) <EOL> exit ( <NUM_LIT> ) </s>
