gerrit_upload.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 getpass
  16. import os
  17. import subprocess
  18. import sys
  19. from tempfile import mkstemp
  20. from codereview.proto_client import HttpRpc, Proxy
  21. from codereview.review_pb2 import ReviewService_Stub
  22. from codereview.upload_bundle_pb2 import *
  23. from git_command import GitCommand
  24. from error import UploadError
  25. try:
  26. import readline
  27. except ImportError:
  28. pass
  29. MAX_SEGMENT_SIZE = 1020 * 1024
  30. def _GetRpcServer(email, server, save_cookies):
  31. """Returns an RpcServer.
  32. Returns:
  33. A new RpcServer, on which RPC calls can be made.
  34. """
  35. def GetUserCredentials():
  36. """Prompts the user for a username and password."""
  37. e = email
  38. if e is None:
  39. e = raw_input("Email: ").strip()
  40. password = getpass.getpass("Password for %s: " % e)
  41. return (e, password)
  42. # If this is the dev_appserver, use fake authentication.
  43. lc_server = server.lower()
  44. if lc_server == "localhost" or lc_server.startswith("localhost:"):
  45. if email is None:
  46. email = "test@example.com"
  47. server = HttpRpc(
  48. server,
  49. lambda: (email, "password"),
  50. extra_headers={"Cookie":
  51. 'dev_appserver_login="%s:False"' % email})
  52. # Don't try to talk to ClientLogin.
  53. server.authenticated = True
  54. return server
  55. if save_cookies:
  56. cookie_file = ".gerrit_cookies"
  57. else:
  58. cookie_file = None
  59. return HttpRpc(server, GetUserCredentials,
  60. cookie_file=cookie_file)
  61. def UploadBundle(project,
  62. server,
  63. email,
  64. dest_project,
  65. dest_branch,
  66. src_branch,
  67. bases,
  68. save_cookies=True):
  69. srv = _GetRpcServer(email, server, save_cookies)
  70. review = Proxy(ReviewService_Stub(srv))
  71. tmp_fd, tmp_bundle = mkstemp(".bundle", ".gpq")
  72. os.close(tmp_fd)
  73. srcid = project.bare_git.rev_parse(src_branch)
  74. revlist = project._revlist(src_branch, *bases)
  75. if srcid not in revlist:
  76. # This can happen if src_branch is an annotated tag
  77. #
  78. revlist.append(srcid)
  79. revlist_size = len(revlist) * 42
  80. try:
  81. cmd = ['bundle', 'create', tmp_bundle, src_branch]
  82. cmd.extend(bases)
  83. if GitCommand(project, cmd).Wait() != 0:
  84. raise UploadError('cannot create bundle')
  85. fd = open(tmp_bundle, "rb")
  86. bundle_id = None
  87. segment_id = 0
  88. next_data = fd.read(MAX_SEGMENT_SIZE - revlist_size)
  89. while True:
  90. this_data = next_data
  91. next_data = fd.read(MAX_SEGMENT_SIZE)
  92. segment_id += 1
  93. if bundle_id is None:
  94. req = UploadBundleRequest()
  95. req.dest_project = str(dest_project)
  96. req.dest_branch = str(dest_branch)
  97. for c in revlist:
  98. req.contained_object.append(c)
  99. else:
  100. req = UploadBundleContinue()
  101. req.bundle_id = bundle_id
  102. req.segment_id = segment_id
  103. req.bundle_data = this_data
  104. if len(next_data) > 0:
  105. req.partial_upload = True
  106. else:
  107. req.partial_upload = False
  108. if bundle_id is None:
  109. rsp = review.UploadBundle(req)
  110. else:
  111. rsp = review.ContinueBundle(req)
  112. if rsp.status_code == UploadBundleResponse.CONTINUE:
  113. bundle_id = rsp.bundle_id
  114. elif rsp.status_code == UploadBundleResponse.RECEIVED:
  115. bundle_id = rsp.bundle_id
  116. return bundle_id
  117. else:
  118. if rsp.status_code == UploadBundleResponse.UNKNOWN_PROJECT:
  119. reason = 'unknown project "%s"' % dest_project
  120. elif rsp.status_code == UploadBundleResponse.UNKNOWN_BRANCH:
  121. reason = 'unknown branch "%s"' % dest_branch
  122. elif rsp.status_code == UploadBundleResponse.UNKNOWN_BUNDLE:
  123. reason = 'unknown bundle'
  124. elif rsp.status_code == UploadBundleResponse.NOT_BUNDLE_OWNER:
  125. reason = 'not bundle owner'
  126. elif rsp.status_code == UploadBundleResponse.BUNDLE_CLOSED:
  127. reason = 'bundle closed'
  128. elif rsp.status_code == UploadBundleResponse.UNAUTHORIZED_USER:
  129. reason = ('Unauthorized user. Visit http://%s/hello to sign up.'
  130. % server)
  131. else:
  132. reason = 'unknown error ' + str(rsp.status_code)
  133. raise UploadError(reason)
  134. finally:
  135. os.unlink(tmp_bundle)