<s> """<STR_LIT>""" <EOL> import cookielib <EOL> import getpass <EOL> import logging <EOL> import mimetypes <EOL> import optparse <EOL> import os <EOL> import re <EOL> import socket <EOL> import subprocess <EOL> import sys <EOL> import urllib <EOL> import urllib2 <EOL> import urlparse <EOL> try : <EOL> from hashlib import md5 <EOL> except ImportError : <EOL> from md5 import md5 <EOL> try : <EOL> import readline <EOL> except ImportError : <EOL> pass <EOL> verbosity = <NUM_LIT:1> <EOL> MAX_UPLOAD_SIZE = <NUM_LIT> * <NUM_LIT> <EOL> VCS_GIT = "<STR_LIT>" <EOL> VCS_MERCURIAL = "<STR_LIT>" <EOL> VCS_SUBVERSION = "<STR_LIT>" <EOL> VCS_UNKNOWN = "<STR_LIT>" <EOL> TEXT_MIMETYPES = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def GetEmail ( prompt ) : <EOL> """<STR_LIT>""" <EOL> last_email_file_name = os . path . expanduser ( "<STR_LIT>" ) <EOL> last_email = "<STR_LIT>" <EOL> if os . path . exists ( last_email_file_name ) : <EOL> try : <EOL> last_email_file = open ( last_email_file_name , "<STR_LIT:r>" ) <EOL> last_email = last_email_file . readline ( ) . strip ( "<STR_LIT:\n>" ) <EOL> last_email_file . close ( ) <EOL> prompt += "<STR_LIT>" % last_email <EOL> except IOError , e : <EOL> pass <EOL> email = raw_input ( prompt + "<STR_LIT>" ) . strip ( ) <EOL> if email : <EOL> try : <EOL> last_email_file = open ( last_email_file_name , "<STR_LIT:w>" ) <EOL> last_email_file . write ( email ) <EOL> last_email_file . close ( ) <EOL> except IOError , e : <EOL> pass <EOL> else : <EOL> email = last_email <EOL> return email <EOL> def StatusUpdate ( msg ) : <EOL> """<STR_LIT>""" <EOL> if verbosity > <NUM_LIT:0> : <EOL> print msg <EOL> def ErrorExit ( msg ) : <EOL> """<STR_LIT>""" <EOL> print >> sys . stderr , msg <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> class ClientLoginError ( urllib2 . HTTPError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , url , code , msg , headers , args ) : <EOL> urllib2 . HTTPError . __init__ ( self , url , code , msg , headers , None ) <EOL> self . args = args <EOL> self . reason = args [ "<STR_LIT>" ] <EOL> class AbstractRpcServer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , auth_function , host_override = None , extra_headers = { } , <EOL> save_cookies = False ) : <EOL> """<STR_LIT>""" <EOL> self . host = host <EOL> self . host_override = host_override <EOL> self . auth_function = auth_function <EOL> self . authenticated = False <EOL> self . extra_headers = extra_headers <EOL> self . save_cookies = save_cookies <EOL> self . opener = self . _GetOpener ( ) <EOL> if self . host_override : <EOL> logging . info ( "<STR_LIT>" , self . host , self . host_override ) <EOL> else : <EOL> logging . info ( "<STR_LIT>" , self . host ) <EOL> def _GetOpener ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _CreateRequest ( self , url , data = None ) : <EOL> """<STR_LIT>""" <EOL> logging . debug ( "<STR_LIT>" , url , data ) <EOL> req = urllib2 . Request ( url , data = data ) <EOL> if self . host_override : <EOL> req . add_header ( "<STR_LIT>" , self . host_override ) <EOL> for key , value in self . extra_headers . iteritems ( ) : <EOL> req . add_header ( key , value ) <EOL> return req <EOL> def _GetAuthToken ( self , email , password ) : <EOL> """<STR_LIT>""" <EOL> account_type = "<STR_LIT>" <EOL> if self . host . endswith ( "<STR_LIT>" ) : <EOL> account_type = "<STR_LIT>" <EOL> req = self . _CreateRequest ( <EOL> url = "<STR_LIT>" , <EOL> data = urllib . urlencode ( { <EOL> "<STR_LIT>" : email , <EOL> "<STR_LIT>" : password , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:source>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : account_type , <EOL> } ) , <EOL> ) <EOL> try : <EOL> response = self . opener . open ( req ) <EOL> response_body = response . read ( ) <EOL> response_dict = dict ( x . split ( "<STR_LIT:=>" ) <EOL> for x in response_body . split ( "<STR_LIT:\n>" ) if x ) <EOL> return response_dict [ "<STR_LIT>" ] <EOL> except urllib2 . HTTPError , e : <EOL> if e . code == <NUM_LIT> : <EOL> body = e . read ( ) <EOL> response_dict = dict ( x . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) for x in body . split ( "<STR_LIT:\n>" ) if x ) <EOL> raise ClientLoginError ( req . get_full_url ( ) , e . code , e . msg , <EOL> e . headers , response_dict ) <EOL> else : <EOL> raise <EOL> def _GetAuthCookie ( self , auth_token ) : <EOL> """<STR_LIT>""" <EOL> continue_location = "<STR_LIT>" <EOL> args = { "<STR_LIT>" : continue_location , "<STR_LIT>" : auth_token } <EOL> req = self . _CreateRequest ( "<STR_LIT>" % <EOL> ( self . host , urllib . urlencode ( args ) ) ) <EOL> try : <EOL> response = self . opener . open ( req ) <EOL> except urllib2 . HTTPError , e : <EOL> response = e <EOL> if ( response . code != <NUM_LIT> or <EOL> response . info ( ) [ "<STR_LIT:location>" ] != continue_location ) : <EOL> raise urllib2 . HTTPError ( req . get_full_url ( ) , response . code , response . msg , <EOL> response . headers , response . fp ) <EOL> self . authenticated = True <EOL> def _Authenticate ( self ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> credentials = self . auth_function ( ) <EOL> try : <EOL> auth_token = self . _GetAuthToken ( credentials [ <NUM_LIT:0> ] , credentials [ <NUM_LIT:1> ] ) <EOL> except ClientLoginError , e : <EOL> if e . reason == "<STR_LIT>" : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> continue <EOL> if e . reason == "<STR_LIT>" : <EOL> print >> sys . stderr , ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> break <EOL> if e . reason == "<STR_LIT>" : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> break <EOL> if e . reason == "<STR_LIT>" : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> break <EOL> if e . reason == "<STR_LIT>" : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> break <EOL> if e . reason == "<STR_LIT>" : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> break <EOL> if e . reason == "<STR_LIT>" : <EOL> print >> sys . stderr , ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> break <EOL> if e . reason == "<STR_LIT>" : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> break <EOL> raise <EOL> self . _GetAuthCookie ( auth_token ) <EOL> return <EOL> def Send ( self , request_path , payload = None , <EOL> content_type = "<STR_LIT>" , <EOL> timeout = None , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not self . authenticated : <EOL> self . _Authenticate ( ) <EOL> old_timeout = socket . getdefaulttimeout ( ) <EOL> socket . setdefaulttimeout ( timeout ) <EOL> try : <EOL> tries = <NUM_LIT:0> <EOL> while True : <EOL> tries += <NUM_LIT:1> <EOL> args = dict ( kwargs ) <EOL> url = "<STR_LIT>" % ( self . host , request_path ) <EOL> if args : <EOL> url += "<STR_LIT:?>" + urllib . urlencode ( args ) <EOL> req = self . _CreateRequest ( url = url , data = payload ) <EOL> req . add_header ( "<STR_LIT:Content-Type>" , content_type ) <EOL> try : <EOL> f = self . opener . open ( req ) <EOL> response = f . read ( ) <EOL> f . close ( ) <EOL> return response <EOL> except urllib2 . HTTPError , e : <EOL> if tries > <NUM_LIT:3> : <EOL> raise <EOL> elif e . code == <NUM_LIT> or e . code == <NUM_LIT> : <EOL> self . _Authenticate ( ) <EOL> else : <EOL> raise <EOL> finally : <EOL> socket . setdefaulttimeout ( old_timeout ) <EOL> class HttpRpcServer ( AbstractRpcServer ) : <EOL> """<STR_LIT>""" <EOL> def _Authenticate ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( HttpRpcServer , self ) . _Authenticate ( ) <EOL> if self . save_cookies : <EOL> StatusUpdate ( "<STR_LIT>" % self . cookie_file ) <EOL> self . cookie_jar . save ( ) <EOL> def _GetOpener ( self ) : <EOL> """<STR_LIT>""" <EOL> opener = urllib2 . OpenerDirector ( ) <EOL> opener . add_handler ( urllib2 . ProxyHandler ( ) ) <EOL> opener . add_handler ( urllib2 . UnknownHandler ( ) ) <EOL> opener . add_handler ( urllib2 . HTTPHandler ( ) ) <EOL> opener . add_handler ( urllib2 . HTTPDefaultErrorHandler ( ) ) <EOL> opener . add_handler ( urllib2 . HTTPSHandler ( ) ) <EOL> opener . add_handler ( urllib2 . HTTPErrorProcessor ( ) ) <EOL> if self . save_cookies : <EOL> self . cookie_file = os . path . expanduser ( "<STR_LIT>" ) <EOL> self . cookie_jar = cookielib . MozillaCookieJar ( self . cookie_file ) <EOL> if os . path . exists ( self . cookie_file ) : <EOL> try : <EOL> self . cookie_jar . load ( ) <EOL> self . authenticated = True <EOL> StatusUpdate ( "<STR_LIT>" % <EOL> self . cookie_file ) <EOL> except ( cookielib . LoadError , IOError ) : <EOL> pass <EOL> else : <EOL> fd = os . open ( self . cookie_file , os . O_CREAT , <NUM_LIT:0> <NUM_LIT> ) <EOL> os . close ( fd ) <EOL> os . chmod ( self . cookie_file , <NUM_LIT:0> <NUM_LIT> ) <EOL> else : <EOL> self . cookie_jar = cookielib . CookieJar ( ) <EOL> opener . add_handler ( urllib2 . HTTPCookieProcessor ( self . cookie_jar ) ) <EOL> return opener <EOL> parser = optparse . OptionParser ( usage = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> dest = "<STR_LIT>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> group = parser . add_option_group ( "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT>" , const = <NUM_LIT:0> , <EOL> dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT>" , const = <NUM_LIT:2> , <EOL> dest = "<STR_LIT>" , default = <NUM_LIT:1> , <EOL> help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , action = "<STR_LIT>" , const = <NUM_LIT:3> , <EOL> dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> group = parser . add_option_group ( "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> default = "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" , <EOL> help = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT:email>" , <EOL> metavar = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT:host>" , <EOL> metavar = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , action = "<STR_LIT>" , <EOL> dest = "<STR_LIT>" , default = True , <EOL> help = "<STR_LIT>" ) <EOL> group = parser . add_option_group ( "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT:description>" , <EOL> metavar = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , <EOL> dest = "<STR_LIT>" , metavar = "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> default = False , <EOL> help = "<STR_LIT>" ) <EOL> group = parser . add_option_group ( "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT:message>" , <EOL> metavar = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , "<STR_LIT>" , type = "<STR_LIT:int>" , action = "<STR_LIT:store>" , <EOL> metavar = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> dest = "<STR_LIT>" , default = False , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> metavar = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> group . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> dest = "<STR_LIT>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> def GetRpcServer ( options ) : <EOL> """<STR_LIT>""" <EOL> rpc_server_class = HttpRpcServer <EOL> def GetUserCredentials ( ) : <EOL> """<STR_LIT>""" <EOL> email = options . email <EOL> if email is None : <EOL> email = GetEmail ( "<STR_LIT>" % options . server ) <EOL> password = getpass . getpass ( "<STR_LIT>" % email ) <EOL> return ( email , password ) <EOL> host = ( options . host or options . server ) . lower ( ) <EOL> if host == "<STR_LIT:localhost>" or host . startswith ( "<STR_LIT>" ) : <EOL> email = options . email <EOL> if email is None : <EOL> email = "<STR_LIT>" <EOL> logging . info ( "<STR_LIT>" % email ) <EOL> server = rpc_server_class ( <EOL> options . server , <EOL> lambda : ( email , "<STR_LIT:password>" ) , <EOL> host_override = options . host , <EOL> extra_headers = { "<STR_LIT>" : <EOL> '<STR_LIT>' % email } , <EOL> save_cookies = options . save_cookies ) <EOL> server . authenticated = True <EOL> return server <EOL> return rpc_server_class ( options . server , GetUserCredentials , <EOL> host_override = options . host , <EOL> save_cookies = options . save_cookies ) <EOL> def EncodeMultipartFormData ( fields , files ) : <EOL> """<STR_LIT>""" <EOL> BOUNDARY = '<STR_LIT>' <EOL> CRLF = '<STR_LIT:\r\n>' <EOL> lines = [ ] <EOL> for ( key , value ) in fields : <EOL> lines . append ( '<STR_LIT>' + BOUNDARY ) <EOL> lines . append ( '<STR_LIT>' % key ) <EOL> lines . append ( '<STR_LIT>' ) <EOL> lines . append ( value ) <EOL> for ( key , filename , value ) in files : <EOL> lines . append ( '<STR_LIT>' + BOUNDARY ) <EOL> lines . append ( '<STR_LIT>' % <EOL> ( key , filename ) ) <EOL> lines . append ( '<STR_LIT>' % GetContentType ( filename ) ) <EOL> lines . append ( '<STR_LIT>' ) <EOL> lines . append ( value ) <EOL> lines . append ( '<STR_LIT>' + BOUNDARY + '<STR_LIT>' ) <EOL> lines . append ( '<STR_LIT>' ) <EOL> body = CRLF . join ( lines ) <EOL> content_type = '<STR_LIT>' % BOUNDARY <EOL> return content_type , body <EOL> def GetContentType ( filename ) : <EOL> """<STR_LIT>""" <EOL> return mimetypes . guess_type ( filename ) [ <NUM_LIT:0> ] or '<STR_LIT>' <EOL> use_shell = sys . platform . startswith ( "<STR_LIT>" ) <EOL> def RunShellWithReturnCode ( command , print_output = False , <EOL> universal_newlines = True , <EOL> env = os . environ ) : <EOL> """<STR_LIT>""" <EOL> logging . info ( "<STR_LIT>" , command ) <EOL> p = subprocess . Popen ( command , stdout = subprocess . PIPE , stderr = subprocess . PIPE , <EOL> shell = use_shell , universal_newlines = universal_newlines , <EOL> env = env ) <EOL> if print_output : <EOL> output_array = [ ] <EOL> while True : <EOL> line = p . stdout . readline ( ) <EOL> if not line : <EOL> break <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> errout = p . stderr . read ( ) <EOL> if print_output and errout : <EOL> print >> sys . stderr , errout <EOL> p . stdout . close ( ) <EOL> p . stderr . close ( ) <EOL> return output , p . returncode <EOL> def RunShell ( command , silent_ok = False , universal_newlines = True , <EOL> print_output = False , env = os . environ ) : <EOL> data , retcode = RunShellWithReturnCode ( command , print_output , <EOL> universal_newlines , env ) <EOL> if retcode : <EOL> ErrorExit ( "<STR_LIT>" % ( command , data ) ) <EOL> if not silent_ok and not data : <EOL> ErrorExit ( "<STR_LIT>" % command ) <EOL> return data <EOL> class VersionControlSystem ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , options ) : <EOL> """<STR_LIT>""" <EOL> self . options = options <EOL> def GenerateDiff ( self , args ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" % self . __class__ ) <EOL> def GetUnknownFiles ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" % self . __class__ ) <EOL> def CheckForUnknownFiles ( self ) : <EOL> """<STR_LIT>""" <EOL> unknown_files = self . GetUnknownFiles ( ) <EOL> if unknown_files : <EOL> print "<STR_LIT>" <EOL> for line in unknown_files : <EOL> print line <EOL> prompt = "<STR_LIT>" <EOL> answer = raw_input ( prompt ) . strip ( ) <EOL> if answer != "<STR_LIT:y>" : <EOL> ErrorExit ( "<STR_LIT>" ) <EOL> def GetBaseFile ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" % self . __class__ ) <EOL> def GetBaseFiles ( self , diff ) : <EOL> """<STR_LIT>""" <EOL> files = { } <EOL> for line in diff . splitlines ( True ) : <EOL> if line . startswith ( '<STR_LIT>' ) or line . startswith ( '<STR_LIT>' ) : <EOL> unused , filename = line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> filename = filename . strip ( ) . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> files [ filename ] = self . GetBaseFile ( filename ) <EOL> return files <EOL> def UploadBaseFiles ( self , issue , rpc_server , patch_list , patchset , options , <EOL> files ) : <EOL> """<STR_LIT>""" <EOL> def UploadFile ( filename , file_id , content , is_binary , status , is_base ) : <EOL> """<STR_LIT>""" <EOL> file_too_large = False <EOL> if is_base : <EOL> type = "<STR_LIT>" <EOL> else : <EOL> type = "<STR_LIT>" <EOL> if len ( content ) > MAX_UPLOAD_SIZE : <EOL> print ( "<STR_LIT>" % <EOL> ( type , filename ) ) <EOL> file_too_large = True <EOL> content = "<STR_LIT>" <EOL> checksum = md5 ( content ) . hexdigest ( ) <EOL> if options . verbose > <NUM_LIT:0> and not file_too_large : <EOL> print "<STR_LIT>" % ( type , filename ) <EOL> url = "<STR_LIT>" % ( int ( issue ) , int ( patchset ) , file_id ) <EOL> form_fields = [ ( "<STR_LIT:filename>" , filename ) , <EOL> ( "<STR_LIT:status>" , status ) , <EOL> ( "<STR_LIT>" , checksum ) , <EOL> ( "<STR_LIT>" , str ( is_binary ) ) , <EOL> ( "<STR_LIT>" , str ( not is_base ) ) , <EOL> ] <EOL> if file_too_large : <EOL> form_fields . append ( ( "<STR_LIT>" , "<STR_LIT:1>" ) ) <EOL> if options . email : <EOL> form_fields . append ( ( "<STR_LIT:user>" , options . email ) ) <EOL> ctype , body = EncodeMultipartFormData ( form_fields , <EOL> [ ( "<STR_LIT:data>" , filename , content ) ] ) <EOL> response_body = rpc_server . Send ( url , body , <EOL> content_type = ctype ) <EOL> if not response_body . startswith ( "<STR_LIT:OK>" ) : <EOL> StatusUpdate ( "<STR_LIT>" % response_body ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> patches = dict ( ) <EOL> [ patches . setdefault ( v , k ) for k , v in patch_list ] <EOL> for filename in patches . keys ( ) : <EOL> base_content , new_content , is_binary , status = files [ filename ] <EOL> file_id_str = patches . get ( filename ) <EOL> if file_id_str . find ( "<STR_LIT>" ) != - <NUM_LIT:1> : <EOL> base_content = None <EOL> file_id_str = file_id_str [ file_id_str . rfind ( "<STR_LIT:_>" ) + <NUM_LIT:1> : ] <EOL> file_id = int ( file_id_str ) <EOL> if base_content != None : <EOL> UploadFile ( filename , file_id , base_content , is_binary , status , True ) <EOL> if new_content != None : <EOL> UploadFile ( filename , file_id , new_content , is_binary , status , False ) <EOL> def IsImage ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> mimetype = mimetypes . guess_type ( filename ) [ <NUM_LIT:0> ] <EOL> if not mimetype : <EOL> return False <EOL> return mimetype . startswith ( "<STR_LIT>" ) <EOL> def IsBinary ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> mimetype = mimetypes . guess_type ( filename ) [ <NUM_LIT:0> ] <EOL> if not mimetype : <EOL> return False <EOL> if mimetype in TEXT_MIMETYPES : <EOL> return False <EOL> return not mimetype . startswith ( "<STR_LIT>" ) <EOL> class SubversionVCS ( VersionControlSystem ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , options ) : <EOL> super ( SubversionVCS , self ) . __init__ ( options ) <EOL> if self . options . revision : <EOL> match = re . match ( r"<STR_LIT>" , self . options . revision ) <EOL> if not match : <EOL> ErrorExit ( "<STR_LIT>" % self . options . revision ) <EOL> self . rev_start = match . group ( <NUM_LIT:1> ) <EOL> self . rev_end = match . group ( <NUM_LIT:3> ) <EOL> else : <EOL> self . rev_start = self . rev_end = None <EOL> self . svnls_cache = { } <EOL> required = self . options . download_base or self . options . revision is not None <EOL> self . svn_base = self . _GuessBase ( required ) <EOL> def GuessBase ( self , required ) : <EOL> """<STR_LIT>""" <EOL> return self . svn_base <EOL> def _GuessBase ( self , required ) : <EOL> """<STR_LIT>""" <EOL> info = RunShell ( [ "<STR_LIT>" , "<STR_LIT:info>" ] ) <EOL> for line in info . splitlines ( ) : <EOL> words = line . split ( ) <EOL> if len ( words ) == <NUM_LIT:2> and words [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> url = words [ <NUM_LIT:1> ] <EOL> scheme , netloc , path , params , query , fragment = urlparse . urlparse ( url ) <EOL> username , netloc = urllib . splituser ( netloc ) <EOL> if username : <EOL> logging . info ( "<STR_LIT>" ) <EOL> if netloc . endswith ( "<STR_LIT>" ) : <EOL> if netloc == "<STR_LIT>" : <EOL> if path . startswith ( "<STR_LIT>" ) : <EOL> path = path [ <NUM_LIT:9> : ] <EOL> elif netloc != "<STR_LIT>" : <EOL> ErrorExit ( "<STR_LIT>" % url ) <EOL> base = "<STR_LIT>" % path <EOL> logging . info ( "<STR_LIT>" , base ) <EOL> elif netloc . endswith ( "<STR_LIT>" ) : <EOL> if path . startswith ( "<STR_LIT>" ) : <EOL> path = path [ <NUM_LIT:6> : ] <EOL> base = "<STR_LIT>" % path <EOL> logging . info ( "<STR_LIT>" , base ) <EOL> elif netloc . endswith ( "<STR_LIT>" ) : <EOL> path = path + "<STR_LIT:/>" <EOL> base = urlparse . urlunparse ( ( "<STR_LIT:http>" , netloc , path , params , <EOL> query , fragment ) ) <EOL> logging . info ( "<STR_LIT>" , base ) <EOL> else : <EOL> path = path + "<STR_LIT:/>" <EOL> base = urlparse . urlunparse ( ( scheme , netloc , path , params , <EOL> query , fragment ) ) <EOL> logging . info ( "<STR_LIT>" , base ) <EOL> return base <EOL> if required : <EOL> ErrorExit ( "<STR_LIT>" ) <EOL> return None <EOL> def GenerateDiff ( self , args ) : <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> if self . options . revision : <EOL> cmd += [ "<STR_LIT>" , self . options . revision ] <EOL> cmd . extend ( args ) <EOL> data = RunShell ( cmd ) <EOL> count = <NUM_LIT:0> <EOL> for line in data . splitlines ( ) : <EOL> if line . startswith ( "<STR_LIT>" ) or line . startswith ( "<STR_LIT>" ) : <EOL> count += <NUM_LIT:1> <EOL> logging . info ( line ) <EOL> if not count : <EOL> ErrorExit ( "<STR_LIT>" ) <EOL> return data <EOL> def _CollapseKeywords ( self , content , keyword_str ) : <EOL> """<STR_LIT>""" <EOL> svn_keywords = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } <EOL> def repl ( m ) : <EOL> if m . group ( <NUM_LIT:2> ) : <EOL> return "<STR_LIT>" % ( m . group ( <NUM_LIT:1> ) , "<STR_LIT:U+0020>" * len ( m . group ( <NUM_LIT:3> ) ) ) <EOL> return "<STR_LIT>" % m . group ( <NUM_LIT:1> ) <EOL> keywords = [ keyword <EOL> for name in keyword_str . split ( "<STR_LIT:U+0020>" ) <EOL> for keyword in svn_keywords . get ( name , [ ] ) ] <EOL> return re . sub ( r"<STR_LIT>" % '<STR_LIT:|>' . join ( keywords ) , repl , content ) <EOL> def GetUnknownFiles ( self ) : <EOL> status = RunShell ( [ "<STR_LIT>" , "<STR_LIT:status>" , "<STR_LIT>" ] , silent_ok = True ) <EOL> unknown_files = [ ] <EOL> for line in status . split ( "<STR_LIT:\n>" ) : <EOL> if line and line [ <NUM_LIT:0> ] == "<STR_LIT:?>" : <EOL> unknown_files . append ( line ) <EOL> return unknown_files <EOL> def ReadFile ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> file = open ( filename , '<STR_LIT:rb>' ) <EOL> result = "<STR_LIT>" <EOL> try : <EOL> result = file . read ( ) <EOL> finally : <EOL> file . close ( ) <EOL> return result <EOL> def GetStatus ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> if not self . options . revision : <EOL> status = RunShell ( [ "<STR_LIT>" , "<STR_LIT:status>" , "<STR_LIT>" , filename ] ) <EOL> if not status : <EOL> ErrorExit ( "<STR_LIT>" % filename ) <EOL> status_lines = status . splitlines ( ) <EOL> if ( len ( status_lines ) == <NUM_LIT:3> and <EOL> not status_lines [ <NUM_LIT:0> ] and <EOL> status_lines [ <NUM_LIT:1> ] . startswith ( "<STR_LIT>" ) ) : <EOL> status = status_lines [ <NUM_LIT:2> ] <EOL> else : <EOL> status = status_lines [ <NUM_LIT:0> ] <EOL> else : <EOL> dirname , relfilename = os . path . split ( filename ) <EOL> if dirname not in self . svnls_cache : <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT:list>" , "<STR_LIT>" , self . rev_start , dirname or "<STR_LIT:.>" ] <EOL> out , returncode = RunShellWithReturnCode ( cmd ) <EOL> if returncode : <EOL> ErrorExit ( "<STR_LIT>" % filename ) <EOL> old_files = out . splitlines ( ) <EOL> args = [ "<STR_LIT>" , "<STR_LIT:list>" ] <EOL> if self . rev_end : <EOL> args += [ "<STR_LIT>" , self . rev_end ] <EOL> cmd = args + [ dirname or "<STR_LIT:.>" ] <EOL> out , returncode = RunShellWithReturnCode ( cmd ) <EOL> if returncode : <EOL> ErrorExit ( "<STR_LIT>" % cmd ) <EOL> self . svnls_cache [ dirname ] = ( old_files , out . splitlines ( ) ) <EOL> old_files , new_files = self . svnls_cache [ dirname ] <EOL> if relfilename in old_files and relfilename not in new_files : <EOL> status = "<STR_LIT>" <EOL> elif relfilename in old_files and relfilename in new_files : <EOL> status = "<STR_LIT>" <EOL> else : <EOL> status = "<STR_LIT>" <EOL> return status <EOL> def GetBaseFile ( self , filename ) : <EOL> status = self . GetStatus ( filename ) <EOL> base_content = None <EOL> new_content = None <EOL> if status [ <NUM_LIT:0> ] == "<STR_LIT:A>" and status [ <NUM_LIT:3> ] != "<STR_LIT:+>" : <EOL> mimetype = RunShell ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , filename ] , <EOL> silent_ok = True ) <EOL> base_content = "<STR_LIT>" <EOL> is_binary = bool ( mimetype ) and not mimetype . startswith ( "<STR_LIT>" ) <EOL> if is_binary and self . IsImage ( filename ) : <EOL> new_content = self . ReadFile ( filename ) <EOL> elif ( status [ <NUM_LIT:0> ] in ( "<STR_LIT:M>" , "<STR_LIT:D>" , "<STR_LIT:R>" ) or <EOL> ( status [ <NUM_LIT:0> ] == "<STR_LIT:A>" and status [ <NUM_LIT:3> ] == "<STR_LIT:+>" ) or <EOL> ( status [ <NUM_LIT:0> ] == "<STR_LIT:U+0020>" and status [ <NUM_LIT:1> ] == "<STR_LIT:M>" ) ) : <EOL> args = [ ] <EOL> if self . options . revision : <EOL> url = "<STR_LIT>" % ( self . svn_base , filename , self . rev_start ) <EOL> else : <EOL> url = filename <EOL> args += [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> cmd = [ "<STR_LIT>" ] + args + [ "<STR_LIT>" , "<STR_LIT>" , url ] <EOL> mimetype , returncode = RunShellWithReturnCode ( cmd ) <EOL> if returncode : <EOL> mimetype = "<STR_LIT>" <EOL> get_base = False <EOL> is_binary = bool ( mimetype ) and not mimetype . startswith ( "<STR_LIT>" ) <EOL> if status [ <NUM_LIT:0> ] == "<STR_LIT:U+0020>" : <EOL> base_content = "<STR_LIT>" <EOL> elif is_binary : <EOL> if self . IsImage ( filename ) : <EOL> get_base = True <EOL> if status [ <NUM_LIT:0> ] == "<STR_LIT:M>" : <EOL> if not self . rev_end : <EOL> new_content = self . ReadFile ( filename ) <EOL> else : <EOL> url = "<STR_LIT>" % ( self . svn_base , filename , self . rev_end ) <EOL> new_content = RunShell ( [ "<STR_LIT>" , "<STR_LIT>" , url ] , <EOL> universal_newlines = True , silent_ok = True ) <EOL> else : <EOL> base_content = "<STR_LIT>" <EOL> else : <EOL> get_base = True <EOL> if get_base : <EOL> if is_binary : <EOL> universal_newlines = False <EOL> else : <EOL> universal_newlines = True <EOL> if self . rev_start : <EOL> url = "<STR_LIT>" % ( self . svn_base , filename , self . rev_start ) <EOL> base_content = RunShell ( [ "<STR_LIT>" , "<STR_LIT>" , url ] , <EOL> universal_newlines = universal_newlines , <EOL> silent_ok = True ) <EOL> else : <EOL> base_content = RunShell ( [ "<STR_LIT>" , "<STR_LIT>" , filename ] , <EOL> universal_newlines = universal_newlines , <EOL> silent_ok = True ) <EOL> if not is_binary : <EOL> args = [ ] <EOL> if self . rev_start : <EOL> url = "<STR_LIT>" % ( self . svn_base , filename , self . rev_start ) <EOL> else : <EOL> url = filename <EOL> args += [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> cmd = [ "<STR_LIT>" ] + args + [ "<STR_LIT>" , "<STR_LIT>" , url ] <EOL> keywords , returncode = RunShellWithReturnCode ( cmd ) <EOL> if keywords and not returncode : <EOL> base_content = self . _CollapseKeywords ( base_content , keywords ) <EOL> else : <EOL> StatusUpdate ( "<STR_LIT>" % status ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return base_content , new_content , is_binary , status [ <NUM_LIT:0> : <NUM_LIT:5> ] <EOL> class GitVCS ( VersionControlSystem ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , options ) : <EOL> super ( GitVCS , self ) . __init__ ( options ) <EOL> self . hashes = { } <EOL> self . renames = { } <EOL> def GenerateDiff ( self , extra_args ) : <EOL> NULL_HASH = "<STR_LIT:0>" * <NUM_LIT> <EOL> extra_args = extra_args [ : ] <EOL> if self . options . revision : <EOL> extra_args = [ self . options . revision ] + extra_args <EOL> extra_args . append ( '<STR_LIT>' ) <EOL> env = os . environ . copy ( ) <EOL> if '<STR_LIT>' in env : del env [ '<STR_LIT>' ] <EOL> gitdiff = RunShell ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> + extra_args , env = env ) <EOL> svndiff = [ ] <EOL> filecount = <NUM_LIT:0> <EOL> filename = None <EOL> for line in gitdiff . splitlines ( ) : <EOL> match = re . match ( r"<STR_LIT>" , line ) <EOL> if match : <EOL> filecount += <NUM_LIT:1> <EOL> filename = match . group ( <NUM_LIT:2> ) <EOL> svndiff . append ( "<STR_LIT>" % filename ) <EOL> if match . group ( <NUM_LIT:1> ) != match . group ( <NUM_LIT:2> ) : <EOL> self . renames [ match . group ( <NUM_LIT:2> ) ] = match . group ( <NUM_LIT:1> ) <EOL> else : <EOL> match = re . match ( r"<STR_LIT>" , line ) <EOL> if match : <EOL> before , after = ( match . group ( <NUM_LIT:1> ) , match . group ( <NUM_LIT:2> ) ) <EOL> if before == NULL_HASH : <EOL> before = None <EOL> if after == NULL_HASH : <EOL> after = None <EOL> self . hashes [ filename ] = ( before , after ) <EOL> svndiff . append ( line + "<STR_LIT:\n>" ) <EOL> if not filecount : <EOL> ErrorExit ( "<STR_LIT>" ) <EOL> return "<STR_LIT>" . join ( svndiff ) <EOL> def GetUnknownFiles ( self ) : <EOL> status = RunShell ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> silent_ok = True ) <EOL> return status . splitlines ( ) <EOL> def GetFileContent ( self , file_hash , is_binary ) : <EOL> """<STR_LIT>""" <EOL> data , retcode = RunShellWithReturnCode ( [ "<STR_LIT>" , "<STR_LIT>" , file_hash ] , <EOL> universal_newlines = not is_binary ) <EOL> if retcode : <EOL> ErrorExit ( "<STR_LIT>" % file_hash ) <EOL> return data <EOL> def GetBaseFile ( self , filename ) : <EOL> hash_before , hash_after = self . hashes . get ( filename , ( None , None ) ) <EOL> base_content = None <EOL> new_content = None <EOL> is_binary = self . IsBinary ( filename ) <EOL> status = None <EOL> if filename in self . renames : <EOL> status = "<STR_LIT>" <EOL> if filename not in self . hashes : <EOL> base_content = RunShell ( [ "<STR_LIT>" , "<STR_LIT>" , filename ] ) <EOL> elif not hash_before : <EOL> status = "<STR_LIT:A>" <EOL> base_content = "<STR_LIT>" <EOL> elif not hash_after : <EOL> status = "<STR_LIT:D>" <EOL> else : <EOL> status = "<STR_LIT:M>" <EOL> is_image = self . IsImage ( filename ) <EOL> if not is_binary or is_image : <EOL> if base_content is None and hash_before : <EOL> base_content = self . GetFileContent ( hash_before , is_binary ) <EOL> if is_image and hash_after : <EOL> new_content = self . GetFileContent ( hash_after , is_binary ) <EOL> return ( base_content , new_content , is_binary , status ) <EOL> class MercurialVCS ( VersionControlSystem ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , options , repo_dir ) : <EOL> super ( MercurialVCS , self ) . __init__ ( options ) <EOL> self . repo_dir = os . path . normpath ( repo_dir ) <EOL> cwd = os . path . normpath ( os . getcwd ( ) ) <EOL> assert cwd . startswith ( self . repo_dir ) <EOL> self . subdir = cwd [ len ( self . repo_dir ) : ] . lstrip ( r"<STR_LIT>" ) <EOL> if self . options . revision : <EOL> self . base_rev = self . options . revision <EOL> else : <EOL> self . base_rev = RunShell ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) . split ( '<STR_LIT::>' ) [ <NUM_LIT:1> ] . strip ( ) <EOL> def _GetRelPath ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> assert filename . startswith ( self . subdir ) , filename <EOL> return filename [ len ( self . subdir ) : ] . lstrip ( r"<STR_LIT>" ) <EOL> def GenerateDiff ( self , extra_args ) : <EOL> extra_args = extra_args or [ "<STR_LIT:.>" ] <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , self . base_rev ] + extra_args <EOL> data = RunShell ( cmd , silent_ok = True ) <EOL> svndiff = [ ] <EOL> filecount = <NUM_LIT:0> <EOL> for line in data . splitlines ( ) : <EOL> m = re . match ( "<STR_LIT>" , line ) <EOL> if m : <EOL> filename = m . group ( <NUM_LIT:2> ) <EOL> svndiff . append ( "<STR_LIT>" % filename ) <EOL> svndiff . append ( "<STR_LIT:=>" * <NUM_LIT> ) <EOL> filecount += <NUM_LIT:1> <EOL> logging . info ( line ) <EOL> else : <EOL> svndiff . append ( line ) <EOL> if not filecount : <EOL> ErrorExit ( "<STR_LIT>" ) <EOL> return "<STR_LIT:\n>" . join ( svndiff ) + "<STR_LIT:\n>" <EOL> def GetUnknownFiles ( self ) : <EOL> """<STR_LIT>""" <EOL> args = [ ] <EOL> status = RunShell ( [ "<STR_LIT>" , "<STR_LIT:status>" , "<STR_LIT>" , self . base_rev , "<STR_LIT>" , "<STR_LIT:.>" ] , <EOL> silent_ok = True ) <EOL> unknown_files = [ ] <EOL> for line in status . splitlines ( ) : <EOL> st , fn = line . split ( "<STR_LIT:U+0020>" , <NUM_LIT:1> ) <EOL> if st == "<STR_LIT:?>" : <EOL> unknown_files . append ( fn ) <EOL> return unknown_files <EOL> def GetBaseFile ( self , filename ) : <EOL> base_content = "<STR_LIT>" <EOL> new_content = None <EOL> is_binary = False <EOL> oldrelpath = relpath = self . _GetRelPath ( filename ) <EOL> out = RunShell ( [ "<STR_LIT>" , "<STR_LIT:status>" , "<STR_LIT>" , "<STR_LIT>" , self . base_rev , relpath ] ) <EOL> out = out . splitlines ( ) <EOL> if out [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' % relpath ) : <EOL> out = out [ <NUM_LIT:1> : ] <EOL> if len ( out ) > <NUM_LIT:1> : <EOL> oldrelpath = out [ <NUM_LIT:1> ] . strip ( ) <EOL> status = "<STR_LIT:M>" <EOL> else : <EOL> status , _ = out [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if "<STR_LIT::>" in self . base_rev : <EOL> base_rev = self . base_rev . split ( "<STR_LIT::>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> else : <EOL> base_rev = self . base_rev <EOL> if status != "<STR_LIT:A>" : <EOL> base_content = RunShell ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , base_rev , oldrelpath ] , <EOL> silent_ok = True ) <EOL> is_binary = "<STR_LIT>" in base_content <EOL> if status != "<STR_LIT:R>" : <EOL> new_content = open ( relpath , "<STR_LIT:rb>" ) . read ( ) <EOL> is_binary = is_binary or "<STR_LIT>" in new_content <EOL> if is_binary and base_content : <EOL> base_content = RunShell ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , base_rev , oldrelpath ] , <EOL> silent_ok = True , universal_newlines = False ) <EOL> if not is_binary or not self . IsImage ( relpath ) : <EOL> new_content = None <EOL> return base_content , new_content , is_binary , status <EOL> def SplitPatch ( data ) : <EOL> """<STR_LIT>""" <EOL> patches = [ ] <EOL> filename = None <EOL> diff = [ ] <EOL> for line in data . splitlines ( True ) : <EOL> new_filename = None <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> unused , new_filename = line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> new_filename = new_filename . strip ( ) <EOL> elif line . startswith ( '<STR_LIT>' ) : <EOL> unused , temp_filename = line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> temp_filename = temp_filename . strip ( ) . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> if temp_filename != filename : <EOL> new_filename = temp_filename <EOL> if new_filename : <EOL> if filename and diff : <EOL> patches . append ( ( filename , '<STR_LIT>' . join ( diff ) ) ) <EOL> filename = new_filename <EOL> diff = [ line ] <EOL> continue <EOL> if diff is not None : <EOL> diff . append ( line ) <EOL> if filename and diff : <EOL> patches . append ( ( filename , '<STR_LIT>' . join ( diff ) ) ) <EOL> return patches <EOL> def UploadSeparatePatches ( issue , rpc_server , patchset , data , options ) : <EOL> """<STR_LIT>""" <EOL> patches = SplitPatch ( data ) <EOL> rv = [ ] <EOL> for patch in patches : <EOL> if len ( patch [ <NUM_LIT:1> ] ) > MAX_UPLOAD_SIZE : <EOL> print ( "<STR_LIT>" + patch [ <NUM_LIT:0> ] + <EOL> "<STR_LIT>" ) <EOL> continue <EOL> form_fields = [ ( "<STR_LIT:filename>" , patch [ <NUM_LIT:0> ] ) ] <EOL> if not options . download_base : <EOL> form_fields . append ( ( "<STR_LIT>" , "<STR_LIT:1>" ) ) <EOL> files = [ ( "<STR_LIT:data>" , "<STR_LIT>" , patch [ <NUM_LIT:1> ] ) ] <EOL> ctype , body = EncodeMultipartFormData ( form_fields , files ) <EOL> url = "<STR_LIT>" % ( int ( issue ) , int ( patchset ) ) <EOL> print "<STR_LIT>" + patch [ <NUM_LIT:0> ] <EOL> response_body = rpc_server . Send ( url , body , content_type = ctype ) <EOL> lines = response_body . splitlines ( ) <EOL> if not lines or lines [ <NUM_LIT:0> ] != "<STR_LIT:OK>" : <EOL> StatusUpdate ( "<STR_LIT>" % response_body ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> rv . append ( [ lines [ <NUM_LIT:1> ] , patch [ <NUM_LIT:0> ] ] ) <EOL> return rv <EOL> def GuessVCSName ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> out , returncode = RunShellWithReturnCode ( [ "<STR_LIT>" , "<STR_LIT:root>" ] ) <EOL> if returncode == <NUM_LIT:0> : <EOL> return ( VCS_MERCURIAL , out . strip ( ) ) <EOL> except OSError , ( errno , message ) : <EOL> if errno != <NUM_LIT:2> : <EOL> raise <EOL> if os . path . isdir ( '<STR_LIT>' ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> return ( VCS_SUBVERSION , None ) <EOL> try : <EOL> out , returncode = RunShellWithReturnCode ( [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> if returncode == <NUM_LIT:0> : <EOL> return ( VCS_GIT , None ) <EOL> except OSError , ( errno , message ) : <EOL> if errno != <NUM_LIT:2> : <EOL> raise <EOL> return ( VCS_UNKNOWN , None ) <EOL> def GuessVCS ( options ) : <EOL> """<STR_LIT>""" <EOL> ( vcs , extra_output ) = GuessVCSName ( ) <EOL> if vcs == VCS_MERCURIAL : <EOL> return MercurialVCS ( options , extra_output ) <EOL> elif vcs == VCS_SUBVERSION : <EOL> return SubversionVCS ( options ) <EOL> elif vcs == VCS_GIT : <EOL> return GitVCS ( options ) <EOL> ErrorExit ( ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def RealMain ( argv , data = None ) : <EOL> """<STR_LIT>""" <EOL> logging . basicConfig ( format = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:C>' <EOL> options , args = parser . parse_args ( argv [ <NUM_LIT:1> : ] ) <EOL> global verbosity <EOL> verbosity = options . verbose <EOL> if verbosity >= <NUM_LIT:3> : <EOL> logging . getLogger ( ) . setLevel ( logging . DEBUG ) <EOL> elif verbosity >= <NUM_LIT:2> : <EOL> logging . getLogger ( ) . setLevel ( logging . INFO ) <EOL> vcs = GuessVCS ( options ) <EOL> if isinstance ( vcs , SubversionVCS ) : <EOL> base = vcs . GuessBase ( options . download_base ) <EOL> else : <EOL> base = None <EOL> if not base and options . download_base : <EOL> options . download_base = True <EOL> logging . info ( "<STR_LIT>" ) <EOL> if not options . assume_yes : <EOL> vcs . CheckForUnknownFiles ( ) <EOL> if data is None : <EOL> data = vcs . GenerateDiff ( args ) <EOL> files = vcs . GetBaseFiles ( data ) <EOL> if verbosity >= <NUM_LIT:1> : <EOL> print "<STR_LIT>" , options . server , "<STR_LIT>" <EOL> if options . issue : <EOL> prompt = "<STR_LIT>" <EOL> else : <EOL> prompt = "<STR_LIT>" <EOL> message = options . message or raw_input ( prompt ) . strip ( ) <EOL> if not message : <EOL> ErrorExit ( "<STR_LIT>" ) <EOL> rpc_server = GetRpcServer ( options ) <EOL> form_fields = [ ( "<STR_LIT>" , message ) ] <EOL> if base : <EOL> form_fields . append ( ( "<STR_LIT>" , base ) ) <EOL> if options . issue : <EOL> form_fields . append ( ( "<STR_LIT>" , str ( options . issue ) ) ) <EOL> if options . email : <EOL> form_fields . append ( ( "<STR_LIT:user>" , options . email ) ) <EOL> if options . reviewers : <EOL> for reviewer in options . reviewers . split ( '<STR_LIT:U+002C>' ) : <EOL> if "<STR_LIT:@>" in reviewer and not reviewer . split ( "<STR_LIT:@>" ) [ <NUM_LIT:1> ] . count ( "<STR_LIT:.>" ) == <NUM_LIT:1> : <EOL> ErrorExit ( "<STR_LIT>" % reviewer ) <EOL> form_fields . append ( ( "<STR_LIT>" , options . reviewers ) ) <EOL> if options . cc : <EOL> for cc in options . cc . split ( '<STR_LIT:U+002C>' ) : <EOL> if "<STR_LIT:@>" in cc and not cc . split ( "<STR_LIT:@>" ) [ <NUM_LIT:1> ] . count ( "<STR_LIT:.>" ) == <NUM_LIT:1> : <EOL> ErrorExit ( "<STR_LIT>" % cc ) <EOL> form_fields . append ( ( "<STR_LIT>" , options . cc ) ) <EOL> description = options . description <EOL> if options . description_file : <EOL> if options . description : <EOL> ErrorExit ( "<STR_LIT>" ) <EOL> file = open ( options . description_file , '<STR_LIT:r>' ) <EOL> description = file . read ( ) <EOL> file . close ( ) <EOL> if description : <EOL> form_fields . append ( ( "<STR_LIT:description>" , description ) ) <EOL> base_hashes = "<STR_LIT>" <EOL> for file , info in files . iteritems ( ) : <EOL> if not info [ <NUM_LIT:0> ] is None : <EOL> checksum = md5 ( info [ <NUM_LIT:0> ] ) . hexdigest ( ) <EOL> if base_hashes : <EOL> base_hashes += "<STR_LIT:|>" <EOL> base_hashes += checksum + "<STR_LIT::>" + file <EOL> form_fields . append ( ( "<STR_LIT>" , base_hashes ) ) <EOL> if options . private : <EOL> if options . issue : <EOL> print "<STR_LIT>" <EOL> else : <EOL> form_fields . append ( ( "<STR_LIT>" , "<STR_LIT:1>" ) ) <EOL> if options . send_mail and options . download_base : <EOL> form_fields . append ( ( "<STR_LIT>" , "<STR_LIT:1>" ) ) <EOL> if not options . download_base : <EOL> form_fields . append ( ( "<STR_LIT>" , "<STR_LIT:1>" ) ) <EOL> if len ( data ) > MAX_UPLOAD_SIZE : <EOL> print "<STR_LIT>" <EOL> uploaded_diff_file = [ ] <EOL> form_fields . append ( ( "<STR_LIT>" , "<STR_LIT:1>" ) ) <EOL> else : <EOL> uploaded_diff_file = [ ( "<STR_LIT:data>" , "<STR_LIT>" , data ) ] <EOL> ctype , body = EncodeMultipartFormData ( form_fields , uploaded_diff_file ) <EOL> response_body = rpc_server . Send ( "<STR_LIT>" , body , content_type = ctype ) <EOL> patchset = None <EOL> if not options . download_base or not uploaded_diff_file : <EOL> lines = response_body . splitlines ( ) <EOL> if len ( lines ) >= <NUM_LIT:2> : <EOL> msg = lines [ <NUM_LIT:0> ] <EOL> patchset = lines [ <NUM_LIT:1> ] . strip ( ) <EOL> patches = [ x . split ( "<STR_LIT:U+0020>" , <NUM_LIT:1> ) for x in lines [ <NUM_LIT:2> : ] ] <EOL> else : <EOL> msg = response_body <EOL> else : <EOL> msg = response_body <EOL> StatusUpdate ( msg ) <EOL> if not response_body . startswith ( "<STR_LIT>" ) and not response_body . startswith ( "<STR_LIT>" ) : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> issue = msg [ msg . rfind ( "<STR_LIT:/>" ) + <NUM_LIT:1> : ] <EOL> if not uploaded_diff_file : <EOL> result = UploadSeparatePatches ( issue , rpc_server , patchset , data , options ) <EOL> if not options . download_base : <EOL> patches = result <EOL> if not options . download_base : <EOL> vcs . UploadBaseFiles ( issue , rpc_server , patches , patchset , options , files ) <EOL> if options . send_mail : <EOL> rpc_server . Send ( "<STR_LIT:/>" + issue + "<STR_LIT>" , payload = "<STR_LIT>" ) <EOL> return issue , patchset <EOL> def main ( ) : <EOL> try : <EOL> RealMain ( sys . argv ) <EOL> except KeyboardInterrupt : <EOL> print <EOL> StatusUpdate ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
