<s> """<STR_LIT>""" <EOL> import datetime <EOL> import sys <EOL> import subprocess <EOL> import xmlrpclib <EOL> import tempfile <EOL> import shutil <EOL> import os , os . path <EOL> import time <EOL> import traceback <EOL> from optparse import OptionParser <EOL> import pip <EOL> DEFAULT_CACHE_DIR = '<STR_LIT>' <EOL> def guess_cache_dir ( dirname ) : <EOL> parent = os . environ . get ( '<STR_LIT>' , DEFAULT_CACHE_DIR ) <EOL> parent = os . path . expanduser ( parent ) <EOL> result = os . path . join ( parent , dirname ) <EOL> return result <EOL> def _run_command ( command_list , cwd = None , variables = None ) : <EOL> print command_list <EOL> environment = os . environ . copy ( ) <EOL> environment [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> environment [ '<STR_LIT>' ] = "<STR_LIT>" % ( os . getcwd ( ) , environment [ '<STR_LIT>' ] ) <EOL> if variables : <EOL> x = [ ] <EOL> for cmd in command_list : <EOL> x . append ( cmd ) <EOL> command_list = x <EOL> try : <EOL> p = subprocess . Popen ( command_list , shell = False , cwd = cwd , <EOL> stdout = subprocess . PIPE , stderr = subprocess . PIPE , <EOL> env = environment ) <EOL> out , err = p . communicate ( ) <EOL> ret = p . returncode <EOL> except : <EOL> out = '<STR_LIT>' <EOL> err = traceback . format_exc ( ) <EOL> ret = - <NUM_LIT:1> <EOL> return ( ret , out , err ) <EOL> class Context ( object ) : <EOL> def __init__ ( self ) : <EOL> self . history = [ ] <EOL> self . start_time = self . end_time = None <EOL> self . build_dir = None <EOL> def initialize ( self ) : <EOL> self . start_time = time . time ( ) <EOL> def finish ( self ) : <EOL> self . end_time = time . time ( ) <EOL> def start_command ( self , command ) : <EOL> if self . build_dir : <EOL> os . chdir ( self . build_dir ) <EOL> def end_command ( self , command ) : <EOL> self . history . append ( command ) <EOL> def update_client_info ( self , info ) : <EOL> info [ '<STR_LIT>' ] = self . end_time - self . start_time <EOL> class CurrentDirectoryContext ( Context ) : <EOL> def __init__ ( self , cleanup = True ) : <EOL> Context . __init__ ( self ) <EOL> self . cleanup = cleanup <EOL> def initialize ( self ) : <EOL> Context . initialize ( self ) <EOL> print "<STR_LIT>" % os . getcwd ( ) <EOL> class TempDirectoryContext ( Context ) : <EOL> def __init__ ( self , cleanup = True ) : <EOL> Context . __init__ ( self ) <EOL> self . cleanup = cleanup <EOL> def initialize ( self ) : <EOL> Context . initialize ( self ) <EOL> self . tempdir = tempfile . mkdtemp ( ) <EOL> self . cwd = os . getcwd ( ) <EOL> print '<STR_LIT>' , self . tempdir <EOL> os . chdir ( self . tempdir ) <EOL> class VirtualenvContext ( Context ) : <EOL> def __init__ ( self , always_cleanup = True , use_site_packages = False , dependencies = [ ] , ** kwargs ) : <EOL> Context . __init__ ( self ) <EOL> self . cleanup = always_cleanup <EOL> self . dependencies = dependencies <EOL> base_build = kwargs [ '<STR_LIT>' ] <EOL> import virtualenv <EOL> self . tempdir = tempfile . mkdtemp ( ) <EOL> print '<STR_LIT>' <EOL> if use_site_packages : <EOL> _run_command ( [ base_build . py_name , '<STR_LIT>' , '<STR_LIT>' , self . tempdir ] ) <EOL> else : <EOL> _run_command ( [ base_build . py_name , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , self . tempdir ] ) <EOL> self . python = os . path . join ( self . tempdir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . djangoadmin = os . path . join ( self . tempdir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . easy_install = os . path . join ( self . tempdir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . pip = os . path . join ( self . tempdir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def initialize ( self ) : <EOL> self . start_time = datetime . datetime . now ( ) <EOL> print '<STR_LIT>' , self . tempdir <EOL> self . cwd = os . getcwd ( ) <EOL> os . chdir ( self . tempdir ) <EOL> print '<STR_LIT>' <EOL> _run_command ( [ self . easy_install , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for dep in self . dependencies : <EOL> print "<STR_LIT>" , dep <EOL> _run_command ( [ self . pip , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] + dep . split ( ) ) <EOL> def finish ( self ) : <EOL> os . chdir ( self . cwd ) <EOL> try : <EOL> self . end_time = datetime . datetime . now ( ) <EOL> finally : <EOL> if self . cleanup : <EOL> print '<STR_LIT>' , self . tempdir <EOL> shutil . rmtree ( self . tempdir , ignore_errors = True ) <EOL> def update_client_info ( self , info ) : <EOL> info [ '<STR_LIT>' ] = str ( self . start_time ) <EOL> info [ '<STR_LIT>' ] = str ( self . end_time ) <EOL> info [ '<STR_LIT>' ] = self . tempdir <EOL> info [ '<STR_LIT>' ] = True <EOL> class BaseCommand ( object ) : <EOL> def __init__ ( self , command_list , name = '<STR_LIT>' , run_cwd = None ) : <EOL> self . command_list = command_list <EOL> if name : <EOL> self . command_name = name <EOL> self . run_cwd = run_cwd <EOL> self . status = None <EOL> self . output = None <EOL> self . errout = None <EOL> self . duration = None <EOL> self . variables = None <EOL> def set_variables ( self , v ) : <EOL> self . variables = dict ( v ) <EOL> def run ( self , context ) : <EOL> start = time . time ( ) <EOL> ( ret , out , err ) = _run_command ( self . command_list , cwd = self . run_cwd , <EOL> variables = self . variables ) <EOL> self . status = ret <EOL> self . output = out <EOL> self . errout = err <EOL> end = time . time ( ) <EOL> self . duration = end - start <EOL> def success ( self ) : <EOL> return self . status == <NUM_LIT:0> <EOL> def get_results ( self ) : <EOL> results = dict ( status = self . status , <EOL> output = self . output , <EOL> errout = self . errout , <EOL> command = str ( self . command_list ) , <EOL> type = self . command_type , <EOL> name = self . command_name , <EOL> duration = self . duration ) <EOL> return results <EOL> class SetupCommand ( BaseCommand ) : <EOL> command_type = '<STR_LIT>' <EOL> command_name = '<STR_LIT>' <EOL> class BuildCommand ( BaseCommand ) : <EOL> command_type = '<STR_LIT>' <EOL> command_name = '<STR_LIT>' <EOL> class TestCommand ( BaseCommand ) : <EOL> command_type = '<STR_LIT:test>' <EOL> command_name = '<STR_LIT:test>' <EOL> class VCSClone ( SetupCommand ) : <EOL> command_name = '<STR_LIT>' <EOL> def __init__ ( self , repository , branch = '<STR_LIT>' , cache_dir = None , <EOL> use_cache = True , egg = None , ** kwargs ) : <EOL> SetupCommand . __init__ ( self , [ ] , ** kwargs ) <EOL> self . repository = repository <EOL> self . branch = branch <EOL> self . egg = egg <EOL> self . use_cache = use_cache <EOL> self . cache_dir = cache_dir <EOL> self . duration = - <NUM_LIT:1> <EOL> self . version_info = '<STR_LIT>' <EOL> self . results_dict = { } <EOL> def run ( self , context ) : <EOL> if self . use_cache : <EOL> cache_dir = self . cache_dir <EOL> if not cache_dir : <EOL> cache_dir = guess_cache_dir ( self . egg ) <EOL> vcs = pip . vcs . get_backend ( self . vcs ) <EOL> vcs_repo = vcs ( "<STR_LIT>" % ( self . vcs , self . repository , self . egg ) ) <EOL> try : <EOL> if os . path . exists ( cache_dir ) : <EOL> try : <EOL> if vcs . name is '<STR_LIT>' : <EOL> _run_command ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , self . cache_dir ] ) <EOL> vcs_repo . update ( cache_dir , [ '<STR_LIT>' ] ) <EOL> else : <EOL> vcs_repo . update ( cache_dir , [ ] ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" % e <EOL> else : <EOL> vcs_repo . obtain ( cache_dir ) <EOL> self . status = <NUM_LIT:0> <EOL> except Exception , e : <EOL> print "<STR_LIT>" % e <EOL> self . status = <NUM_LIT:1> <EOL> context . build_dir = cache_dir <EOL> def get_results ( self ) : <EOL> self . results_dict [ '<STR_LIT>' ] = self . results_dict [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . results_dict [ '<STR_LIT>' ] = '<STR_LIT>' % ( self . repository , <EOL> self . branch ) <EOL> self . results_dict [ '<STR_LIT:status>' ] = self . status <EOL> self . results_dict [ '<STR_LIT:type>' ] = self . command_type <EOL> self . results_dict [ '<STR_LIT:name>' ] = self . command_name <EOL> self . results_dict [ '<STR_LIT>' ] = self . vcs <EOL> if self . version_info : <EOL> self . results_dict [ '<STR_LIT>' ] = self . version_info <EOL> return self . results_dict <EOL> class GitClone ( VCSClone ) : <EOL> vcs = "<STR_LIT>" <EOL> class HgClone ( VCSClone ) : <EOL> vcs = "<STR_LIT>" <EOL> class SvnUpdate ( VCSClone ) : <EOL> vcs = "<STR_LIT>" <EOL> def get_hostname ( ) : <EOL> import socket <EOL> return socket . gethostname ( ) <EOL> def get_arch ( ) : <EOL> import distutils . util <EOL> return distutils . util . get_platform ( ) <EOL> def _send ( server , info , results ) : <EOL> print '<STR_LIT>' , server <EOL> s = xmlrpclib . ServerProxy ( server , allow_none = True ) <EOL> s . add_results ( info , results ) <EOL> def do ( name , commands , context = None , arch = None , stop_if_failure = True ) : <EOL> reslist = [ ] <EOL> if context : <EOL> context . initialize ( ) <EOL> for c in commands : <EOL> print '<STR_LIT>' % ( c . command_name , c . command_type ) <EOL> if context : <EOL> context . start_command ( c ) <EOL> c . run ( context ) <EOL> if context : <EOL> context . end_command ( c ) <EOL> reslist . append ( c . get_results ( ) ) <EOL> if stop_if_failure and not c . success ( ) : <EOL> break <EOL> if context : <EOL> context . finish ( ) <EOL> if arch is None : <EOL> arch = get_arch ( ) <EOL> success = all ( [ c . success ( ) for c in commands ] ) <EOL> client_info = dict ( package = name , arch = arch , success = success ) <EOL> if context : <EOL> context . update_client_info ( client_info ) <EOL> return ( client_info , reslist ) <EOL> def send ( server_url , x , hostname = None , tags = ( ) ) : <EOL> client_info , reslist = x <EOL> if hostname is None : <EOL> import socket <EOL> hostname = socket . gethostname ( ) <EOL> client_info [ '<STR_LIT:host>' ] = hostname <EOL> client_info [ '<STR_LIT>' ] = tags <EOL> print '<STR_LIT>' , server_url <EOL> _send ( server_url , client_info , reslist ) <EOL> def check ( name , server_url , tags = ( ) , hostname = None , arch = None , reserve_time = <NUM_LIT:0> ) : <EOL> if hostname is None : <EOL> hostname = get_hostname ( ) <EOL> if arch is None : <EOL> arch = get_arch ( ) <EOL> client_info = dict ( package = name , host = hostname , arch = arch , tags = tags ) <EOL> s = xmlrpclib . ServerProxy ( server_url , allow_none = True ) <EOL> ( flag , reason ) = s . check_should_build ( client_info , True , reserve_time ) <EOL> return flag </s>
