<s> import getpass <EOL> import linecache <EOL> import os <EOL> import random <EOL> import re <EOL> import string <EOL> import subprocess <EOL> import sys <EOL> import tempfile <EOL> import upload <EOL> import urllib2 <EOL> CODEREVIEW_SETTINGS = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> use_shell = sys . platform . startswith ( "<STR_LIT>" ) <EOL> repository_root = "<STR_LIT>" <EOL> gcl_info_dir = "<STR_LIT>" <EOL> CODEREVIEW_SETTINGS_FILE = "<STR_LIT>" <EOL> MISSING_TEST_MSG = "<STR_LIT>" <EOL> read_gcl_info = False <EOL> def DiffPrint ( filename ) : <EOL> """<STR_LIT>""" <EOL> file_handle = open ( filename , '<STR_LIT:rb>' ) <EOL> file_content = file_handle . read ( ) <EOL> file_handle . close ( ) <EOL> file_content = [ '<STR_LIT>' + i for i in file_content . splitlines ( True ) ] <EOL> nb_lines = len ( file_content ) <EOL> filename = filename . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> data = "<STR_LIT>" % filename <EOL> data += ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> data += "<STR_LIT>" % filename <EOL> data += "<STR_LIT>" % filename <EOL> data += "<STR_LIT>" % nb_lines <EOL> data += '<STR_LIT>' . join ( file_content ) <EOL> return data <EOL> def IsSVNMoved ( filename ) : <EOL> """<STR_LIT>""" <EOL> info = GetSVNFileInfo ( filename ) <EOL> return ( info . get ( '<STR_LIT>' ) and <EOL> info . get ( '<STR_LIT>' ) and <EOL> info . get ( '<STR_LIT>' ) == '<STR_LIT>' ) <EOL> def GetSVNFileInfo ( file ) : <EOL> """<STR_LIT>""" <EOL> output = RunShell ( [ "<STR_LIT>" , "<STR_LIT:info>" , file ] ) <EOL> result = { } <EOL> re_key_value_pair = re . compile ( '<STR_LIT>' ) <EOL> for line in output . splitlines ( ) : <EOL> key_value_pair = re_key_value_pair . match ( line ) <EOL> if key_value_pair : <EOL> result [ key_value_pair . group ( <NUM_LIT:1> ) ] = key_value_pair . group ( <NUM_LIT:2> ) <EOL> return result <EOL> def GetSVNFileProperty ( file , property_name ) : <EOL> """<STR_LIT>""" <EOL> output = RunShell ( [ "<STR_LIT>" , "<STR_LIT>" , property_name , file ] ) <EOL> if ( output . startswith ( "<STR_LIT>" ) and <EOL> output . endswith ( "<STR_LIT>" ) ) : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return output <EOL> def GetRepositoryRoot ( ) : <EOL> """<STR_LIT>""" <EOL> global repository_root <EOL> if not repository_root : <EOL> cur_dir_repo_root = GetSVNFileInfo ( os . getcwd ( ) ) . get ( "<STR_LIT>" ) <EOL> if not cur_dir_repo_root : <EOL> ErrorExit ( "<STR_LIT>" ) <EOL> repository_root = os . getcwd ( ) <EOL> while True : <EOL> parent = os . path . dirname ( repository_root ) <EOL> if GetSVNFileInfo ( parent ) . get ( "<STR_LIT>" ) != cur_dir_repo_root : <EOL> break <EOL> repository_root = parent <EOL> return repository_root <EOL> def GetInfoDir ( ) : <EOL> """<STR_LIT>""" <EOL> global gcl_info_dir <EOL> if not gcl_info_dir : <EOL> gcl_info_dir = os . path . join ( GetRepositoryRoot ( ) , '<STR_LIT>' , '<STR_LIT>' ) <EOL> return gcl_info_dir <EOL> def GetCodeReviewSetting ( key ) : <EOL> """<STR_LIT>""" <EOL> global read_gcl_info <EOL> if not read_gcl_info : <EOL> read_gcl_info = True <EOL> cached_settings_file = os . path . join ( GetInfoDir ( ) , CODEREVIEW_SETTINGS_FILE ) <EOL> if ( not os . path . exists ( cached_settings_file ) or <EOL> os . stat ( cached_settings_file ) . st_mtime > <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:3> ) : <EOL> dir_info = GetSVNFileInfo ( "<STR_LIT:.>" ) <EOL> repo_root = dir_info [ "<STR_LIT>" ] <EOL> url_path = dir_info [ "<STR_LIT>" ] <EOL> settings = "<STR_LIT>" <EOL> while True : <EOL> svn_path = url_path + "<STR_LIT:/>" + CODEREVIEW_SETTINGS_FILE <EOL> settings , rc = RunShellWithReturnCode ( [ "<STR_LIT>" , "<STR_LIT>" , svn_path ] ) <EOL> if not rc : <EOL> break <EOL> settings = "<STR_LIT>" <EOL> if url_path == repo_root : <EOL> break ; <EOL> url_path = os . path . dirname ( url_path ) <EOL> WriteFile ( cached_settings_file , settings ) <EOL> output = ReadFile ( cached_settings_file ) <EOL> for line in output . splitlines ( ) : <EOL> if not line or line . startswith ( "<STR_LIT:#>" ) : <EOL> continue <EOL> k , v = line . split ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> CODEREVIEW_SETTINGS [ k ] = v <EOL> return CODEREVIEW_SETTINGS . get ( key , "<STR_LIT>" ) <EOL> def IsTreeOpen ( ) : <EOL> """<STR_LIT>""" <EOL> url = GetCodeReviewSetting ( '<STR_LIT>' ) <EOL> status = "<STR_LIT>" <EOL> if url : <EOL> status = urllib2 . urlopen ( url ) . read ( ) <EOL> return status . find ( '<STR_LIT:0>' ) == - <NUM_LIT:1> <EOL> def Warn ( msg ) : <EOL> ErrorExit ( msg , exit = False ) <EOL> def ErrorExit ( msg , exit = True ) : <EOL> """<STR_LIT>""" <EOL> print >> sys . stderr , msg <EOL> if exit : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def RunShellWithReturnCode ( command , print_output = False ) : <EOL> """<STR_LIT>""" <EOL> p = subprocess . Popen ( command , stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT , shell = use_shell , <EOL> universal_newlines = True ) <EOL> if print_output : <EOL> output_array = [ ] <EOL> while True : <EOL> line = p . stdout . readline ( ) <EOL> if not line : <EOL> break <EOL> if print_output : <EOL> print line . strip ( '<STR_LIT:\n>' ) <EOL> output_array . append ( line ) <EOL> output = "<STR_LIT>" . join ( output_array ) <EOL> else : <EOL> output = p . stdout . read ( ) <EOL> p . wait ( ) <EOL> p . stdout . close ( ) <EOL> return output , p . returncode <EOL> def RunShell ( command , print_output = False ) : <EOL> """<STR_LIT>""" <EOL> return RunShellWithReturnCode ( command , print_output ) [ <NUM_LIT:0> ] <EOL> def ReadFile ( filename ) : <EOL> """<STR_LIT>""" <EOL> file = open ( filename , '<STR_LIT:r>' ) <EOL> result = file . read ( ) <EOL> file . close ( ) <EOL> return result <EOL> def WriteFile ( filename , contents ) : <EOL> """<STR_LIT>""" <EOL> file = open ( filename , '<STR_LIT:w>' ) <EOL> file . write ( contents ) <EOL> file . close ( ) <EOL> class ChangeInfo : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = "<STR_LIT>" , issue = "<STR_LIT>" , description = "<STR_LIT>" , files = [ ] ) : <EOL> self . name = name <EOL> self . issue = issue <EOL> self . description = description <EOL> self . files = files <EOL> self . patch = None <EOL> def FileList ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ file [ <NUM_LIT:1> ] for file in self . files ] <EOL> def _NonDeletedFileList ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ file [ <NUM_LIT:1> ] for file in self . files if not file [ <NUM_LIT:0> ] . startswith ( "<STR_LIT:D>" ) ] <EOL> def _AddedFileList ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ file [ <NUM_LIT:1> ] for file in self . files if file [ <NUM_LIT:0> ] . startswith ( "<STR_LIT:A>" ) ] <EOL> def Save ( self ) : <EOL> """<STR_LIT>""" <EOL> data = SEPARATOR . join ( [ self . issue , <EOL> "<STR_LIT:\n>" . join ( [ f [ <NUM_LIT:0> ] + f [ <NUM_LIT:1> ] for f in self . files ] ) , <EOL> self . description ] ) <EOL> WriteFile ( GetChangelistInfoFile ( self . name ) , data ) <EOL> def Delete ( self ) : <EOL> """<STR_LIT>""" <EOL> os . remove ( GetChangelistInfoFile ( self . name ) ) <EOL> def CloseIssue ( self ) : <EOL> """<STR_LIT>""" <EOL> data = [ ( "<STR_LIT:description>" , self . description ) , ] <EOL> ctype , body = upload . EncodeMultipartFormData ( data , [ ] ) <EOL> SendToRietveld ( "<STR_LIT:/>" + self . issue + "<STR_LIT>" , body , ctype ) <EOL> def UpdateRietveldDescription ( self ) : <EOL> """<STR_LIT>""" <EOL> data = [ ( "<STR_LIT:description>" , self . description ) , ] <EOL> ctype , body = upload . EncodeMultipartFormData ( data , [ ] ) <EOL> SendToRietveld ( "<STR_LIT:/>" + self . issue + "<STR_LIT>" , body , ctype ) <EOL> def GetMoved ( self , root = None ) : <EOL> """<STR_LIT>""" <EOL> moved = [ f [ <NUM_LIT:1> ] for f in self . files if IsSVNMoved ( f [ <NUM_LIT:1> ] ) ] <EOL> if root : <EOL> moved = [ os . path . join ( root , f ) for f in moved ] <EOL> return moved <EOL> def MissingTests ( self ) : <EOL> """<STR_LIT>""" <EOL> SOURCE_SUFFIXES = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> files = [ file for file in self . _NonDeletedFileList ( ) <EOL> if file . find ( "<STR_LIT>" ) == - <NUM_LIT:1> ] <EOL> added_files = [ file for file in self . _AddedFileList ( ) <EOL> if file . find ( "<STR_LIT>" ) == - <NUM_LIT:1> ] <EOL> if len ( files ) == <NUM_LIT:0> : <EOL> return False <EOL> test_files = [ test for test in files <EOL> if os . path . splitext ( test ) [ <NUM_LIT:0> ] . rstrip ( "<STR_LIT:s>" ) . endswith ( "<STR_LIT:test>" ) ] <EOL> if len ( test_files ) > <NUM_LIT:0> : <EOL> return False <EOL> source_files = [ file for file in added_files <EOL> if os . path . splitext ( file ) [ <NUM_LIT:1> ] in SOURCE_SUFFIXES ] <EOL> if len ( source_files ) > <NUM_LIT:0> : <EOL> return True <EOL> return self . _HasNewMethod ( ) <EOL> def _HasNewMethod ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . patch is None : <EOL> self . patch = GenerateDiff ( self . FileList ( ) ) <EOL> definition = "<STR_LIT>" <EOL> for line in self . patch . splitlines ( ) : <EOL> if not line . startswith ( "<STR_LIT:+>" ) : <EOL> continue <EOL> line = line . strip ( "<STR_LIT:+>" ) . rstrip ( "<STR_LIT>" ) <EOL> if line == "<STR_LIT>" or line . startswith ( "<STR_LIT:/>" ) or line . startswith ( "<STR_LIT:#>" ) : <EOL> continue <EOL> if definition . endswith ( "<STR_LIT:{>" ) : <EOL> if definition . find ( "<STR_LIT:(>" ) != - <NUM_LIT:1> : <EOL> return True <EOL> definition = "<STR_LIT>" <EOL> if ( definition != "<STR_LIT>" and <EOL> ( line == "<STR_LIT:{>" or line . startswith ( "<STR_LIT:U+0020>" ) or line . startswith ( "<STR_LIT:\t>" ) ) ) : <EOL> definition += line <EOL> elif not line . startswith ( "<STR_LIT:U+0020>" ) and not line . startswith ( "<STR_LIT:\t>" ) : <EOL> definition = line <EOL> return False <EOL> SEPARATOR = "<STR_LIT>" <EOL> def GetChangelistInfoFile ( changename ) : <EOL> """<STR_LIT>""" <EOL> if not changename or re . search ( r'<STR_LIT>' , changename ) : <EOL> ErrorExit ( "<STR_LIT>" + changename ) <EOL> return os . path . join ( GetInfoDir ( ) , changename ) <EOL> def LoadChangelistInfoForMultiple ( changenames , fail_on_not_found = True , <EOL> update_status = False ) : <EOL> """<STR_LIT>""" <EOL> changes = changenames . split ( '<STR_LIT:U+002C>' ) <EOL> aggregate_change_info = ChangeInfo ( name = changenames ) <EOL> for change in changes : <EOL> aggregate_change_info . files += LoadChangelistInfo ( change , <EOL> fail_on_not_found , <EOL> update_status ) . files <EOL> return aggregate_change_info <EOL> def LoadChangelistInfo ( changename , fail_on_not_found = True , <EOL> update_status = False ) : <EOL> """<STR_LIT>""" <EOL> info_file = GetChangelistInfoFile ( changename ) <EOL> if not os . path . exists ( info_file ) : <EOL> if fail_on_not_found : <EOL> ErrorExit ( "<STR_LIT>" + changename + "<STR_LIT>" ) <EOL> return ChangeInfo ( changename ) <EOL> data = ReadFile ( info_file ) <EOL> split_data = data . split ( SEPARATOR , <NUM_LIT:2> ) <EOL> if len ( split_data ) != <NUM_LIT:3> : <EOL> os . remove ( info_file ) <EOL> ErrorExit ( "<STR_LIT>" % info_file ) <EOL> issue = split_data [ <NUM_LIT:0> ] <EOL> files = [ ] <EOL> for line in split_data [ <NUM_LIT:1> ] . splitlines ( ) : <EOL> status = line [ : <NUM_LIT:7> ] <EOL> file = line [ <NUM_LIT:7> : ] <EOL> files . append ( ( status , file ) ) <EOL> description = split_data [ <NUM_LIT:2> ] <EOL> save = False <EOL> if update_status : <EOL> for file in files : <EOL> filename = os . path . join ( GetRepositoryRoot ( ) , file [ <NUM_LIT:1> ] ) <EOL> status = RunShell ( [ "<STR_LIT>" , "<STR_LIT:status>" , filename ] ) [ : <NUM_LIT:7> ] <EOL> if not status : <EOL> save = True <EOL> files . remove ( file ) <EOL> elif status != file [ <NUM_LIT:0> ] : <EOL> save = True <EOL> files [ files . index ( file ) ] = ( status , file [ <NUM_LIT:1> ] ) <EOL> change_info = ChangeInfo ( changename , issue , description , files ) <EOL> if save : <EOL> change_info . Save ( ) <EOL> return change_info <EOL> def GetCLs ( ) : <EOL> """<STR_LIT>""" <EOL> cls = os . listdir ( GetInfoDir ( ) ) <EOL> if CODEREVIEW_SETTINGS_FILE in cls : <EOL> cls . remove ( CODEREVIEW_SETTINGS_FILE ) <EOL> return cls <EOL> def GenerateChangeName ( ) : <EOL> """<STR_LIT>""" <EOL> random . seed ( ) <EOL> current_cl_names = GetCLs ( ) <EOL> while True : <EOL> cl_name = ( random . choice ( string . ascii_lowercase ) + <EOL> random . choice ( string . digits ) + <EOL> random . choice ( string . ascii_lowercase ) + <EOL> random . choice ( string . digits ) ) <EOL> if cl_name not in current_cl_names : <EOL> return cl_name <EOL> def GetModifiedFiles ( ) : <EOL> """<STR_LIT>""" <EOL> files = { } <EOL> dir_prefix = os . getcwd ( ) [ len ( GetRepositoryRoot ( ) ) : ] . strip ( os . sep ) <EOL> files_in_cl = { } <EOL> for cl in GetCLs ( ) : <EOL> change_info = LoadChangelistInfo ( cl ) <EOL> for status , filename in change_info . files : <EOL> files_in_cl [ filename ] = change_info . name <EOL> status = RunShell ( [ "<STR_LIT>" , "<STR_LIT:status>" ] ) <EOL> for line in status . splitlines ( ) : <EOL> if not len ( line ) or line [ <NUM_LIT:0> ] == "<STR_LIT:?>" : <EOL> continue <EOL> status = line [ : <NUM_LIT:7> ] <EOL> filename = line [ <NUM_LIT:7> : ] <EOL> if dir_prefix : <EOL> filename = os . path . join ( dir_prefix , filename ) <EOL> change_list_name = "<STR_LIT>" <EOL> if filename in files_in_cl : <EOL> change_list_name = files_in_cl [ filename ] <EOL> files . setdefault ( change_list_name , [ ] ) . append ( ( status , filename ) ) <EOL> return files <EOL> def GetFilesNotInCL ( ) : <EOL> """<STR_LIT>""" <EOL> modified_files = GetModifiedFiles ( ) <EOL> if "<STR_LIT>" not in modified_files : <EOL> return [ ] <EOL> return modified_files [ "<STR_LIT>" ] <EOL> def SendToRietveld ( request_path , payload = None , <EOL> content_type = "<STR_LIT>" , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> def GetUserCredentials ( ) : <EOL> """<STR_LIT>""" <EOL> email = upload . GetEmail ( ) <EOL> password = getpass . getpass ( "<STR_LIT>" % email ) <EOL> return email , password <EOL> server = GetCodeReviewSetting ( "<STR_LIT>" ) <EOL> rpc_server = upload . HttpRpcServer ( server , <EOL> GetUserCredentials , <EOL> host_override = server , <EOL> save_cookies = True ) <EOL> try : <EOL> return rpc_server . Send ( request_path , payload , content_type , timeout ) <EOL> except urllib2 . URLError , e : <EOL> if timeout is None : <EOL> ErrorExit ( "<STR_LIT>" % request_path ) <EOL> else : <EOL> return None <EOL> def GetIssueDescription ( issue ) : <EOL> """<STR_LIT>""" <EOL> return SendToRietveld ( "<STR_LIT:/>" + issue + "<STR_LIT>" ) <EOL> def UnknownFiles ( extra_args ) : <EOL> """<STR_LIT>""" <EOL> args = [ "<STR_LIT>" , "<STR_LIT:status>" ] <EOL> args += extra_args <EOL> p = subprocess . Popen ( args , stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT , shell = use_shell ) <EOL> while <NUM_LIT:1> : <EOL> line = p . stdout . readline ( ) <EOL> if not line : <EOL> break <EOL> if line [ <NUM_LIT:0> ] != '<STR_LIT:?>' : <EOL> continue <EOL> print line [ <NUM_LIT:7> : ] . strip ( ) <EOL> p . wait ( ) <EOL> p . stdout . close ( ) <EOL> def Opened ( ) : <EOL> """<STR_LIT>""" <EOL> files = GetModifiedFiles ( ) <EOL> cl_keys = files . keys ( ) <EOL> cl_keys . sort ( ) <EOL> for cl_name in cl_keys : <EOL> if cl_name : <EOL> note = "<STR_LIT>" <EOL> if len ( LoadChangelistInfo ( cl_name ) . files ) != len ( files [ cl_name ] ) : <EOL> note = "<STR_LIT>" <EOL> print "<STR_LIT>" + cl_name + note + "<STR_LIT::>" <EOL> for file in files [ cl_name ] : <EOL> print "<STR_LIT>" . join ( file ) <EOL> def Help ( argv = None ) : <EOL> if argv and argv [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> TryChange ( None , [ '<STR_LIT>' ] ) <EOL> return <EOL> print ( <EOL> """<STR_LIT>""" ) <EOL> def GetEditor ( ) : <EOL> editor = os . environ . get ( "<STR_LIT>" ) <EOL> if not editor : <EOL> editor = os . environ . get ( "<STR_LIT>" ) <EOL> if not editor : <EOL> if sys . platform . startswith ( "<STR_LIT>" ) : <EOL> editor = "<STR_LIT>" <EOL> else : <EOL> editor = "<STR_LIT>" <EOL> return editor <EOL> def GenerateDiff ( files , root = None ) : <EOL> """<STR_LIT>""" <EOL> previous_cwd = os . getcwd ( ) <EOL> if root is None : <EOL> os . chdir ( GetRepositoryRoot ( ) ) <EOL> else : <EOL> os . chdir ( root ) <EOL> diff = [ ] <EOL> for file in files : <EOL> if GetSVNFileInfo ( file ) . get ( "<STR_LIT>" ) == "<STR_LIT>" : <EOL> continue <EOL> if sys . platform . startswith ( "<STR_LIT>" ) : <EOL> parent_dir = tempfile . gettempdir ( ) <EOL> else : <EOL> parent_dir = sys . path [ <NUM_LIT:0> ] <EOL> bogus_dir = os . path . join ( parent_dir , "<STR_LIT>" ) <EOL> if not os . path . exists ( bogus_dir ) : <EOL> os . mkdir ( bogus_dir ) <EOL> diff . append ( RunShell ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , bogus_dir , file ] ) ) <EOL> os . chdir ( previous_cwd ) <EOL> return "<STR_LIT>" . join ( diff ) <EOL> def UploadCL ( change_info , args ) : <EOL> if not change_info . FileList ( ) : <EOL> print "<STR_LIT>" <EOL> return <EOL> if not "<STR_LIT>" in args : <EOL> if not DoPresubmitChecks ( change_info , committing = False ) : <EOL> return <EOL> else : <EOL> args . remove ( "<STR_LIT>" ) <EOL> no_try = "<STR_LIT>" in args <EOL> if no_try : <EOL> args . remove ( "<STR_LIT>" ) <EOL> else : <EOL> no_try = "<STR_LIT>" in args <EOL> if no_try : <EOL> args . remove ( "<STR_LIT>" ) <EOL> if "<STR_LIT>" in args : <EOL> args . remove ( "<STR_LIT>" ) <EOL> args . append ( "<STR_LIT>" ) <EOL> upload_arg = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> upload_arg . append ( "<STR_LIT>" + GetCodeReviewSetting ( "<STR_LIT>" ) ) <EOL> upload_arg . extend ( args ) <EOL> desc_file = "<STR_LIT>" <EOL> if change_info . issue : <EOL> found_message = False <EOL> for arg in args : <EOL> if arg . startswith ( "<STR_LIT>" ) or arg . startswith ( "<STR_LIT>" ) : <EOL> found_message = True <EOL> break <EOL> if not found_message : <EOL> upload_arg . append ( "<STR_LIT>" ) <EOL> upload_arg . append ( "<STR_LIT>" + change_info . issue ) <EOL> else : <EOL> handle , desc_file = tempfile . mkstemp ( text = True ) <EOL> os . write ( handle , change_info . description ) <EOL> os . close ( handle ) <EOL> upload_arg . append ( "<STR_LIT>" + GetCodeReviewSetting ( "<STR_LIT>" ) ) <EOL> upload_arg . append ( "<STR_LIT>" + desc_file + "<STR_LIT>" ) <EOL> if change_info . description : <EOL> subject = change_info . description [ : <NUM_LIT> ] <EOL> if subject . find ( "<STR_LIT:\r\n>" ) != - <NUM_LIT:1> : <EOL> subject = subject [ : subject . find ( "<STR_LIT:\r\n>" ) ] <EOL> if subject . find ( "<STR_LIT:\n>" ) != - <NUM_LIT:1> : <EOL> subject = subject [ : subject . find ( "<STR_LIT:\n>" ) ] <EOL> if len ( change_info . description ) > <NUM_LIT> : <EOL> subject = subject + "<STR_LIT>" <EOL> upload_arg . append ( "<STR_LIT>" + subject ) <EOL> previous_cwd = os . getcwd ( ) <EOL> os . chdir ( GetRepositoryRoot ( ) ) <EOL> if change_info . patch is None : <EOL> change_info . patch = GenerateDiff ( change_info . FileList ( ) ) <EOL> issue , patchset = upload . RealMain ( upload_arg , change_info . patch ) <EOL> if issue and issue != change_info . issue : <EOL> change_info . issue = issue <EOL> change_info . Save ( ) <EOL> if desc_file : <EOL> os . remove ( desc_file ) <EOL> SendToRietveld ( "<STR_LIT>" % ( issue , patchset ) , timeout = <NUM_LIT:0.5> ) <EOL> if not no_try and GetCodeReviewSetting ( '<STR_LIT>' ) . lower ( ) == '<STR_LIT:true>' : <EOL> TryChange ( change_info , [ ] , True ) <EOL> os . chdir ( previous_cwd ) <EOL> def PresubmitCL ( change_info ) : <EOL> """<STR_LIT>""" <EOL> if not change_info . FileList ( ) : <EOL> print "<STR_LIT>" <EOL> return <EOL> print "<STR_LIT>" <EOL> DoPresubmitChecks ( change_info , committing = False ) <EOL> print "<STR_LIT>" <EOL> DoPresubmitChecks ( change_info , committing = True ) <EOL> def TryChange ( change_info , args , swallow_exception = False , patchset = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import trychange <EOL> except ImportError : <EOL> if swallow_exception : <EOL> return <EOL> ErrorExit ( "<STR_LIT>" ) <EOL> if change_info : <EOL> trychange . TryChange ( args , change_info . name , change_info . FileList ( ) , <EOL> swallow_exception , patchset ) <EOL> else : <EOL> trychange . TryChange ( args ) <EOL> def Commit ( change_info , args ) : <EOL> if not change_info . FileList ( ) : <EOL> print "<STR_LIT>" <EOL> return <EOL> if not "<STR_LIT>" in args : <EOL> if not DoPresubmitChecks ( change_info , committing = True ) : <EOL> return <EOL> else : <EOL> args . remove ( "<STR_LIT>" ) <EOL> no_tree_status_check = ( "<STR_LIT>" in args or "<STR_LIT>" in args ) <EOL> if not no_tree_status_check and not IsTreeOpen ( ) : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return <EOL> commit_cmd = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> filename = '<STR_LIT>' <EOL> if change_info . issue : <EOL> change_info . description = GetIssueDescription ( change_info . issue ) <EOL> commit_message = change_info . description . replace ( '<STR_LIT:\r\n>' , '<STR_LIT:\n>' ) <EOL> if change_info . issue : <EOL> commit_message += ( '<STR_LIT>' % <EOL> ( GetCodeReviewSetting ( "<STR_LIT>" ) , <EOL> change_info . issue ) ) <EOL> handle , commit_filename = tempfile . mkstemp ( text = True ) <EOL> os . write ( handle , commit_message ) <EOL> os . close ( handle ) <EOL> handle , targets_filename = tempfile . mkstemp ( text = True ) <EOL> os . write ( handle , "<STR_LIT:\n>" . join ( change_info . FileList ( ) ) ) <EOL> os . close ( handle ) <EOL> commit_cmd += [ '<STR_LIT>' + commit_filename ] <EOL> commit_cmd += [ '<STR_LIT>' + targets_filename ] <EOL> previous_cwd = os . getcwd ( ) <EOL> os . chdir ( GetRepositoryRoot ( ) ) <EOL> output = RunShell ( commit_cmd , True ) <EOL> os . remove ( commit_filename ) <EOL> os . remove ( targets_filename ) <EOL> if output . find ( "<STR_LIT>" ) != - <NUM_LIT:1> : <EOL> change_info . Delete ( ) <EOL> if change_info . issue : <EOL> revision = re . compile ( "<STR_LIT>" , <EOL> re . DOTALL ) . match ( output ) . group ( <NUM_LIT:1> ) <EOL> viewvc_url = GetCodeReviewSetting ( "<STR_LIT>" ) <EOL> change_info . description = ( change_info . description + <EOL> "<STR_LIT>" + viewvc_url + revision ) <EOL> change_info . CloseIssue ( ) <EOL> os . chdir ( previous_cwd ) <EOL> def Change ( change_info ) : <EOL> """<STR_LIT>""" <EOL> if change_info . issue : <EOL> try : <EOL> description = GetIssueDescription ( change_info . issue ) <EOL> except urllib2 . HTTPError , err : <EOL> if err . code == <NUM_LIT> : <EOL> description = change_info . description <EOL> change_info . issue = "<STR_LIT>" <EOL> change_info . Save ( ) <EOL> else : <EOL> ErrorExit ( "<STR_LIT>" + err ) <EOL> else : <EOL> description = change_info . description <EOL> other_files = GetFilesNotInCL ( ) <EOL> separator1 = ( "<STR_LIT>" <EOL> "<STR_LIT>" + GetRepositoryRoot ( ) + "<STR_LIT:\n>" <EOL> "<STR_LIT>" + change_info . name + "<STR_LIT>" ) <EOL> separator2 = "<STR_LIT>" <EOL> text = ( description + separator1 + '<STR_LIT:\n>' + <EOL> '<STR_LIT:\n>' . join ( [ f [ <NUM_LIT:0> ] + f [ <NUM_LIT:1> ] for f in change_info . files ] ) + separator2 + <EOL> '<STR_LIT:\n>' . join ( [ f [ <NUM_LIT:0> ] + f [ <NUM_LIT:1> ] for f in other_files ] ) + '<STR_LIT:\n>' ) <EOL> handle , filename = tempfile . mkstemp ( text = True ) <EOL> os . write ( handle , text ) <EOL> os . close ( handle ) <EOL> os . system ( GetEditor ( ) + "<STR_LIT:U+0020>" + filename ) <EOL> result = ReadFile ( filename ) <EOL> os . remove ( filename ) <EOL> if not result : <EOL> return <EOL> split_result = result . split ( separator1 , <NUM_LIT:1> ) <EOL> if len ( split_result ) != <NUM_LIT:2> : <EOL> ErrorExit ( "<STR_LIT>" + result ) <EOL> new_description = split_result [ <NUM_LIT:0> ] <EOL> cl_files_text = split_result [ <NUM_LIT:1> ] <EOL> if new_description != description : <EOL> change_info . description = new_description <EOL> if change_info . issue : <EOL> change_info . UpdateRietveldDescription ( ) <EOL> new_cl_files = [ ] <EOL> for line in cl_files_text . splitlines ( ) : <EOL> if not len ( line ) : <EOL> continue <EOL> if line . startswith ( "<STR_LIT>" ) : <EOL> break <EOL> status = line [ : <NUM_LIT:7> ] <EOL> file = line [ <NUM_LIT:7> : ] <EOL> new_cl_files . append ( ( status , file ) ) <EOL> change_info . files = new_cl_files <EOL> change_info . Save ( ) <EOL> print change_info . name + "<STR_LIT>" <EOL> if change_info . MissingTests ( ) : <EOL> Warn ( "<STR_LIT>" + MISSING_TEST_MSG ) <EOL> IGNORE_PATHS = ( "<STR_LIT>" , ) <EOL> CPP_EXTENSIONS = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:h>" ) <EOL> def Lint ( change_info , args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import cpplint <EOL> except ImportError : <EOL> ErrorExit ( "<STR_LIT>" ) <EOL> previous_cwd = os . getcwd ( ) <EOL> os . chdir ( GetRepositoryRoot ( ) ) <EOL> filenames = cpplint . ParseArguments ( args + change_info . FileList ( ) ) <EOL> for file in filenames : <EOL> if len ( [ file for suffix in CPP_EXTENSIONS if file . endswith ( suffix ) ] ) : <EOL> if len ( [ file for prefix in IGNORE_PATHS if file . startswith ( prefix ) ] ) : <EOL> print "<STR_LIT>" % file <EOL> else : <EOL> cpplint . ProcessFile ( file , cpplint . _cpplint_state . verbose_level ) <EOL> print "<STR_LIT>" % cpplint . _cpplint_state . error_count <EOL> os . chdir ( previous_cwd ) <EOL> def DoPresubmitChecks ( change_info , committing ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> import presubmit <EOL> result = presubmit . DoPresubmitChecks ( change_info , <EOL> committing , <EOL> verbose = False , <EOL> output_stream = sys . stdout , <EOL> input_stream = sys . stdin ) <EOL> if not result : <EOL> print "<STR_LIT>" <EOL> return result <EOL> def Changes ( ) : <EOL> """<STR_LIT>""" <EOL> for cl in GetCLs ( ) : <EOL> change_info = LoadChangelistInfo ( cl , True , True ) <EOL> print "<STR_LIT>" + change_info . name + "<STR_LIT::>" <EOL> for file in change_info . files : <EOL> print "<STR_LIT>" . join ( file ) <EOL> def main ( argv = None ) : <EOL> if argv is None : <EOL> argv = sys . argv <EOL> if len ( argv ) == <NUM_LIT:1> : <EOL> Help ( ) <EOL> return <NUM_LIT:0> ; <EOL> if not os . path . exists ( GetInfoDir ( ) ) : <EOL> os . mkdir ( GetInfoDir ( ) ) <EOL> command = argv [ <NUM_LIT:1> ] <EOL> if command == "<STR_LIT>" : <EOL> Opened ( ) <EOL> return <NUM_LIT:0> <EOL> if command == "<STR_LIT:status>" : <EOL> Opened ( ) <EOL> print "<STR_LIT>" <EOL> UnknownFiles ( [ ] ) <EOL> return <NUM_LIT:0> <EOL> if command == "<STR_LIT>" : <EOL> UnknownFiles ( argv [ <NUM_LIT:2> : ] ) <EOL> return <NUM_LIT:0> <EOL> if command == "<STR_LIT>" : <EOL> Changes ( ) <EOL> return <NUM_LIT:0> <EOL> if command == "<STR_LIT>" : <EOL> Help ( argv [ <NUM_LIT:2> : ] ) <EOL> return <NUM_LIT:0> <EOL> if command == "<STR_LIT>" and len ( argv ) == <NUM_LIT:2> : <EOL> files = GetFilesNotInCL ( ) <EOL> print GenerateDiff ( [ x [ <NUM_LIT:1> ] for x in files ] ) <EOL> return <NUM_LIT:0> <EOL> if command == "<STR_LIT>" : <EOL> ignore = GetCodeReviewSetting ( "<STR_LIT>" ) ; <EOL> print CODEREVIEW_SETTINGS <EOL> return <NUM_LIT:0> <EOL> if len ( argv ) == <NUM_LIT:2> : <EOL> if command == "<STR_LIT>" : <EOL> changename = GenerateChangeName ( ) <EOL> else : <EOL> ErrorExit ( "<STR_LIT>" ) <EOL> else : <EOL> changename = argv [ <NUM_LIT:2> ] <EOL> fail_on_not_found = command != "<STR_LIT>" and command != "<STR_LIT>" <EOL> if command == "<STR_LIT>" and changename . find ( '<STR_LIT:U+002C>' ) != - <NUM_LIT:1> : <EOL> change_info = LoadChangelistInfoForMultiple ( changename , True , True ) <EOL> else : <EOL> change_info = LoadChangelistInfo ( changename , fail_on_not_found , True ) <EOL> if command == "<STR_LIT>" : <EOL> Change ( change_info ) <EOL> elif command == "<STR_LIT>" : <EOL> Lint ( change_info , argv [ <NUM_LIT:3> : ] ) <EOL> elif command == "<STR_LIT>" : <EOL> UploadCL ( change_info , argv [ <NUM_LIT:3> : ] ) <EOL> elif command == "<STR_LIT>" : <EOL> PresubmitCL ( change_info ) <EOL> elif command in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> Commit ( change_info , argv [ <NUM_LIT:3> : ] ) <EOL> elif command == "<STR_LIT>" : <EOL> change_info . Delete ( ) <EOL> elif command == "<STR_LIT>" : <EOL> if change_info . files : <EOL> args = argv [ <NUM_LIT:3> : ] <EOL> else : <EOL> change_info = None <EOL> args = argv [ <NUM_LIT:2> : ] <EOL> TryChange ( change_info , args ) <EOL> else : <EOL> args = [ "<STR_LIT>" , command ] <EOL> root = GetRepositoryRoot ( ) <EOL> args . extend ( [ os . path . join ( root , x ) for x in change_info . FileList ( ) ] ) <EOL> if command == "<STR_LIT>" : <EOL> moved_files = change_info . GetMoved ( root = root ) <EOL> for f in moved_files : <EOL> args . remove ( f ) <EOL> print DiffPrint ( f ) <EOL> RunShell ( args , True ) <EOL> return <NUM_LIT:0> <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( ) ) </s>
