trychange.py 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Client-side script to send a try job to the try server. It communicates to
  6. the try server by either writting to a svn/git repository or by directly
  7. connecting to the server by HTTP.
  8. """
  9. import contextlib
  10. import datetime
  11. import errno
  12. import getpass
  13. import itertools
  14. import json
  15. import logging
  16. import optparse
  17. import os
  18. import posixpath
  19. import re
  20. import shutil
  21. import sys
  22. import tempfile
  23. import urllib
  24. import urllib2
  25. import urlparse
  26. import fix_encoding
  27. import gcl
  28. import gclient_utils
  29. import gerrit_util
  30. import scm
  31. import subprocess2
  32. __version__ = '1.2'
  33. # Constants
  34. HELP_STRING = "Sorry, Tryserver is not available."
  35. USAGE = r"""%prog [options]
  36. Client-side script to send a try job to the try server. It communicates to
  37. the try server by either writting to a svn repository or by directly connecting
  38. to the server by HTTP."""
  39. EPILOG = """
  40. Examples:
  41. Send a patch directly from rietveld:
  42. %(prog)s -R codereview.chromium.org/1337
  43. --email recipient@example.com --root src
  44. Try a change against a particular revision:
  45. %(prog)s -r 123
  46. Try a change including changes to a sub repository:
  47. %(prog)s -s third_party/WebKit
  48. A git patch off a web site (git inserts a/ and b/) and fix the base dir:
  49. %(prog)s --url http://url/to/patch.diff --patchlevel 1 --root src
  50. Use svn to store the try job, specify an alternate email address and use a
  51. premade diff file on the local drive:
  52. %(prog)s --email user@example.com
  53. --svn_repo svn://svn.chromium.org/chrome-try/try --diff foo.diff
  54. Running only on a 'mac' slave with revision 123 and clobber first; specify
  55. manually the 3 source files to use for the try job:
  56. %(prog)s --bot mac --revision 123 --clobber -f src/a.cc -f src/a.h
  57. -f include/b.h
  58. """
  59. GIT_PATCH_DIR_BASENAME = os.path.join('git-try', 'patches-git')
  60. GIT_BRANCH_FILE = 'ref'
  61. _GIT_PUSH_ATTEMPTS = 3
  62. def DieWithError(message):
  63. print >> sys.stderr, message
  64. sys.exit(1)
  65. def RunCommand(args, error_ok=False, error_message=None, **kwargs):
  66. try:
  67. return subprocess2.check_output(args, shell=False, **kwargs)
  68. except subprocess2.CalledProcessError, e:
  69. if not error_ok:
  70. DieWithError(
  71. 'Command "%s" failed.\n%s' % (
  72. ' '.join(args), error_message or e.stdout or ''))
  73. return e.stdout
  74. def RunGit(args, **kwargs):
  75. """Returns stdout."""
  76. return RunCommand(['git'] + args, **kwargs)
  77. class Error(Exception):
  78. """An error during a try job submission.
  79. For this error, trychange.py does not display stack trace, only message
  80. """
  81. class InvalidScript(Error):
  82. def __str__(self):
  83. return self.args[0] + '\n' + HELP_STRING
  84. class NoTryServerAccess(Error):
  85. def __str__(self):
  86. return self.args[0] + '\n' + HELP_STRING
  87. def Escape(name):
  88. """Escapes characters that could interfere with the file system or try job
  89. parsing.
  90. """
  91. return re.sub(r'[^\w#-]', '_', name)
  92. class SCM(object):
  93. """Simplistic base class to implement one function: ProcessOptions."""
  94. def __init__(self, options, path, file_list):
  95. items = path.split('@')
  96. assert len(items) <= 2
  97. self.checkout_root = os.path.abspath(items[0])
  98. items.append(None)
  99. self.diff_against = items[1]
  100. self.options = options
  101. # Lazy-load file list from the SCM unless files were specified in options.
  102. self._files = None
  103. self._file_tuples = None
  104. if file_list:
  105. self._files = file_list
  106. self._file_tuples = [('M', f) for f in self.files]
  107. self.options.files = None
  108. self.codereview_settings = None
  109. self.codereview_settings_file = 'codereview.settings'
  110. self.toplevel_root = None
  111. def GetFileNames(self):
  112. """Return the list of files in the diff."""
  113. return self.files
  114. def GetCodeReviewSetting(self, key):
  115. """Returns a value for the given key for this repository.
  116. Uses gcl-style settings from the repository.
  117. """
  118. if gcl:
  119. gcl_setting = gcl.GetCodeReviewSetting(key)
  120. if gcl_setting != '':
  121. return gcl_setting
  122. if self.codereview_settings is None:
  123. self.codereview_settings = {}
  124. settings_file = self.ReadRootFile(self.codereview_settings_file)
  125. if settings_file:
  126. for line in settings_file.splitlines():
  127. if not line or line.lstrip().startswith('#'):
  128. continue
  129. k, v = line.split(":", 1)
  130. self.codereview_settings[k.strip()] = v.strip()
  131. return self.codereview_settings.get(key, '')
  132. def _GclStyleSettings(self):
  133. """Set default settings based on the gcl-style settings from the repository.
  134. The settings in the self.options object will only be set if no previous
  135. value exists (i.e. command line flags to the try command will override the
  136. settings in codereview.settings).
  137. """
  138. settings = {
  139. 'port': self.GetCodeReviewSetting('TRYSERVER_HTTP_PORT'),
  140. 'host': self.GetCodeReviewSetting('TRYSERVER_HTTP_HOST'),
  141. 'svn_repo': self.GetCodeReviewSetting('TRYSERVER_SVN_URL'),
  142. 'gerrit_url': self.GetCodeReviewSetting('TRYSERVER_GERRIT_URL'),
  143. 'git_repo': self.GetCodeReviewSetting('TRYSERVER_GIT_URL'),
  144. 'project': self.GetCodeReviewSetting('TRYSERVER_PROJECT'),
  145. # Primarily for revision=auto
  146. 'revision': self.GetCodeReviewSetting('TRYSERVER_REVISION'),
  147. 'root': self.GetCodeReviewSetting('TRYSERVER_ROOT'),
  148. 'patchlevel': self.GetCodeReviewSetting('TRYSERVER_PATCHLEVEL'),
  149. }
  150. logging.info('\n'.join(['%s: %s' % (k, v)
  151. for (k, v) in settings.iteritems() if v]))
  152. for (k, v) in settings.iteritems():
  153. # Avoid overwriting options already set using command line flags.
  154. if v and getattr(self.options, k) is None:
  155. setattr(self.options, k, v)
  156. def AutomagicalSettings(self):
  157. """Determines settings based on supported code review and checkout tools.
  158. """
  159. # Try to find gclient or repo root first.
  160. if not self.options.no_search:
  161. self.toplevel_root = gclient_utils.FindGclientRoot(self.checkout_root)
  162. if self.toplevel_root:
  163. logging.info('Found .gclient at %s' % self.toplevel_root)
  164. else:
  165. self.toplevel_root = gclient_utils.FindFileUpwards(
  166. os.path.join('..', '.repo'), self.checkout_root)
  167. if self.toplevel_root:
  168. logging.info('Found .repo dir at %s'
  169. % os.path.dirname(self.toplevel_root))
  170. # Parse TRYSERVER_* settings from codereview.settings before falling back
  171. # on setting self.options.root manually further down. Otherwise
  172. # TRYSERVER_ROOT would never be used in codereview.settings.
  173. self._GclStyleSettings()
  174. if self.toplevel_root and not self.options.root:
  175. assert os.path.abspath(self.toplevel_root) == self.toplevel_root
  176. self.options.root = gclient_utils.PathDifference(self.toplevel_root,
  177. self.checkout_root)
  178. else:
  179. self._GclStyleSettings()
  180. def ReadRootFile(self, filename):
  181. cur = self.checkout_root
  182. root = self.toplevel_root or self.checkout_root
  183. assert cur.startswith(root), (root, cur)
  184. while cur.startswith(root):
  185. filepath = os.path.join(cur, filename)
  186. if os.path.isfile(filepath):
  187. logging.info('Found %s at %s' % (filename, cur))
  188. return gclient_utils.FileRead(filepath)
  189. cur = os.path.dirname(cur)
  190. logging.warning('Didn\'t find %s' % filename)
  191. return None
  192. def _SetFileTuples(self, file_tuples):
  193. excluded = ['!', '?', 'X', ' ', '~']
  194. def Excluded(f):
  195. if f[0][0] in excluded:
  196. return True
  197. for r in self.options.exclude:
  198. if re.search(r, f[1]):
  199. logging.info('Ignoring "%s"' % f[1])
  200. return True
  201. return False
  202. self._file_tuples = [f for f in file_tuples if not Excluded(f)]
  203. self._files = [f[1] for f in self._file_tuples]
  204. def CaptureStatus(self):
  205. """Returns the 'svn status' emulated output as an array of (status, file)
  206. tuples."""
  207. raise NotImplementedError(
  208. "abstract method -- subclass %s must override" % self.__class__)
  209. @property
  210. def files(self):
  211. if self._files is None:
  212. self._SetFileTuples(self.CaptureStatus())
  213. return self._files
  214. @property
  215. def file_tuples(self):
  216. if self._file_tuples is None:
  217. self._SetFileTuples(self.CaptureStatus())
  218. return self._file_tuples
  219. class SVN(SCM):
  220. """Gathers the options and diff for a subversion checkout."""
  221. def __init__(self, *args, **kwargs):
  222. SCM.__init__(self, *args, **kwargs)
  223. self.checkout_root = scm.SVN.GetCheckoutRoot(self.checkout_root)
  224. if not self.options.email:
  225. # Assumes the svn credential is an email address.
  226. self.options.email = scm.SVN.GetEmail(self.checkout_root)
  227. logging.info("SVN(%s)" % self.checkout_root)
  228. def ReadRootFile(self, filename):
  229. data = SCM.ReadRootFile(self, filename)
  230. if data:
  231. return data
  232. # Try to search on the subversion repository for the file.
  233. if not gcl:
  234. return None
  235. data = gcl.GetCachedFile(filename)
  236. logging.debug('%s:\n%s' % (filename, data))
  237. return data
  238. def CaptureStatus(self):
  239. return scm.SVN.CaptureStatus(None, self.checkout_root)
  240. def GenerateDiff(self):
  241. """Returns a string containing the diff for the given file list.
  242. The files in the list should either be absolute paths or relative to the
  243. given root.
  244. """
  245. return scm.SVN.GenerateDiff(self.files, self.checkout_root, full_move=True,
  246. revision=self.diff_against)
  247. class GIT(SCM):
  248. """Gathers the options and diff for a git checkout."""
  249. def __init__(self, *args, **kwargs):
  250. SCM.__init__(self, *args, **kwargs)
  251. self.checkout_root = scm.GIT.GetCheckoutRoot(self.checkout_root)
  252. if not self.options.name:
  253. self.options.name = scm.GIT.GetPatchName(self.checkout_root)
  254. if not self.options.email:
  255. self.options.email = scm.GIT.GetEmail(self.checkout_root)
  256. if not self.diff_against:
  257. self.diff_against = scm.GIT.GetUpstreamBranch(self.checkout_root)
  258. if not self.diff_against:
  259. raise NoTryServerAccess(
  260. "Unable to determine default branch to diff against. "
  261. "Verify this branch is set up to track another"
  262. "(via the --track argument to \"git checkout -b ...\"")
  263. logging.info("GIT(%s)" % self.checkout_root)
  264. def CaptureStatus(self):
  265. return scm.GIT.CaptureStatus(
  266. [],
  267. self.checkout_root.replace(os.sep, '/'),
  268. self.diff_against)
  269. def GenerateDiff(self):
  270. if RunGit(['diff-index', 'HEAD']):
  271. print 'Cannot try with a dirty tree. You must commit locally first.'
  272. return None
  273. return scm.GIT.GenerateDiff(
  274. self.checkout_root,
  275. files=self.files,
  276. full_move=True,
  277. branch=self.diff_against)
  278. def _ParseBotList(botlist, testfilter):
  279. """Parses bot configurations from a list of strings."""
  280. bots = []
  281. if testfilter:
  282. for bot in itertools.chain.from_iterable(botspec.split(',')
  283. for botspec in botlist):
  284. tests = set()
  285. if ':' in bot:
  286. if bot.endswith(':compile'):
  287. tests |= set(['compile'])
  288. else:
  289. raise ValueError(
  290. 'Can\'t use both --testfilter and --bot builder:test formats '
  291. 'at the same time')
  292. bots.append((bot, tests))
  293. else:
  294. for botspec in botlist:
  295. botname = botspec.split(':')[0]
  296. tests = set()
  297. if ':' in botspec:
  298. tests |= set(filter(None, botspec.split(':')[1].split(',')))
  299. bots.append((botname, tests))
  300. return bots
  301. def _ApplyTestFilter(testfilter, bot_spec):
  302. """Applies testfilter from CLI.
  303. Specifying a testfilter strips off any builder-specified tests (except for
  304. compile).
  305. """
  306. if testfilter:
  307. return [(botname, set(testfilter) | (tests & set(['compile'])))
  308. for botname, tests in bot_spec]
  309. else:
  310. return bot_spec
  311. def _GenTSBotSpec(checkouts, change, changed_files, options):
  312. bot_spec = []
  313. # Get try slaves from PRESUBMIT.py files if not specified.
  314. # Even if the diff comes from options.url, use the local checkout for bot
  315. # selection.
  316. try:
  317. import presubmit_support
  318. root_presubmit = checkouts[0].ReadRootFile('PRESUBMIT.py')
  319. if not change:
  320. if not changed_files:
  321. changed_files = checkouts[0].file_tuples
  322. change = presubmit_support.Change(options.name,
  323. '',
  324. checkouts[0].checkout_root,
  325. changed_files,
  326. options.issue,
  327. options.patchset,
  328. options.email)
  329. masters = presubmit_support.DoGetTryMasters(
  330. change,
  331. checkouts[0].GetFileNames(),
  332. checkouts[0].checkout_root,
  333. root_presubmit,
  334. options.project,
  335. options.verbose,
  336. sys.stdout)
  337. # Compatibility for old checkouts and bots that were on tryserver.chromium.
  338. trybots = masters.get('tryserver.chromium', [])
  339. # Compatibility for checkouts that are not using tryserver.chromium
  340. # but are stuck with git-try or gcl-try.
  341. if not trybots and len(masters) == 1:
  342. trybots = masters.values()[0]
  343. if trybots:
  344. old_style = filter(lambda x: isinstance(x, basestring), trybots)
  345. new_style = filter(lambda x: isinstance(x, tuple), trybots)
  346. # _ParseBotList's testfilter is set to None otherwise it will complain.
  347. bot_spec = _ApplyTestFilter(options.testfilter,
  348. _ParseBotList(old_style, None))
  349. bot_spec.extend(_ApplyTestFilter(options.testfilter, new_style))
  350. except ImportError:
  351. pass
  352. return bot_spec
  353. def _ParseSendChangeOptions(bot_spec, options):
  354. """Parse common options passed to _SendChangeHTTP, _SendChangeSVN and
  355. _SendChangeGit.
  356. """
  357. values = [
  358. ('user', options.user),
  359. ('name', options.name),
  360. ]
  361. # A list of options to copy.
  362. optional_values = (
  363. 'email',
  364. 'revision',
  365. 'root',
  366. 'patchlevel',
  367. 'issue',
  368. 'patchset',
  369. 'target',
  370. 'project',
  371. )
  372. for option_name in optional_values:
  373. value = getattr(options, option_name)
  374. if value:
  375. values.append((option_name, value))
  376. # Not putting clobber to optional_names
  377. # because it used to have lower-case 'true'.
  378. if options.clobber:
  379. values.append(('clobber', 'true'))
  380. for bot, tests in bot_spec:
  381. values.append(('bot', ('%s:%s' % (bot, ','.join(tests)))))
  382. return values
  383. def _SendChangeHTTP(bot_spec, options):
  384. """Send a change to the try server using the HTTP protocol."""
  385. if not options.host:
  386. raise NoTryServerAccess('Please use the --host option to specify the try '
  387. 'server host to connect to.')
  388. if not options.port:
  389. raise NoTryServerAccess('Please use the --port option to specify the try '
  390. 'server port to connect to.')
  391. values = _ParseSendChangeOptions(bot_spec, options)
  392. values.append(('patch', options.diff))
  393. url = 'http://%s:%s/send_try_patch' % (options.host, options.port)
  394. logging.info('Sending by HTTP')
  395. logging.info(''.join("%s=%s\n" % (k, v) for k, v in values))
  396. logging.info(url)
  397. logging.info(options.diff)
  398. if options.dry_run:
  399. return
  400. try:
  401. logging.info('Opening connection...')
  402. connection = urllib2.urlopen(url, urllib.urlencode(values))
  403. logging.info('Done')
  404. except IOError, e:
  405. logging.info(str(e))
  406. if bot_spec and len(e.args) > 2 and e.args[2] == 'got a bad status line':
  407. raise NoTryServerAccess('%s is unaccessible. Bad --bot argument?' % url)
  408. else:
  409. raise NoTryServerAccess('%s is unaccessible. Reason: %s' % (url,
  410. str(e.args)))
  411. if not connection:
  412. raise NoTryServerAccess('%s is unaccessible.' % url)
  413. logging.info('Reading response...')
  414. response = connection.read()
  415. logging.info('Done')
  416. if response != 'OK':
  417. raise NoTryServerAccess('%s is unaccessible. Got:\n%s' % (url, response))
  418. PrintSuccess(bot_spec, options)
  419. @contextlib.contextmanager
  420. def _TempFilename(name, contents=None):
  421. """Create a temporary directory, append the specified name and yield.
  422. In contrast to NamedTemporaryFile, does not keep the file open.
  423. Deletes the file on __exit__.
  424. """
  425. temp_dir = tempfile.mkdtemp(prefix=name)
  426. try:
  427. path = os.path.join(temp_dir, name)
  428. if contents is not None:
  429. with open(path, 'wb') as f:
  430. f.write(contents)
  431. yield path
  432. finally:
  433. shutil.rmtree(temp_dir, True)
  434. @contextlib.contextmanager
  435. def _PrepareDescriptionAndPatchFiles(description, options):
  436. """Creates temporary files with description and patch.
  437. __enter__ called on the return value returns a tuple of patch_filename and
  438. description_filename.
  439. Args:
  440. description: contents of description file.
  441. options: patchset options object. Must have attributes: user,
  442. name (of patch) and diff (contents of patch).
  443. """
  444. current_time = str(datetime.datetime.now()).replace(':', '.')
  445. patch_basename = '%s.%s.%s.diff' % (Escape(options.user),
  446. Escape(options.name), current_time)
  447. with _TempFilename('description', description) as description_filename:
  448. with _TempFilename(patch_basename, options.diff) as patch_filename:
  449. yield patch_filename, description_filename
  450. def _SendChangeSVN(bot_spec, options):
  451. """Send a change to the try server by committing a diff file on a subversion
  452. server."""
  453. if not options.svn_repo:
  454. raise NoTryServerAccess('Please use the --svn_repo option to specify the'
  455. ' try server svn repository to connect to.')
  456. values = _ParseSendChangeOptions(bot_spec, options)
  457. description = ''.join("%s=%s\n" % (k, v) for k, v in values)
  458. logging.info('Sending by SVN')
  459. logging.info(description)
  460. logging.info(options.svn_repo)
  461. logging.info(options.diff)
  462. if options.dry_run:
  463. return
  464. with _PrepareDescriptionAndPatchFiles(description, options) as (
  465. patch_filename, description_filename):
  466. if sys.platform == "cygwin":
  467. # Small chromium-specific issue here:
  468. # git-try uses /usr/bin/python on cygwin but svn.bat will be used
  469. # instead of /usr/bin/svn by default. That causes bad things(tm) since
  470. # Windows' svn.exe has no clue about cygwin paths. Hence force to use
  471. # the cygwin version in this particular context.
  472. exe = "/usr/bin/svn"
  473. else:
  474. exe = "svn"
  475. patch_dir = os.path.dirname(patch_filename)
  476. command = [exe, 'import', '-q', patch_dir, options.svn_repo, '--file',
  477. description_filename]
  478. if scm.SVN.AssertVersion("1.5")[0]:
  479. command.append('--no-ignore')
  480. try:
  481. subprocess2.check_call(command)
  482. except subprocess2.CalledProcessError, e:
  483. raise NoTryServerAccess(str(e))
  484. PrintSuccess(bot_spec, options)
  485. def _GetPatchGitRepo(git_url):
  486. """Gets a path to a Git repo with patches.
  487. Stores patches in .git/git-try/patches-git directory, a git repo. If it
  488. doesn't exist yet or its origin URL is different, cleans up and clones it.
  489. If it existed before, then pulls changes.
  490. Does not support SVN repo.
  491. Returns a path to the directory with patches.
  492. """
  493. git_dir = scm.GIT.GetGitDir(os.getcwd())
  494. patch_dir = os.path.join(git_dir, GIT_PATCH_DIR_BASENAME)
  495. logging.info('Looking for git repo for patches')
  496. # Is there already a repo with the expected url or should we clone?
  497. clone = True
  498. if os.path.exists(patch_dir) and scm.GIT.IsInsideWorkTree(patch_dir):
  499. existing_url = scm.GIT.Capture(
  500. ['config', '--local', 'remote.origin.url'],
  501. cwd=patch_dir)
  502. clone = existing_url != git_url
  503. if clone:
  504. if os.path.exists(patch_dir):
  505. logging.info('Cleaning up')
  506. shutil.rmtree(patch_dir, True)
  507. logging.info('Cloning patch repo')
  508. scm.GIT.Capture(['clone', git_url, GIT_PATCH_DIR_BASENAME], cwd=git_dir)
  509. email = scm.GIT.GetEmail(cwd=os.getcwd())
  510. scm.GIT.Capture(['config', '--local', 'user.email', email], cwd=patch_dir)
  511. else:
  512. if scm.GIT.IsWorkTreeDirty(patch_dir):
  513. logging.info('Work dir is dirty: hard reset!')
  514. scm.GIT.Capture(['reset', '--hard'], cwd=patch_dir)
  515. logging.info('Updating patch repo')
  516. scm.GIT.Capture(['pull', 'origin', 'master'], cwd=patch_dir)
  517. return os.path.abspath(patch_dir)
  518. def _SendChangeGit(bot_spec, options):
  519. """Sends a change to the try server by committing a diff file to a GIT repo.
  520. Creates a temp orphan branch, commits patch.diff, creates a ref pointing to
  521. that commit, deletes the temp branch, checks master out, adds 'ref' file
  522. containing the name of the new ref, pushes master and the ref to the origin.
  523. TODO: instead of creating a temp branch, use git-commit-tree.
  524. """
  525. if not options.git_repo:
  526. raise NoTryServerAccess('Please use the --git_repo option to specify the '
  527. 'try server git repository to connect to.')
  528. values = _ParseSendChangeOptions(bot_spec, options)
  529. comment_subject = '%s.%s' % (options.user, options.name)
  530. comment_body = ''.join("%s=%s\n" % (k, v) for k, v in values)
  531. description = '%s\n\n%s' % (comment_subject, comment_body)
  532. logging.info('Sending by GIT')
  533. logging.info(description)
  534. logging.info(options.git_repo)
  535. logging.info(options.diff)
  536. if options.dry_run:
  537. return
  538. patch_dir = _GetPatchGitRepo(options.git_repo)
  539. def patch_git(*args):
  540. return scm.GIT.Capture(list(args), cwd=patch_dir)
  541. def add_and_commit(filename, comment_filename):
  542. patch_git('add', filename)
  543. patch_git('commit', '-F', comment_filename)
  544. assert scm.GIT.IsInsideWorkTree(patch_dir)
  545. assert not scm.GIT.IsWorkTreeDirty(patch_dir)
  546. with _PrepareDescriptionAndPatchFiles(description, options) as (
  547. patch_filename, description_filename):
  548. logging.info('Committing patch')
  549. temp_branch = 'tmp_patch'
  550. target_ref = 'refs/patches/%s/%s' % (
  551. Escape(options.user),
  552. os.path.basename(patch_filename).replace(' ','_'))
  553. target_filename = os.path.join(patch_dir, 'patch.diff')
  554. branch_file = os.path.join(patch_dir, GIT_BRANCH_FILE)
  555. patch_git('checkout', 'master')
  556. try:
  557. # Try deleting an existing temp branch, if any.
  558. try:
  559. patch_git('branch', '-D', temp_branch)
  560. logging.debug('Deleted an existing temp branch.')
  561. except subprocess2.CalledProcessError:
  562. pass
  563. # Create a new branch and put the patch there.
  564. patch_git('checkout', '--orphan', temp_branch)
  565. patch_git('reset')
  566. patch_git('clean', '-f')
  567. shutil.copyfile(patch_filename, target_filename)
  568. add_and_commit(target_filename, description_filename)
  569. assert not scm.GIT.IsWorkTreeDirty(patch_dir)
  570. # Create a ref and point it to the commit referenced by temp_branch.
  571. patch_git('update-ref', target_ref, temp_branch)
  572. # Delete the temp ref.
  573. patch_git('checkout', 'master')
  574. patch_git('branch', '-D', temp_branch)
  575. # Update the branch file in the master.
  576. def update_branch():
  577. with open(branch_file, 'w') as f:
  578. f.write(target_ref)
  579. add_and_commit(branch_file, description_filename)
  580. update_branch()
  581. # Push master and target_ref to origin.
  582. logging.info('Pushing patch')
  583. for attempt in xrange(_GIT_PUSH_ATTEMPTS):
  584. try:
  585. patch_git('push', 'origin', 'master', target_ref)
  586. except subprocess2.CalledProcessError as e:
  587. is_last = attempt == _GIT_PUSH_ATTEMPTS - 1
  588. if is_last:
  589. raise NoTryServerAccess(str(e))
  590. # Fetch, reset, update branch file again.
  591. patch_git('fetch', 'origin')
  592. patch_git('reset', '--hard', 'origin/master')
  593. update_branch()
  594. except subprocess2.CalledProcessError, e:
  595. # Restore state.
  596. patch_git('checkout', 'master')
  597. patch_git('reset', '--hard', 'origin/master')
  598. raise
  599. PrintSuccess(bot_spec, options)
  600. def _SendChangeGerrit(bot_spec, options):
  601. """Posts a try job to a Gerrit change.
  602. Reads Change-Id from the HEAD commit, resolves the current revision, checks
  603. that local revision matches the uploaded one, posts a try job in form of a
  604. message, sets Tryjob-Request label to 1.
  605. Gerrit message format: starts with !tryjob, optionally followed by a tryjob
  606. definition in JSON format:
  607. buildNames: list of strings specifying build names.
  608. build_properties: a dict of build properties.
  609. """
  610. logging.info('Sending by Gerrit')
  611. if not options.gerrit_url:
  612. raise NoTryServerAccess('Please use --gerrit_url option to specify the '
  613. 'Gerrit instance url to connect to')
  614. gerrit_host = urlparse.urlparse(options.gerrit_url).hostname
  615. logging.debug('Gerrit host: %s' % gerrit_host)
  616. def GetChangeId(commmitish):
  617. """Finds Change-ID of the HEAD commit."""
  618. CHANGE_ID_RGX = '^Change-Id: (I[a-f0-9]{10,})'
  619. comment = scm.GIT.Capture(['log', '-1', commmitish, '--format=%b'],
  620. cwd=os.getcwd())
  621. change_id_match = re.search(CHANGE_ID_RGX, comment, re.I | re.M)
  622. if not change_id_match:
  623. raise Error('Change-Id was not found in the HEAD commit. Make sure you '
  624. 'have a Git hook installed that generates and inserts a '
  625. 'Change-Id into a commit message automatically.')
  626. change_id = change_id_match.group(1)
  627. return change_id
  628. def FormatMessage():
  629. # Build job definition.
  630. job_def = {}
  631. build_properties = {}
  632. if options.testfilter:
  633. build_properties['testfilter'] = options.testfilter
  634. builderNames = [builder for builder, _ in bot_spec]
  635. if builderNames:
  636. job_def['builderNames'] = builderNames
  637. if build_properties:
  638. job_def['build_properties'] = build_properties
  639. # Format message.
  640. msg = '!tryjob'
  641. if job_def:
  642. msg = '%s %s' % (msg, json.dumps(job_def, sort_keys=True))
  643. return msg
  644. def PostTryjob(message):
  645. logging.info('Posting gerrit message: %s' % message)
  646. if not options.dry_run:
  647. # Post a message and set TryJob=1 label.
  648. try:
  649. gerrit_util.SetReview(gerrit_host, change_id, msg=message,
  650. labels={'Tryjob-Request': 1})
  651. except gerrit_util.GerritError, e:
  652. if e.http_status == 400:
  653. raise Error(e.message)
  654. else:
  655. raise
  656. head_sha = scm.GIT.Capture(['log', '-1', '--format=%H'], cwd=os.getcwd())
  657. change_id = GetChangeId(head_sha)
  658. try:
  659. # Check that the uploaded revision matches the local one.
  660. changes = gerrit_util.GetChangeCurrentRevision(gerrit_host, change_id)
  661. except gerrit_util.GerritAuthenticationError, e:
  662. raise NoTryServerAccess(e.message)
  663. assert len(changes) <= 1, 'Multiple changes with id %s' % change_id
  664. if not changes:
  665. raise Error('A change %s was not found on the server. Was it uploaded?' %
  666. change_id)
  667. logging.debug('Found Gerrit change: %s' % changes[0])
  668. if changes[0]['current_revision'] != head_sha:
  669. raise Error('Please upload your latest local changes to Gerrit.')
  670. # Post a try job.
  671. message = FormatMessage()
  672. PostTryjob(message)
  673. change_url = urlparse.urljoin(options.gerrit_url,
  674. '/#/c/%s' % changes[0]['_number'])
  675. print('A tryjob was posted on change %s' % change_url)
  676. def PrintSuccess(bot_spec, options):
  677. if not options.dry_run:
  678. text = 'Patch \'%s\' sent to try server' % options.name
  679. if bot_spec:
  680. text += ': %s' % ', '.join(
  681. '%s:%s' % (b[0], ','.join(b[1])) for b in bot_spec)
  682. print(text)
  683. def GuessVCS(options, path, file_list):
  684. """Helper to guess the version control system.
  685. NOTE: Very similar to upload.GuessVCS. Doesn't look for hg since we don't
  686. support it yet.
  687. This examines the path directory, guesses which SCM we're using, and
  688. returns an instance of the appropriate class. Exit with an error if we can't
  689. figure it out.
  690. Returns:
  691. A SCM instance. Exits if the SCM can't be guessed.
  692. """
  693. __pychecker__ = 'no-returnvalues'
  694. real_path = path.split('@')[0]
  695. logging.info("GuessVCS(%s)" % path)
  696. # Subversion has a .svn in all working directories.
  697. if os.path.isdir(os.path.join(real_path, '.svn')):
  698. return SVN(options, path, file_list)
  699. # Git has a command to test if you're in a git tree.
  700. # Try running it, but don't die if we don't have git installed.
  701. try:
  702. subprocess2.check_output(
  703. ['git', 'rev-parse', '--is-inside-work-tree'], cwd=real_path,
  704. stderr=subprocess2.VOID)
  705. return GIT(options, path, file_list)
  706. except OSError, e:
  707. if e.errno != errno.ENOENT:
  708. raise
  709. except subprocess2.CalledProcessError, e:
  710. if e.returncode != errno.ENOENT and e.returncode != 128:
  711. # ENOENT == 2 = they don't have git installed.
  712. # 128 = git error code when not in a repo.
  713. logging.warning('Unexpected error code: %s' % e.returncode)
  714. raise
  715. raise NoTryServerAccess(
  716. ( 'Could not guess version control system for %s.\n'
  717. 'Are you in a working copy directory?') % path)
  718. def GetMungedDiff(path_diff, diff):
  719. # Munge paths to match svn.
  720. changed_files = []
  721. for i in range(len(diff)):
  722. if diff[i].startswith('--- ') or diff[i].startswith('+++ '):
  723. new_file = posixpath.join(path_diff, diff[i][4:]).replace('\\', '/')
  724. if diff[i].startswith('--- '):
  725. file_path = new_file.split('\t')[0].strip()
  726. if file_path.startswith('a/'):
  727. file_path = file_path[2:]
  728. changed_files.append(('M', file_path))
  729. diff[i] = diff[i][0:4] + new_file
  730. return (diff, changed_files)
  731. class OptionParser(optparse.OptionParser):
  732. def format_epilog(self, _):
  733. """Removes epilog formatting."""
  734. return self.epilog or ''
  735. def gen_parser(prog):
  736. # Parse argv
  737. parser = OptionParser(usage=USAGE, version=__version__, prog=prog)
  738. parser.add_option("-v", "--verbose", action="count", default=0,
  739. help="Prints debugging infos")
  740. group = optparse.OptionGroup(parser, "Result and status")
  741. group.add_option("-u", "--user", default=getpass.getuser(),
  742. help="Owner user name [default: %default]")
  743. group.add_option("-e", "--email",
  744. default=os.environ.get('TRYBOT_RESULTS_EMAIL_ADDRESS',
  745. os.environ.get('EMAIL_ADDRESS')),
  746. help="Email address where to send the results. Use either "
  747. "the TRYBOT_RESULTS_EMAIL_ADDRESS environment "
  748. "variable or EMAIL_ADDRESS to set the email address "
  749. "the try bots report results to [default: %default]")
  750. group.add_option("-n", "--name",
  751. help="Descriptive name of the try job")
  752. group.add_option("--issue", type='int',
  753. help="Update rietveld issue try job status")
  754. group.add_option("--patchset", type='int',
  755. help="Update rietveld issue try job status. This is "
  756. "optional if --issue is used, In that case, the "
  757. "latest patchset will be used.")
  758. group.add_option("--dry_run", action='store_true',
  759. help="Don't send the try job. This implies --verbose, so "
  760. "it will print the diff.")
  761. parser.add_option_group(group)
  762. group = optparse.OptionGroup(parser, "Try job options")
  763. group.add_option(
  764. "-b", "--bot", action="append",
  765. help=("IMPORTANT: specify ONE builder per --bot flag. Use it multiple "
  766. "times to specify multiple builders. ex: "
  767. "'-bwin_rel:ui_tests,webkit_unit_tests -bwin_layout'. See "
  768. "the try server waterfall for the builders name and the tests "
  769. "available. Can also be used to specify gtest_filter, e.g. "
  770. "-bwin_rel:base_unittests:ValuesTest.*Value"))
  771. group.add_option("-B", "--print_bots", action="store_true",
  772. help="Print bots we would use (e.g. from PRESUBMIT.py)"
  773. " and exit. Do not send patch. Like --dry_run"
  774. " but less verbose.")
  775. group.add_option("-r", "--revision",
  776. help="Revision to use for the try job. If 'auto' is "
  777. "specified, it is resolved to the revision a patch is "
  778. "generated against (Git only). Default: the "
  779. "revision will be determined by the try server; see "
  780. "its waterfall for more info")
  781. group.add_option("-c", "--clobber", action="store_true",
  782. help="Force a clobber before building; e.g. don't do an "
  783. "incremental build")
  784. # TODO(maruel): help="Select a specific configuration, usually 'debug' or "
  785. # "'release'"
  786. group.add_option("--target", help=optparse.SUPPRESS_HELP)
  787. group.add_option("--project",
  788. help="Override which project to use. Projects are defined "
  789. "server-side to define what default bot set to use")
  790. group.add_option(
  791. "-t", "--testfilter", action="append", default=[],
  792. help=("Apply a testfilter to all the selected builders. Unless the "
  793. "builders configurations are similar, use multiple "
  794. "--bot <builder>:<test> arguments."))
  795. parser.add_option_group(group)
  796. group = optparse.OptionGroup(parser, "Patch to run")
  797. group.add_option("-f", "--file", default=[], dest="files",
  798. metavar="FILE", action="append",
  799. help="Use many times to list the files to include in the "
  800. "try, relative to the repository root")
  801. group.add_option("--diff",
  802. help="File containing the diff to try")
  803. group.add_option("--url",
  804. help="Url where to grab a patch, e.g. "
  805. "http://example.com/x.diff")
  806. group.add_option("-R", "--rietveld_url", default="codereview.appspot.com",
  807. metavar="URL",
  808. help="Has 2 usages, both refer to the rietveld instance: "
  809. "Specify which code review patch to use as the try job "
  810. "or rietveld instance to update the try job results "
  811. "Default:%default")
  812. group.add_option("--root",
  813. help="Root to use for the patch; base subdirectory for "
  814. "patch created in a subdirectory")
  815. group.add_option("-p", "--patchlevel", type='int', metavar="LEVEL",
  816. help="Used as -pN parameter to patch")
  817. group.add_option("-s", "--sub_rep", action="append", default=[],
  818. help="Subcheckout to use in addition. This is mainly "
  819. "useful for gclient-style checkouts. In git, checkout "
  820. "the branch with changes first. Use @rev or "
  821. "@branch to specify the "
  822. "revision/branch to diff against. If no @branch is "
  823. "given the diff will be against the upstream branch. "
  824. "If @branch then the diff is branch..HEAD. "
  825. "All edits must be checked in.")
  826. group.add_option("--no_search", action="store_true",
  827. help=("Disable automatic search for gclient or repo "
  828. "checkout root."))
  829. group.add_option("-E", "--exclude", action="append",
  830. default=['ChangeLog'], metavar='REGEXP',
  831. help="Regexp patterns to exclude files. Default: %default")
  832. group.add_option("--upstream_branch", action="store",
  833. help="Specify the upstream branch to diff against in the "
  834. "main checkout")
  835. parser.add_option_group(group)
  836. group = optparse.OptionGroup(parser, "Access the try server by HTTP")
  837. group.add_option("--use_http",
  838. action="store_const",
  839. const=_SendChangeHTTP,
  840. dest="send_patch",
  841. help="Use HTTP to talk to the try server [default]")
  842. group.add_option("-H", "--host",
  843. help="Host address")
  844. group.add_option("-P", "--port", type="int",
  845. help="HTTP port")
  846. parser.add_option_group(group)
  847. group = optparse.OptionGroup(parser, "Access the try server with SVN")
  848. group.add_option("--use_svn",
  849. action="store_const",
  850. const=_SendChangeSVN,
  851. dest="send_patch",
  852. help="Use SVN to talk to the try server")
  853. group.add_option("-S", "--svn_repo",
  854. metavar="SVN_URL",
  855. help="SVN url to use to write the changes in; --use_svn is "
  856. "implied when using --svn_repo")
  857. parser.add_option_group(group)
  858. group = optparse.OptionGroup(parser, "Access the try server with Git")
  859. group.add_option("--use_git",
  860. action="store_const",
  861. const=_SendChangeGit,
  862. dest="send_patch",
  863. help="Use GIT to talk to the try server")
  864. group.add_option("-G", "--git_repo",
  865. metavar="GIT_URL",
  866. help="GIT url to use to write the changes in; --use_git is "
  867. "implied when using --git_repo")
  868. parser.add_option_group(group)
  869. group = optparse.OptionGroup(parser, "Access the try server with Gerrit")
  870. group.add_option("--use_gerrit",
  871. action="store_const",
  872. const=_SendChangeGerrit,
  873. dest="send_patch",
  874. help="Use Gerrit to talk to the try server")
  875. group.add_option("--gerrit_url",
  876. metavar="GERRIT_URL",
  877. help="Gerrit url to post a tryjob to; --use_gerrit is "
  878. "implied when using --gerrit_url")
  879. parser.add_option_group(group)
  880. return parser
  881. def TryChange(argv,
  882. change,
  883. swallow_exception,
  884. prog=None,
  885. extra_epilog=None):
  886. """
  887. Args:
  888. argv: Arguments and options.
  889. change: Change instance corresponding to the CL.
  890. swallow_exception: Whether we raise or swallow exceptions.
  891. """
  892. parser = gen_parser(prog)
  893. epilog = EPILOG % { 'prog': prog }
  894. if extra_epilog:
  895. epilog += extra_epilog
  896. parser.epilog = epilog
  897. options, args = parser.parse_args(argv)
  898. # If they've asked for help, give it to them
  899. if len(args) == 1 and args[0] == 'help':
  900. parser.print_help()
  901. return 0
  902. # If they've said something confusing, don't spawn a try job until you
  903. # understand what they want.
  904. if args:
  905. parser.error('Extra argument(s) "%s" not understood' % ' '.join(args))
  906. if options.dry_run:
  907. options.verbose += 1
  908. LOG_FORMAT = '%(levelname)s %(filename)s(%(lineno)d): %(message)s'
  909. if not swallow_exception:
  910. if options.verbose == 0:
  911. logging.basicConfig(level=logging.WARNING, format=LOG_FORMAT)
  912. elif options.verbose == 1:
  913. logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
  914. elif options.verbose > 1:
  915. logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
  916. logging.debug(argv)
  917. if (options.patchlevel is not None and
  918. (options.patchlevel < 0 or options.patchlevel > 10)):
  919. parser.error(
  920. 'Have you tried --port instead? You probably confused -p and -P.')
  921. # Strip off any @ in the user, otherwise svn gets confused.
  922. options.user = options.user.split('@', 1)[0]
  923. if options.rietveld_url:
  924. # Try to extract the review number if possible and fix the protocol.
  925. if not '://' in options.rietveld_url:
  926. options.rietveld_url = 'http://' + options.rietveld_url
  927. match = re.match(r'^(.*)/(\d+)/?$', options.rietveld_url)
  928. if match:
  929. if options.issue or options.patchset:
  930. parser.error('Cannot use both --issue and use a review number url')
  931. options.issue = int(match.group(2))
  932. options.rietveld_url = match.group(1)
  933. try:
  934. changed_files = None
  935. # Always include os.getcwd() in the checkout settings.
  936. path = os.getcwd()
  937. file_list = []
  938. if options.files:
  939. file_list = options.files
  940. elif change:
  941. file_list = [f.LocalPath() for f in change.AffectedFiles()]
  942. if options.upstream_branch:
  943. path += '@' + options.upstream_branch
  944. # Clear file list so that the correct list will be retrieved from the
  945. # upstream branch.
  946. file_list = []
  947. current_vcs = GuessVCS(options, path, file_list)
  948. current_vcs.AutomagicalSettings()
  949. options = current_vcs.options
  950. vcs_is_git = type(current_vcs) is GIT
  951. # So far, git_repo doesn't work with SVN
  952. if options.git_repo and not vcs_is_git:
  953. parser.error('--git_repo option is supported only for GIT repositories')
  954. # If revision==auto, resolve it
  955. if options.revision and options.revision.lower() == 'auto':
  956. if not vcs_is_git:
  957. parser.error('--revision=auto is supported only for GIT repositories')
  958. options.revision = scm.GIT.Capture(
  959. ['rev-parse', current_vcs.diff_against],
  960. cwd=path)
  961. checkouts = [current_vcs]
  962. for item in options.sub_rep:
  963. # Pass file_list=None because we don't know the sub repo's file list.
  964. checkout = GuessVCS(options,
  965. os.path.join(current_vcs.checkout_root, item),
  966. None)
  967. if checkout.checkout_root in [c.checkout_root for c in checkouts]:
  968. parser.error('Specified the root %s two times.' %
  969. checkout.checkout_root)
  970. checkouts.append(checkout)
  971. can_http = options.port and options.host
  972. can_svn = options.svn_repo
  973. can_git = options.git_repo
  974. can_gerrit = options.gerrit_url
  975. can_something = can_http or can_svn or can_git or can_gerrit
  976. # If there was no transport selected yet, now we must have enough data to
  977. # select one.
  978. if not options.send_patch and not can_something:
  979. parser.error('Please specify an access method.')
  980. # Convert options.diff into the content of the diff.
  981. if options.url:
  982. if options.files:
  983. parser.error('You cannot specify files and --url at the same time.')
  984. options.diff = urllib2.urlopen(options.url).read()
  985. elif options.diff:
  986. if options.files:
  987. parser.error('You cannot specify files and --diff at the same time.')
  988. options.diff = gclient_utils.FileRead(options.diff, 'rb')
  989. elif options.issue and options.patchset is None:
  990. # Retrieve the patch from rietveld when the diff is not specified.
  991. # When patchset is specified, it's because it's done by gcl/git-try.
  992. api_url = '%s/api/%d' % (options.rietveld_url, options.issue)
  993. logging.debug(api_url)
  994. contents = json.loads(urllib2.urlopen(api_url).read())
  995. options.patchset = contents['patchsets'][-1]
  996. diff_url = ('%s/download/issue%d_%d.diff' %
  997. (options.rietveld_url, options.issue, options.patchset))
  998. diff = GetMungedDiff('', urllib2.urlopen(diff_url).readlines())
  999. options.diff = ''.join(diff[0])
  1000. changed_files = diff[1]
  1001. else:
  1002. # Use this as the base.
  1003. root = checkouts[0].checkout_root
  1004. diffs = []
  1005. for checkout in checkouts:
  1006. raw_diff = checkout.GenerateDiff()
  1007. if not raw_diff:
  1008. continue
  1009. diff = raw_diff.splitlines(True)
  1010. path_diff = gclient_utils.PathDifference(root, checkout.checkout_root)
  1011. # Munge it.
  1012. diffs.extend(GetMungedDiff(path_diff, diff)[0])
  1013. if not diffs:
  1014. logging.error('Empty or non-existant diff, exiting.')
  1015. return 1
  1016. options.diff = ''.join(diffs)
  1017. if not options.name:
  1018. if options.issue:
  1019. options.name = 'Issue %s' % options.issue
  1020. else:
  1021. options.name = 'Unnamed'
  1022. print('Note: use --name NAME to change the try job name.')
  1023. if not options.email:
  1024. parser.error('Using an anonymous checkout. Please use --email or set '
  1025. 'the TRYBOT_RESULTS_EMAIL_ADDRESS environment variable.')
  1026. print('Results will be emailed to: ' + options.email)
  1027. if options.bot:
  1028. bot_spec = _ApplyTestFilter(
  1029. options.testfilter, _ParseBotList(options.bot, options.testfilter))
  1030. else:
  1031. bot_spec = _GenTSBotSpec(checkouts, change, changed_files, options)
  1032. if options.testfilter:
  1033. bot_spec = _ApplyTestFilter(options.testfilter, bot_spec)
  1034. if any('triggered' in b[0] for b in bot_spec):
  1035. print >> sys.stderr, (
  1036. 'ERROR You are trying to send a job to a triggered bot. This type of'
  1037. ' bot requires an\ninitial job from a parent (usually a builder). '
  1038. 'Instead send your job to the parent.\nBot list: %s' % bot_spec)
  1039. return 1
  1040. if options.print_bots:
  1041. print 'Bots which would be used:'
  1042. for bot in bot_spec:
  1043. if bot[1]:
  1044. print ' %s:%s' % (bot[0], ','.join(bot[1]))
  1045. else:
  1046. print ' %s' % (bot[0])
  1047. return 0
  1048. # Determine sending protocol
  1049. if options.send_patch:
  1050. # If forced.
  1051. senders = [options.send_patch]
  1052. else:
  1053. # Try sending patch using avaialble protocols
  1054. all_senders = [
  1055. (_SendChangeHTTP, can_http),
  1056. (_SendChangeSVN, can_svn),
  1057. (_SendChangeGerrit, can_gerrit),
  1058. (_SendChangeGit, can_git),
  1059. ]
  1060. senders = [sender for sender, can in all_senders if can]
  1061. # Send the patch.
  1062. for sender in senders:
  1063. try:
  1064. sender(bot_spec, options)
  1065. return 0
  1066. except NoTryServerAccess:
  1067. is_last = sender == senders[-1]
  1068. if is_last:
  1069. raise
  1070. assert False, "Unreachable code"
  1071. except Error, e:
  1072. if swallow_exception:
  1073. return 1
  1074. print >> sys.stderr, e
  1075. return 1
  1076. except (gclient_utils.Error, subprocess2.CalledProcessError), e:
  1077. print >> sys.stderr, e
  1078. return 1
  1079. return 0
  1080. if __name__ == "__main__":
  1081. fix_encoding.fix_encoding()
  1082. sys.exit(TryChange(None, None, False))