You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2230 lines
81 KiB

  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2007 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Tool for uploading diffs from a version control system to the codereview app.
  17. Usage summary: upload.py [options] [-- diff_options] [path...]
  18. Diff options are passed to the diff command of the underlying system.
  19. Supported version control systems:
  20. Git
  21. Mercurial
  22. Subversion
  23. Perforce
  24. CVS
  25. It is important for Git/Mercurial users to specify a tree/node/branch to diff
  26. against by using the '--rev' option.
  27. """
  28. # This code is derived from appcfg.py in the App Engine SDK (open source),
  29. # and from ASPN recipe #146306.
  30. import ConfigParser
  31. import cookielib
  32. import errno
  33. import fnmatch
  34. import getpass
  35. import logging
  36. import marshal
  37. import mimetypes
  38. import optparse
  39. import os
  40. import re
  41. import socket
  42. import subprocess
  43. import sys
  44. import urllib
  45. import urllib2
  46. import urlparse
  47. # The md5 module was deprecated in Python 2.5.
  48. try:
  49. from hashlib import md5
  50. except ImportError:
  51. from md5 import md5
  52. try:
  53. import readline
  54. except ImportError:
  55. pass
  56. try:
  57. import keyring
  58. except ImportError:
  59. keyring = None
  60. # The logging verbosity:
  61. # 0: Errors only.
  62. # 1: Status messages.
  63. # 2: Info logs.
  64. # 3: Debug logs.
  65. verbosity = 1
  66. # The account type used for authentication.
  67. # This line could be changed by the review server (see handler for
  68. # upload.py).
  69. AUTH_ACCOUNT_TYPE = "GOOGLE"
  70. # URL of the default review server. As for AUTH_ACCOUNT_TYPE, this line could be
  71. # changed by the review server (see handler for upload.py).
  72. DEFAULT_REVIEW_SERVER = "codereview.appspot.com"
  73. # Max size of patch or base file.
  74. MAX_UPLOAD_SIZE = 900 * 1024
  75. # Constants for version control names. Used by GuessVCSName.
  76. VCS_GIT = "Git"
  77. VCS_MERCURIAL = "Mercurial"
  78. VCS_SUBVERSION = "Subversion"
  79. VCS_PERFORCE = "Perforce"
  80. VCS_CVS = "CVS"
  81. VCS_UNKNOWN = "Unknown"
  82. # whitelist for non-binary filetypes which do not start with "text/"
  83. # .mm (Objective-C) shows up as application/x-freemind on my Linux box.
  84. TEXT_MIMETYPES = ['application/javascript', 'application/json',
  85. 'application/x-javascript', 'application/xml',
  86. 'application/x-freemind', 'application/x-sh']
  87. VCS_ABBREVIATIONS = {
  88. VCS_MERCURIAL.lower(): VCS_MERCURIAL,
  89. "hg": VCS_MERCURIAL,
  90. VCS_SUBVERSION.lower(): VCS_SUBVERSION,
  91. "svn": VCS_SUBVERSION,
  92. VCS_PERFORCE.lower(): VCS_PERFORCE,
  93. "p4": VCS_PERFORCE,
  94. VCS_GIT.lower(): VCS_GIT,
  95. VCS_CVS.lower(): VCS_CVS,
  96. }
  97. # The result of parsing Subversion's [auto-props] setting.
  98. svn_auto_props_map = None
  99. def GetEmail(prompt):
  100. """Prompts the user for their email address and returns it.
  101. The last used email address is saved to a file and offered up as a suggestion
  102. to the user. If the user presses enter without typing in anything the last
  103. used email address is used. If the user enters a new address, it is saved
  104. for next time we prompt.
  105. """
  106. last_email_file_name = os.path.expanduser("~/.last_codereview_email_address")
  107. last_email = ""
  108. if os.path.exists(last_email_file_name):
  109. try:
  110. last_email_file = open(last_email_file_name, "r")
  111. last_email = last_email_file.readline().strip("\n")
  112. last_email_file.close()
  113. prompt += " [%s]" % last_email
  114. except IOError, e:
  115. pass
  116. email = raw_input(prompt + ": ").strip()
  117. if email:
  118. try:
  119. last_email_file = open(last_email_file_name, "w")
  120. last_email_file.write(email)
  121. last_email_file.close()
  122. except IOError, e:
  123. pass
  124. else:
  125. email = last_email
  126. return email
  127. def StatusUpdate(msg):
  128. """Print a status message to stdout.
  129. If 'verbosity' is greater than 0, print the message.
  130. Args:
  131. msg: The string to print.
  132. """
  133. if verbosity > 0:
  134. print msg
  135. def ErrorExit(msg):
  136. """Print an error message to stderr and exit."""
  137. print >>sys.stderr, msg
  138. sys.exit(1)
  139. class ClientLoginError(urllib2.HTTPError):
  140. """Raised to indicate there was an error authenticating with ClientLogin."""
  141. def __init__(self, url, code, msg, headers, args):
  142. urllib2.HTTPError.__init__(self, url, code, msg, headers, None)
  143. self.args = args
  144. self.reason = args["Error"]
  145. class AbstractRpcServer(object):
  146. """Provides a common interface for a simple RPC server."""
  147. def __init__(self, host, auth_function, host_override=None, extra_headers={},
  148. save_cookies=False, account_type=AUTH_ACCOUNT_TYPE):
  149. """Creates a new HttpRpcServer.
  150. Args:
  151. host: The host to send requests to.
  152. auth_function: A function that takes no arguments and returns an
  153. (email, password) tuple when called. Will be called if authentication
  154. is required.
  155. host_override: The host header to send to the server (defaults to host).
  156. extra_headers: A dict of extra headers to append to every request.
  157. save_cookies: If True, save the authentication cookies to local disk.
  158. If False, use an in-memory cookiejar instead. Subclasses must
  159. implement this functionality. Defaults to False.
  160. account_type: Account type used for authentication. Defaults to
  161. AUTH_ACCOUNT_TYPE.
  162. """
  163. self.host = host
  164. if (not self.host.startswith("http://") and
  165. not self.host.startswith("https://")):
  166. self.host = "http://" + self.host
  167. self.host_override = host_override
  168. self.auth_function = auth_function
  169. self.authenticated = False
  170. self.extra_headers = extra_headers
  171. self.save_cookies = save_cookies
  172. self.account_type = account_type
  173. self.opener = self._GetOpener()
  174. if self.host_override:
  175. logging.info("Server: %s; Host: %s", self.host, self.host_override)
  176. else:
  177. logging.info("Server: %s", self.host)
  178. def _GetOpener(self):
  179. """Returns an OpenerDirector for making HTTP requests.
  180. Returns:
  181. A urllib2.OpenerDirector object.
  182. """
  183. raise NotImplementedError()
  184. def _CreateRequest(self, url, data=None):
  185. """Creates a new urllib request."""
  186. logging.debug("Creating request for: '%s' with payload:\n%s", url, data)
  187. req = urllib2.Request(url, data=data)
  188. if self.host_override:
  189. req.add_header("Host", self.host_override)
  190. for key, value in self.extra_headers.iteritems():
  191. req.add_header(key, value)
  192. return req
  193. def _GetAuthToken(self, email, password):
  194. """Uses ClientLogin to authenticate the user, returning an auth token.
  195. Args:
  196. email: The user's email address
  197. password: The user's password
  198. Raises:
  199. ClientLoginError: If there was an error authenticating with ClientLogin.
  200. HTTPError: If there was some other form of HTTP error.
  201. Returns:
  202. The authentication token returned by ClientLogin.
  203. """
  204. account_type = self.account_type
  205. if self.host.endswith(".google.com"):
  206. # Needed for use inside Google.
  207. account_type = "HOSTED"
  208. req = self._CreateRequest(
  209. url="https://www.google.com/accounts/ClientLogin",
  210. data=urllib.urlencode({
  211. "Email": email,
  212. "Passwd": password,
  213. "service": "ah",
  214. "source": "rietveld-codereview-upload",
  215. "accountType": account_type,
  216. }),
  217. )
  218. try:
  219. response = self.opener.open(req)
  220. response_body = response.read()
  221. response_dict = dict(x.split("=")
  222. for x in response_body.split("\n") if x)
  223. return response_dict["Auth"]
  224. except urllib2.HTTPError, e:
  225. if e.code == 403:
  226. body = e.read()
  227. response_dict = dict(x.split("=", 1) for x in body.split("\n") if x)
  228. raise ClientLoginError(req.get_full_url(), e.code, e.msg,
  229. e.headers, response_dict)
  230. else:
  231. raise
  232. def _GetAuthCookie(self, auth_token):
  233. """Fetches authentication cookies for an authentication token.
  234. Args:
  235. auth_token: The authentication token returned by ClientLogin.
  236. Raises:
  237. HTTPError: If there was an error fetching the authentication cookies.
  238. """
  239. # This is a dummy value to allow us to identify when we're successful.
  240. continue_location = "http://localhost/"
  241. args = {"continue": continue_location, "auth": auth_token}
  242. req = self._CreateRequest("%s/_ah/login?%s" %
  243. (self.host, urllib.urlencode(args)))
  244. try:
  245. response = self.opener.open(req)
  246. except urllib2.HTTPError, e:
  247. response = e
  248. if (response.code != 302 or
  249. response.info()["location"] != continue_location):
  250. raise urllib2.HTTPError(req.get_full_url(), response.code, response.msg,
  251. response.headers, response.fp)
  252. self.authenticated = True
  253. def _Authenticate(self):
  254. """Authenticates the user.
  255. The authentication process works as follows:
  256. 1) We get a username and password from the user
  257. 2) We use ClientLogin to obtain an AUTH token for the user
  258. (see http://code.google.com/apis/accounts/AuthForInstalledApps.html).
  259. 3) We pass the auth token to /_ah/login on the server to obtain an
  260. authentication cookie. If login was successful, it tries to redirect
  261. us to the URL we provided.
  262. If we attempt to access the upload API without first obtaining an
  263. authentication cookie, it returns a 401 response (or a 302) and
  264. directs us to authenticate ourselves with ClientLogin.
  265. """
  266. for i in range(3):
  267. credentials = self.auth_function()
  268. try:
  269. auth_token = self._GetAuthToken(credentials[0], credentials[1])
  270. except ClientLoginError, e:
  271. if e.reason == "BadAuthentication":
  272. print >>sys.stderr, "Invalid username or password."
  273. continue
  274. if e.reason == "CaptchaRequired":
  275. print >>sys.stderr, (
  276. "Please go to\n"
  277. "https://www.google.com/accounts/DisplayUnlockCaptcha\n"
  278. "and verify you are a human. Then try again.\n"
  279. "If you are using a Google Apps account the URL is:\n"
  280. "https://www.google.com/a/yourdomain.com/UnlockCaptcha")
  281. break
  282. if e.reason == "NotVerified":
  283. print >>sys.stderr, "Account not verified."
  284. break
  285. if e.reason == "TermsNotAgreed":
  286. print >>sys.stderr, "User has not agreed to TOS."
  287. break
  288. if e.reason == "AccountDeleted":
  289. print >>sys.stderr, "The user account has been deleted."
  290. break
  291. if e.reason == "AccountDisabled":
  292. print >>sys.stderr, "The user account has been disabled."
  293. break
  294. if e.reason == "ServiceDisabled":
  295. print >>sys.stderr, ("The user's access to the service has been "
  296. "disabled.")
  297. break
  298. if e.reason == "ServiceUnavailable":
  299. print >>sys.stderr, "The service is not available; try again later."
  300. break
  301. raise
  302. self._GetAuthCookie(auth_token)
  303. return
  304. def Send(self, request_path, payload=None,
  305. content_type="application/octet-stream",
  306. timeout=None,
  307. extra_headers=None,
  308. **kwargs):
  309. """Sends an RPC and returns the response.
  310. Args:
  311. request_path: The path to send the request to, eg /api/appversion/create.
  312. payload: The body of the request, or None to send an empty request.
  313. content_type: The Content-Type header to use.
  314. timeout: timeout in seconds; default None i.e. no timeout.
  315. (Note: for large requests on OS X, the timeout doesn't work right.)
  316. extra_headers: Dict containing additional HTTP headers that should be
  317. included in the request (string header names mapped to their values),
  318. or None to not include any additional headers.
  319. kwargs: Any keyword arguments are converted into query string parameters.
  320. Returns:
  321. The response body, as a string.
  322. """
  323. # TODO: Don't require authentication. Let the server say
  324. # whether it is necessary.
  325. if not self.authenticated:
  326. self._Authenticate()
  327. old_timeout = socket.getdefaulttimeout()
  328. socket.setdefaulttimeout(timeout)
  329. try:
  330. tries = 0
  331. while True:
  332. tries += 1
  333. args = dict(kwargs)
  334. url = "%s%s" % (self.host, request_path)
  335. if args:
  336. url += "?" + urllib.urlencode(args)
  337. req = self._CreateRequest(url=url, data=payload)
  338. req.add_header("Content-Type", content_type)
  339. if extra_headers:
  340. for header, value in extra_headers.items():
  341. req.add_header(header, value)
  342. try:
  343. f = self.opener.open(req)
  344. response = f.read()
  345. f.close()
  346. return response
  347. except urllib2.HTTPError, e:
  348. if tries > 3:
  349. raise
  350. elif e.code == 401 or e.code == 302:
  351. self._Authenticate()
  352. ## elif e.code >= 500 and e.code < 600:
  353. ## # Server Error - try again.
  354. ## continue
  355. elif e.code == 301:
  356. # Handle permanent redirect manually.
  357. url = e.info()["location"]
  358. url_loc = urlparse.urlparse(url)
  359. self.host = '%s://%s' % (url_loc[0], url_loc[1])
  360. else:
  361. raise
  362. finally:
  363. socket.setdefaulttimeout(old_timeout)
  364. class HttpRpcServer(AbstractRpcServer):
  365. """Provides a simplified RPC-style interface for HTTP requests."""
  366. def _Authenticate(self):
  367. """Save the cookie jar after authentication."""
  368. super(HttpRpcServer, self)._Authenticate()
  369. if self.save_cookies:
  370. StatusUpdate("Saving authentication cookies to %s" % self.cookie_file)
  371. self.cookie_jar.save()
  372. def _GetOpener(self):
  373. """Returns an OpenerDirector that supports cookies and ignores redirects.
  374. Returns:
  375. A urllib2.OpenerDirector object.
  376. """
  377. opener = urllib2.OpenerDirector()
  378. opener.add_handler(urllib2.ProxyHandler())
  379. opener.add_handler(urllib2.UnknownHandler())
  380. opener.add_handler(urllib2.HTTPHandler())
  381. opener.add_handler(urllib2.HTTPDefaultErrorHandler())
  382. opener.add_handler(urllib2.HTTPSHandler())
  383. opener.add_handler(urllib2.HTTPErrorProcessor())
  384. if self.save_cookies:
  385. self.cookie_file = os.path.expanduser("~/.codereview_upload_cookies")
  386. self.cookie_jar = cookielib.MozillaCookieJar(self.cookie_file)
  387. if os.path.exists(self.cookie_file):
  388. try:
  389. self.cookie_jar.load()
  390. self.authenticated = True
  391. StatusUpdate("Loaded authentication cookies from %s" %
  392. self.cookie_file)
  393. except (cookielib.LoadError, IOError):
  394. # Failed to load cookies - just ignore them.
  395. pass
  396. else:
  397. # Create an empty cookie file with mode 600
  398. fd = os.open(self.cookie_file, os.O_CREAT, 0600)
  399. os.close(fd)
  400. # Always chmod the cookie file
  401. os.chmod(self.cookie_file, 0600)
  402. else:
  403. # Don't save cookies across runs of update.py.
  404. self.cookie_jar = cookielib.CookieJar()
  405. opener.add_handler(urllib2.HTTPCookieProcessor(self.cookie_jar))
  406. return opener
  407. parser = optparse.OptionParser(
  408. usage="%prog [options] [-- diff_options] [path...]")
  409. parser.add_option("-y", "--assume_yes", action="store_true",
  410. dest="assume_yes", default=False,
  411. help="Assume that the answer to yes/no questions is 'yes'.")
  412. # Logging
  413. group = parser.add_option_group("Logging options")
  414. group.add_option("-q", "--quiet", action="store_const", const=0,
  415. dest="verbose", help="Print errors only.")
  416. group.add_option("-v", "--verbose", action="store_const", const=2,
  417. dest="verbose", default=1,
  418. help="Print info level logs.")
  419. group.add_option("--noisy", action="store_const", const=3,
  420. dest="verbose", help="Print all logs.")
  421. group.add_option("--print_diffs", dest="print_diffs", action="store_true",
  422. help="Print full diffs.")
  423. # Review server
  424. group = parser.add_option_group("Review server options")
  425. group.add_option("-s", "--server", action="store", dest="server",
  426. default=DEFAULT_REVIEW_SERVER,
  427. metavar="SERVER",
  428. help=("The server to upload to. The format is host[:port]. "
  429. "Defaults to '%default'."))
  430. group.add_option("-e", "--email", action="store", dest="email",
  431. metavar="EMAIL", default=None,
  432. help="The username to use. Will prompt if omitted.")
  433. group.add_option("-H", "--host", action="store", dest="host",
  434. metavar="HOST", default=None,
  435. help="Overrides the Host header sent with all RPCs.")
  436. group.add_option("--no_cookies", action="store_false",
  437. dest="save_cookies", default=True,
  438. help="Do not save authentication cookies to local disk.")
  439. group.add_option("--account_type", action="store", dest="account_type",
  440. metavar="TYPE", default=AUTH_ACCOUNT_TYPE,
  441. choices=["GOOGLE", "HOSTED"],
  442. help=("Override the default account type "
  443. "(defaults to '%default', "
  444. "valid choices are 'GOOGLE' and 'HOSTED')."))
  445. # Issue
  446. group = parser.add_option_group("Issue options")
  447. group.add_option("-d", "--description", action="store", dest="description",
  448. metavar="DESCRIPTION", default=None,
  449. help="Optional description when creating an issue.")
  450. group.add_option("-f", "--description_file", action="store",
  451. dest="description_file", metavar="DESCRIPTION_FILE",
  452. default=None,
  453. help="Optional path of a file that contains "
  454. "the description when creating an issue.")
  455. group.add_option("-r", "--reviewers", action="store", dest="reviewers",
  456. metavar="REVIEWERS", default=None,
  457. help="Add reviewers (comma separated email addresses).")
  458. group.add_option("--cc", action="store", dest="cc",
  459. metavar="CC", default=None,
  460. help="Add CC (comma separated email addresses).")
  461. group.add_option("--private", action="store_true", dest="private",
  462. default=False,
  463. help="Make the issue restricted to reviewers and those CCed")
  464. # Upload options
  465. group = parser.add_option_group("Patch options")
  466. group.add_option("-m", "--message", action="store", dest="message",
  467. metavar="MESSAGE", default=None,
  468. help="A message to identify the patch. "
  469. "Will prompt if omitted.")
  470. group.add_option("-i", "--issue", type="int", action="store",
  471. metavar="ISSUE", default=None,
  472. help="Issue number to which to add. Defaults to new issue.")
  473. group.add_option("--base_url", action="store", dest="base_url", default=None,
  474. help="Base repository URL (listed as \"Base URL\" when "
  475. "viewing issue). If omitted, will be guessed automatically "
  476. "for SVN repos and left blank for others.")
  477. group.add_option("--download_base", action="store_true",
  478. dest="download_base", default=False,
  479. help="Base files will be downloaded by the server "
  480. "(side-by-side diffs may not work on files with CRs).")
  481. group.add_option("--rev", action="store", dest="revision",
  482. metavar="REV", default=None,
  483. help="Base revision/branch/tree to diff against. Use "
  484. "rev1:rev2 range to review already committed changeset.")
  485. group.add_option("--send_mail", action="store_true",
  486. dest="send_mail", default=False,
  487. help="Send notification email to reviewers.")
  488. group.add_option("--vcs", action="store", dest="vcs",
  489. metavar="VCS", default=None,
  490. help=("Version control system (optional, usually upload.py "
  491. "already guesses the right VCS)."))
  492. group.add_option("--emulate_svn_auto_props", action="store_true",
  493. dest="emulate_svn_auto_props", default=False,
  494. help=("Emulate Subversion's auto properties feature."))
  495. # Perforce-specific
  496. group = parser.add_option_group("Perforce-specific options "
  497. "(overrides P4 environment variables)")
  498. group.add_option("--p4_port", action="store", dest="p4_port",
  499. metavar="P4_PORT", default=None,
  500. help=("Perforce server and port (optional)"))
  501. group.add_option("--p4_changelist", action="store", dest="p4_changelist",
  502. metavar="P4_CHANGELIST", default=None,
  503. help=("Perforce changelist id"))
  504. group.add_option("--p4_client", action="store", dest="p4_client",
  505. metavar="P4_CLIENT", default=None,
  506. help=("Perforce client/workspace"))
  507. group.add_option("--p4_user", action="store", dest="p4_user",
  508. metavar="P4_USER", default=None,
  509. help=("Perforce user"))
  510. def GetRpcServer(server, email=None, host_override=None, save_cookies=True,
  511. account_type=AUTH_ACCOUNT_TYPE):
  512. """Returns an instance of an AbstractRpcServer.
  513. Args:
  514. server: String containing the review server URL.
  515. email: String containing user's email address.
  516. host_override: If not None, string containing an alternate hostname to use
  517. in the host header.
  518. save_cookies: Whether authentication cookies should be saved to disk.
  519. account_type: Account type for authentication, either 'GOOGLE'
  520. or 'HOSTED'. Defaults to AUTH_ACCOUNT_TYPE.
  521. Returns:
  522. A new AbstractRpcServer, on which RPC calls can be made.
  523. """
  524. rpc_server_class = HttpRpcServer
  525. # If this is the dev_appserver, use fake authentication.
  526. host = (host_override or server).lower()
  527. if re.match(r'(http://)?localhost([:/]|$)', host):
  528. if email is None:
  529. email = "test@example.com"
  530. logging.info("Using debug user %s. Override with --email" % email)
  531. server = rpc_server_class(
  532. server,
  533. lambda: (email, "password"),
  534. host_override=host_override,
  535. extra_headers={"Cookie":
  536. 'dev_appserver_login="%s:False"' % email},
  537. save_cookies=save_cookies,
  538. account_type=account_type)
  539. # Don't try to talk to ClientLogin.
  540. server.authenticated = True
  541. return server
  542. def GetUserCredentials():
  543. """Prompts the user for a username and password."""
  544. # Create a local alias to the email variable to avoid Python's crazy
  545. # scoping rules.
  546. local_email = email
  547. if local_email is None:
  548. local_email = GetEmail("Email (login for uploading to %s)" % server)
  549. password = None
  550. if keyring:
  551. password = keyring.get_password(host, local_email)
  552. if password is not None:
  553. print "Using password from system keyring."
  554. else:
  555. password = getpass.getpass("Password for %s: " % local_email)
  556. if keyring:
  557. answer = raw_input("Store password in system keyring?(y/N) ").strip()
  558. if answer == "y":
  559. keyring.set_password(host, local_email, password)
  560. return (local_email, password)
  561. return rpc_server_class(server,
  562. GetUserCredentials,
  563. host_override=host_override,
  564. save_cookies=save_cookies)
  565. def EncodeMultipartFormData(fields, files):
  566. """Encode form fields for multipart/form-data.
  567. Args:
  568. fields: A sequence of (name, value) elements for regular form fields.
  569. files: A sequence of (name, filename, value) elements for data to be
  570. uploaded as files.
  571. Returns:
  572. (content_type, body) ready for httplib.HTTP instance.
  573. Source:
  574. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306
  575. """
  576. BOUNDARY = '-M-A-G-I-C---B-O-U-N-D-A-R-Y-'
  577. CRLF = '\r\n'
  578. lines = []
  579. for (key, value) in fields:
  580. lines.append('--' + BOUNDARY)
  581. lines.append('Content-Disposition: form-data; name="%s"' % key)
  582. lines.append('')
  583. if isinstance(value, unicode):
  584. value = value.encode('utf-8')
  585. lines.append(value)
  586. for (key, filename, value) in files:
  587. lines.append('--' + BOUNDARY)
  588. lines.append('Content-Disposition: form-data; name="%s"; filename="%s"' %
  589. (key, filename))
  590. lines.append('Content-Type: %s' % GetContentType(filename))
  591. lines.append('')
  592. if isinstance(value, unicode):
  593. value = value.encode('utf-8')
  594. lines.append(value)
  595. lines.append('--' + BOUNDARY + '--')
  596. lines.append('')
  597. body = CRLF.join(lines)
  598. content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
  599. return content_type, body
  600. def GetContentType(filename):
  601. """Helper to guess the content-type from the filename."""
  602. return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
  603. # Use a shell for subcommands on Windows to get a PATH search.
  604. use_shell = sys.platform.startswith("win")
  605. def RunShellWithReturnCodeAndStderr(command, print_output=False,
  606. universal_newlines=True,
  607. env=os.environ):
  608. """Executes a command and returns the output from stdout, stderr and the return code.
  609. Args:
  610. command: Command to execute.
  611. print_output: If True, the output is printed to stdout.
  612. If False, both stdout and stderr are ignored.
  613. universal_newlines: Use universal_newlines flag (default: True).
  614. Returns:
  615. Tuple (stdout, stderr, return code)
  616. """
  617. logging.info("Running %s", command)
  618. env = env.copy()
  619. env['LC_MESSAGES'] = 'C'
  620. p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  621. shell=use_shell, universal_newlines=universal_newlines,
  622. env=env)
  623. if print_output:
  624. output_array = []
  625. while True:
  626. line = p.stdout.readline()
  627. if not line:
  628. break
  629. print line.strip("\n")
  630. output_array.append(line)
  631. output = "".join(output_array)
  632. else:
  633. output = p.stdout.read()
  634. p.wait()
  635. errout = p.stderr.read()
  636. if print_output and errout:
  637. print >>sys.stderr, errout
  638. p.stdout.close()
  639. p.stderr.close()
  640. return output, errout, p.returncode
  641. def RunShellWithReturnCode(command, print_output=False,
  642. universal_newlines=True,
  643. env=os.environ):
  644. """Executes a command and returns the output from stdout and the return code."""
  645. out, err, retcode = RunShellWithReturnCodeAndStderr(command, print_output,
  646. universal_newlines, env)
  647. return out, retcode
  648. def RunShell(command, silent_ok=False, universal_newlines=True,
  649. print_output=False, env=os.environ):
  650. data, retcode = RunShellWithReturnCode(command, print_output,
  651. universal_newlines, env)
  652. if retcode:
  653. ErrorExit("Got error status from %s:\n%s" % (command, data))
  654. if not silent_ok and not data:
  655. ErrorExit("No output from %s" % command)
  656. return data
  657. class VersionControlSystem(object):
  658. """Abstract base class providing an interface to the VCS."""
  659. def __init__(self, options):
  660. """Constructor.
  661. Args:
  662. options: Command line options.
  663. """
  664. self.options = options
  665. def PostProcessDiff(self, diff):
  666. """Return the diff with any special post processing this VCS needs, e.g.
  667. to include an svn-style "Index:"."""
  668. return diff
  669. def GenerateDiff(self, args):
  670. """Return the current diff as a string.
  671. Args:
  672. args: Extra arguments to pass to the diff command.
  673. """
  674. raise NotImplementedError(
  675. "abstract method -- subclass %s must override" % self.__class__)
  676. def GetUnknownFiles(self):
  677. """Return a list of files unknown to the VCS."""
  678. raise NotImplementedError(
  679. "abstract method -- subclass %s must override" % self.__class__)
  680. def CheckForUnknownFiles(self):
  681. """Show an "are you sure?" prompt if there are unknown files."""
  682. unknown_files = self.GetUnknownFiles()
  683. if unknown_files:
  684. print "The following files are not added to version control:"
  685. for line in unknown_files:
  686. print line
  687. prompt = "Are you sure to continue?(y/N) "
  688. answer = raw_input(prompt).strip()
  689. if answer != "y":
  690. ErrorExit("User aborted")
  691. def GetBaseFile(self, filename):
  692. """Get the content of the upstream version of a file.
  693. Returns:
  694. A tuple (base_content, new_content, is_binary, status)
  695. base_content: The contents of the base file.
  696. new_content: For text files, this is empty. For binary files, this is
  697. the contents of the new file, since the diff output won't contain
  698. information to reconstruct the current file.
  699. is_binary: True iff the file is binary.
  700. status: The status of the file.
  701. """
  702. raise NotImplementedError(
  703. "abstract method -- subclass %s must override" % self.__class__)
  704. def GetBaseFiles(self, diff):
  705. """Helper that calls GetBase file for each file in the patch.
  706. Returns:
  707. A dictionary that maps from filename to GetBaseFile's tuple. Filenames
  708. are retrieved based on lines that start with "Index:" or
  709. "Property changes on:".
  710. """
  711. files = {}
  712. for line in diff.splitlines(True):
  713. if line.startswith('Index:') or line.startswith('Property changes on:'):
  714. unused, filename = line.split(':', 1)
  715. # On Windows if a file has property changes its filename uses '\'
  716. # instead of '/'.
  717. filename = filename.strip().replace('\\', '/')
  718. files[filename] = self.GetBaseFile(filename)
  719. return files
  720. def UploadBaseFiles(self, issue, rpc_server, patch_list, patchset, options,
  721. files):
  722. """Uploads the base files (and if necessary, the current ones as well)."""
  723. def UploadFile(filename, file_id, content, is_binary, status, is_base):
  724. """Uploads a file to the server."""
  725. file_too_large = False
  726. if is_base:
  727. type = "base"
  728. else:
  729. type = "current"
  730. if len(content) > MAX_UPLOAD_SIZE:
  731. print ("Not uploading the %s file for %s because it's too large." %
  732. (type, filename))
  733. file_too_large = True
  734. content = ""
  735. checksum = md5(content).hexdigest()
  736. if options.verbose > 0 and not file_too_large:
  737. print "Uploading %s file for %s" % (type, filename)
  738. url = "/%d/upload_content/%d/%d" % (int(issue), int(patchset), file_id)
  739. form_fields = [("filename", filename),
  740. ("status", status),
  741. ("checksum", checksum),
  742. ("is_binary", str(is_binary)),
  743. ("is_current", str(not is_base)),
  744. ]
  745. if file_too_large:
  746. form_fields.append(("file_too_large", "1"))
  747. if options.email:
  748. form_fields.append(("user", options.email))
  749. ctype, body = EncodeMultipartFormData(form_fields,
  750. [("data", filename, content)])
  751. response_body = rpc_server.Send(url, body,
  752. content_type=ctype)
  753. if not response_body.startswith("OK"):
  754. StatusUpdate(" --> %s" % response_body)
  755. sys.exit(1)
  756. patches = dict()
  757. [patches.setdefault(v, k) for k, v in patch_list]
  758. for filename in patches.keys():
  759. base_content, new_content, is_binary, status = files[filename]
  760. file_id_str = patches.get(filename)
  761. if file_id_str.find("nobase") != -1:
  762. base_content = None
  763. file_id_str = file_id_str[file_id_str.rfind("_") + 1:]
  764. file_id = int(file_id_str)
  765. if base_content != None:
  766. UploadFile(filename, file_id, base_content, is_binary, status, True)
  767. if new_content != None:
  768. UploadFile(filename, file_id, new_content, is_binary, status, False)
  769. def IsImage(self, filename):
  770. """Returns true if the filename has an image extension."""
  771. mimetype = mimetypes.guess_type(filename)[0]
  772. if not mimetype:
  773. return False
  774. return mimetype.startswith("image/")
  775. def IsBinary(self, filename):
  776. """Returns true if the guessed mimetyped isnt't in text group."""
  777. mimetype = mimetypes.guess_type(filename)[0]
  778. if not mimetype:
  779. return False # e.g. README, "real" binaries usually have an extension
  780. # special case for text files which don't start with text/
  781. if mimetype in TEXT_MIMETYPES:
  782. return False
  783. return not mimetype.startswith("text/")
  784. class SubversionVCS(VersionControlSystem):
  785. """Implementation of the VersionControlSystem interface for Subversion."""
  786. def __init__(self, options):
  787. super(SubversionVCS, self).__init__(options)
  788. if self.options.revision:
  789. match = re.match(r"(\d+)(:(\d+))?", self.options.revision)
  790. if not match:
  791. ErrorExit("Invalid Subversion revision %s." % self.options.revision)
  792. self.rev_start = match.group(1)
  793. self.rev_end = match.group(3)
  794. else:
  795. self.rev_start = self.rev_end = None
  796. # Cache output from "svn list -r REVNO dirname".
  797. # Keys: dirname, Values: 2-tuple (ouput for start rev and end rev).
  798. self.svnls_cache = {}
  799. # Base URL is required to fetch files deleted in an older revision.
  800. # Result is cached to not guess it over and over again in GetBaseFile().
  801. required = self.options.download_base or self.options.revision is not None
  802. self.svn_base = self._GuessBase(required)
  803. def GuessBase(self, required):
  804. """Wrapper for _GuessBase."""
  805. return self.svn_base
  806. def _GuessBase(self, required):
  807. """Returns base URL for current diff.
  808. Args:
  809. required: If true, exits if the url can't be guessed, otherwise None is
  810. returned.
  811. """
  812. info = RunShell(["svn", "info"])
  813. for line in info.splitlines():
  814. if line.startswith("URL: "):
  815. url = line.split()[1]
  816. scheme, netloc, path, params, query, fragment = urlparse.urlparse(url)
  817. guess = ""
  818. if netloc == "svn.python.org" and scheme == "svn+ssh":
  819. path = "projects" + path
  820. scheme = "http"
  821. guess = "Python "
  822. elif netloc.endswith(".googlecode.com"):
  823. scheme = "http"
  824. guess = "Google Code "
  825. path = path + "/"
  826. base = urlparse.urlunparse((scheme, netloc, path, params,
  827. query, fragment))
  828. logging.info("Guessed %sbase = %s", guess, base)
  829. return base
  830. if required:
  831. ErrorExit("Can't find URL in output from svn info")
  832. return None
  833. def GenerateDiff(self, args):
  834. cmd = ["svn", "diff"]
  835. if self.options.revision:
  836. cmd += ["-r", self.options.revision]
  837. cmd.extend(args)
  838. data = RunShell(cmd)
  839. count = 0
  840. for line in data.splitlines():
  841. if line.startswith("Index:") or line.startswith("Property changes on:"):
  842. count += 1
  843. logging.info(line)
  844. if not count:
  845. ErrorExit("No valid patches found in output from svn diff")
  846. return data
  847. def _CollapseKeywords(self, content, keyword_str):
  848. """Collapses SVN keywords."""
  849. # svn cat translates keywords but svn diff doesn't. As a result of this
  850. # behavior patching.PatchChunks() fails with a chunk mismatch error.
  851. # This part was originally written by the Review Board development team
  852. # who had the same problem (http://reviews.review-board.org/r/276/).
  853. # Mapping of keywords to known aliases
  854. svn_keywords = {
  855. # Standard keywords
  856. 'Date': ['Date', 'LastChangedDate'],
  857. 'Revision': ['Revision', 'LastChangedRevision', 'Rev'],
  858. 'Author': ['Author', 'LastChangedBy'],
  859. 'HeadURL': ['HeadURL', 'URL'],
  860. 'Id': ['Id'],
  861. # Aliases
  862. 'LastChangedDate': ['LastChangedDate', 'Date'],
  863. 'LastChangedRevision': ['LastChangedRevision', 'Rev', 'Revision'],
  864. 'LastChangedBy': ['LastChangedBy', 'Author'],
  865. 'URL': ['URL', 'HeadURL'],
  866. }
  867. def repl(m):
  868. if m.group(2):
  869. return "$%s::%s$" % (m.group(1), " " * len(m.group(3)))
  870. return "$%s$" % m.group(1)
  871. keywords = [keyword
  872. for name in keyword_str.split(" ")
  873. for keyword in svn_keywords.get(name, [])]
  874. return re.sub(r"\$(%s):(:?)([^\$]+)\$" % '|'.join(keywords), repl, content)
  875. def GetUnknownFiles(self):
  876. status = RunShell(["svn", "status", "--ignore-externals"], silent_ok=True)
  877. unknown_files = []
  878. for line in status.split("\n"):
  879. if line and line[0] == "?":
  880. unknown_files.append(line)
  881. return unknown_files
  882. def ReadFile(self, filename):
  883. """Returns the contents of a file."""
  884. file = open(filename, 'rb')
  885. result = ""
  886. try:
  887. result = file.read()
  888. finally:
  889. file.close()
  890. return result
  891. def GetStatus(self, filename):
  892. """Returns the status of a file."""
  893. if not self.options.revision:
  894. status = RunShell(["svn", "status", "--ignore-externals", filename])
  895. if not status:
  896. ErrorExit("svn status returned no output for %s" % filename)
  897. status_lines = status.splitlines()
  898. # If file is in a cl, the output will begin with
  899. # "\n--- Changelist 'cl_name':\n". See
  900. # http://svn.collab.net/repos/svn/trunk/notes/changelist-design.txt
  901. if (len(status_lines) == 3 and
  902. not status_lines[0] and
  903. status_lines[1].startswith("--- Changelist")):
  904. status = status_lines[2]
  905. else:
  906. status = status_lines[0]
  907. # If we have a revision to diff against we need to run "svn list"
  908. # for the old and the new revision and compare the results to get
  909. # the correct status for a file.
  910. else:
  911. dirname, relfilename = os.path.split(filename)
  912. if dirname not in self.svnls_cache:
  913. cmd = ["svn", "list", "-r", self.rev_start, dirname or "."]
  914. out, err, returncode = RunShellWithReturnCodeAndStderr(cmd)
  915. if returncode:
  916. # Directory might not yet exist at start revison
  917. # svn: Unable to find repository location for 'abc' in revision nnn
  918. if re.match('^svn: Unable to find repository location for .+ in revision \d+', err):
  919. old_files = ()
  920. else:
  921. ErrorExit("Failed to get status for %s:\n%s" % (filename, err))
  922. else:
  923. old_files = out.splitlines()
  924. args = ["svn", "list"]
  925. if self.rev_end:
  926. args += ["-r", self.rev_end]
  927. cmd = args + [dirname or "."]
  928. out, returncode = RunShellWithReturnCode(cmd)
  929. if returncode:
  930. ErrorExit("Failed to run command %s" % cmd)
  931. self.svnls_cache[dirname] = (old_files, out.splitlines())
  932. old_files, new_files = self.svnls_cache[dirname]
  933. if relfilename in old_files and relfilename not in new_files:
  934. status = "D "
  935. elif relfilename in old_files and relfilename in new_files:
  936. status = "M "
  937. else:
  938. status = "A "
  939. return status
  940. def GetBaseFile(self, filename):
  941. status = self.GetStatus(filename)
  942. base_content = None
  943. new_content = None
  944. # If a file is copied its status will be "A +", which signifies
  945. # "addition-with-history". See "svn st" for more information. We need to
  946. # upload the original file or else diff parsing will fail if the file was
  947. # edited.
  948. if status[0] == "A" and status[3] != "+":
  949. # We'll need to upload the new content if we're adding a binary file
  950. # since diff's output won't contain it.
  951. mimetype = RunShell(["svn", "propget", "svn:mime-type", filename],
  952. silent_ok=True)
  953. base_content = ""
  954. is_binary = bool(mimetype) and not mimetype.startswith("text/")
  955. if is_binary and self.IsImage(filename):
  956. new_content = self.ReadFile(filename)
  957. elif (status[0] in ("M", "D", "R") or
  958. (status[0] == "A" and status[3] == "+") or # Copied file.
  959. (status[0] == " " and status[1] == "M")): # Property change.
  960. args = []
  961. if self.options.revision:
  962. url = "%s/%s@%s" % (self.svn_base, filename, self.rev_start)
  963. else:
  964. # Don't change filename, it's needed later.
  965. url = filename
  966. args += ["-r", "BASE"]
  967. cmd = ["svn"] + args + ["propget", "svn:mime-type", url]
  968. mimetype, returncode = RunShellWithReturnCode(cmd)
  969. if returncode:
  970. # File does not exist in the requested revision.
  971. # Reset mimetype, it contains an error message.
  972. mimetype = ""
  973. else:
  974. mimetype = mimetype.strip()
  975. get_base = False
  976. is_binary = (bool(mimetype) and
  977. not mimetype.startswith("text/") and
  978. not mimetype in TEXT_MIMETYPES)
  979. if status[0] == " ":
  980. # Empty base content just to force an upload.
  981. base_content = ""
  982. elif is_binary:
  983. if self.IsImage(filename):
  984. get_base = True
  985. if status[0] == "M":
  986. if not self.rev_end:
  987. new_content = self.ReadFile(filename)
  988. else:
  989. url = "%s/%s@%s" % (self.svn_base, filename, self.rev_end)
  990. new_content = RunShell(["svn", "cat", url],
  991. universal_newlines=True, silent_ok=True)
  992. else:
  993. base_content = ""
  994. else:
  995. get_base = True
  996. if get_base:
  997. if is_binary:
  998. universal_newlines = False
  999. else:
  1000. universal_newlines = True
  1001. if self.rev_start:
  1002. # "svn cat -r REV delete_file.txt" doesn't work. cat requires
  1003. # the full URL with "@REV" appended instead of using "-r" option.
  1004. url = "%s/%s@%s" % (self.svn_base, filename, self.rev_start)
  1005. base_content = RunShell(["svn", "cat", url],
  1006. universal_newlines=universal_newlines,
  1007. silent_ok=True)
  1008. else:
  1009. base_content, ret_code = RunShellWithReturnCode(
  1010. ["svn", "cat", filename], universal_newlines=universal_newlines)
  1011. if ret_code and status[0] == "R":
  1012. # It's a replaced file without local history (see issue208).
  1013. # The base file needs to be fetched from the server.
  1014. url = "%s/%s" % (self.svn_base, filename)
  1015. base_content = RunShell(["svn", "cat", url],
  1016. universal_newlines=universal_newlines,
  1017. silent_ok=True)
  1018. elif ret_code:
  1019. ErrorExit("Got error status from 'svn cat %s'" % filename)
  1020. if not is_binary:
  1021. args = []
  1022. if self.rev_start:
  1023. url = "%s/%s@%s" % (self.svn_base, filename, self.rev_start)
  1024. else:
  1025. url = filename
  1026. args += ["-r", "BASE"]
  1027. cmd = ["svn"] + args + ["propget", "svn:keywords", url]
  1028. keywords, returncode = RunShellWithReturnCode(cmd)
  1029. if keywords and not returncode:
  1030. base_content = self._CollapseKeywords(base_content, keywords)
  1031. else:
  1032. StatusUpdate("svn status returned unexpected output: %s" % status)
  1033. sys.exit(1)
  1034. return base_content, new_content, is_binary, status[0:5]
  1035. class GitVCS(VersionControlSystem):
  1036. """Implementation of the VersionControlSystem interface for Git."""
  1037. def __init__(self, options):
  1038. super(GitVCS, self).__init__(options)
  1039. # Map of filename -> (hash before, hash after) of base file.
  1040. # Hashes for "no such file" are represented as None.
  1041. self.hashes = {}
  1042. # Map of new filename -> old filename for renames.
  1043. self.renames = {}
  1044. def PostProcessDiff(self, gitdiff):
  1045. """Converts the diff output to include an svn-style "Index:" line as well
  1046. as record the hashes of the files, so we can upload them along with our
  1047. diff."""
  1048. # Special used by git to indicate "no such content".
  1049. NULL_HASH = "0"*40
  1050. def IsFileNew(filename):
  1051. return filename in self.hashes and self.hashes[filename][0] is None
  1052. def AddSubversionPropertyChange(filename):
  1053. """Add svn's property change information into the patch if given file is
  1054. new file.
  1055. We use Subversion's auto-props setting to retrieve its property.
  1056. See http://svnbook.red-bean.com/en/1.1/ch07.html#svn-ch-7-sect-1.3.2 for
  1057. Subversion's [auto-props] setting.
  1058. """
  1059. if self.options.emulate_svn_auto_props and IsFileNew(filename):
  1060. svnprops = GetSubversionPropertyChanges(filename)
  1061. if svnprops:
  1062. svndiff.append("\n" + svnprops + "\n")
  1063. svndiff = []
  1064. filecount = 0
  1065. filename = None
  1066. for line in gitdiff.splitlines():
  1067. match = re.match(r"diff --git a/(.*) b/(.*)$", line)
  1068. if match:
  1069. # Add auto property here for previously seen file.
  1070. if filename is not None:
  1071. AddSubversionPropertyChange(filename)
  1072. filecount += 1
  1073. # Intentionally use the "after" filename so we can show renames.
  1074. filename = match.group(2)
  1075. svndiff.append("Index: %s\n" % filename)
  1076. if match.group(1) != match.group(2):
  1077. self.renames[match.group(2)] = match.group(1)
  1078. else:
  1079. # The "index" line in a git diff looks like this (long hashes elided):
  1080. # index 82c0d44..b2cee3f 100755
  1081. # We want to save the left hash, as that identifies the base file.
  1082. match = re.match(r"index (\w+)\.\.(\w+)", line)
  1083. if match:
  1084. before, after = (match.group(1), match.group(2))
  1085. if before == NULL_HASH:
  1086. before = None
  1087. if after == NULL_HASH:
  1088. after = None
  1089. self.hashes[filename] = (before, after)
  1090. svndiff.append(line + "\n")
  1091. if not filecount:
  1092. ErrorExit("No valid patches found in output from git diff")
  1093. # Add auto property for the last seen file.
  1094. assert filename is not None
  1095. AddSubversionPropertyChange(filename)
  1096. return "".join(svndiff)
  1097. def GenerateDiff(self, extra_args):
  1098. extra_args = extra_args[:]
  1099. if self.options.revision:
  1100. if ":" in self.options.revision:
  1101. extra_args = self.options.revision.split(":", 1) + extra_args
  1102. else:
  1103. extra_args = [self.options.revision] + extra_args
  1104. # --no-ext-diff is broken in some versions of Git, so try to work around
  1105. # this by overriding the environment (but there is still a problem if the
  1106. # git config key "diff.external" is used).
  1107. env = os.environ.copy()
  1108. if 'GIT_EXTERNAL_DIFF' in env: del env['GIT_EXTERNAL_DIFF']
  1109. return RunShell(["git", "diff", "--no-ext-diff", "--full-index", "-M"]
  1110. + extra_args, env=env)
  1111. def GetUnknownFiles(self):
  1112. status = RunShell(["git", "ls-files", "--exclude-standard", "--others"],
  1113. silent_ok=True)
  1114. return status.splitlines()
  1115. def GetFileContent(self, file_hash, is_binary):
  1116. """Returns the content of a file identified by its git hash."""
  1117. data, retcode = RunShellWithReturnCode(["git", "show", file_hash],
  1118. universal_newlines=not is_binary)
  1119. if retcode:
  1120. ErrorExit("Got error status from 'git show %s'" % file_hash)
  1121. return data
  1122. def GetBaseFile(self, filename):
  1123. hash_before, hash_after = self.hashes.get(filename, (None,None))
  1124. base_content = None
  1125. new_content = None
  1126. is_binary = self.IsBinary(filename)
  1127. status = None
  1128. if filename in self.renames:
  1129. status = "A +" # Match svn attribute name for renames.
  1130. if filename not in self.hashes:
  1131. # If a rename doesn't change the content, we never get a hash.
  1132. base_content = RunShell(["git", "show", "HEAD:" + filename])
  1133. elif not hash_before:
  1134. status = "A"
  1135. base_content = ""
  1136. elif not hash_after:
  1137. status = "D"
  1138. else:
  1139. status = "M"
  1140. is_image = self.IsImage(filename)
  1141. # Grab the before/after content if we need it.
  1142. # We should include file contents if it's text or it's an image.
  1143. if not is_binary or is_image:
  1144. # Grab the base content if we don't have it already.
  1145. if base_content is None and hash_before:
  1146. base_content = self.GetFileContent(hash_before, is_binary)
  1147. # Only include the "after" file if it's an image; otherwise it
  1148. # it is reconstructed from the diff.
  1149. if is_image and hash_after:
  1150. new_content = self.GetFileContent(hash_after, is_binary)
  1151. return (base_content, new_content, is_binary, status)
  1152. class CVSVCS(VersionControlSystem):
  1153. """Implementation of the VersionControlSystem interface for CVS."""
  1154. def __init__(self, options):
  1155. super(CVSVCS, self).__init__(options)
  1156. def GetOriginalContent_(self, filename):
  1157. RunShell(["cvs", "up", filename], silent_ok=True)
  1158. # TODO need detect file content encoding
  1159. content = open(filename).read()
  1160. return content.replace("\r\n", "\n")
  1161. def GetBaseFile(self, filename):
  1162. base_content = None
  1163. new_content = None
  1164. is_binary = False
  1165. status = "A"
  1166. output, retcode = RunShellWithReturnCode(["cvs", "status", filename])
  1167. if retcode:
  1168. ErrorExit("Got error status from 'cvs status %s'" % filename)
  1169. if output.find("Status: Locally Modified") != -1:
  1170. status = "M"
  1171. temp_filename = "%s.tmp123" % filename
  1172. os.rename(filename, temp_filename)
  1173. base_content = self.GetOriginalContent_(filename)
  1174. os.rename(temp_filename, filename)
  1175. elif output.find("Status: Locally Added"):
  1176. status = "A"
  1177. base_content = ""
  1178. elif output.find("Status: Needs Checkout"):
  1179. status = "D"
  1180. base_content = self.GetOriginalContent_(filename)
  1181. return (base_content, new_content, is_binary, status)
  1182. def GenerateDiff(self, extra_args):
  1183. cmd = ["cvs", "diff", "-u", "-N"]
  1184. if self.options.revision:
  1185. cmd += ["-r", self.options.revision]
  1186. cmd.extend(extra_args)
  1187. data, retcode = RunShellWithReturnCode(cmd)
  1188. count = 0
  1189. if retcode in [0, 1]:
  1190. for line in data.splitlines():
  1191. if line.startswith("Index:"):
  1192. count += 1
  1193. logging.info(line)
  1194. if not count:
  1195. ErrorExit("No valid patches found in output from cvs diff")
  1196. return data
  1197. def GetUnknownFiles(self):
  1198. data, retcode = RunShellWithReturnCode(["cvs", "diff"])
  1199. if retcode not in [0, 1]:
  1200. ErrorExit("Got error status from 'cvs diff':\n%s" % (data,))
  1201. unknown_files = []
  1202. for line in data.split("\n"):
  1203. if line and line[0] == "?":
  1204. unknown_files.append(line)
  1205. return unknown_files
  1206. class MercurialVCS(VersionControlSystem):
  1207. """Implementation of the VersionControlSystem interface for Mercurial."""
  1208. def __init__(self, options, repo_dir):
  1209. super(MercurialVCS, self).__init__(options)
  1210. # Absolute path to repository (we can be in a subdir)
  1211. self.repo_dir = os.path.normpath(repo_dir)
  1212. # Compute the subdir
  1213. cwd = os.path.normpath(os.getcwd())
  1214. assert cwd.startswith(self.repo_dir)
  1215. self.subdir = cwd[len(self.repo_dir):].lstrip(r"\/")
  1216. if self.options.revision:
  1217. self.base_rev = self.options.revision
  1218. else:
  1219. self.base_rev = RunShell(["hg", "parent", "-q"]).split(':')[1].strip()
  1220. def _GetRelPath(self, filename):
  1221. """Get relative path of a file according to the current directory,
  1222. given its logical path in the repo."""
  1223. assert filename.startswith(self.subdir), (filename, self.subdir)
  1224. return filename[len(self.subdir):].lstrip(r"\/")
  1225. def GenerateDiff(self, extra_args):
  1226. cmd = ["hg", "diff", "--git", "-r", self.base_rev] + extra_args
  1227. data = RunShell(cmd, silent_ok=True)
  1228. svndiff = []
  1229. filecount = 0
  1230. for line in data.splitlines():
  1231. m = re.match("diff --git a/(\S+) b/(\S+)", line)
  1232. if m:
  1233. # Modify line to make it look like as it comes from svn diff.
  1234. # With this modification no changes on the server side are required
  1235. # to make upload.py work with Mercurial repos.
  1236. # NOTE: for proper handling of moved/copied files, we have to use
  1237. # the second filename.
  1238. filename = m.group(2)
  1239. svndiff.append("Index: %s" % filename)
  1240. svndiff.append("=" * 67)
  1241. filecount += 1
  1242. logging.info(line)
  1243. else:
  1244. svndiff.append(line)
  1245. if not filecount:
  1246. ErrorExit("No valid patches found in output from hg diff")
  1247. return "\n".join(svndiff) + "\n"
  1248. def GetUnknownFiles(self):
  1249. """Return a list of files unknown to the VCS."""
  1250. args = []
  1251. status = RunShell(["hg", "status", "--rev", self.base_rev, "-u", "."],
  1252. silent_ok=True)
  1253. unknown_files = []
  1254. for line in status.splitlines():
  1255. st, fn = line.split(" ", 1)
  1256. if st == "?":
  1257. unknown_files.append(fn)
  1258. return unknown_files
  1259. def GetBaseFile(self, filename):
  1260. # "hg status" and "hg cat" both take a path relative to the current subdir
  1261. # rather than to the repo root, but "hg diff" has given us the full path
  1262. # to the repo root.
  1263. base_content = ""
  1264. new_content = None
  1265. is_binary = False
  1266. oldrelpath = relpath = self._GetRelPath(filename)
  1267. # "hg status -C" returns two lines for moved/copied files, one otherwise
  1268. out = RunShell(["hg", "status", "-C", "--rev", self.base_rev, relpath])
  1269. out = out.splitlines()
  1270. # HACK: strip error message about missing file/directory if it isn't in
  1271. # the working copy
  1272. if out[0].startswith('%s: ' % relpath):
  1273. out = out[1:]
  1274. status, _ = out[0].split(' ', 1)
  1275. if len(out) > 1 and status == "A":
  1276. # Moved/copied => considered as modified, use old filename to
  1277. # retrieve base contents
  1278. oldrelpath = out[1].strip()
  1279. status = "M"
  1280. if ":" in self.base_rev:
  1281. base_rev = self.base_rev.split(":", 1)[0]
  1282. else:
  1283. base_rev = self.base_rev
  1284. if status != "A":
  1285. base_content = RunShell(["hg", "cat", "-r", base_rev, oldrelpath],
  1286. silent_ok=True)
  1287. is_binary = "\0" in base_content # Mercurial's heuristic
  1288. if status != "R":
  1289. new_content = open(relpath, "rb").read()
  1290. is_binary = is_binary or "\0" in new_content
  1291. if is_binary and base_content:
  1292. # Fetch again without converting newlines
  1293. base_content = RunShell(["hg", "cat", "-r", base_rev, oldrelpath],
  1294. silent_ok=True, universal_newlines=False)
  1295. if not is_binary or not self.IsImage(relpath):
  1296. new_content = None
  1297. return base_content, new_content, is_binary, status
  1298. class PerforceVCS(VersionControlSystem):
  1299. """Implementation of the VersionControlSystem interface for Perforce."""
  1300. def __init__(self, options):
  1301. def ConfirmLogin():
  1302. # Make sure we have a valid perforce session
  1303. while True:
  1304. data, retcode = self.RunPerforceCommandWithReturnCode(
  1305. ["login", "-s"], marshal_output=True)
  1306. if not data:
  1307. ErrorExit("Error checking perforce login")
  1308. if not retcode and (not "code" in data or data["code"] != "error"):
  1309. break
  1310. print "Enter perforce password: "
  1311. self.RunPerforceCommandWithReturnCode(["login"])
  1312. super(PerforceVCS, self).__init__(options)
  1313. self.p4_changelist = options.p4_changelist
  1314. if not self.p4_changelist:
  1315. ErrorExit("A changelist id is required")
  1316. if (options.revision):
  1317. ErrorExit("--rev is not supported for perforce")
  1318. self.p4_port = options.p4_port
  1319. self.p4_client = options.p4_client
  1320. self.p4_user = options.p4_user
  1321. ConfirmLogin()
  1322. if not options.message:
  1323. description = self.RunPerforceCommand(["describe", self.p4_changelist],
  1324. marshal_output=True)
  1325. if description and "desc" in description:
  1326. # Rietveld doesn't support multi-line descriptions
  1327. raw_message = description["desc"].strip()
  1328. lines = raw_message.splitlines()
  1329. if len(lines):
  1330. options.message = lines[0]
  1331. def RunPerforceCommandWithReturnCode(self, extra_args, marshal_output=False,
  1332. universal_newlines=True):
  1333. args = ["p4"]
  1334. if marshal_output:
  1335. # -G makes perforce format its output as marshalled python objects
  1336. args.extend(["-G"])
  1337. if self.p4_port:
  1338. args.extend(["-p", self.p4_port])
  1339. if self.p4_client:
  1340. args.extend(["-c", self.p4_client])
  1341. if self.p4_user:
  1342. args.extend(["-u", self.p4_user])
  1343. args.extend(extra_args)
  1344. data, retcode = RunShellWithReturnCode(
  1345. args, print_output=False, universal_newlines=universal_newlines)
  1346. if marshal_output and data:
  1347. data = marshal.loads(data)
  1348. return data, retcode
  1349. def RunPerforceCommand(self, extra_args, marshal_output=False,
  1350. universal_newlines=True):
  1351. # This might be a good place to cache call results, since things like
  1352. # describe or fstat might get called repeatedly.
  1353. data, retcode = self.RunPerforceCommandWithReturnCode(
  1354. extra_args, marshal_output, universal_newlines)
  1355. if retcode:
  1356. ErrorExit("Got error status from %s:\n%s" % (extra_args, data))
  1357. return data
  1358. def GetFileProperties(self, property_key_prefix = "", command = "describe"):
  1359. description = self.RunPerforceCommand(["describe", self.p4_changelist],
  1360. marshal_output=True)
  1361. changed_files = {}
  1362. file_index = 0
  1363. # Try depotFile0, depotFile1, ... until we don't find a match
  1364. while True:
  1365. file_key = "depotFile%d" % file_index
  1366. if file_key in description:
  1367. filename = description[file_key]
  1368. change_type = description[property_key_prefix + str(file_index)]
  1369. changed_files[filename] = change_type
  1370. file_index += 1
  1371. else:
  1372. break
  1373. return changed_files
  1374. def GetChangedFiles(self):
  1375. return self.GetFileProperties("action")
  1376. def GetUnknownFiles(self):
  1377. # Perforce doesn't detect new files, they have to be explicitly added
  1378. return []
  1379. def IsBaseBinary(self, filename):
  1380. base_filename = self.GetBaseFilename(filename)
  1381. return self.IsBinaryHelper(base_filename, "files")
  1382. def IsPendingBinary(self, filename):
  1383. return self.IsBinaryHelper(filename, "describe")
  1384. def IsBinary(self, filename):
  1385. ErrorExit("IsBinary is not safe: call IsBaseBinary or IsPendingBinary")
  1386. def IsBinaryHelper(self, filename, command):
  1387. file_types = self.GetFileProperties("type", command)
  1388. if not filename in file_types:
  1389. ErrorExit("Trying to check binary status of unknown file %s." % filename)
  1390. # This treats symlinks, macintosh resource files, temporary objects, and
  1391. # unicode as binary. See the Perforce docs for more details:
  1392. # http://www.perforce.com/perforce/doc.current/manuals/cmdref/o.ftypes.html
  1393. return not file_types[filename].endswith("text")
  1394. def GetFileContent(self, filename, revision, is_binary):
  1395. file_arg = filename
  1396. if revision:
  1397. file_arg += "#" + revision
  1398. # -q suppresses the initial line that displays the filename and revision
  1399. return self.RunPerforceCommand(["print", "-q", file_arg],
  1400. universal_newlines=not is_binary)
  1401. def GetBaseFilename(self, filename):
  1402. actionsWithDifferentBases = [
  1403. "move/add", # p4 move
  1404. "branch", # p4 integrate (to a new file), similar to hg "add"
  1405. "add", # p4 integrate (to a new file), after modifying the new file
  1406. ]
  1407. # We only see a different base for "add" if this is a downgraded branch
  1408. # after a file was branched (integrated), then edited.
  1409. if self.GetAction(filename) in actionsWithDifferentBases:
  1410. # -Or shows information about pending integrations/moves
  1411. fstat_result = self.RunPerforceCommand(["fstat", "-Or", filename],
  1412. marshal_output=True)
  1413. baseFileKey = "resolveFromFile0" # I think it's safe to use only file0
  1414. if baseFileKey in fstat_result:
  1415. return fstat_result[baseFileKey]
  1416. return filename
  1417. def GetBaseRevision(self, filename):
  1418. base_filename = self.GetBaseFilename(filename)
  1419. have_result = self.RunPerforceCommand(["have", base_filename],
  1420. marshal_output=True)
  1421. if "haveRev" in have_result:
  1422. return have_result["haveRev"]
  1423. def GetLocalFilename(self, filename):
  1424. where = self.RunPerforceCommand(["where", filename], marshal_output=True)
  1425. if "path" in where:
  1426. return where["path"]
  1427. def GenerateDiff(self, args):
  1428. class DiffData:
  1429. def __init__(self, perforceVCS, filename, action):
  1430. self.perforceVCS = perforceVCS
  1431. self.filename = filename
  1432. self.action = action
  1433. self.base_filename = perforceVCS.GetBaseFilename(filename)
  1434. self.file_body = None
  1435. self.base_rev = None
  1436. self.prefix = None
  1437. self.working_copy = True
  1438. self.change_summary = None
  1439. def GenerateDiffHeader(diffData):
  1440. header = []
  1441. header.append("Index: %s" % diffData.filename)
  1442. header.append("=" * 67)
  1443. if diffData.base_filename != diffData.filename:
  1444. if diffData.action.startswith("move"):
  1445. verb = "rename"
  1446. else:
  1447. verb = "copy"
  1448. header.append("%s from %s" % (verb, diffData.base_filename))
  1449. header.append("%s to %s" % (verb, diffData.filename))
  1450. suffix = "\t(revision %s)" % diffData.base_rev
  1451. header.append("--- " + diffData.base_filename + suffix)
  1452. if diffData.working_copy:
  1453. suffix = "\t(working copy)"
  1454. header.append("+++ " + diffData.filename + suffix)
  1455. if diffData.change_summary:
  1456. header.append(diffData.change_summary)
  1457. return header
  1458. def GenerateMergeDiff(diffData, args):
  1459. # -du generates a unified diff, which is nearly svn format
  1460. diffData.file_body = self.RunPerforceCommand(
  1461. ["diff", "-du", diffData.filename] + args)
  1462. diffData.base_rev = self.GetBaseRevision(diffData.filename)
  1463. diffData.prefix = ""
  1464. # We have to replace p4's file status output (the lines starting
  1465. # with +++ or ---) to match svn's diff format
  1466. lines = diffData.file_body.splitlines()
  1467. first_good_line = 0
  1468. while (first_good_line < len(lines) and
  1469. not lines[first_good_line].startswith("@@")):
  1470. first_good_line += 1
  1471. diffData.file_body = "\n".join(lines[first_good_line:])
  1472. return diffData
  1473. def GenerateAddDiff(diffData):
  1474. fstat = self.RunPerforceCommand(["fstat", diffData.filename],
  1475. marshal_output=True)
  1476. if "headRev" in fstat:
  1477. diffData.base_rev = fstat["headRev"] # Re-adding a deleted file
  1478. else:
  1479. diffData.base_rev = "0" # Brand new file
  1480. diffData.working_copy = False
  1481. rel_path = self.GetLocalFilename(diffData.filename)
  1482. diffData.file_body = open(rel_path, 'r').read()
  1483. # Replicate svn's list of changed lines
  1484. line_count = len(diffData.file_body.splitlines())
  1485. diffData.change_summary = "@@ -0,0 +1"
  1486. if line_count > 1:
  1487. diffData.change_summary += ",%d" % line_count
  1488. diffData.change_summary += " @@"
  1489. diffData.prefix = "+"
  1490. return diffData
  1491. def GenerateDeleteDiff(diffData):
  1492. diffData.base_rev = self.GetBaseRevision(diffData.filename)
  1493. is_base_binary = self.IsBaseBinary(diffData.filename)
  1494. # For deletes, base_filename == filename
  1495. diffData.file_body = self.GetFileContent(diffData.base_filename,
  1496. None,
  1497. is_base_binary)
  1498. # Replicate svn's list of changed lines
  1499. line_count = len(diffData.file_body.splitlines())
  1500. diffData.change_summary = "@@ -1"
  1501. if line_count > 1:
  1502. diffData.change_summary += ",%d" % line_count
  1503. diffData.change_summary += " +0,0 @@"
  1504. diffData.prefix = "-"
  1505. return diffData
  1506. changed_files = self.GetChangedFiles()
  1507. svndiff = []
  1508. filecount = 0
  1509. for (filename, action) in changed_files.items():
  1510. svn_status = self.PerforceActionToSvnStatus(action)
  1511. if svn_status == "SKIP":
  1512. continue
  1513. diffData = DiffData(self, filename, action)
  1514. # Is it possible to diff a branched file? Stackoverflow says no:
  1515. # http://stackoverflow.com/questions/1771314/in-perforce-command-line-how-to-diff-a-file-reopened-for-add
  1516. if svn_status == "M":
  1517. diffData = GenerateMergeDiff(diffData, args)
  1518. elif svn_status == "A":
  1519. diffData = GenerateAddDiff(diffData)
  1520. elif svn_status == "D":
  1521. diffData = GenerateDeleteDiff(diffData)
  1522. else:
  1523. ErrorExit("Unknown file action %s (svn action %s)." % \
  1524. (action, svn_status))
  1525. svndiff += GenerateDiffHeader(diffData)
  1526. for line in diffData.file_body.splitlines():
  1527. svndiff.append(diffData.prefix + line)
  1528. filecount += 1
  1529. if not filecount:
  1530. ErrorExit("No valid patches found in output from p4 diff")
  1531. return "\n".join(svndiff) + "\n"
  1532. def PerforceActionToSvnStatus(self, status):
  1533. # Mirroring the list at http://permalink.gmane.org/gmane.comp.version-control.mercurial.devel/28717
  1534. # Is there something more official?
  1535. return {
  1536. "add" : "A",
  1537. "branch" : "A",
  1538. "delete" : "D",
  1539. "edit" : "M", # Also includes changing file types.
  1540. "integrate" : "M",
  1541. "move/add" : "M",
  1542. "move/delete": "SKIP",
  1543. "purge" : "D", # How does a file's status become "purge"?
  1544. }[status]
  1545. def GetAction(self, filename):
  1546. changed_files = self.GetChangedFiles()
  1547. if not filename in changed_files:
  1548. ErrorExit("Trying to get base version of unknown file %s." % filename)
  1549. return changed_files[filename]
  1550. def GetBaseFile(self, filename):
  1551. base_filename = self.GetBaseFilename(filename)
  1552. base_content = ""
  1553. new_content = None
  1554. status = self.PerforceActionToSvnStatus(self.GetAction(filename))
  1555. if status != "A":
  1556. revision = self.GetBaseRevision(base_filename)
  1557. if not revision:
  1558. ErrorExit("Couldn't find base revision for file %s" % filename)
  1559. is_base_binary = self.IsBaseBinary(base_filename)
  1560. base_content = self.GetFileContent(base_filename,
  1561. revision,
  1562. is_base_binary)
  1563. is_binary = self.IsPendingBinary(filename)
  1564. if status != "D" and status != "SKIP":
  1565. relpath = self.GetLocalFilename(filename)
  1566. if is_binary and self.IsImage(relpath):
  1567. new_content = open(relpath, "rb").read()
  1568. return base_content, new_content, is_binary, status
  1569. # NOTE: The SplitPatch function is duplicated in engine.py, keep them in sync.
  1570. def SplitPatch(data):
  1571. """Splits a patch into separate pieces for each file.
  1572. Args:
  1573. data: A string containing the output of svn diff.
  1574. Returns:
  1575. A list of 2-tuple (filename, text) where text is the svn diff output
  1576. pertaining to filename.
  1577. """
  1578. patches = []
  1579. filename = None
  1580. diff = []
  1581. for line in data.splitlines(True):
  1582. new_filename = None
  1583. if line.startswith('Index:'):
  1584. unused, new_filename = line.split(':', 1)
  1585. new_filename = new_filename.strip()
  1586. elif line.startswith('Property changes on:'):
  1587. unused, temp_filename = line.split(':', 1)
  1588. # When a file is modified, paths use '/' between directories, however
  1589. # when a property is modified '\' is used on Windows. Make them the same
  1590. # otherwise the file shows up twice.
  1591. temp_filename = temp_filename.strip().replace('\\', '/')
  1592. if temp_filename != filename:
  1593. # File has property changes but no modifications, create a new diff.
  1594. new_filename = temp_filename
  1595. if new_filename:
  1596. if filename and diff:
  1597. patches.append((filename, ''.join(diff)))
  1598. filename = new_filename
  1599. diff = [line]
  1600. continue
  1601. if diff is not None:
  1602. diff.append(line)
  1603. if filename and diff:
  1604. patches.append((filename, ''.join(diff)))
  1605. return patches
  1606. def UploadSeparatePatches(issue, rpc_server, patchset, data, options):
  1607. """Uploads a separate patch for each file in the diff output.
  1608. Returns a list of [patch_key, filename] for each file.
  1609. """
  1610. patches = SplitPatch(data)
  1611. rv = []
  1612. for patch in patches:
  1613. if len(patch[1]) > MAX_UPLOAD_SIZE:
  1614. print ("Not uploading the patch for " + patch[0] +
  1615. " because the file is too large.")
  1616. continue
  1617. form_fields = [("filename", patch[0])]
  1618. if not options.download_base:
  1619. form_fields.append(("content_upload", "1"))
  1620. files = [("data", "data.diff", patch[1])]
  1621. ctype, body = EncodeMultipartFormData(form_fields, files)
  1622. url = "/%d/upload_patch/%d" % (int(issue), int(patchset))
  1623. print "Uploading patch for " + patch[0]
  1624. response_body = rpc_server.Send(url, body, content_type=ctype)
  1625. lines = response_body.splitlines()
  1626. if not lines or lines[0] != "OK":
  1627. StatusUpdate(" --> %s" % response_body)
  1628. sys.exit(1)
  1629. rv.append([lines[1], patch[0]])
  1630. return rv
  1631. def GuessVCSName(options):
  1632. """Helper to guess the version control system.
  1633. This examines the current directory, guesses which VersionControlSystem
  1634. we're using, and returns an string indicating which VCS is detected.
  1635. Returns:
  1636. A pair (vcs, output). vcs is a string indicating which VCS was detected
  1637. and is one of VCS_GIT, VCS_MERCURIAL, VCS_SUBVERSION, VCS_PERFORCE,
  1638. VCS_CVS, or VCS_UNKNOWN.
  1639. Since local perforce repositories can't be easily detected, this method
  1640. will only guess VCS_PERFORCE if any perforce options have been specified.
  1641. output is a string containing any interesting output from the vcs
  1642. detection routine, or None if there is nothing interesting.
  1643. """
  1644. for attribute, value in options.__dict__.iteritems():
  1645. if attribute.startswith("p4") and value != None:
  1646. return (VCS_PERFORCE, None)
  1647. def RunDetectCommand(vcs_type, command):
  1648. """Helper to detect VCS by executing command.
  1649. Returns:
  1650. A pair (vcs, output) or None. Throws exception on error.
  1651. """
  1652. try:
  1653. out, returncode = RunShellWithReturnCode(command)
  1654. if returncode == 0:
  1655. return (vcs_type, out.strip())
  1656. except OSError, (errcode, message):
  1657. if errcode != errno.ENOENT: # command not found code
  1658. raise
  1659. # Mercurial has a command to get the base directory of a repository
  1660. # Try running it, but don't die if we don't have hg installed.
  1661. # NOTE: we try Mercurial first as it can sit on top of an SVN working copy.
  1662. res = RunDetectCommand(VCS_MERCURIAL, ["hg", "root"])
  1663. if res != None:
  1664. return res
  1665. # Subversion has a .svn in all working directories.
  1666. if os.path.isdir('.svn'):
  1667. logging.info("Guessed VCS = Subversion")
  1668. return (VCS_SUBVERSION, None)
  1669. # Git has a command to test if you're in a git tree.
  1670. # Try running it, but don't die if we don't have git installed.
  1671. res = RunDetectCommand(VCS_GIT, ["git", "rev-parse",
  1672. "--is-inside-work-tree"])
  1673. if res != None:
  1674. return res
  1675. # detect CVS repos use `cvs status && $? == 0` rules
  1676. res = RunDetectCommand(VCS_CVS, ["cvs", "status"])
  1677. if res != None:
  1678. return res
  1679. return (VCS_UNKNOWN, None)
  1680. def GuessVCS(options):
  1681. """Helper to guess the version control system.
  1682. This verifies any user-specified VersionControlSystem (by command line
  1683. or environment variable). If the user didn't specify one, this examines
  1684. the current directory, guesses which VersionControlSystem we're using,
  1685. and returns an instance of the appropriate class. Exit with an error
  1686. if we can't figure it out.
  1687. Returns:
  1688. A VersionControlSystem instance. Exits if the VCS can't be guessed.
  1689. """
  1690. vcs = options.vcs
  1691. if not vcs:
  1692. vcs = os.environ.get("CODEREVIEW_VCS")
  1693. if vcs:
  1694. v = VCS_ABBREVIATIONS.get(vcs.lower())
  1695. if v is None:
  1696. ErrorExit("Unknown version control system %r specified." % vcs)
  1697. (vcs, extra_output) = (v, None)
  1698. else:
  1699. (vcs, extra_output) = GuessVCSName(options)
  1700. if vcs == VCS_MERCURIAL:
  1701. if extra_output is None:
  1702. extra_output = RunShell(["hg", "root"]).strip()
  1703. return MercurialVCS(options, extra_output)
  1704. elif vcs == VCS_SUBVERSION:
  1705. return SubversionVCS(options)
  1706. elif vcs == VCS_PERFORCE:
  1707. return PerforceVCS(options)
  1708. elif vcs == VCS_GIT:
  1709. return GitVCS(options)
  1710. elif vcs == VCS_CVS:
  1711. return CVSVCS(options)
  1712. ErrorExit(("Could not guess version control system. "
  1713. "Are you in a working copy directory?"))
  1714. def CheckReviewer(reviewer):
  1715. """Validate a reviewer -- either a nickname or an email addres.
  1716. Args:
  1717. reviewer: A nickname or an email address.
  1718. Calls ErrorExit() if it is an invalid email address.
  1719. """
  1720. if "@" not in reviewer:
  1721. return # Assume nickname
  1722. parts = reviewer.split("@")
  1723. if len(parts) > 2:
  1724. ErrorExit("Invalid email address: %r" % reviewer)
  1725. assert len(parts) == 2
  1726. if "." not in parts[1]:
  1727. ErrorExit("Invalid email address: %r" % reviewer)
  1728. def LoadSubversionAutoProperties():
  1729. """Returns the content of [auto-props] section of Subversion's config file as
  1730. a dictionary.
  1731. Returns:
  1732. A dictionary whose key-value pair corresponds the [auto-props] section's
  1733. key-value pair.
  1734. In following cases, returns empty dictionary:
  1735. - config file doesn't exist, or
  1736. - 'enable-auto-props' is not set to 'true-like-value' in [miscellany].
  1737. """
  1738. if os.name == 'nt':
  1739. subversion_config = os.environ.get("APPDATA") + "\\Subversion\\config"
  1740. else:
  1741. subversion_config = os.path.expanduser("~/.subversion/config")
  1742. if not os.path.exists(subversion_config):
  1743. return {}
  1744. config = ConfigParser.ConfigParser()
  1745. config.read(subversion_config)
  1746. if (config.has_section("miscellany") and
  1747. config.has_option("miscellany", "enable-auto-props") and
  1748. config.getboolean("miscellany", "enable-auto-props") and
  1749. config.has_section("auto-props")):
  1750. props = {}
  1751. for file_pattern in config.options("auto-props"):
  1752. props[file_pattern] = ParseSubversionPropertyValues(
  1753. config.get("auto-props", file_pattern))
  1754. return props
  1755. else:
  1756. return {}
  1757. def ParseSubversionPropertyValues(props):
  1758. """Parse the given property value which comes from [auto-props] section and
  1759. returns a list whose element is a (svn_prop_key, svn_prop_value) pair.
  1760. See the following doctest for example.
  1761. >>> ParseSubversionPropertyValues('svn:eol-style=LF')
  1762. [('svn:eol-style', 'LF')]
  1763. >>> ParseSubversionPropertyValues('svn:mime-type=image/jpeg')
  1764. [('svn:mime-type', 'image/jpeg')]
  1765. >>> ParseSubversionPropertyValues('svn:eol-style=LF;svn:executable')
  1766. [('svn:eol-style', 'LF'), ('svn:executable', '*')]
  1767. """
  1768. key_value_pairs = []
  1769. for prop in props.split(";"):
  1770. key_value = prop.split("=")
  1771. assert len(key_value) <= 2
  1772. if len(key_value) == 1:
  1773. # If value is not given, use '*' as a Subversion's convention.
  1774. key_value_pairs.append((key_value[0], "*"))
  1775. else:
  1776. key_value_pairs.append((key_value[0], key_value[1]))
  1777. return key_value_pairs
  1778. def GetSubversionPropertyChanges(filename):
  1779. """Return a Subversion's 'Property changes on ...' string, which is used in
  1780. the patch file.
  1781. Args:
  1782. filename: filename whose property might be set by [auto-props] config.
  1783. Returns:
  1784. A string like 'Property changes on |filename| ...' if given |filename|
  1785. matches any entries in [auto-props] section. None, otherwise.
  1786. """
  1787. global svn_auto_props_map
  1788. if svn_auto_props_map is None:
  1789. svn_auto_props_map = LoadSubversionAutoProperties()
  1790. all_props = []
  1791. for file_pattern, props in svn_auto_props_map.items():
  1792. if fnmatch.fnmatch(filename, file_pattern):
  1793. all_props.extend(props)
  1794. if all_props:
  1795. return FormatSubversionPropertyChanges(filename, all_props)
  1796. return None
  1797. def FormatSubversionPropertyChanges(filename, props):
  1798. """Returns Subversion's 'Property changes on ...' strings using given filename
  1799. and properties.
  1800. Args:
  1801. filename: filename
  1802. props: A list whose element is a (svn_prop_key, svn_prop_value) pair.
  1803. Returns:
  1804. A string which can be used in the patch file for Subversion.
  1805. See the following doctest for example.
  1806. >>> print FormatSubversionPropertyChanges('foo.cc', [('svn:eol-style', 'LF')])
  1807. Property changes on: foo.cc
  1808. ___________________________________________________________________
  1809. Added: svn:eol-style
  1810. + LF
  1811. <BLANKLINE>
  1812. """
  1813. prop_changes_lines = [
  1814. "Property changes on: %s" % filename,
  1815. "___________________________________________________________________"]
  1816. for key, value in props:
  1817. prop_changes_lines.append("Added: " + key)
  1818. prop_changes_lines.append(" + " + value)
  1819. return "\n".join(prop_changes_lines) + "\n"
  1820. def RealMain(argv, data=None):
  1821. """The real main function.
  1822. Args:
  1823. argv: Command line arguments.
  1824. data: Diff contents. If None (default) the diff is generated by
  1825. the VersionControlSystem implementation returned by GuessVCS().
  1826. Returns:
  1827. A 2-tuple (issue id, patchset id).
  1828. The patchset id is None if the base files are not uploaded by this
  1829. script (applies only to SVN checkouts).
  1830. """
  1831. options, args = parser.parse_args(argv[1:])
  1832. global verbosity
  1833. verbosity = options.verbose
  1834. if verbosity >= 3:
  1835. logging.getLogger().setLevel(logging.DEBUG)
  1836. elif verbosity >= 2:
  1837. logging.getLogger().setLevel(logging.INFO)
  1838. vcs = GuessVCS(options)
  1839. base = options.base_url
  1840. if isinstance(vcs, SubversionVCS):
  1841. # Guessing the base field is only supported for Subversion.
  1842. # Note: Fetching base files may become deprecated in future releases.
  1843. guessed_base = vcs.GuessBase(options.download_base)
  1844. if base:
  1845. if guessed_base and base != guessed_base:
  1846. print "Using base URL \"%s\" from --base_url instead of \"%s\"" % \
  1847. (base, guessed_base)
  1848. else:
  1849. base = guessed_base
  1850. if not base and options.download_base:
  1851. options.download_base = True
  1852. logging.info("Enabled upload of base file")
  1853. if not options.assume_yes:
  1854. vcs.CheckForUnknownFiles()
  1855. if data is None:
  1856. data = vcs.GenerateDiff(args)
  1857. data = vcs.PostProcessDiff(data)
  1858. if options.print_diffs:
  1859. print "Rietveld diff start:*****"
  1860. print data
  1861. print "Rietveld diff end:*****"
  1862. files = vcs.GetBaseFiles(data)
  1863. if verbosity >= 1:
  1864. print "Upload server:", options.server, "(change with -s/--server)"
  1865. if options.issue:
  1866. prompt = "Message describing this patch set: "
  1867. else:
  1868. prompt = "New issue subject: "
  1869. message = options.message or raw_input(prompt).strip()
  1870. if not message:
  1871. ErrorExit("A non-empty message is required")
  1872. rpc_server = GetRpcServer(options.server,
  1873. options.email,
  1874. options.host,
  1875. options.save_cookies,
  1876. options.account_type)
  1877. form_fields = [("subject", message)]
  1878. if base:
  1879. b = urlparse.urlparse(base)
  1880. username, netloc = urllib.splituser(b.netloc)
  1881. if username:
  1882. logging.info("Removed username from base URL")
  1883. base = urlparse.urlunparse((b.scheme, netloc, b.path, b.params,
  1884. b.query, b.fragment))
  1885. form_fields.append(("base", base))
  1886. if options.issue:
  1887. form_fields.append(("issue", str(options.issue)))
  1888. if options.email:
  1889. form_fields.append(("user", options.email))
  1890. if options.reviewers:
  1891. for reviewer in options.reviewers.split(','):
  1892. CheckReviewer(reviewer)
  1893. form_fields.append(("reviewers", options.reviewers))
  1894. if options.cc:
  1895. for cc in options.cc.split(','):
  1896. CheckReviewer(cc)
  1897. form_fields.append(("cc", options.cc))
  1898. description = options.description
  1899. if options.description_file:
  1900. if options.description:
  1901. ErrorExit("Can't specify description and description_file")
  1902. file = open(options.description_file, 'r')
  1903. description = file.read()
  1904. file.close()
  1905. if description:
  1906. form_fields.append(("description", description))
  1907. # Send a hash of all the base file so the server can determine if a copy
  1908. # already exists in an earlier patchset.
  1909. base_hashes = ""
  1910. for file, info in files.iteritems():
  1911. if not info[0] is None:
  1912. checksum = md5(info[0]).hexdigest()
  1913. if base_hashes:
  1914. base_hashes += "|"
  1915. base_hashes += checksum + ":" + file
  1916. form_fields.append(("base_hashes", base_hashes))
  1917. if options.private:
  1918. if options.issue:
  1919. print "Warning: Private flag ignored when updating an existing issue."
  1920. else:
  1921. form_fields.append(("private", "1"))
  1922. # If we're uploading base files, don't send the email before the uploads, so
  1923. # that it contains the file status.
  1924. if options.send_mail and options.download_base:
  1925. form_fields.append(("send_mail", "1"))
  1926. if not options.download_base:
  1927. form_fields.append(("content_upload", "1"))
  1928. if len(data) > MAX_UPLOAD_SIZE:
  1929. print "Patch is large, so uploading file patches separately."
  1930. uploaded_diff_file = []
  1931. form_fields.append(("separate_patches", "1"))
  1932. else:
  1933. uploaded_diff_file = [("data", "data.diff", data)]
  1934. ctype, body = EncodeMultipartFormData(form_fields, uploaded_diff_file)
  1935. response_body = rpc_server.Send("/upload", body, content_type=ctype)
  1936. patchset = None
  1937. if not options.download_base or not uploaded_diff_file:
  1938. lines = response_body.splitlines()
  1939. if len(lines) >= 2:
  1940. msg = lines[0]
  1941. patchset = lines[1].strip()
  1942. patches = [x.split(" ", 1) for x in lines[2:]]
  1943. else:
  1944. msg = response_body
  1945. else:
  1946. msg = response_body
  1947. StatusUpdate(msg)
  1948. if not response_body.startswith("Issue created.") and \
  1949. not response_body.startswith("Issue updated."):
  1950. sys.exit(0)
  1951. issue = msg[msg.rfind("/")+1:]
  1952. if not uploaded_diff_file:
  1953. result = UploadSeparatePatches(issue, rpc_server, patchset, data, options)
  1954. if not options.download_base:
  1955. patches = result
  1956. if not options.download_base:
  1957. vcs.UploadBaseFiles(issue, rpc_server, patches, patchset, options, files)
  1958. if options.send_mail:
  1959. rpc_server.Send("/" + issue + "/mail", payload="")
  1960. return issue, patchset
  1961. def main():
  1962. try:
  1963. logging.basicConfig(format=("%(asctime).19s %(levelname)s %(filename)s:"
  1964. "%(lineno)s %(message)s "))
  1965. os.environ['LC_ALL'] = 'C'
  1966. RealMain(sys.argv)
  1967. except KeyboardInterrupt:
  1968. print
  1969. StatusUpdate("Interrupted.")
  1970. sys.exit(1)
  1971. if __name__ == "__main__":
  1972. main()