thirdparty/rietveld/upload.py
author Lennard de Rijk <ljvderijk@gmail.com>
Fri, 23 Jan 2009 12:00:48 +0000
changeset 920 39badbfb80be
parent 687 4755caf1d7a6
permissions -rwxr-xr-x
Added the new way to process invites for club_admin only. This patch contains the separability needed to ensure that we can successfully do the access checks we need. Host invites can for the time being only be completed by developers due to the reorganization. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
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 = "HOSTED_OR_GOOGLE"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   155
    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
   156
      # Needed for use inside Google.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   157
      account_type = "HOSTED"
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   158
    req = self._CreateRequest(
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   159
        url="https://www.google.com/accounts/ClientLogin",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   160
        data=urllib.urlencode({
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   161
            "Email": email,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   162
            "Passwd": password,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   163
            "service": "ah",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   164
            "source": "rietveld-codereview-upload",
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   165
            "accountType": account_type,
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   166
        }),
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   167
    )
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   168
    try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   169
      response = self.opener.open(req)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   170
      response_body = response.read()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   171
      response_dict = dict(x.split("=")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   172
                           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
   173
      return response_dict["Auth"]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   174
    except urllib2.HTTPError, e:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   175
      if e.code == 403:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   176
        body = e.read()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   177
        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
   178
        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
   179
                               e.headers, response_dict)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   180
      else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   181
        raise
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   182
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   183
  def _GetAuthCookie(self, auth_token):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   184
    """Fetches authentication cookies for an authentication token.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   185
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   186
    Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   187
      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
   188
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   189
    Raises:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   190
      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
   191
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   192
    # 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
   193
    continue_location = "http://localhost/"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   194
    args = {"continue": continue_location, "auth": auth_token}
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   195
    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
   196
                              (self.host, urllib.urlencode(args)))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   197
    try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   198
      response = self.opener.open(req)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   199
    except urllib2.HTTPError, e:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   200
      response = e
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   201
    if (response.code != 302 or
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   202
        response.info()["location"] != continue_location):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   203
      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
   204
                              response.headers, response.fp)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   205
    self.authenticated = True
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   206
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   207
  def _Authenticate(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   208
    """Authenticates the user.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   209
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   210
    The authentication process works as follows:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   211
     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
   212
     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
   213
        (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
   214
     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
   215
        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
   216
        us to the URL we provided.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   217
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   218
    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
   219
    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
   220
    authenticate ourselves with ClientLogin.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   221
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   222
    for i in range(3):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   223
      credentials = self.auth_function()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   224
      try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   225
        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
   226
      except ClientLoginError, e:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   227
        if e.reason == "BadAuthentication":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   228
          print >>sys.stderr, "Invalid username or password."
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   229
          continue
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   230
        if e.reason == "CaptchaRequired":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   231
          print >>sys.stderr, (
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   232
              "Please go to\n"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   233
              "https://www.google.com/accounts/DisplayUnlockCaptcha\n"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   234
              "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
   235
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   236
        if e.reason == "NotVerified":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   237
          print >>sys.stderr, "Account not verified."
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   238
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   239
        if e.reason == "TermsNotAgreed":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   240
          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
   241
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   242
        if e.reason == "AccountDeleted":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   243
          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
   244
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   245
        if e.reason == "AccountDisabled":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   246
          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
   247
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   248
        if e.reason == "ServiceDisabled":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   249
          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
   250
                               "disabled.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   251
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   252
        if e.reason == "ServiceUnavailable":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   253
          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
   254
          break
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   255
        raise
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   256
      self._GetAuthCookie(auth_token)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   257
      return
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   258
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   259
  def Send(self, request_path, payload=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   260
           content_type="application/octet-stream",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   261
           timeout=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   262
           **kwargs):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   263
    """Sends an RPC and returns the response.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   264
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   265
    Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   266
      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
   267
      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
   268
      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
   269
      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
   270
        (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
   271
      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
   272
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   273
    Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   274
      The response body, as a string.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   275
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   276
    # 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
   277
    # whether it is necessary.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   278
    if not self.authenticated:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   279
      self._Authenticate()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   280
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   281
    old_timeout = socket.getdefaulttimeout()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   282
    socket.setdefaulttimeout(timeout)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   283
    try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   284
      tries = 0
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   285
      while True:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   286
        tries += 1
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   287
        args = dict(kwargs)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   288
        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
   289
        if args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   290
          url += "?" + urllib.urlencode(args)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   291
        req = self._CreateRequest(url=url, data=payload)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   292
        req.add_header("Content-Type", content_type)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   293
        try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   294
          f = self.opener.open(req)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   295
          response = f.read()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   296
          f.close()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   297
          return response
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   298
        except urllib2.HTTPError, e:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   299
          if tries > 3:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   300
            raise
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   301
          elif e.code == 401:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   302
            self._Authenticate()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   303
##           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
   304
##             # Server Error - try again.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   305
##             continue
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   306
          else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   307
            raise
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   308
    finally:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   309
      socket.setdefaulttimeout(old_timeout)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   310
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
class HttpRpcServer(AbstractRpcServer):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   313
  """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
   314
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   315
  def _Authenticate(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   316
    """Save the cookie jar after authentication."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   317
    super(HttpRpcServer, self)._Authenticate()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   318
    if self.save_cookies:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   319
      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
   320
      self.cookie_jar.save()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   321
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   322
  def _GetOpener(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   323
    """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
   324
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   325
    Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   326
      A urllib2.OpenerDirector object.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   327
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   328
    opener = urllib2.OpenerDirector()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   329
    opener.add_handler(urllib2.ProxyHandler())
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   330
    opener.add_handler(urllib2.UnknownHandler())
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   331
    opener.add_handler(urllib2.HTTPHandler())
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   332
    opener.add_handler(urllib2.HTTPDefaultErrorHandler())
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   333
    opener.add_handler(urllib2.HTTPSHandler())
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   334
    opener.add_handler(urllib2.HTTPErrorProcessor())
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   335
    if self.save_cookies:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   336
      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
   337
      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
   338
      if os.path.exists(self.cookie_file):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   339
        try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   340
          self.cookie_jar.load()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   341
          self.authenticated = True
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   342
          StatusUpdate("Loaded authentication cookies from %s" %
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   343
                       self.cookie_file)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   344
        except (cookielib.LoadError, IOError):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   345
          # Failed to load cookies - just ignore them.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   346
          pass
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   347
      else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   348
        # 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
   349
        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
   350
        os.close(fd)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   351
      # Always chmod the cookie file
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   352
      os.chmod(self.cookie_file, 0600)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   353
    else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   354
      # 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
   355
      self.cookie_jar = cookielib.CookieJar()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   356
    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
   357
    return opener
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   358
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
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
   361
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
   362
                  dest="assume_yes", default=False,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   363
                  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
   364
# Logging
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   365
group = parser.add_option_group("Logging options")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   366
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
   367
                 dest="verbose", help="Print errors only.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   368
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
   369
                 dest="verbose", default=1,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   370
                 help="Print info level logs (default).")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   371
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
   372
                 dest="verbose", help="Print all logs.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   373
# Review server
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   374
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
   375
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
   376
                 default="codereviews.googleopensourceprograms.com",
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   377
                 metavar="SERVER",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   378
                 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
   379
                       "Defaults to '%default'."))
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   380
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
   381
                 metavar="EMAIL", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   382
                 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
   383
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
   384
                 metavar="HOST", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   385
                 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
   386
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
   387
                 dest="save_cookies", default=True,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   388
                 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
   389
# Issue
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   390
group = parser.add_option_group("Issue options")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   391
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
   392
                 metavar="DESCRIPTION", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   393
                 help="Optional description when creating an issue.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   394
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
   395
                 dest="description_file", metavar="DESCRIPTION_FILE",
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   396
                 default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   397
                 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
   398
                      "the description when creating an issue.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   399
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
   400
                 metavar="REVIEWERS", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   401
                 help="Add reviewers (comma separated email addresses).")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   402
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
   403
                 metavar="CC", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   404
                 help="Add CC (comma separated email addresses).")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   405
# Upload options
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   406
group = parser.add_option_group("Patch options")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   407
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
   408
                 metavar="MESSAGE", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   409
                 help="A message to identify the patch. "
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   410
                      "Will prompt if omitted.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   411
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
   412
                 metavar="ISSUE", default=None,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   413
                 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
   414
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
   415
                 dest="download_base", default=False,
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   416
                 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
   417
                 "(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
   418
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
   419
                 metavar="REV", default=None,
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   420
                 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
   421
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
   422
                 dest="send_mail", default=False,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   423
                 help="Send notification email to reviewers.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   424
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
def GetRpcServer(options):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   427
  """Returns an instance of an AbstractRpcServer.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   428
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   429
  Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   430
    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
   431
  """
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
  rpc_server_class = HttpRpcServer
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   434
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   435
  def GetUserCredentials():
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   436
    """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
   437
    email = options.email
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   438
    if email is None:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   439
      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
   440
      email = raw_input(prompt).strip()
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   441
    password = getpass.getpass("Password for %s: " % email)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   442
    return (email, password)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   443
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   444
  # 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
   445
  host = (options.host or options.server).lower()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   446
  if host == "localhost" or host.startswith("localhost:"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   447
    email = options.email
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   448
    if email is None:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   449
      email = "test@example.com"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   450
      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
   451
    server = rpc_server_class(
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   452
        options.server,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   453
        lambda: (email, "password"),
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   454
        host_override=options.host,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   455
        extra_headers={"Cookie":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   456
                       'dev_appserver_login="%s:False"' % email},
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   457
        save_cookies=options.save_cookies)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   458
    # Don't try to talk to ClientLogin.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   459
    server.authenticated = True
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   460
    return server
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   461
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   462
  return rpc_server_class(options.server, GetUserCredentials,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   463
                          host_override=options.host,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   464
                          save_cookies=options.save_cookies)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   465
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
def EncodeMultipartFormData(fields, files):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   468
  """Encode form fields for multipart/form-data.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   469
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   470
  Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   471
    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
   472
    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
   473
           uploaded as files.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   474
  Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   475
    (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
   476
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   477
  Source:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   478
    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
   479
  """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   480
  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
   481
  CRLF = '\r\n'
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   482
  lines = []
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   483
  for (key, value) in fields:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   484
    lines.append('--' + BOUNDARY)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   485
    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
   486
    lines.append('')
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   487
    lines.append(value)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   488
  for (key, filename, value) in files:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   489
    lines.append('--' + BOUNDARY)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   490
    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
   491
             (key, filename))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   492
    lines.append('Content-Type: %s' % GetContentType(filename))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   493
    lines.append('')
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   494
    lines.append(value)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   495
  lines.append('--' + BOUNDARY + '--')
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   496
  lines.append('')
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   497
  body = CRLF.join(lines)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   498
  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
   499
  return content_type, body
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   500
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
def GetContentType(filename):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   503
  """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
   504
  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
   505
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
# 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
   508
use_shell = sys.platform.startswith("win")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   509
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   510
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
   511
                           universal_newlines=True):
687
4755caf1d7a6 Updated to the latest version of upload.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 478
diff changeset
   512
  """Executes a command and returns the output from stdout and the return code.
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   513
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   514
  Args:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   515
    command: Command to execute.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   516
    print_output: If True, the output is printed to stdout.
687
4755caf1d7a6 Updated to the latest version of upload.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 478
diff changeset
   517
                  If False, both stdout and stderr are ignored.
478
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)
687
4755caf1d7a6 Updated to the latest version of upload.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 478
diff changeset
   524
  p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
4755caf1d7a6 Updated to the latest version of upload.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 478
diff changeset
   525
                       shell=use_shell, 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()
687
4755caf1d7a6 Updated to the latest version of upload.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 478
diff changeset
   538
  errout = p.stderr.read()
4755caf1d7a6 Updated to the latest version of upload.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 478
diff changeset
   539
  if print_output and errout:
4755caf1d7a6 Updated to the latest version of upload.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 478
diff changeset
   540
    print >>sys.stderr, errout
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   541
  p.stdout.close()
687
4755caf1d7a6 Updated to the latest version of upload.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 478
diff changeset
   542
  p.stderr.close()
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   543
  return output, p.returncode
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   544
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   545
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   546
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
   547
             print_output=False):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   548
  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
   549
                                         universal_newlines)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   550
  if retcode:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   551
    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
   552
  if not silent_ok and not data:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   553
    ErrorExit("No output from %s" % command)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   554
  return data
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   555
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   556
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   557
class VersionControlSystem(object):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   558
  """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
   559
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   560
  def __init__(self, options):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   561
    """Constructor.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   562
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   563
    Args:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   564
      options: Command line options.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   565
    """
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   566
    self.options = options
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   567
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   568
  def GenerateDiff(self, args):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   569
    """Return the current diff as a string.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   570
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   571
    Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   572
      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
   573
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   574
    raise NotImplementedError(
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   575
        "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
   576
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   577
  def GetUnknownFiles(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   578
    """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
   579
    raise NotImplementedError(
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   580
        "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
   581
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   582
  def CheckForUnknownFiles(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   583
    """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
   584
    unknown_files = self.GetUnknownFiles()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   585
    if unknown_files:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   586
      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
   587
      for line in unknown_files:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   588
        print line
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   589
      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
   590
      answer = raw_input(prompt).strip()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   591
      if answer != "y":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   592
        ErrorExit("User aborted")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   593
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   594
  def GetBaseFile(self, filename):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   595
    """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
   596
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   597
    Returns:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   598
      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
   599
        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
   600
        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
   601
          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
   602
          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
   603
        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
   604
        status: The status of the file.
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   605
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   606
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   607
    raise NotImplementedError(
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   608
        "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
   609
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   610
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   611
  def GetBaseFiles(self, diff):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   612
    """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
   613
    
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   614
    Returns:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   615
      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
   616
      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
   617
      "Property changes on:".
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   618
    """
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   619
    files = {}
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   620
    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
   621
      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
   622
        unused, filename = line.split(':', 1)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   623
        # 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
   624
        # instead of '/'.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   625
        filename = filename.strip().replace('\\', '/')
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   626
        files[filename] = self.GetBaseFile(filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   627
    return files
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   628
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 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
   631
                      files):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   632
    """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
   633
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   634
    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
   635
      """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
   636
      file_too_large = False
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   637
      if is_base:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   638
        type = "base"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   639
      else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   640
        type = "current"
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   641
      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
   642
        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
   643
               (type, filename))
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   644
        file_too_large = True
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   645
        content = ""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   646
      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
   647
      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
   648
        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
   649
      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
   650
      form_fields = [("filename", filename),
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   651
                     ("status", status),
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   652
                     ("checksum", checksum),
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   653
                     ("is_binary", str(is_binary)),
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   654
                     ("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
   655
                    ]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   656
      if file_too_large:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   657
        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
   658
      if options.email:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   659
        form_fields.append(("user", options.email))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   660
      ctype, body = EncodeMultipartFormData(form_fields,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   661
                                            [("data", filename, content)])
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   662
      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
   663
                                      content_type=ctype)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   664
      if not response_body.startswith("OK"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   665
        StatusUpdate("  --> %s" % response_body)
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   666
        sys.exit(1)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   667
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   668
    patches = dict()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   669
    [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
   670
    for filename in patches.keys():
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   671
      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
   672
      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
   673
      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
   674
        base_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   675
        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
   676
      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
   677
      if base_content != None:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   678
        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
   679
      if new_content != None:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   680
        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
   681
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   682
  def IsImage(self, filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   683
    """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
   684
    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
   685
    if not mimetype:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   686
      return False
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   687
    return mimetype.startswith("image/")
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   688
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   689
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   690
class SubversionVCS(VersionControlSystem):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   691
  """Implementation of the VersionControlSystem interface for Subversion."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   692
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   693
  def __init__(self, options):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   694
    super(SubversionVCS, self).__init__(options)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   695
    if self.options.revision:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   696
      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
   697
      if not match:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   698
        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
   699
      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
   700
      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
   701
    else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   702
      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
   703
    # 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
   704
    # 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
   705
    self.svnls_cache = {}
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   706
    # 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
   707
    # 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
   708
    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
   709
    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
   710
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   711
  def GuessBase(self, required):
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   712
    """Wrapper for _GuessBase."""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   713
    return self.svn_base
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   714
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   715
  def _GuessBase(self, required):
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   716
    """Returns the SVN base URL.
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
    Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   719
      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
   720
        returned.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   721
    """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   722
    info = RunShell(["svn", "info"])
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   723
    for line in info.splitlines():
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   724
      words = line.split()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   725
      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
   726
        url = words[1]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   727
        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
   728
        username, netloc = urllib.splituser(netloc)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   729
        if username:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   730
          logging.info("Removed username from base URL")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   731
        if netloc.endswith("svn.python.org"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   732
          if netloc == "svn.python.org":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   733
            if path.startswith("/projects/"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   734
              path = path[9:]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   735
          elif netloc != "pythondev@svn.python.org":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   736
            ErrorExit("Unrecognized Python URL: %s" % url)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   737
          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
   738
          logging.info("Guessed Python base = %s", base)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   739
        elif netloc.endswith("svn.collab.net"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   740
          if path.startswith("/repos/"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   741
            path = path[6:]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   742
          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
   743
          logging.info("Guessed CollabNet base = %s", base)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   744
        elif netloc.endswith(".googlecode.com"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   745
          path = path + "/"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   746
          base = urlparse.urlunparse(("http", netloc, path, params,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   747
                                      query, fragment))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   748
          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
   749
        else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   750
          path = path + "/"
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   751
          base = urlparse.urlunparse((scheme, netloc, path, params,
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   752
                                      query, fragment))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   753
          logging.info("Guessed base = %s", base)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   754
        return base
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   755
    if required:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   756
      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
   757
    return None
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   758
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   759
  def GenerateDiff(self, args):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   760
    cmd = ["svn", "diff"]
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   761
    if self.options.revision:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   762
      cmd += ["-r", self.options.revision]
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   763
    cmd.extend(args)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   764
    data = RunShell(cmd)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   765
    count = 0
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   766
    for line in data.splitlines():
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   767
      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
   768
        count += 1
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   769
        logging.info(line)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   770
    if not count:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   771
      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
   772
    return data
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   773
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   774
  def _CollapseKeywords(self, content, keyword_str):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   775
    """Collapses SVN keywords."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   776
    # 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
   777
    # 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
   778
    # 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
   779
    # 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
   780
    # Mapping of keywords to known aliases
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   781
    svn_keywords = {
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   782
      # Standard keywords
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   783
      'Date':                ['Date', 'LastChangedDate'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   784
      'Revision':            ['Revision', 'LastChangedRevision', 'Rev'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   785
      'Author':              ['Author', 'LastChangedBy'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   786
      'HeadURL':             ['HeadURL', 'URL'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   787
      'Id':                  ['Id'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   788
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   789
      # Aliases
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   790
      'LastChangedDate':     ['LastChangedDate', 'Date'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   791
      'LastChangedRevision': ['LastChangedRevision', 'Rev', 'Revision'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   792
      'LastChangedBy':       ['LastChangedBy', 'Author'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   793
      'URL':                 ['URL', 'HeadURL'],
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   794
    }
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   795
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   796
    def repl(m):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   797
       if m.group(2):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   798
         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
   799
       return "$%s$" % m.group(1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   800
    keywords = [keyword
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   801
                for name in keyword_str.split(" ")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   802
                for keyword in svn_keywords.get(name, [])]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   803
    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
   804
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   805
  def GetUnknownFiles(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   806
    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
   807
    unknown_files = []
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   808
    for line in status.split("\n"):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   809
      if line and line[0] == "?":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   810
        unknown_files.append(line)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   811
    return unknown_files
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   812
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   813
  def ReadFile(self, filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   814
    """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
   815
    file = open(filename, 'rb')
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   816
    result = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   817
    try:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   818
      result = file.read()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   819
    finally:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   820
      file.close()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   821
    return result
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   822
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   823
  def GetStatus(self, filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   824
    """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
   825
    if not self.options.revision:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   826
      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
   827
      if not status:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   828
        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
   829
      status_lines = status.splitlines()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   830
      # 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
   831
      # "\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
   832
      # 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
   833
      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
   834
          not status_lines[0] and
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   835
          status_lines[1].startswith("--- Changelist")):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   836
        status = status_lines[2]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   837
      else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   838
        status = status_lines[0]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   839
    # 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
   840
    # 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
   841
    # 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
   842
    else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   843
      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
   844
      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
   845
        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
   846
        out, returncode = RunShellWithReturnCode(cmd)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   847
        if returncode:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   848
          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
   849
        old_files = out.splitlines()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   850
        args = ["svn", "list"]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   851
        if self.rev_end:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   852
          args += ["-r", self.rev_end]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   853
        cmd = args + [dirname or "."]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   854
        out, returncode = RunShellWithReturnCode(cmd)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   855
        if returncode:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   856
          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
   857
        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
   858
      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
   859
      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
   860
        status = "D   "
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   861
      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
   862
        status = "M   "
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   863
      else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   864
        status = "A   "
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   865
    return status
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   866
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   867
  def GetBaseFile(self, filename):
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   868
    status = self.GetStatus(filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   869
    base_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   870
    new_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   871
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   872
    # 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
   873
    # "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
   874
    # 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
   875
    # edited.
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   876
    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
   877
      # 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
   878
      # 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
   879
      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
   880
                          silent_ok=True)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   881
      base_content = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   882
      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
   883
      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
   884
        new_content = self.ReadFile(filename)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   885
    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
   886
          (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
   887
          (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
   888
      args = []
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   889
      if self.options.revision:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   890
        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
   891
      else:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   892
        # 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
   893
        url = filename
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   894
        args += ["-r", "BASE"]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   895
      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
   896
      mimetype, returncode = RunShellWithReturnCode(cmd)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   897
      if returncode:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   898
        # 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
   899
        # 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
   900
        mimetype = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   901
      get_base = False
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   902
      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
   903
      if status[0] == " ":
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   904
        # 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
   905
        base_content = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   906
      elif is_binary:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   907
        if self.IsImage(filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   908
          get_base = True
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   909
          if status[0] == "M":
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   910
            if not self.rev_end:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   911
              new_content = self.ReadFile(filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   912
            else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   913
              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
   914
              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
   915
                                     universal_newlines=True)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   916
        else:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   917
          base_content = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   918
      else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   919
        get_base = True
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   920
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   921
      if get_base:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   922
        if is_binary:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   923
          universal_newlines = False
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   924
        else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   925
          universal_newlines = True
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   926
        if self.rev_start:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   927
          # "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
   928
          # 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
   929
          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
   930
          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
   931
                                  universal_newlines=universal_newlines)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   932
        else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   933
          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
   934
                                  universal_newlines=universal_newlines)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   935
        if not is_binary:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   936
          args = []
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   937
          if self.rev_start:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   938
            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
   939
          else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   940
            url = filename
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   941
            args += ["-r", "BASE"]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   942
          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
   943
          keywords, returncode = RunShellWithReturnCode(cmd)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   944
          if keywords and not returncode:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   945
            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
   946
    else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   947
      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
   948
      sys.exit(1)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   949
    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
   950
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   951
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   952
class GitVCS(VersionControlSystem):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   953
  """Implementation of the VersionControlSystem interface for Git."""
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   954
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   955
  def __init__(self, options):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   956
    super(GitVCS, self).__init__(options)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   957
    # Map of filename -> hash of base file.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   958
    self.base_hashes = {}
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   959
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   960
  def GenerateDiff(self, extra_args):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   961
    # 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
   962
    # 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
   963
    # 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
   964
    if self.options.revision:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   965
      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
   966
    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
   967
    svndiff = []
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   968
    filecount = 0
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   969
    filename = None
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   970
    for line in gitdiff.splitlines():
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   971
      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
   972
      if match:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   973
        filecount += 1
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   974
        filename = match.group(1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   975
        svndiff.append("Index: %s\n" % filename)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   976
      else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   977
        # 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
   978
        #   index 82c0d44..b2cee3f 100755
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   979
        # 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
   980
        match = re.match(r"index (\w+)\.\.", line)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   981
        if match:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   982
          self.base_hashes[filename] = match.group(1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   983
      svndiff.append(line + "\n")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   984
    if not filecount:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   985
      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
   986
    return "".join(svndiff)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   987
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   988
  def GetUnknownFiles(self):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   989
    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
   990
                      silent_ok=True)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   991
    return status.splitlines()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   992
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   993
  def GetBaseFile(self, filename):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   994
    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
   995
    base_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   996
    new_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
   997
    is_binary = False
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   998
    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
   999
      status = "A"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1000
      base_content = ""
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1001
    else:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1002
      status = "M"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1003
      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
  1004
    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
  1005
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1006
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1007
class MercurialVCS(VersionControlSystem):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1008
  """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
  1009
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1010
  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
  1011
    super(MercurialVCS, self).__init__(options)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1012
    # 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
  1013
    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
  1014
    # Compute the subdir
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1015
    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
  1016
    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
  1017
    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
  1018
    if self.options.revision:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1019
      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
  1020
    else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1021
      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
  1022
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1023
  def _GetRelPath(self, filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1024
    """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
  1025
    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
  1026
    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
  1027
    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
  1028
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1029
  def GenerateDiff(self, extra_args):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1030
    # 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
  1031
    extra_args = extra_args or ["."]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1032
    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
  1033
    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
  1034
    svndiff = []
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1035
    filecount = 0
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1036
    for line in data.splitlines():
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1037
      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
  1038
      if m:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1039
        # 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
  1040
        # 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
  1041
        # 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
  1042
        # 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
  1043
        # the second filename.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1044
        filename = m.group(2)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1045
        svndiff.append("Index: %s" % filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1046
        svndiff.append("=" * 67)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1047
        filecount += 1
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1048
        logging.info(line)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1049
      else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1050
        svndiff.append(line)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1051
    if not filecount:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1052
      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
  1053
    return "\n".join(svndiff) + "\n"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1054
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1055
  def GetUnknownFiles(self):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1056
    """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
  1057
    args = []
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1058
    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
  1059
        silent_ok=True)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1060
    unknown_files = []
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1061
    for line in status.splitlines():
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1062
      st, fn = line.split(" ", 1)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1063
      if st == "?":
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1064
        unknown_files.append(fn)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1065
    return unknown_files
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1066
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1067
  def GetBaseFile(self, filename):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1068
    # "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
  1069
    # 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
  1070
    # to the repo root.
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1071
    base_content = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1072
    new_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1073
    is_binary = False
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1074
    oldrelpath = relpath = self._GetRelPath(filename)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1075
    # "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
  1076
    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
  1077
    out = out.splitlines()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1078
    # 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
  1079
    # the working copy
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1080
    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
  1081
      out = out[1:]
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1082
    if len(out) > 1:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1083
      # 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
  1084
      # retrieve base contents
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1085
      oldrelpath = out[1].strip()
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1086
      status = "M"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1087
    else:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1088
      status, _ = out[0].split(' ', 1)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1089
    if status != "A":
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1090
      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
  1091
        silent_ok=True)
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1092
      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
  1093
    if status != "R":
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1094
      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
  1095
      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
  1096
    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
  1097
      # Fetch again without converting newlines
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1098
      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
  1099
        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
  1100
    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
  1101
      new_content = None
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1102
    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
  1103
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1104
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1105
# 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
  1106
def SplitPatch(data):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1107
  """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
  1108
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1109
  Args:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1110
    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
  1111
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1112
  Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1113
    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
  1114
      pertaining to filename.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1115
  """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1116
  patches = []
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1117
  filename = None
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1118
  diff = []
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1119
  for line in data.splitlines(True):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1120
    new_filename = None
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1121
    if line.startswith('Index:'):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1122
      unused, new_filename = line.split(':', 1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1123
      new_filename = new_filename.strip()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1124
    elif line.startswith('Property changes on:'):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1125
      unused, temp_filename = line.split(':', 1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1126
      # 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
  1127
      # 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
  1128
      # otherwise the file shows up twice.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1129
      temp_filename = temp_filename.strip().replace('\\', '/')
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1130
      if temp_filename != filename:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1131
        # 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
  1132
        new_filename = temp_filename
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1133
    if new_filename:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1134
      if filename and diff:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1135
        patches.append((filename, ''.join(diff)))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1136
      filename = new_filename
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1137
      diff = [line]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1138
      continue
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1139
    if diff is not None:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1140
      diff.append(line)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1141
  if filename and diff:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1142
    patches.append((filename, ''.join(diff)))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1143
  return patches
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
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1146
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
  1147
  """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
  1148
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1149
  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
  1150
  """
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1151
  patches = SplitPatch(data)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1152
  rv = []
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1153
  for patch in patches:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1154
    if len(patch[1]) > MAX_UPLOAD_SIZE:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1155
      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
  1156
             " because the file is too large.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1157
      continue
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1158
    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
  1159
    if not options.download_base:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1160
      form_fields.append(("content_upload", "1"))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1161
    files = [("data", "data.diff", patch[1])]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1162
    ctype, body = EncodeMultipartFormData(form_fields, files)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1163
    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
  1164
    print "Uploading patch for " + patch[0]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1165
    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
  1166
    lines = response_body.splitlines()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1167
    if not lines or lines[0] != "OK":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1168
      StatusUpdate("  --> %s" % response_body)
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1169
      sys.exit(1)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1170
    rv.append([lines[1], patch[0]])
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1171
  return rv
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
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1174
def GuessVCS(options):
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1175
  """Helper to guess the version control system.
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
  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
  1178
  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
  1179
  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
  1180
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1181
  Returns:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1182
    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
  1183
  """
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1184
  # 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
  1185
  # 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
  1186
  # 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
  1187
  try:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1188
    out, returncode = RunShellWithReturnCode(["hg", "root"])
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1189
    if returncode == 0:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1190
      return MercurialVCS(options, out.strip())
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1191
  except OSError, (errno, message):
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1192
    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
  1193
      raise
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1194
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1195
  # 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
  1196
  if os.path.isdir('.svn'):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1197
    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
  1198
    return SubversionVCS(options)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1199
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1200
  # 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
  1201
  # 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
  1202
  try:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1203
    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
  1204
                                              "--is-inside-work-tree"])
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1205
    if returncode == 0:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1206
      return GitVCS(options)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1207
  except OSError, (errno, message):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1208
    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
  1209
      raise
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
  ErrorExit(("Could not guess version control system. "
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1212
             "Are you in a working copy directory?"))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1213
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1214
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1215
def RealMain(argv, data=None):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1216
  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
  1217
                              "%(lineno)s %(message)s "))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1218
  os.environ['LC_ALL'] = 'C'
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1219
  options, args = parser.parse_args(argv[1:])
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1220
  global verbosity
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1221
  verbosity = options.verbose
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1222
  if verbosity >= 3:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1223
    logging.getLogger().setLevel(logging.DEBUG)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1224
  elif verbosity >= 2:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1225
    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
  1226
  vcs = GuessVCS(options)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1227
  if isinstance(vcs, SubversionVCS):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1228
    # base field is only allowed for Subversion.
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1229
    # 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
  1230
    base = vcs.GuessBase(options.download_base)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1231
  else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1232
    base = None
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1233
  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
  1234
    options.download_base = True
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1235
    logging.info("Enabled upload of base file")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1236
  if not options.assume_yes:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1237
    vcs.CheckForUnknownFiles()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1238
  if data is None:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1239
    data = vcs.GenerateDiff(args)
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1240
  files = vcs.GetBaseFiles(data)
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1241
  if verbosity >= 1:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1242
    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
  1243
  if options.issue:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1244
    prompt = "Message describing this patch set: "
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1245
  else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1246
    prompt = "New issue subject: "
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1247
  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
  1248
  if not message:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1249
    ErrorExit("A non-empty message is required")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1250
  rpc_server = GetRpcServer(options)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1251
  form_fields = [("subject", message)]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1252
  if base:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1253
    form_fields.append(("base", base))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1254
  if options.issue:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1255
    form_fields.append(("issue", str(options.issue)))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1256
  if options.email:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1257
    form_fields.append(("user", options.email))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1258
  if options.reviewers:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1259
    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
  1260
      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
  1261
        ErrorExit("Invalid email address: %s" % reviewer)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1262
    form_fields.append(("reviewers", options.reviewers))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1263
  if options.cc:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1264
    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
  1265
      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
  1266
        ErrorExit("Invalid email address: %s" % cc)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1267
    form_fields.append(("cc", options.cc))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1268
  description = options.description
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1269
  if options.description_file:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1270
    if options.description:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1271
      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
  1272
    file = open(options.description_file, 'r')
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1273
    description = file.read()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1274
    file.close()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1275
  if description:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1276
    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
  1277
  # 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
  1278
  # 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
  1279
  base_hashes = ""
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1280
  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
  1281
    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
  1282
      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
  1283
      if base_hashes:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1284
        base_hashes += "|"
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1285
      base_hashes += checksum + ":" + file
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1286
  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
  1287
  # 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
  1288
  # 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
  1289
  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
  1290
    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
  1291
  if not options.download_base:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1292
    form_fields.append(("content_upload", "1"))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1293
  if len(data) > MAX_UPLOAD_SIZE:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1294
    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
  1295
    uploaded_diff_file = []
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1296
    form_fields.append(("separate_patches", "1"))
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1297
  else:
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1298
    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
  1299
  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
  1300
  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
  1301
  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
  1302
    lines = response_body.splitlines()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1303
    if len(lines) >= 2:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1304
      msg = lines[0]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1305
      patchset = lines[1].strip()
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1306
      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
  1307
    else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1308
      msg = response_body
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1309
  else:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1310
    msg = response_body
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1311
  StatusUpdate(msg)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1312
  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
  1313
  not response_body.startswith("Issue updated."):
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1314
    sys.exit(0)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1315
  issue = msg[msg.rfind("/")+1:]
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1316
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1317
  if not uploaded_diff_file:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1318
    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
  1319
    if not options.download_base:
147
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1320
      patches = result
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1321
478
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1322
  if not options.download_base:
613951c35706 upload.py (customized for use with codereviews.googleopensourceprograms.com)
Todd Larsen <tlarsen@google.com>
parents: 148
diff changeset
  1323
    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
  1324
    if options.send_mail:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1325
      rpc_server.Send("/" + issue + "/mail", payload="")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1326
  return issue
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1327
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1328
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1329
def main():
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1330
  try:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1331
    RealMain(sys.argv)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1332
  except KeyboardInterrupt:
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1333
    print
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1334
    StatusUpdate("Interrupted.")
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1335
    sys.exit(1)
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1336
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1337
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1338
if __name__ == "__main__":
cf9f7d81edec Pristine initial commit of upload.py script from
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1339
  main()