First stab at using Rietveld wrapper object in both gcl.py and git-cl.

Exposes InputApi.rietveld and deprecate host_url.

This is useful since it places authentication at a single place.

BUG=
TEST=

Review URL: http://codereview.chromium.org/6825008

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@81019 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/gcl.py b/gcl.py
index 69dedb8e..76f302e 100755
--- a/gcl.py
+++ b/gcl.py
@@ -8,7 +8,6 @@
 of files.
 """
 
-import getpass
 import optparse
 import os
 import random
@@ -39,9 +38,10 @@
 import fix_encoding
 import gclient_utils
 import presubmit_support
+import rietveld
 import subprocess2
 
-__version__ = '1.2'
+__version__ = '1.2.1'
 
 
 CODEREVIEW_SETTINGS = {
@@ -283,7 +283,7 @@
   _SEPARATOR = "\n-----\n"
 
   def __init__(self, name, issue, patchset, description, files, local_root,
-               rietveld, needs_upload=False):
+               rietveld_url, needs_upload=False):
     self.name = name
     self.issue = int(issue)
     self.patchset = int(patchset)
@@ -297,10 +297,11 @@
     self.patch = None
     self._local_root = local_root
     self.needs_upload = needs_upload
-    self.rietveld = rietveld
+    self.rietveld = rietveld_url
     if not self.rietveld:
       # Set the default value.
       self.rietveld = GetCodeReviewSetting('CODE_REVIEW_SERVER')
+    self._rpc_server = None
 
   def _get_description(self):
     return self._description
@@ -384,6 +385,13 @@
     """Removes the changelist information from disk."""
     os.remove(GetChangelistInfoFile(self.name))
 
+  def RpcServer(self):
+    if not self._rpc_server:
+      if not self.rietveld:
+        ErrorExit(CODEREVIEW_SETTINGS_FILE_NOT_FOUND)
+      self._rpc_server = rietveld.Rietveld(self.rietveld, None, None)
+    return self._rpc_server
+
   def CloseIssue(self):
     """Closes the Rietveld issue for this changelist."""
     # Newer versions of Rietveld require us to pass an XSRF token to POST, so
@@ -420,19 +428,8 @@
 
   def SendToRietveld(self, request_path, timeout=None, **kwargs):
     """Send a POST/GET to Rietveld.  Returns the response body."""
-    if not self.rietveld:
-      ErrorExit(CODEREVIEW_SETTINGS_FILE_NOT_FOUND)
-    def GetUserCredentials():
-      """Prompts the user for a username and password."""
-      email = upload.GetEmail('Email (login for uploading to %s)' %
-          self.rietveld)
-      password = getpass.getpass('Password for %s: ' % email)
-      return email, password
-    rpc_server = upload.HttpRpcServer(self.rietveld,
-                                      GetUserCredentials,
-                                      save_cookies=True)
     try:
-      return rpc_server.Send(request_path, timeout=timeout, **kwargs)
+      return self.RpcServer().Send(request_path, timeout=timeout, **kwargs)
     except urllib2.URLError:
       if timeout is None:
         ErrorExit('Error accessing url %s' % request_path)
@@ -536,8 +533,9 @@
     if not os.path.exists(info_file):
       if fail_on_not_found:
         ErrorExit("Changelist " + changename + " not found.")
-      return ChangeInfo(changename, 0, 0, '', None, local_root, rietveld=None,
-                        needs_upload=False)
+      return ChangeInfo(
+          changename, 0, 0, '', None, local_root, rietveld_url=None,
+          needs_upload=False)
     content = gclient_utils.FileRead(info_file, 'r')
     save = False
     try:
@@ -1222,15 +1220,16 @@
                                        change_info.GetFiles(),
                                        change_info.issue,
                                        change_info.patchset)
-  output = presubmit_support.DoPresubmitChecks(change=change,
-                                               committing=committing,
-                                               verbose=False,
-                                               output_stream=sys.stdout,
-                                               input_stream=sys.stdin,
-                                               default_presubmit=root_presubmit,
-                                               may_prompt=may_prompt,
-                                               tbr=False,
-                                               host_url=change_info.rietveld)
+  output = presubmit_support.DoPresubmitChecks(
+      change=change,
+      committing=committing,
+      verbose=False,
+      output_stream=sys.stdout,
+      input_stream=sys.stdin,
+      default_presubmit=root_presubmit,
+      may_prompt=may_prompt,
+      tbr=False,
+      rietveld=change_info.RpcServer())
   if not output.should_continue() and may_prompt:
     # TODO(dpranke): move into DoPresubmitChecks(), unify cmd line args.
     print "\nPresubmit errors, can't continue (use --no_presubmit to bypass)"