<s> import copy <EOL> import re <EOL> import sys <EOL> from command import InteractiveCommand <EOL> from editor import Editor <EOL> from error import HookError , UploadError <EOL> from project import RepoHook <EOL> UNUSUAL_COMMIT_THRESHOLD = <NUM_LIT:5> <EOL> def _ConfirmManyUploads ( multiple_branches = False ) : <EOL> if multiple_branches : <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> answer = raw_input ( "<STR_LIT>" ) . strip ( ) <EOL> return answer == "<STR_LIT:yes>" <EOL> def _die ( fmt , * args ) : <EOL> msg = fmt % args <EOL> print >> sys . stderr , '<STR_LIT>' % msg <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def _SplitEmails ( values ) : <EOL> result = [ ] <EOL> for str in values : <EOL> result . extend ( [ s . strip ( ) for s in str . split ( '<STR_LIT:U+002C>' ) ] ) <EOL> return result <EOL> class Upload ( InteractiveCommand ) : <EOL> common = True <EOL> helpSummary = "<STR_LIT>" <EOL> helpUsage = """<STR_LIT>""" <EOL> helpDescription = """<STR_LIT>""" <EOL> def _Options ( self , p ) : <EOL> p . add_option ( '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> type = '<STR_LIT:string>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_option ( '<STR_LIT>' , <EOL> type = '<STR_LIT:string>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_option ( '<STR_LIT>' , <EOL> type = '<STR_LIT:string>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_option ( '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_option ( '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> def _SingleBranch ( self , opt , branch , people ) : <EOL> project = branch . project <EOL> name = branch . name <EOL> remote = project . GetBranch ( name ) . remote <EOL> key = '<STR_LIT>' % remote . review <EOL> answer = project . config . GetBoolean ( key ) <EOL> if answer is False : <EOL> _die ( "<STR_LIT>" % key ) <EOL> if answer is None : <EOL> date = branch . date <EOL> list = branch . commits <EOL> print '<STR_LIT>' % project . relpath <EOL> print '<STR_LIT>' % ( <EOL> name , <EOL> len ( list ) , <EOL> len ( list ) != <NUM_LIT:1> and '<STR_LIT:s>' or '<STR_LIT>' , <EOL> date ) <EOL> for commit in list : <EOL> print '<STR_LIT>' % commit <EOL> sys . stdout . write ( '<STR_LIT>' % remote . review ) <EOL> answer = sys . stdin . readline ( ) . strip ( ) <EOL> answer = answer in ( '<STR_LIT:y>' , '<STR_LIT:Y>' , '<STR_LIT:yes>' , '<STR_LIT:1>' , '<STR_LIT:true>' , '<STR_LIT:t>' ) <EOL> if answer : <EOL> if len ( branch . commits ) > UNUSUAL_COMMIT_THRESHOLD : <EOL> answer = _ConfirmManyUploads ( ) <EOL> if answer : <EOL> self . _UploadAndReport ( opt , [ branch ] , people ) <EOL> else : <EOL> _die ( "<STR_LIT>" ) <EOL> def _MultipleBranches ( self , opt , pending , people ) : <EOL> projects = { } <EOL> branches = { } <EOL> script = [ ] <EOL> script . append ( '<STR_LIT>' ) <EOL> for project , avail in pending : <EOL> script . append ( '<STR_LIT:#>' ) <EOL> script . append ( '<STR_LIT>' % project . relpath ) <EOL> b = { } <EOL> for branch in avail : <EOL> name = branch . name <EOL> date = branch . date <EOL> list = branch . commits <EOL> if b : <EOL> script . append ( '<STR_LIT:#>' ) <EOL> script . append ( '<STR_LIT>' % ( <EOL> name , <EOL> len ( list ) , <EOL> len ( list ) != <NUM_LIT:1> and '<STR_LIT:s>' or '<STR_LIT>' , <EOL> date ) ) <EOL> for commit in list : <EOL> script . append ( '<STR_LIT>' % commit ) <EOL> b [ name ] = branch <EOL> projects [ project . relpath ] = project <EOL> branches [ project . name ] = b <EOL> script . append ( '<STR_LIT>' ) <EOL> script = Editor . EditString ( "<STR_LIT:\n>" . join ( script ) ) . split ( "<STR_LIT:\n>" ) <EOL> project_re = re . compile ( r'<STR_LIT>' ) <EOL> branch_re = re . compile ( r'<STR_LIT>' ) <EOL> project = None <EOL> todo = [ ] <EOL> for line in script : <EOL> m = project_re . match ( line ) <EOL> if m : <EOL> name = m . group ( <NUM_LIT:1> ) <EOL> project = projects . get ( name ) <EOL> if not project : <EOL> _die ( '<STR_LIT>' , name ) <EOL> continue <EOL> m = branch_re . match ( line ) <EOL> if m : <EOL> name = m . group ( <NUM_LIT:1> ) <EOL> if not project : <EOL> _die ( '<STR_LIT>' , name ) <EOL> branch = branches [ project . name ] . get ( name ) <EOL> if not branch : <EOL> _die ( '<STR_LIT>' , name , project . relpath ) <EOL> todo . append ( branch ) <EOL> if not todo : <EOL> _die ( "<STR_LIT>" ) <EOL> many_commits = False <EOL> for branch in todo : <EOL> if len ( branch . commits ) > UNUSUAL_COMMIT_THRESHOLD : <EOL> many_commits = True <EOL> break <EOL> if many_commits : <EOL> if not _ConfirmManyUploads ( multiple_branches = True ) : <EOL> _die ( "<STR_LIT>" ) <EOL> self . _UploadAndReport ( opt , todo , people ) <EOL> def _AppendAutoCcList ( self , branch , people ) : <EOL> """<STR_LIT>""" <EOL> name = branch . name <EOL> project = branch . project <EOL> key = '<STR_LIT>' % project . GetBranch ( name ) . remote . review <EOL> raw_list = project . config . GetString ( key ) <EOL> if not raw_list is None and len ( people [ <NUM_LIT:0> ] ) > <NUM_LIT:0> : <EOL> people [ <NUM_LIT:1> ] . extend ( [ entry . strip ( ) for entry in raw_list . split ( '<STR_LIT:U+002C>' ) ] ) <EOL> def _FindGerritChange ( self , branch ) : <EOL> last_pub = branch . project . WasPublished ( branch . name ) <EOL> if last_pub is None : <EOL> return "<STR_LIT>" <EOL> refs = branch . GetPublishedRefs ( ) <EOL> try : <EOL> return refs . get ( last_pub ) . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:2> ] <EOL> except : <EOL> return "<STR_LIT>" <EOL> def _UploadAndReport ( self , opt , todo , original_people ) : <EOL> have_errors = False <EOL> for branch in todo : <EOL> try : <EOL> people = copy . deepcopy ( original_people ) <EOL> self . _AppendAutoCcList ( branch , people ) <EOL> if branch . project . HasChanges ( ) : <EOL> key = '<STR_LIT>' % branch . project . remote . review <EOL> answer = branch . project . config . GetBoolean ( key ) <EOL> if answer is None : <EOL> sys . stdout . write ( '<STR_LIT>' + branch . project . name + '<STR_LIT>' ) <EOL> a = sys . stdin . readline ( ) . strip ( ) . lower ( ) <EOL> if a not in ( '<STR_LIT:y>' , '<STR_LIT:yes>' , '<STR_LIT:t>' , '<STR_LIT:true>' , '<STR_LIT>' ) : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> branch . uploaded = False <EOL> branch . error = '<STR_LIT>' <EOL> continue <EOL> branch . UploadForReview ( people , auto_topic = opt . auto_topic ) <EOL> branch . uploaded = True <EOL> except UploadError , e : <EOL> branch . error = e <EOL> branch . uploaded = False <EOL> have_errors = True <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> if have_errors : <EOL> for branch in todo : <EOL> if not branch . uploaded : <EOL> if len ( str ( branch . error ) ) <= <NUM_LIT:30> : <EOL> fmt = '<STR_LIT>' <EOL> else : <EOL> fmt = '<STR_LIT>' <EOL> print >> sys . stderr , ( '<STR_LIT>' + fmt ) % ( <EOL> branch . project . relpath + '<STR_LIT:/>' , branch . name , str ( branch . error ) ) <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> for branch in todo : <EOL> if branch . uploaded : <EOL> print >> sys . stderr , '<STR_LIT>' % ( <EOL> branch . project . relpath + '<STR_LIT:/>' , <EOL> branch . name ) <EOL> if have_errors : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def Execute ( self , opt , args ) : <EOL> project_list = self . GetProjects ( args ) <EOL> pending = [ ] <EOL> reviewers = [ ] <EOL> cc = [ ] <EOL> branch = None <EOL> if opt . branch : <EOL> branch = opt . branch <EOL> for project in project_list : <EOL> avail = project . GetUploadableBranches ( branch ) <EOL> if avail : <EOL> pending . append ( ( project , avail ) ) <EOL> if pending and ( not opt . bypass_hooks ) : <EOL> hook = RepoHook ( '<STR_LIT>' , self . manifest . repo_hooks_project , <EOL> self . manifest . topdir , abort_if_user_denies = True ) <EOL> pending_proj_names = [ project . name for ( project , avail ) in pending ] <EOL> try : <EOL> hook . Run ( opt . allow_all_hooks , project_list = pending_proj_names ) <EOL> except HookError , e : <EOL> print >> sys . stderr , "<STR_LIT>" % str ( e ) <EOL> return <EOL> if opt . reviewers : <EOL> reviewers = _SplitEmails ( opt . reviewers ) <EOL> if opt . cc : <EOL> cc = _SplitEmails ( opt . cc ) <EOL> people = ( reviewers , cc ) <EOL> if not pending : <EOL> print >> sys . stdout , "<STR_LIT>" <EOL> elif len ( pending ) == <NUM_LIT:1> and len ( pending [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) == <NUM_LIT:1> : <EOL> self . _SingleBranch ( opt , pending [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , people ) <EOL> else : <EOL> self . _MultipleBranches ( opt , pending , people ) </s>
