thirdparty/rietveld/upload.py
author Todd Larsen <tlarsen@google.com>
Sat, 22 Nov 2008 09:22:44 +0000
changeset 548 2ceb3b14349c
parent 478 613951c35706
child 687 4755caf1d7a6
permissions -rwxr-xr-x
Fix for 404 occuring when signing out during the creation of, for example, a new Document. This patch makes sure that the request.path is not altered when requesting the create page (alteration of which caused the 404). This also results in the seeds now being remembered when being forced to log in. The editPost method now correctly switches to the edit path after a successful edit. Patch by: Lennard de Rijk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/env python
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2007 Google Inc.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#     http://www.apache.org/licenses/LICENSE-2.0
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Tool for uploading diffs from a version control system to the codereview app.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
Usage summary: upload.py [options] [-- diff_options]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
Diff options are passed to the diff command of the underlying system.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
Supported version control systems:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
  Git
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
    25
  Mercurial
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
  Subversion
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
    28
It is important for Git/Mercurial users to specify a tree/node/branch to diff
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
    29
against by using the '--rev' option.
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
"""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
# This code is derived from appcfg.py in the App Engine SDK (open source),
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
# and from ASPN recipe #146306.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
import cookielib
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
import getpass
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
import logging
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
import md5
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
import mimetypes
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
import optparse
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
import os
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
import re
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
import socket
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
import subprocess
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
import sys
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
import urllib
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
import urllib2
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
import urlparse
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
  import readline
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
except ImportError:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
  pass
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
# The logging verbosity:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
#  0: Errors only.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
#  1: Status messages.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
#  2: Info logs.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
#  3: Debug logs.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
verbosity = 1
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
# Max size of patch or base file.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
MAX_UPLOAD_SIZE = 900 * 1024
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
def StatusUpdate(msg):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
  """Print a status message to stdout.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
  If 'verbosity' is greater than 0, print the message.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
  Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
    msg: The string to print.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
  """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
  if verbosity > 0:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    74
    print msg
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    76
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
def ErrorExit(msg):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
  """Print an error message to stderr and exit."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
  print >>sys.stderr, msg
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
  sys.exit(1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
class ClientLoginError(urllib2.HTTPError):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
  """Raised to indicate there was an error authenticating with ClientLogin."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    86
  def __init__(self, url, code, msg, headers, args):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    87
    urllib2.HTTPError.__init__(self, url, code, msg, headers, None)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    88
    self.args = args
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    89
    self.reason = args["Error"]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    90
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    91
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    92
class AbstractRpcServer(object):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    93
  """Provides a common interface for a simple RPC server."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    94
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    95
  def __init__(self, host, auth_function, host_override=None, extra_headers={},
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    96
               save_cookies=False):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    97
    """Creates a new HttpRpcServer.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    98
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    99
    Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   100
      host: The host to send requests to.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   101
      auth_function: A function that takes no arguments and returns an
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
        (email, password) tuple when called. Will be called if authentication
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   103
        is required.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   104
      host_override: The host header to send to the server (defaults to host).
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   105
      extra_headers: A dict of extra headers to append to every request.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   106
      save_cookies: If True, save the authentication cookies to local disk.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   107
        If False, use an in-memory cookiejar instead.  Subclasses must
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   108
        implement this functionality.  Defaults to False.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   109
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   110
    self.host = host
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   111
    self.host_override = host_override
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   112
    self.auth_function = auth_function
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   113
    self.authenticated = False
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   114
    self.extra_headers = extra_headers
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   115
    self.save_cookies = save_cookies
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   116
    self.opener = self._GetOpener()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   117
    if self.host_override:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   118
      logging.info("Server: %s; Host: %s", self.host, self.host_override)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   119
    else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   120
      logging.info("Server: %s", self.host)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   121
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   122
  def _GetOpener(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   123
    """Returns an OpenerDirector for making HTTP requests.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   124
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   125
    Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   126
      A urllib2.OpenerDirector object.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   127
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   128
    raise NotImplementedError()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   129
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   130
  def _CreateRequest(self, url, data=None):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   131
    """Creates a new urllib request."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   132
    logging.debug("Creating request for: '%s' with payload:\n%s", url, data)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   133
    req = urllib2.Request(url, data=data)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   134
    if self.host_override:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   135
      req.add_header("Host", self.host_override)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   136
    for key, value in self.extra_headers.iteritems():
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   137
      req.add_header(key, value)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   138
    return req
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   139
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   140
  def _GetAuthToken(self, email, password):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   141
    """Uses ClientLogin to authenticate the user, returning an auth token.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   142
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   143
    Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   144
      email:    The user's email address
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   145
      password: The user's password
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   146
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   147
    Raises:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   148
      ClientLoginError: If there was an error authenticating with ClientLogin.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   149
      HTTPError: If there was some other form of HTTP error.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   150
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   151
    Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   152
      The authentication token returned by ClientLogin.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   153
    """
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   154
    # account_type = "GOOGLE"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   155
    account_type = "HOSTED_OR_GOOGLE"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   156
    if self.host.endswith(".google.com"):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   157
      # Needed for use inside Google.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   158
      account_type = "HOSTED"
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   159
    req = self._CreateRequest(
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   160
        url="https://www.google.com/accounts/ClientLogin",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   161
        data=urllib.urlencode({
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   162
            "Email": email,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   163
            "Passwd": password,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   164
            "service": "ah",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   165
            "source": "rietveld-codereview-upload",
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   166
            "accountType": account_type,
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   167
        }),
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   168
    )
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   169
    try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   170
      response = self.opener.open(req)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   171
      response_body = response.read()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   172
      response_dict = dict(x.split("=")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   173
                           for x in response_body.split("\n") if x)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   174
      return response_dict["Auth"]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   175
    except urllib2.HTTPError, e:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   176
      if e.code == 403:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   177
        body = e.read()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   178
        response_dict = dict(x.split("=", 1) for x in body.split("\n") if x)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   179
        raise ClientLoginError(req.get_full_url(), e.code, e.msg,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   180
                               e.headers, response_dict)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   181
      else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   182
        raise
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   183
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   184
  def _GetAuthCookie(self, auth_token):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   185
    """Fetches authentication cookies for an authentication token.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   186
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   187
    Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   188
      auth_token: The authentication token returned by ClientLogin.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   189
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   190
    Raises:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   191
      HTTPError: If there was an error fetching the authentication cookies.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   192
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   193
    # This is a dummy value to allow us to identify when we're successful.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   194
    continue_location = "http://localhost/"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   195
    args = {"continue": continue_location, "auth": auth_token}
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   196
    req = self._CreateRequest("http://%s/_ah/login?%s" %
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   197
                              (self.host, urllib.urlencode(args)))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   198
    try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   199
      response = self.opener.open(req)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   200
    except urllib2.HTTPError, e:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   201
      response = e
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   202
    if (response.code != 302 or
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   203
        response.info()["location"] != continue_location):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   204
      raise urllib2.HTTPError(req.get_full_url(), response.code, response.msg,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   205
                              response.headers, response.fp)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   206
    self.authenticated = True
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   207
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   208
  def _Authenticate(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   209
    """Authenticates the user.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   210
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   211
    The authentication process works as follows:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   212
     1) We get a username and password from the user
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   213
     2) We use ClientLogin to obtain an AUTH token for the user
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   214
        (see http://code.google.com/apis/accounts/AuthForInstalledApps.html).
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   215
     3) We pass the auth token to /_ah/login on the server to obtain an
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   216
        authentication cookie. If login was successful, it tries to redirect
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   217
        us to the URL we provided.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   218
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   219
    If we attempt to access the upload API without first obtaining an
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   220
    authentication cookie, it returns a 401 response and directs us to
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   221
    authenticate ourselves with ClientLogin.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   222
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   223
    for i in range(3):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   224
      credentials = self.auth_function()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   225
      try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   226
        auth_token = self._GetAuthToken(credentials[0], credentials[1])
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   227
      except ClientLoginError, e:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   228
        if e.reason == "BadAuthentication":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   229
          print >>sys.stderr, "Invalid username or password."
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   230
          continue
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   231
        if e.reason == "CaptchaRequired":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   232
          print >>sys.stderr, (
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   233
              "Please go to\n"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   234
              "https://www.google.com/accounts/DisplayUnlockCaptcha\n"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   235
              "and verify you are a human.  Then try again.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   236
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   237
        if e.reason == "NotVerified":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   238
          print >>sys.stderr, "Account not verified."
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   239
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   240
        if e.reason == "TermsNotAgreed":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   241
          print >>sys.stderr, "User has not agreed to TOS."
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   242
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   243
        if e.reason == "AccountDeleted":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   244
          print >>sys.stderr, "The user account has been deleted."
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   245
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   246
        if e.reason == "AccountDisabled":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   247
          print >>sys.stderr, "The user account has been disabled."
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   248
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   249
        if e.reason == "ServiceDisabled":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   250
          print >>sys.stderr, ("The user's access to the service has been "
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   251
                               "disabled.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   252
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   253
        if e.reason == "ServiceUnavailable":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   254
          print >>sys.stderr, "The service is not available; try again later."
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   255
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   256
        raise
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   257
      self._GetAuthCookie(auth_token)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   258
      return
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   259
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   260
  def Send(self, request_path, payload=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   261
           content_type="application/octet-stream",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   262
           timeout=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   263
           **kwargs):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   264
    """Sends an RPC and returns the response.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   265
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   266
    Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   267
      request_path: The path to send the request to, eg /api/appversion/create.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   268
      payload: The body of the request, or None to send an empty request.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   269
      content_type: The Content-Type header to use.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   270
      timeout: timeout in seconds; default None i.e. no timeout.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   271
        (Note: for large requests on OS X, the timeout doesn't work right.)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   272
      kwargs: Any keyword arguments are converted into query string parameters.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   273
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   274
    Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   275
      The response body, as a string.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   276
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   277
    # TODO: Don't require authentication.  Let the server say
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   278
    # whether it is necessary.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   279
    if not self.authenticated:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   280
      self._Authenticate()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   281
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   282
    old_timeout = socket.getdefaulttimeout()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   283
    socket.setdefaulttimeout(timeout)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   284
    try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   285
      tries = 0
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   286
      while True:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   287
        tries += 1
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   288
        args = dict(kwargs)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   289
        url = "http://%s%s" % (self.host, request_path)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   290
        if args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   291
          url += "?" + urllib.urlencode(args)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   292
        req = self._CreateRequest(url=url, data=payload)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   293
        req.add_header("Content-Type", content_type)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   294
        try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   295
          f = self.opener.open(req)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   296
          response = f.read()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   297
          f.close()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   298
          return response
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   299
        except urllib2.HTTPError, e:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   300
          if tries > 3:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   301
            raise
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   302
          elif e.code == 401:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   303
            self._Authenticate()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   304
##           elif e.code >= 500 and e.code < 600:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   305
##             # Server Error - try again.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   306
##             continue
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   307
          else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   308
            raise
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   309
    finally:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   310
      socket.setdefaulttimeout(old_timeout)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   311
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   312
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   313
class HttpRpcServer(AbstractRpcServer):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   314
  """Provides a simplified RPC-style interface for HTTP requests."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   315
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   316
  def _Authenticate(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   317
    """Save the cookie jar after authentication."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   318
    super(HttpRpcServer, self)._Authenticate()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   319
    if self.save_cookies:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   320
      StatusUpdate("Saving authentication cookies to %s" % self.cookie_file)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   321
      self.cookie_jar.save()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   322
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   323
  def _GetOpener(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   324
    """Returns an OpenerDirector that supports cookies and ignores redirects.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   325
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   326
    Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   327
      A urllib2.OpenerDirector object.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   328
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   329
    opener = urllib2.OpenerDirector()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   330
    opener.add_handler(urllib2.ProxyHandler())
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   331
    opener.add_handler(urllib2.UnknownHandler())
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   332
    opener.add_handler(urllib2.HTTPHandler())
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   333
    opener.add_handler(urllib2.HTTPDefaultErrorHandler())
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   334
    opener.add_handler(urllib2.HTTPSHandler())
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   335
    opener.add_handler(urllib2.HTTPErrorProcessor())
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   336
    if self.save_cookies:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   337
      self.cookie_file = os.path.expanduser("~/.codereview_upload_cookies")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   338
      self.cookie_jar = cookielib.MozillaCookieJar(self.cookie_file)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   339
      if os.path.exists(self.cookie_file):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   340
        try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   341
          self.cookie_jar.load()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   342
          self.authenticated = True
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   343
          StatusUpdate("Loaded authentication cookies from %s" %
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   344
                       self.cookie_file)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   345
        except (cookielib.LoadError, IOError):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   346
          # Failed to load cookies - just ignore them.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   347
          pass
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   348
      else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   349
        # Create an empty cookie file with mode 600
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   350
        fd = os.open(self.cookie_file, os.O_CREAT, 0600)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   351
        os.close(fd)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   352
      # Always chmod the cookie file
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   353
      os.chmod(self.cookie_file, 0600)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   354
    else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   355
      # Don't save cookies across runs of update.py.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   356
      self.cookie_jar = cookielib.CookieJar()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   357
    opener.add_handler(urllib2.HTTPCookieProcessor(self.cookie_jar))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   358
    return opener
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   359
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   360
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   361
parser = optparse.OptionParser(usage="%prog [options] [-- diff_options]")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   362
parser.add_option("-y", "--assume_yes", action="store_true",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   363
                  dest="assume_yes", default=False,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   364
                  help="Assume that the answer to yes/no questions is 'yes'.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   365
# Logging
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   366
group = parser.add_option_group("Logging options")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   367
group.add_option("-q", "--quiet", action="store_const", const=0,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   368
                 dest="verbose", help="Print errors only.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   369
group.add_option("-v", "--verbose", action="store_const", const=2,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   370
                 dest="verbose", default=1,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   371
                 help="Print info level logs (default).")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   372
group.add_option("--noisy", action="store_const", const=3,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   373
                 dest="verbose", help="Print all logs.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   374
# Review server
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   375
group = parser.add_option_group("Review server options")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   376
group.add_option("-s", "--server", action="store", dest="server",
148
37505d64e57b upload.py customized to work with codereviews.googleopensourceprograms.com.
Todd Larsen <tlarsen@google.com>
parents: 147
diff changeset
   377
                 default="codereviews.googleopensourceprograms.com",
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   378
                 metavar="SERVER",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   379
                 help=("The server to upload to. The format is host[:port]. "
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   380
                       "Defaults to '%default'."))
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   381
group.add_option("-e", "--email", action="store", dest="email",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   382
                 metavar="EMAIL", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   383
                 help="The username to use. Will prompt if omitted.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   384
group.add_option("-H", "--host", action="store", dest="host",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   385
                 metavar="HOST", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   386
                 help="Overrides the Host header sent with all RPCs.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   387
group.add_option("--no_cookies", action="store_false",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   388
                 dest="save_cookies", default=True,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   389
                 help="Do not save authentication cookies to local disk.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   390
# Issue
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   391
group = parser.add_option_group("Issue options")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   392
group.add_option("-d", "--description", action="store", dest="description",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   393
                 metavar="DESCRIPTION", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   394
                 help="Optional description when creating an issue.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   395
group.add_option("-f", "--description_file", action="store",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   396
                 dest="description_file", metavar="DESCRIPTION_FILE",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   397
                 default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   398
                 help="Optional path of a file that contains "
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   399
                      "the description when creating an issue.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   400
group.add_option("-r", "--reviewers", action="store", dest="reviewers",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   401
                 metavar="REVIEWERS", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   402
                 help="Add reviewers (comma separated email addresses).")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   403
group.add_option("--cc", action="store", dest="cc",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   404
                 metavar="CC", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   405
                 help="Add CC (comma separated email addresses).")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   406
# Upload options
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   407
group = parser.add_option_group("Patch options")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   408
group.add_option("-m", "--message", action="store", dest="message",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   409
                 metavar="MESSAGE", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   410
                 help="A message to identify the patch. "
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   411
                      "Will prompt if omitted.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   412
group.add_option("-i", "--issue", type="int", action="store",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   413
                 metavar="ISSUE", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   414
                 help="Issue number to which to add. Defaults to new issue.")
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   415
group.add_option("--download_base", action="store_true",
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   416
                 dest="download_base", default=False,
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   417
                 help="Base files will be downloaded by the server "
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   418
                 "(side-by-side diffs may not work on files with CRs).")
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   419
group.add_option("--rev", action="store", dest="revision",
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   420
                 metavar="REV", default=None,
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   421
                 help="Branch/tree/revision to diff against (used by DVCS).")
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   422
group.add_option("--send_mail", action="store_true",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   423
                 dest="send_mail", default=False,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   424
                 help="Send notification email to reviewers.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   425
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   426
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   427
def GetRpcServer(options):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   428
  """Returns an instance of an AbstractRpcServer.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   429
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   430
  Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   431
    A new AbstractRpcServer, on which RPC calls can be made.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   432
  """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   433
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   434
  rpc_server_class = HttpRpcServer
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   435
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   436
  def GetUserCredentials():
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   437
    """Prompts the user for a username and password."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   438
    email = options.email
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   439
    if email is None:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   440
      prompt = "Email (login for uploading to %s): " % options.server
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   441
      email = raw_input(prompt).strip()
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   442
    password = getpass.getpass("Password for %s: " % email)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   443
    return (email, password)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   444
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   445
  # If this is the dev_appserver, use fake authentication.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   446
  host = (options.host or options.server).lower()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   447
  if host == "localhost" or host.startswith("localhost:"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   448
    email = options.email
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   449
    if email is None:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   450
      email = "test@example.com"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   451
      logging.info("Using debug user %s.  Override with --email" % email)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   452
    server = rpc_server_class(
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   453
        options.server,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   454
        lambda: (email, "password"),
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   455
        host_override=options.host,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   456
        extra_headers={"Cookie":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   457
                       'dev_appserver_login="%s:False"' % email},
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   458
        save_cookies=options.save_cookies)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   459
    # Don't try to talk to ClientLogin.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   460
    server.authenticated = True
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   461
    return server
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   462
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   463
  return rpc_server_class(options.server, GetUserCredentials,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   464
                          host_override=options.host,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   465
                          save_cookies=options.save_cookies)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   466
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   467
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   468
def EncodeMultipartFormData(fields, files):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   469
  """Encode form fields for multipart/form-data.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   470
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   471
  Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   472
    fields: A sequence of (name, value) elements for regular form fields.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   473
    files: A sequence of (name, filename, value) elements for data to be
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   474
           uploaded as files.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   475
  Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   476
    (content_type, body) ready for httplib.HTTP instance.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   477
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   478
  Source:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   479
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   480
  """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   481
  BOUNDARY = '-M-A-G-I-C---B-O-U-N-D-A-R-Y-'
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   482
  CRLF = '\r\n'
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   483
  lines = []
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   484
  for (key, value) in fields:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   485
    lines.append('--' + BOUNDARY)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   486
    lines.append('Content-Disposition: form-data; name="%s"' % key)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   487
    lines.append('')
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   488
    lines.append(value)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   489
  for (key, filename, value) in files:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   490
    lines.append('--' + BOUNDARY)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   491
    lines.append('Content-Disposition: form-data; name="%s"; filename="%s"' %
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   492
             (key, filename))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   493
    lines.append('Content-Type: %s' % GetContentType(filename))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   494
    lines.append('')
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   495
    lines.append(value)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   496
  lines.append('--' + BOUNDARY + '--')
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   497
  lines.append('')
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   498
  body = CRLF.join(lines)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   499
  content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   500
  return content_type, body
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   501
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   502
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   503
def GetContentType(filename):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   504
  """Helper to guess the content-type from the filename."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   505
  return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   506
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   507
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   508
# Use a shell for subcommands on Windows to get a PATH search.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   509
use_shell = sys.platform.startswith("win")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   510
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   511
def RunShellWithReturnCode(command, print_output=False,
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   512
                           universal_newlines=True):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   513
  """Executes a command and returns the output and the return code.
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   514
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   515
  Args:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   516
    command: Command to execute.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   517
    print_output: If True, the output is printed to stdout.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   518
    universal_newlines: Use universal_newlines flag (default: True).
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   519
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   520
  Returns:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   521
    Tuple (output, return code)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   522
  """
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   523
  logging.info("Running %s", command)
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   524
  p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=use_shell,
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   525
                       universal_newlines=universal_newlines)
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   526
  if print_output:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   527
    output_array = []
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   528
    while True:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   529
      line = p.stdout.readline()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   530
      if not line:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   531
        break
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   532
      print line.strip("\n")
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   533
      output_array.append(line)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   534
    output = "".join(output_array)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   535
  else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   536
    output = p.stdout.read()
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   537
  p.wait()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   538
  p.stdout.close()
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   539
  return output, p.returncode
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   540
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   541
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   542
def RunShell(command, silent_ok=False, universal_newlines=True,
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   543
             print_output=False):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   544
  data, retcode = RunShellWithReturnCode(command, print_output,
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   545
                                         universal_newlines)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   546
  if retcode:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   547
    ErrorExit("Got error status from %s:\n%s" % (command, data))
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   548
  if not silent_ok and not data:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   549
    ErrorExit("No output from %s" % command)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   550
  return data
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   551
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   552
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   553
class VersionControlSystem(object):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   554
  """Abstract base class providing an interface to the VCS."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   555
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   556
  def __init__(self, options):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   557
    """Constructor.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   558
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   559
    Args:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   560
      options: Command line options.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   561
    """
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   562
    self.options = options
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   563
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   564
  def GenerateDiff(self, args):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   565
    """Return the current diff as a string.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   566
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   567
    Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   568
      args: Extra arguments to pass to the diff command.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   569
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   570
    raise NotImplementedError(
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   571
        "abstract method -- subclass %s must override" % self.__class__)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   572
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   573
  def GetUnknownFiles(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   574
    """Return a list of files unknown to the VCS."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   575
    raise NotImplementedError(
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   576
        "abstract method -- subclass %s must override" % self.__class__)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   577
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   578
  def CheckForUnknownFiles(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   579
    """Show an "are you sure?" prompt if there are unknown files."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   580
    unknown_files = self.GetUnknownFiles()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   581
    if unknown_files:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   582
      print "The following files are not added to version control:"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   583
      for line in unknown_files:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   584
        print line
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   585
      prompt = "Are you sure to continue?(y/N) "
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   586
      answer = raw_input(prompt).strip()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   587
      if answer != "y":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   588
        ErrorExit("User aborted")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   589
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   590
  def GetBaseFile(self, filename):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   591
    """Get the content of the upstream version of a file.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   592
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   593
    Returns:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   594
      A tuple (base_content, new_content, is_binary, status)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   595
        base_content: The contents of the base file.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   596
        new_content: For text files, this is empty.  For binary files, this is
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   597
          the contents of the new file, since the diff output won't contain
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   598
          information to reconstruct the current file.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   599
        is_binary: True iff the file is binary.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   600
        status: The status of the file.
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   601
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   602
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   603
    raise NotImplementedError(
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   604
        "abstract method -- subclass %s must override" % self.__class__)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   605
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   606
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   607
  def GetBaseFiles(self, diff):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   608
    """Helper that calls GetBase file for each file in the patch.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   609
    
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   610
    Returns:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   611
      A dictionary that maps from filename to GetBaseFile's tuple.  Filenames
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   612
      are retrieved based on lines that start with "Index:" or
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   613
      "Property changes on:".
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   614
    """
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   615
    files = {}
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   616
    for line in diff.splitlines(True):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   617
      if line.startswith('Index:') or line.startswith('Property changes on:'):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   618
        unused, filename = line.split(':', 1)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   619
        # On Windows if a file has property changes its filename uses '\'
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   620
        # instead of '/'.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   621
        filename = filename.strip().replace('\\', '/')
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   622
        files[filename] = self.GetBaseFile(filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   623
    return files
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   624
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   625
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   626
  def UploadBaseFiles(self, issue, rpc_server, patch_list, patchset, options,
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   627
                      files):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   628
    """Uploads the base files (and if necessary, the current ones as well)."""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   629
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   630
    def UploadFile(filename, file_id, content, is_binary, status, is_base):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   631
      """Uploads a file to the server."""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   632
      file_too_large = False
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   633
      if is_base:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   634
        type = "base"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   635
      else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   636
        type = "current"
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   637
      if len(content) > MAX_UPLOAD_SIZE:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   638
        print ("Not uploading the %s file for %s because it's too large." %
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   639
               (type, filename))
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   640
        file_too_large = True
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   641
        content = ""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   642
      checksum = md5.new(content).hexdigest()
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   643
      if options.verbose > 0 and not file_too_large:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   644
        print "Uploading %s file for %s" % (type, filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   645
      url = "/%d/upload_content/%d/%d" % (int(issue), int(patchset), file_id)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   646
      form_fields = [("filename", filename),
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   647
                     ("status", status),
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   648
                     ("checksum", checksum),
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   649
                     ("is_binary", str(is_binary)),
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   650
                     ("is_current", str(not is_base)),
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   651
                    ]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   652
      if file_too_large:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   653
        form_fields.append(("file_too_large", "1"))
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   654
      if options.email:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   655
        form_fields.append(("user", options.email))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   656
      ctype, body = EncodeMultipartFormData(form_fields,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   657
                                            [("data", filename, content)])
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   658
      response_body = rpc_server.Send(url, body,
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   659
                                      content_type=ctype)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   660
      if not response_body.startswith("OK"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   661
        StatusUpdate("  --> %s" % response_body)
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   662
        sys.exit(1)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   663
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   664
    patches = dict()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   665
    [patches.setdefault(v, k) for k, v in patch_list]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   666
    for filename in patches.keys():
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   667
      base_content, new_content, is_binary, status = files[filename]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   668
      file_id_str = patches.get(filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   669
      if file_id_str.find("nobase") != -1:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   670
        base_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   671
        file_id_str = file_id_str[file_id_str.rfind("_") + 1:]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   672
      file_id = int(file_id_str)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   673
      if base_content != None:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   674
        UploadFile(filename, file_id, base_content, is_binary, status, True)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   675
      if new_content != None:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   676
        UploadFile(filename, file_id, new_content, is_binary, status, False)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   677
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   678
  def IsImage(self, filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   679
    """Returns true if the filename has an image extension."""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   680
    mimetype =  mimetypes.guess_type(filename)[0]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   681
    if not mimetype:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   682
      return False
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   683
    return mimetype.startswith("image/")
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   684
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   685
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   686
class SubversionVCS(VersionControlSystem):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   687
  """Implementation of the VersionControlSystem interface for Subversion."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   688
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   689
  def __init__(self, options):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   690
    super(SubversionVCS, self).__init__(options)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   691
    if self.options.revision:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   692
      match = re.match(r"(\d+)(:(\d+))?", self.options.revision)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   693
      if not match:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   694
        ErrorExit("Invalid Subversion revision %s." % self.options.revision)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   695
      self.rev_start = match.group(1)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   696
      self.rev_end = match.group(3)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   697
    else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   698
      self.rev_start = self.rev_end = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   699
    # Cache output from "svn list -r REVNO dirname".
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   700
    # Keys: dirname, Values: 2-tuple (ouput for start rev and end rev).
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   701
    self.svnls_cache = {}
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   702
    # SVN base URL is required to fetch files deleted in an older revision.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   703
    # Result is cached to not guess it over and over again in GetBaseFile().
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   704
    required = self.options.download_base or self.options.revision is not None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   705
    self.svn_base = self._GuessBase(required)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   706
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   707
  def GuessBase(self, required):
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   708
    """Wrapper for _GuessBase."""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   709
    return self.svn_base
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   710
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   711
  def _GuessBase(self, required):
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   712
    """Returns the SVN base URL.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   713
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   714
    Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   715
      required: If true, exits if the url can't be guessed, otherwise None is
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   716
        returned.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   717
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   718
    info = RunShell(["svn", "info"])
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   719
    for line in info.splitlines():
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   720
      words = line.split()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   721
      if len(words) == 2 and words[0] == "URL:":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   722
        url = words[1]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   723
        scheme, netloc, path, params, query, fragment = urlparse.urlparse(url)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   724
        username, netloc = urllib.splituser(netloc)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   725
        if username:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   726
          logging.info("Removed username from base URL")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   727
        if netloc.endswith("svn.python.org"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   728
          if netloc == "svn.python.org":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   729
            if path.startswith("/projects/"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   730
              path = path[9:]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   731
          elif netloc != "pythondev@svn.python.org":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   732
            ErrorExit("Unrecognized Python URL: %s" % url)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   733
          base = "http://svn.python.org/view/*checkout*%s/" % path
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   734
          logging.info("Guessed Python base = %s", base)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   735
        elif netloc.endswith("svn.collab.net"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   736
          if path.startswith("/repos/"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   737
            path = path[6:]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   738
          base = "http://svn.collab.net/viewvc/*checkout*%s/" % path
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   739
          logging.info("Guessed CollabNet base = %s", base)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   740
        elif netloc.endswith(".googlecode.com"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   741
          path = path + "/"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   742
          base = urlparse.urlunparse(("http", netloc, path, params,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   743
                                      query, fragment))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   744
          logging.info("Guessed Google Code base = %s", base)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   745
        else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   746
          path = path + "/"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   747
          base = urlparse.urlunparse((scheme, netloc, path, params,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   748
                                      query, fragment))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   749
          logging.info("Guessed base = %s", base)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   750
        return base
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   751
    if required:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   752
      ErrorExit("Can't find URL in output from svn info")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   753
    return None
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   754
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   755
  def GenerateDiff(self, args):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   756
    cmd = ["svn", "diff"]
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   757
    if self.options.revision:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   758
      cmd += ["-r", self.options.revision]
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   759
    cmd.extend(args)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   760
    data = RunShell(cmd)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   761
    count = 0
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   762
    for line in data.splitlines():
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   763
      if line.startswith("Index:") or line.startswith("Property changes on:"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   764
        count += 1
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   765
        logging.info(line)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   766
    if not count:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   767
      ErrorExit("No valid patches found in output from svn diff")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   768
    return data
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   769
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   770
  def _CollapseKeywords(self, content, keyword_str):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   771
    """Collapses SVN keywords."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   772
    # svn cat translates keywords but svn diff doesn't. As a result of this
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   773
    # behavior patching.PatchChunks() fails with a chunk mismatch error.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   774
    # This part was originally written by the Review Board development team
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   775
    # who had the same problem (http://reviews.review-board.org/r/276/).
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   776
    # Mapping of keywords to known aliases
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   777
    svn_keywords = {
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   778
      # Standard keywords
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   779
      'Date':                ['Date', 'LastChangedDate'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   780
      'Revision':            ['Revision', 'LastChangedRevision', 'Rev'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   781
      'Author':              ['Author', 'LastChangedBy'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   782
      'HeadURL':             ['HeadURL', 'URL'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   783
      'Id':                  ['Id'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   784
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   785
      # Aliases
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   786
      'LastChangedDate':     ['LastChangedDate', 'Date'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   787
      'LastChangedRevision': ['LastChangedRevision', 'Rev', 'Revision'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   788
      'LastChangedBy':       ['LastChangedBy', 'Author'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   789
      'URL':                 ['URL', 'HeadURL'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   790
    }
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   791
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   792
    def repl(m):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   793
       if m.group(2):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   794
         return "$%s::%s$" % (m.group(1), " " * len(m.group(3)))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   795
       return "$%s$" % m.group(1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   796
    keywords = [keyword
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   797
                for name in keyword_str.split(" ")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   798
                for keyword in svn_keywords.get(name, [])]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   799
    return re.sub(r"\$(%s):(:?)([^\$]+)\$" % '|'.join(keywords), repl, content)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   800
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   801
  def GetUnknownFiles(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   802
    status = RunShell(["svn", "status", "--ignore-externals"], silent_ok=True)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   803
    unknown_files = []
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   804
    for line in status.split("\n"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   805
      if line and line[0] == "?":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   806
        unknown_files.append(line)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   807
    return unknown_files
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   808
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   809
  def ReadFile(self, filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   810
    """Returns the contents of a file."""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   811
    file = open(filename, 'rb')
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   812
    result = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   813
    try:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   814
      result = file.read()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   815
    finally:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   816
      file.close()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   817
    return result
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   818
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   819
  def GetStatus(self, filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   820
    """Returns the status of a file."""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   821
    if not self.options.revision:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   822
      status = RunShell(["svn", "status", "--ignore-externals", filename])
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   823
      if not status:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   824
        ErrorExit("svn status returned no output for %s" % filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   825
      status_lines = status.splitlines()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   826
      # If file is in a cl, the output will begin with
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   827
      # "\n--- Changelist 'cl_name':\n".  See
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   828
      # http://svn.collab.net/repos/svn/trunk/notes/changelist-design.txt
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   829
      if (len(status_lines) == 3 and
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   830
          not status_lines[0] and
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   831
          status_lines[1].startswith("--- Changelist")):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   832
        status = status_lines[2]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   833
      else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   834
        status = status_lines[0]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   835
    # If we have a revision to diff against we need to run "svn list"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   836
    # for the old and the new revision and compare the results to get
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   837
    # the correct status for a file.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   838
    else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   839
      dirname, relfilename = os.path.split(filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   840
      if dirname not in self.svnls_cache:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   841
        cmd = ["svn", "list", "-r", self.rev_start, dirname or "."]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   842
        out, returncode = RunShellWithReturnCode(cmd)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   843
        if returncode:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   844
          ErrorExit("Failed to get status for %s." % filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   845
        old_files = out.splitlines()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   846
        args = ["svn", "list"]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   847
        if self.rev_end:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   848
          args += ["-r", self.rev_end]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   849
        cmd = args + [dirname or "."]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   850
        out, returncode = RunShellWithReturnCode(cmd)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   851
        if returncode:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   852
          ErrorExit("Failed to run command %s" % cmd)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   853
        self.svnls_cache[dirname] = (old_files, out.splitlines())
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   854
      old_files, new_files = self.svnls_cache[dirname]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   855
      if relfilename in old_files and relfilename not in new_files:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   856
        status = "D   "
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   857
      elif relfilename in old_files and relfilename in new_files:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   858
        status = "M   "
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   859
      else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   860
        status = "A   "
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   861
    return status
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   862
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   863
  def GetBaseFile(self, filename):
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   864
    status = self.GetStatus(filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   865
    base_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   866
    new_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   867
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   868
    # If a file is copied its status will be "A  +", which signifies
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   869
    # "addition-with-history".  See "svn st" for more information.  We need to
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   870
    # upload the original file or else diff parsing will fail if the file was
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   871
    # edited.
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   872
    if status[0] == "A" and status[3] != "+":
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   873
      # We'll need to upload the new content if we're adding a binary file
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   874
      # since diff's output won't contain it.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   875
      mimetype = RunShell(["svn", "propget", "svn:mime-type", filename],
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   876
                          silent_ok=True)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   877
      base_content = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   878
      is_binary = mimetype and not mimetype.startswith("text/")
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   879
      if is_binary and self.IsImage(filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   880
        new_content = self.ReadFile(filename)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   881
    elif (status[0] in ("M", "D", "R") or
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   882
          (status[0] == "A" and status[3] == "+") or  # Copied file.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   883
          (status[0] == " " and status[1] == "M")):  # Property change.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   884
      args = []
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   885
      if self.options.revision:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   886
        url = "%s/%s@%s" % (self.svn_base, filename, self.rev_start)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   887
      else:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   888
        # Don't change filename, it's needed later.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   889
        url = filename
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   890
        args += ["-r", "BASE"]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   891
      cmd = ["svn"] + args + ["propget", "svn:mime-type", url]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   892
      mimetype, returncode = RunShellWithReturnCode(cmd)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   893
      if returncode:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   894
        # File does not exist in the requested revision.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   895
        # Reset mimetype, it contains an error message.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   896
        mimetype = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   897
      get_base = False
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   898
      is_binary = mimetype and not mimetype.startswith("text/")
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   899
      if status[0] == " ":
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   900
        # Empty base content just to force an upload.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   901
        base_content = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   902
      elif is_binary:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   903
        if self.IsImage(filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   904
          get_base = True
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   905
          if status[0] == "M":
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   906
            if not self.rev_end:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   907
              new_content = self.ReadFile(filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   908
            else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   909
              url = "%s/%s@%s" % (self.svn_base, filename, self.rev_end)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   910
              new_content = RunShell(["svn", "cat", url],
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   911
                                     universal_newlines=True)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   912
        else:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   913
          base_content = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   914
      else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   915
        get_base = True
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   916
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   917
      if get_base:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   918
        if is_binary:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   919
          universal_newlines = False
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   920
        else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   921
          universal_newlines = True
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   922
        if self.rev_start:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   923
          # "svn cat -r REV delete_file.txt" doesn't work. cat requires
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   924
          # the full URL with "@REV" appended instead of using "-r" option.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   925
          url = "%s/%s@%s" % (self.svn_base, filename, self.rev_start)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   926
          base_content = RunShell(["svn", "cat", url],
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   927
                                  universal_newlines=universal_newlines)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   928
        else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   929
          base_content = RunShell(["svn", "cat", filename],
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   930
                                  universal_newlines=universal_newlines)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   931
        if not is_binary:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   932
          args = []
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   933
          if self.rev_start:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   934
            url = "%s/%s@%s" % (self.svn_base, filename, self.rev_start)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   935
          else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   936
            url = filename
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   937
            args += ["-r", "BASE"]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   938
          cmd = ["svn"] + args + ["propget", "svn:keywords", url]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   939
          keywords, returncode = RunShellWithReturnCode(cmd)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   940
          if keywords and not returncode:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   941
            base_content = self._CollapseKeywords(base_content, keywords)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   942
    else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   943
      StatusUpdate("svn status returned unexpected output: %s" % status)
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   944
      sys.exit(1)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   945
    return base_content, new_content, is_binary, status[0:5]
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   946
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   947
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   948
class GitVCS(VersionControlSystem):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   949
  """Implementation of the VersionControlSystem interface for Git."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   950
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   951
  def __init__(self, options):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   952
    super(GitVCS, self).__init__(options)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   953
    # Map of filename -> hash of base file.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   954
    self.base_hashes = {}
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   955
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   956
  def GenerateDiff(self, extra_args):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   957
    # This is more complicated than svn's GenerateDiff because we must convert
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   958
    # the diff output to include an svn-style "Index:" line as well as record
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   959
    # the hashes of the base files, so we can upload them along with our diff.
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   960
    if self.options.revision:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   961
      extra_args = [self.options.revision] + extra_args
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   962
    gitdiff = RunShell(["git", "diff", "--full-index"] + extra_args)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   963
    svndiff = []
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   964
    filecount = 0
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   965
    filename = None
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   966
    for line in gitdiff.splitlines():
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   967
      match = re.match(r"diff --git a/(.*) b/.*$", line)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   968
      if match:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   969
        filecount += 1
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   970
        filename = match.group(1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   971
        svndiff.append("Index: %s\n" % filename)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   972
      else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   973
        # The "index" line in a git diff looks like this (long hashes elided):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   974
        #   index 82c0d44..b2cee3f 100755
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   975
        # We want to save the left hash, as that identifies the base file.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   976
        match = re.match(r"index (\w+)\.\.", line)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   977
        if match:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   978
          self.base_hashes[filename] = match.group(1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   979
      svndiff.append(line + "\n")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   980
    if not filecount:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   981
      ErrorExit("No valid patches found in output from git diff")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   982
    return "".join(svndiff)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   983
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   984
  def GetUnknownFiles(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   985
    status = RunShell(["git", "ls-files", "--exclude-standard", "--others"],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   986
                      silent_ok=True)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   987
    return status.splitlines()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   988
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   989
  def GetBaseFile(self, filename):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   990
    hash = self.base_hashes[filename]
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   991
    base_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   992
    new_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   993
    is_binary = False
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   994
    if hash == "0" * 40:  # All-zero hash indicates no base file.
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   995
      status = "A"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   996
      base_content = ""
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   997
    else:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   998
      status = "M"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   999
      base_content = RunShell(["git", "show", hash])
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1000
    return (base_content, new_content, is_binary, status)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1001
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1002
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1003
class MercurialVCS(VersionControlSystem):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1004
  """Implementation of the VersionControlSystem interface for Mercurial."""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1005
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1006
  def __init__(self, options, repo_dir):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1007
    super(MercurialVCS, self).__init__(options)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1008
    # Absolute path to repository (we can be in a subdir)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1009
    self.repo_dir = os.path.normpath(repo_dir)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1010
    # Compute the subdir
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1011
    cwd = os.path.normpath(os.getcwd())
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1012
    assert cwd.startswith(self.repo_dir)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1013
    self.subdir = cwd[len(self.repo_dir):].lstrip(r"\/")
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1014
    if self.options.revision:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1015
      self.base_rev = self.options.revision
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1016
    else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1017
      self.base_rev = RunShell(["hg", "parent", "-q"]).split(':')[1].strip()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1018
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1019
  def _GetRelPath(self, filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1020
    """Get relative path of a file according to the current directory,
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1021
    given its logical path in the repo."""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1022
    assert filename.startswith(self.subdir), filename
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1023
    return filename[len(self.subdir):].lstrip(r"\/")
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1024
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1025
  def GenerateDiff(self, extra_args):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1026
    # If no file specified, restrict to the current subdir
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1027
    extra_args = extra_args or ["."]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1028
    cmd = ["hg", "diff", "--git", "-r", self.base_rev] + extra_args
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1029
    data = RunShell(cmd, silent_ok=True)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1030
    svndiff = []
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1031
    filecount = 0
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1032
    for line in data.splitlines():
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1033
      m = re.match("diff --git a/(\S+) b/(\S+)", line)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1034
      if m:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1035
        # Modify line to make it look like as it comes from svn diff.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1036
        # With this modification no changes on the server side are required
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1037
        # to make upload.py work with Mercurial repos.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1038
        # NOTE: for proper handling of moved/copied files, we have to use
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1039
        # the second filename.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1040
        filename = m.group(2)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1041
        svndiff.append("Index: %s" % filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1042
        svndiff.append("=" * 67)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1043
        filecount += 1
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1044
        logging.info(line)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1045
      else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1046
        svndiff.append(line)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1047
    if not filecount:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1048
      ErrorExit("No valid patches found in output from hg diff")
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1049
    return "\n".join(svndiff) + "\n"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1050
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1051
  def GetUnknownFiles(self):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1052
    """Return a list of files unknown to the VCS."""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1053
    args = []
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1054
    status = RunShell(["hg", "status", "--rev", self.base_rev, "-u", "."],
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1055
        silent_ok=True)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1056
    unknown_files = []
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1057
    for line in status.splitlines():
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1058
      st, fn = line.split(" ", 1)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1059
      if st == "?":
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1060
        unknown_files.append(fn)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1061
    return unknown_files
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1062
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1063
  def GetBaseFile(self, filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1064
    # "hg status" and "hg cat" both take a path relative to the current subdir
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1065
    # rather than to the repo root, but "hg diff" has given us the full path
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1066
    # to the repo root.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1067
    base_content = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1068
    new_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1069
    is_binary = False
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1070
    oldrelpath = relpath = self._GetRelPath(filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1071
    # "hg status -C" returns two lines for moved/copied files, one otherwise
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1072
    out = RunShell(["hg", "status", "-C", "--rev", self.base_rev, relpath])
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1073
    out = out.splitlines()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1074
    # HACK: strip error message about missing file/directory if it isn't in
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1075
    # the working copy
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1076
    if out[0].startswith('%s: ' % relpath):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1077
      out = out[1:]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1078
    if len(out) > 1:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1079
      # Moved/copied => considered as modified, use old filename to 
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1080
      # retrieve base contents
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1081
      oldrelpath = out[1].strip()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1082
      status = "M"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1083
    else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1084
      status, _ = out[0].split(' ', 1)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1085
    if status != "A":
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1086
      base_content = RunShell(["hg", "cat", "-r", self.base_rev, oldrelpath],
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1087
        silent_ok=True)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1088
      is_binary = "\0" in base_content  # Mercurial's heuristic
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1089
    if status != "R":
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1090
      new_content = open(relpath, "rb").read()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1091
      is_binary = is_binary or "\0" in new_content
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1092
    if is_binary and base_content:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1093
      # Fetch again without converting newlines
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1094
      base_content = RunShell(["hg", "cat", "-r", self.base_rev, oldrelpath],
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1095
        silent_ok=True, universal_newlines=False)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1096
    if not is_binary or not self.IsImage(relpath):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1097
      new_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1098
    return base_content, new_content, is_binary, status
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1099
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1100
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1101
# NOTE: The SplitPatch function is duplicated in engine.py, keep them in sync.
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1102
def SplitPatch(data):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1103
  """Splits a patch into separate pieces for each file.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1104
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1105
  Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1106
    data: A string containing the output of svn diff.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1107
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1108
  Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1109
    A list of 2-tuple (filename, text) where text is the svn diff output
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1110
      pertaining to filename.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1111
  """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1112
  patches = []
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1113
  filename = None
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1114
  diff = []
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1115
  for line in data.splitlines(True):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1116
    new_filename = None
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1117
    if line.startswith('Index:'):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1118
      unused, new_filename = line.split(':', 1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1119
      new_filename = new_filename.strip()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1120
    elif line.startswith('Property changes on:'):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1121
      unused, temp_filename = line.split(':', 1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1122
      # When a file is modified, paths use '/' between directories, however
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1123
      # when a property is modified '\' is used on Windows.  Make them the same
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1124
      # otherwise the file shows up twice.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1125
      temp_filename = temp_filename.strip().replace('\\', '/')
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1126
      if temp_filename != filename:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1127
        # File has property changes but no modifications, create a new diff.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1128
        new_filename = temp_filename
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1129
    if new_filename:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1130
      if filename and diff:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1131
        patches.append((filename, ''.join(diff)))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1132
      filename = new_filename
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1133
      diff = [line]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1134
      continue
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1135
    if diff is not None:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1136
      diff.append(line)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1137
  if filename and diff:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1138
    patches.append((filename, ''.join(diff)))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1139
  return patches
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1140
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1141
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1142
def UploadSeparatePatches(issue, rpc_server, patchset, data, options):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1143
  """Uploads a separate patch for each file in the diff output.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1144
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1145
  Returns a list of [patch_key, filename] for each file.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1146
  """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1147
  patches = SplitPatch(data)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1148
  rv = []
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1149
  for patch in patches:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1150
    if len(patch[1]) > MAX_UPLOAD_SIZE:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1151
      print ("Not uploading the patch for " + patch[0] +
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1152
             " because the file is too large.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1153
      continue
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1154
    form_fields = [("filename", patch[0])]
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1155
    if not options.download_base:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1156
      form_fields.append(("content_upload", "1"))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1157
    files = [("data", "data.diff", patch[1])]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1158
    ctype, body = EncodeMultipartFormData(form_fields, files)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1159
    url = "/%d/upload_patch/%d" % (int(issue), int(patchset))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1160
    print "Uploading patch for " + patch[0]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1161
    response_body = rpc_server.Send(url, body, content_type=ctype)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1162
    lines = response_body.splitlines()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1163
    if not lines or lines[0] != "OK":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1164
      StatusUpdate("  --> %s" % response_body)
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1165
      sys.exit(1)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1166
    rv.append([lines[1], patch[0]])
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1167
  return rv
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1168
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1169
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1170
def GuessVCS(options):
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1171
  """Helper to guess the version control system.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1172
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1173
  This examines the current directory, guesses which VersionControlSystem
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1174
  we're using, and returns an instance of the appropriate class.  Exit with an
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1175
  error if we can't figure it out.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1176
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1177
  Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1178
    A VersionControlSystem instance. Exits if the VCS can't be guessed.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1179
  """
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1180
  # Mercurial has a command to get the base directory of a repository
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1181
  # Try running it, but don't die if we don't have hg installed.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1182
  # NOTE: we try Mercurial first as it can sit on top of an SVN working copy.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1183
  try:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1184
    out, returncode = RunShellWithReturnCode(["hg", "root"])
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1185
    if returncode == 0:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1186
      return MercurialVCS(options, out.strip())
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1187
  except OSError, (errno, message):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1188
    if errno != 2:  # ENOENT -- they don't have hg installed.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1189
      raise
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1190
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1191
  # Subversion has a .svn in all working directories.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1192
  if os.path.isdir('.svn'):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1193
    logging.info("Guessed VCS = Subversion")
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1194
    return SubversionVCS(options)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1195
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1196
  # Git has a command to test if you're in a git tree.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1197
  # Try running it, but don't die if we don't have git installed.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1198
  try:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1199
    out, returncode = RunShellWithReturnCode(["git", "rev-parse",
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1200
                                              "--is-inside-work-tree"])
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1201
    if returncode == 0:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1202
      return GitVCS(options)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1203
  except OSError, (errno, message):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1204
    if errno != 2:  # ENOENT -- they don't have git installed.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1205
      raise
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1206
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1207
  ErrorExit(("Could not guess version control system. "
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1208
             "Are you in a working copy directory?"))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1209
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1210
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1211
def RealMain(argv, data=None):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1212
  logging.basicConfig(format=("%(asctime).19s %(levelname)s %(filename)s:"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1213
                              "%(lineno)s %(message)s "))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1214
  os.environ['LC_ALL'] = 'C'
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1215
  options, args = parser.parse_args(argv[1:])
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1216
  global verbosity
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1217
  verbosity = options.verbose
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1218
  if verbosity >= 3:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1219
    logging.getLogger().setLevel(logging.DEBUG)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1220
  elif verbosity >= 2:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1221
    logging.getLogger().setLevel(logging.INFO)
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1222
  vcs = GuessVCS(options)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1223
  if isinstance(vcs, SubversionVCS):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1224
    # base field is only allowed for Subversion.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1225
    # Note: Fetching base files may become deprecated in future releases.
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1226
    base = vcs.GuessBase(options.download_base)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1227
  else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1228
    base = None
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1229
  if not base and options.download_base:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1230
    options.download_base = True
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1231
    logging.info("Enabled upload of base file")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1232
  if not options.assume_yes:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1233
    vcs.CheckForUnknownFiles()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1234
  if data is None:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1235
    data = vcs.GenerateDiff(args)
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1236
  files = vcs.GetBaseFiles(data)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1237
  if verbosity >= 1:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1238
    print "Upload server:", options.server, "(change with -s/--server)"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1239
  if options.issue:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1240
    prompt = "Message describing this patch set: "
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1241
  else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1242
    prompt = "New issue subject: "
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1243
  message = options.message or raw_input(prompt).strip()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1244
  if not message:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1245
    ErrorExit("A non-empty message is required")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1246
  rpc_server = GetRpcServer(options)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1247
  form_fields = [("subject", message)]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1248
  if base:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1249
    form_fields.append(("base", base))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1250
  if options.issue:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1251
    form_fields.append(("issue", str(options.issue)))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1252
  if options.email:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1253
    form_fields.append(("user", options.email))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1254
  if options.reviewers:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1255
    for reviewer in options.reviewers.split(','):
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1256
      if "@" in reviewer and not reviewer.split("@")[1].count(".") == 1:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1257
        ErrorExit("Invalid email address: %s" % reviewer)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1258
    form_fields.append(("reviewers", options.reviewers))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1259
  if options.cc:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1260
    for cc in options.cc.split(','):
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1261
      if "@" in cc and not cc.split("@")[1].count(".") == 1:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1262
        ErrorExit("Invalid email address: %s" % cc)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1263
    form_fields.append(("cc", options.cc))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1264
  description = options.description
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1265
  if options.description_file:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1266
    if options.description:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1267
      ErrorExit("Can't specify description and description_file")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1268
    file = open(options.description_file, 'r')
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1269
    description = file.read()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1270
    file.close()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1271
  if description:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1272
    form_fields.append(("description", description))
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1273
  # Send a hash of all the base file so the server can determine if a copy
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1274
  # already exists in an earlier patchset.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1275
  base_hashes = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1276
  for file, info in files.iteritems():
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1277
    if not info[0] is None:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1278
      checksum = md5.new(info[0]).hexdigest()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1279
      if base_hashes:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1280
        base_hashes += "|"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1281
      base_hashes += checksum + ":" + file
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1282
  form_fields.append(("base_hashes", base_hashes))
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1283
  # If we're uploading base files, don't send the email before the uploads, so
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1284
  # that it contains the file status.
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1285
  if options.send_mail and options.download_base:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1286
    form_fields.append(("send_mail", "1"))
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1287
  if not options.download_base:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1288
    form_fields.append(("content_upload", "1"))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1289
  if len(data) > MAX_UPLOAD_SIZE:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1290
    print "Patch is large, so uploading file patches separately."
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1291
    uploaded_diff_file = []
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1292
    form_fields.append(("separate_patches", "1"))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1293
  else:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1294
    uploaded_diff_file = [("data", "data.diff", data)]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1295
  ctype, body = EncodeMultipartFormData(form_fields, uploaded_diff_file)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1296
  response_body = rpc_server.Send("/upload", body, content_type=ctype)
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1297
  if not options.download_base or not uploaded_diff_file:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1298
    lines = response_body.splitlines()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1299
    if len(lines) >= 2:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1300
      msg = lines[0]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1301
      patchset = lines[1].strip()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1302
      patches = [x.split(" ", 1) for x in lines[2:]]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1303
    else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1304
      msg = response_body
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1305
  else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1306
    msg = response_body
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1307
  StatusUpdate(msg)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1308
  if not response_body.startswith("Issue created.") and \
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1309
  not response_body.startswith("Issue updated."):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1310
    sys.exit(0)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1311
  issue = msg[msg.rfind("/")+1:]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1312
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1313
  if not uploaded_diff_file:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1314
    result = UploadSeparatePatches(issue, rpc_server, patchset, data, options)
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1315
    if not options.download_base:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1316
      patches = result
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1317
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1318
  if not options.download_base:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1319
    vcs.UploadBaseFiles(issue, rpc_server, patches, patchset, options, files)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1320
    if options.send_mail:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1321
      rpc_server.Send("/" + issue + "/mail", payload="")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1322
  return issue
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1323
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1324
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1325
def main():
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1326
  try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1327
    RealMain(sys.argv)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1328
  except KeyboardInterrupt:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1329
    print
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1330
    StatusUpdate("Interrupted.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1331
    sys.exit(1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1332
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1333
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1334
if __name__ == "__main__":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1335
  main()