<s> import subprocess <EOL> import api <EOL> import configuration <EOL> import dbutils <EOL> import gitutils <EOL> class Repository ( object ) : <EOL> def __init__ ( self , repository_id , name , path ) : <EOL> self . id = repository_id <EOL> self . name = name <EOL> self . path = path <EOL> self . __internal = None <EOL> def getInternal ( self , critic ) : <EOL> if not self . __internal : <EOL> self . __internal = gitutils . Repository . fromId ( <EOL> db = critic . database , repository_id = self . id ) <EOL> return self . __internal <EOL> def getURL ( self , critic ) : <EOL> return gitutils . Repository . constructURL ( <EOL> critic . database , critic . effective_user . internal , self . path ) <EOL> def run ( self , * args ) : <EOL> argv = [ configuration . executables . GIT ] + list ( args ) <EOL> process = subprocess . Popen ( <EOL> argv , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> cwd = self . path ) <EOL> stdout , stderr = process . communicate ( ) <EOL> if process . returncode != <NUM_LIT:0> : <EOL> raise api . repository . GitCommandError ( <EOL> argv , process . returncode , stdout , stderr ) <EOL> return stdout <EOL> def resolveRef ( self , ref , expect , short ) : <EOL> command_line = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> if short : <EOL> if isinstance ( short , int ) : <EOL> command_line . append ( "<STR_LIT>" % short ) <EOL> else : <EOL> command_line . append ( "<STR_LIT>" ) <EOL> if expect is not None : <EOL> ref += "<STR_LIT>" % expect <EOL> command_line . append ( ref ) <EOL> try : <EOL> return self . run ( * command_line ) . strip ( ) <EOL> except api . repository . GitCommandError : <EOL> raise api . repository . InvalidRef ( ref ) <EOL> def listCommits ( self , repository , include , exclude , args , paths ) : <EOL> args = [ '<STR_LIT>' ] + args <EOL> args . extend ( commit . sha1 for commit in include ) <EOL> args . extend ( "<STR_LIT>" + commit . sha1 for commit in exclude ) <EOL> if paths : <EOL> args . append ( "<STR_LIT>" ) <EOL> args . extend ( paths ) <EOL> return [ api . commit . fetch ( repository , sha1 = sha1 ) <EOL> for sha1 in self . run ( * args ) . split ( ) ] <EOL> def wrap ( self , critic ) : <EOL> return api . repository . Repository ( critic , self ) <EOL> def make ( critic , repository_id , name , path ) : <EOL> def callback ( ) : <EOL> return Repository ( repository_id , name , path ) . wrap ( critic ) <EOL> return critic . _impl . cached ( <EOL> api . repository . Repository , repository_id , callback ) <EOL> def fetch ( critic , repository_id , name , path ) : <EOL> cursor = critic . getDatabaseCursor ( ) <EOL> if repository_id is not None : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( repository_id , ) ) <EOL> elif name is not None : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( name , ) ) <EOL> else : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( path , ) ) <EOL> row = cursor . fetchone ( ) <EOL> if not row : <EOL> if repository_id is not None : <EOL> raise api . repository . InvalidRepositoryId ( repository_id ) <EOL> elif name is not None : <EOL> raise api . repository . InvalidRepositoryName ( name ) <EOL> else : <EOL> raise api . repository . InvalidRepositoryPath ( path ) <EOL> return make ( critic , * row ) <EOL> def fetchAll ( critic ) : <EOL> cursor = critic . getDatabaseCursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" ) <EOL> return [ make ( critic , * row ) for row in cursor ] <EOL> def fetchHighlighted ( critic , user ) : <EOL> highlighted = set ( ) <EOL> cursor = critic . getDatabaseCursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( user . id , ) ) <EOL> highlighted . update ( repository_id for ( repository_id , ) in cursor ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( user . id , ) ) <EOL> highlighted . update ( repository_id for ( repository_id , ) in cursor ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( list ( highlighted ) , ) ) <EOL> return [ make ( critic , * row ) for row in cursor ] </s>
