upload.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #
  2. # Copyright (C) 2008 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import re
  16. import sys
  17. from command import InteractiveCommand
  18. from editor import Editor
  19. from error import UploadError
  20. def _die(fmt, *args):
  21. msg = fmt % args
  22. print >>sys.stderr, 'error: %s' % msg
  23. sys.exit(1)
  24. class Upload(InteractiveCommand):
  25. common = True
  26. helpSummary = "Upload changes for code review"
  27. helpUsage="""
  28. %prog [<project>]...
  29. """
  30. helpDescription = """
  31. The '%prog' command is used to send changes to the Gerrit code
  32. review system. It searches for changes in local projects that do
  33. not yet exist in the corresponding remote repository. If multiple
  34. changes are found, '%prog' opens an editor to allow the
  35. user to choose which change to upload. After a successful upload,
  36. repo prints the URL for the change in the Gerrit code review system.
  37. '%prog' searches for uploadable changes in all projects listed
  38. at the command line. Projects can be specified either by name, or
  39. by a relative or absolute path to the project's local directory. If
  40. no projects are specified, '%prog' will search for uploadable
  41. changes in all projects listed in the manifest.
  42. """
  43. def _SingleBranch(self, branch):
  44. project = branch.project
  45. name = branch.name
  46. date = branch.date
  47. list = branch.commits
  48. print 'Upload project %s/:' % project.relpath
  49. print ' branch %s (%2d commit%s, %s):' % (
  50. name,
  51. len(list),
  52. len(list) != 1 and 's' or '',
  53. date)
  54. for commit in list:
  55. print ' %s' % commit
  56. sys.stdout.write('(y/n)? ')
  57. answer = sys.stdin.readline().strip()
  58. if answer in ('y', 'Y', 'yes', '1', 'true', 't'):
  59. self._UploadAndReport([branch])
  60. else:
  61. _die("upload aborted by user")
  62. def _MultipleBranches(self, pending):
  63. projects = {}
  64. branches = {}
  65. script = []
  66. script.append('# Uncomment the branches to upload:')
  67. for project, avail in pending:
  68. script.append('#')
  69. script.append('# project %s/:' % project.relpath)
  70. b = {}
  71. for branch in avail:
  72. name = branch.name
  73. date = branch.date
  74. list = branch.commits
  75. if b:
  76. script.append('#')
  77. script.append('# branch %s (%2d commit%s, %s):' % (
  78. name,
  79. len(list),
  80. len(list) != 1 and 's' or '',
  81. date))
  82. for commit in list:
  83. script.append('# %s' % commit)
  84. b[name] = branch
  85. projects[project.relpath] = project
  86. branches[project.name] = b
  87. script.append('')
  88. script = Editor.EditString("\n".join(script)).split("\n")
  89. project_re = re.compile(r'^#?\s*project\s*([^\s]+)/:$')
  90. branch_re = re.compile(r'^\s*branch\s*([^\s(]+)\s*\(.*')
  91. project = None
  92. todo = []
  93. for line in script:
  94. m = project_re.match(line)
  95. if m:
  96. name = m.group(1)
  97. project = projects.get(name)
  98. if not project:
  99. _die('project %s not available for upload', name)
  100. continue
  101. m = branch_re.match(line)
  102. if m:
  103. name = m.group(1)
  104. if not project:
  105. _die('project for branch %s not in script', name)
  106. branch = branches[project.name].get(name)
  107. if not branch:
  108. _die('branch %s not in %s', name, project.relpath)
  109. todo.append(branch)
  110. if not todo:
  111. _die("nothing uncommented for upload")
  112. self._UploadAndReport(todo)
  113. def _UploadAndReport(self, todo):
  114. have_errors = False
  115. for branch in todo:
  116. try:
  117. branch.UploadForReview()
  118. branch.uploaded = True
  119. except UploadError, e:
  120. branch.error = e
  121. branch.uploaded = False
  122. have_errors = True
  123. print >>sys.stderr, ''
  124. print >>sys.stderr, '--------------------------------------------'
  125. if have_errors:
  126. for branch in todo:
  127. if not branch.uploaded:
  128. print >>sys.stderr, '[FAILED] %-15s %-15s (%s)' % (
  129. branch.project.relpath + '/', \
  130. branch.name, \
  131. branch.error)
  132. print >>sys.stderr, ''
  133. for branch in todo:
  134. if branch.uploaded:
  135. print >>sys.stderr, '[OK ] %-15s %s' % (
  136. branch.project.relpath + '/',
  137. branch.name)
  138. print >>sys.stderr, '%s' % branch.tip_url
  139. print >>sys.stderr, '(as %s)' % branch.owner_email
  140. print >>sys.stderr, ''
  141. if have_errors:
  142. sys.exit(1)
  143. def Execute(self, opt, args):
  144. project_list = self.GetProjects(args)
  145. pending = []
  146. for project in project_list:
  147. avail = project.GetUploadableBranches()
  148. if avail:
  149. pending.append((project, avail))
  150. if not pending:
  151. print >>sys.stdout, "no branches ready for upload"
  152. elif len(pending) == 1 and len(pending[0][1]) == 1:
  153. self._SingleBranch(pending[0][1][0])
  154. else:
  155. self._MultipleBranches(pending)