gerrit_upload.py 4.7 KB

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