<s> import os <EOL> import time <EOL> import tempfile <EOL> import shutil <EOL> import subprocess <EOL> import re <EOL> import testing <EOL> class GitCommandError ( testing . TestFailure ) : <EOL> def __init__ ( self , command , output ) : <EOL> super ( GitCommandError , self ) . __init__ ( <EOL> "<STR_LIT>" <EOL> % ( command , "<STR_LIT>" . join ( output . strip ( ) . splitlines ( ) ) ) ) <EOL> self . command = command <EOL> self . output = output <EOL> def _git ( args , ** kwargs ) : <EOL> argv = [ "<STR_LIT>" ] + args <EOL> if "<STR_LIT>" in kwargs : <EOL> cwd = "<STR_LIT>" % kwargs [ "<STR_LIT>" ] <EOL> else : <EOL> cwd = "<STR_LIT>" <EOL> testing . logger . debug ( "<STR_LIT>" % ( "<STR_LIT:U+0020>" . join ( argv ) , cwd ) ) <EOL> env = kwargs . setdefault ( "<STR_LIT>" , { } ) <EOL> env . setdefault ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> env . setdefault ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> env . setdefault ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> env . setdefault ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> try : <EOL> return subprocess . check_output ( <EOL> argv , stdin = open ( "<STR_LIT>" ) , stderr = subprocess . STDOUT , ** kwargs ) <EOL> except subprocess . CalledProcessError as error : <EOL> raise GitCommandError ( "<STR_LIT:U+0020>" . join ( argv ) , error . output ) <EOL> def submodule_sha1 ( repository_path , parent_sha1 , submodule_path ) : <EOL> try : <EOL> lstree = _git ( [ "<STR_LIT>" , parent_sha1 , submodule_path ] , <EOL> cwd = repository_path ) <EOL> except GitCommandError : <EOL> return None <EOL> mode , object_type , sha1 , path = lstree . strip ( ) . split ( None , <NUM_LIT:3> ) <EOL> if object_type != "<STR_LIT>" : <EOL> return None <EOL> return sha1 <EOL> class Repository ( object ) : <EOL> def __init__ ( self , host , port , tested_commit , instance ) : <EOL> self . host = host <EOL> self . port = port <EOL> self . instance = instance <EOL> self . base_path = tempfile . mkdtemp ( ) <EOL> self . path = os . path . join ( self . base_path , "<STR_LIT>" ) <EOL> self . work = os . path . join ( self . base_path , "<STR_LIT>" ) <EOL> if port : <EOL> self . url = "<STR_LIT>" % ( host , port ) <EOL> else : <EOL> self . url = "<STR_LIT>" % host <EOL> testing . logger . debug ( "<STR_LIT>" % self . base_path ) <EOL> _git ( [ "<STR_LIT>" , "<STR_LIT>" , os . getcwd ( ) , "<STR_LIT>" ] , <EOL> cwd = self . base_path ) <EOL> _git ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:false>" ] , <EOL> cwd = self . path ) <EOL> _git ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:false>" ] , <EOL> cwd = self . path ) <EOL> self . push ( tested_commit ) <EOL> if os . path . exists ( "<STR_LIT>" ) : <EOL> v8_jsshell_path = os . path . join ( os . getcwd ( ) , "<STR_LIT>" ) <EOL> _git ( [ "<STR_LIT>" , "<STR_LIT>" , v8_jsshell_path , "<STR_LIT>" ] , <EOL> cwd = self . base_path ) <EOL> self . v8_jsshell_path = os . path . join ( self . base_path , "<STR_LIT>" ) <EOL> v8_jsshell_sha1 = submodule_sha1 ( os . getcwd ( ) , tested_commit , <EOL> "<STR_LIT>" ) <EOL> if v8_jsshell_sha1 : <EOL> _git ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , self . v8_jsshell_path , <EOL> v8_jsshell_sha1 + "<STR_LIT>" ] , <EOL> cwd = v8_jsshell_path ) <EOL> else : <EOL> self . v8_jsshell_path = None <EOL> v8_jsshell_sha1 = None <EOL> if os . path . exists ( "<STR_LIT>" ) : <EOL> v8_path = os . path . join ( os . getcwd ( ) , "<STR_LIT>" ) <EOL> _git ( [ "<STR_LIT>" , "<STR_LIT>" , v8_path , "<STR_LIT>" ] , <EOL> cwd = self . base_path ) <EOL> self . v8_path = os . path . join ( self . base_path , "<STR_LIT>" ) <EOL> if port : <EOL> self . v8_url = "<STR_LIT>" % ( host , port ) <EOL> else : <EOL> self . v8_url = "<STR_LIT>" % host <EOL> if v8_jsshell_sha1 : <EOL> v8_sha1 = submodule_sha1 ( "<STR_LIT>" , <EOL> v8_jsshell_sha1 , "<STR_LIT>" ) <EOL> if v8_sha1 : <EOL> _git ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , self . v8_path , <EOL> v8_sha1 + "<STR_LIT>" ] , <EOL> cwd = v8_path ) <EOL> else : <EOL> self . v8_path = None <EOL> self . v8_url = None <EOL> def push ( self , commit ) : <EOL> _git ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , self . path , <EOL> "<STR_LIT>" % commit ] ) <EOL> def export ( self ) : <EOL> argv = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" % self . base_path ] <EOL> if self . port : <EOL> argv . append ( "<STR_LIT>" % self . port ) <EOL> argv . append ( self . path ) <EOL> if self . v8_jsshell_path : <EOL> argv . append ( self . v8_jsshell_path ) <EOL> if self . v8_path : <EOL> argv . append ( self . v8_path ) <EOL> self . daemon = subprocess . Popen ( argv ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> pid , status = os . waitpid ( self . daemon . pid , os . WNOHANG ) <EOL> if pid != <NUM_LIT:0> : <EOL> self . daemon = None <EOL> testing . logger . error ( "<STR_LIT>" ) <EOL> return False <EOL> testing . logger . debug ( "<STR_LIT>" % self . path ) <EOL> if self . v8_jsshell_path : <EOL> testing . logger . debug ( "<STR_LIT>" % self . v8_jsshell_path ) <EOL> if self . v8_path : <EOL> testing . logger . debug ( "<STR_LIT>" % self . v8_path ) <EOL> return True <EOL> def run ( self , args , cwd = None , env = None ) : <EOL> if cwd is None : <EOL> cwd = self . path <EOL> if env is None : <EOL> env = { } <EOL> if isinstance ( self . instance , testing . quickstart . Instance ) : <EOL> for index , arg in enumerate ( args [ <NUM_LIT:1> : ] ) : <EOL> if isinstance ( arg , testing . quickstart . RepositoryURL ) : <EOL> args [ index + <NUM_LIT:1> ] = arg . path <EOL> env [ "<STR_LIT>" ] = arg . name <EOL> return _git ( args , cwd = cwd , env = env ) <EOL> def workcopy ( self , name = "<STR_LIT>" , empty = False ) : <EOL> master = self <EOL> class Workcopy ( testing . Context ) : <EOL> def __init__ ( self , path , start , finish ) : <EOL> super ( Workcopy , self ) . __init__ ( start , finish ) <EOL> self . path = path <EOL> def run ( self , args , ** kwargs ) : <EOL> if kwargs : <EOL> env = { } <EOL> for name in kwargs . keys ( ) : <EOL> if name . lower ( ) != name == name . upper ( ) : <EOL> env [ name ] = kwargs [ name ] <EOL> del kwargs [ name ] <EOL> else : <EOL> env = None <EOL> return master . run ( args , cwd = self . path , env = env , ** kwargs ) <EOL> path = os . path . join ( self . work , name ) <EOL> if os . path . exists ( path ) : <EOL> raise testing . InstanceError ( <EOL> "<STR_LIT>" ) <EOL> def start ( ) : <EOL> if not os . path . isdir ( self . work ) : <EOL> os . mkdir ( self . work ) <EOL> if not empty : <EOL> _git ( [ "<STR_LIT>" , self . path , name ] , cwd = self . work ) <EOL> else : <EOL> os . mkdir ( path ) <EOL> _git ( [ "<STR_LIT>" ] , cwd = path ) <EOL> def finish ( ) : <EOL> shutil . rmtree ( path ) <EOL> return Workcopy ( path , start , finish ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , * args ) : <EOL> try : <EOL> if self . daemon : <EOL> self . daemon . terminate ( ) <EOL> self . daemon . wait ( ) <EOL> except : <EOL> testing . logger . exception ( "<STR_LIT>" ) <EOL> try : <EOL> shutil . rmtree ( self . base_path ) <EOL> except : <EOL> testing . logger . exception ( "<STR_LIT>" ) <EOL> return False </s>
