blob: 969fdfcd5709bcc45b9545f90cd8a258b09a70e4 [file] [log] [blame]
borenet@google.com6b5388e2013-01-23 20:54:29 +00001#!/usr/bin/python
2
3# Copyright (c) 2013 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7
8"""
9submit_try: Submit a try request.
10
11This is a thin wrapper around the try request utilities in depot_tools which
12adds some validation and supports both git and svn.
13"""
14
15
borenet@google.com6b5388e2013-01-23 20:54:29 +000016import httplib
17import json
18import os
rmistry@google.comf5c4fc82013-04-09 11:46:46 +000019import re
borenet@google.comd4ba6e72014-01-08 21:00:30 +000020import shutil
borenet@google.com6b5388e2013-01-23 20:54:29 +000021import subprocess
borenet@google.coma74302d2013-03-18 18:18:26 +000022import svn
borenet@google.com6b5388e2013-01-23 20:54:29 +000023import sys
borenet@google.comd4ba6e72014-01-08 21:00:30 +000024import tempfile
borenet@google.comfe7533e2013-03-11 20:09:40 +000025
borenet2e81e512014-06-05 07:32:15 -070026import retrieve_from_googlesource
borenet@google.com6b5388e2013-01-23 20:54:29 +000027
28
29# Alias which can be used to run a try on every builder.
30ALL_BUILDERS = 'all'
rmistry@google.comf5c4fc82013-04-09 11:46:46 +000031# Alias which can be used to run a try on all compile builders.
32COMPILE_BUILDERS = 'compile'
33# Alias which can be used to run a try on all builders that are run in the CQ.
34CQ_BUILDERS = 'cq'
35# Alias which can be used to specify a regex to choose builders.
36REGEX = 'regex'
37
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +000038ALL_ALIASES = [ALL_BUILDERS, COMPILE_BUILDERS, REGEX, CQ_BUILDERS]
borenet@google.com6b5388e2013-01-23 20:54:29 +000039
borenet2e81e512014-06-05 07:32:15 -070040LARGE_NUMBER_OF_BOTS = 5
41
borenet@google.com1a5e83a2014-01-14 18:03:10 +000042GIT = 'git.bat' if os.name == 'nt' else 'git'
43
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +000044# URL of the slaves.cfg file in the Skia buildbot sources.
borenet2e81e512014-06-05 07:32:15 -070045SKIA_REPO = 'https://skia.googlesource.com/buildbot'
46SLAVES_CFG_PATH = 'master/slaves.cfg'
borenet@google.com6b5388e2013-01-23 20:54:29 +000047
48# All try builders have this suffix.
borenet@google.com99da6012013-05-02 12:14:35 +000049TRYBOT_SUFFIX = '-Trybot'
borenet@google.com6b5388e2013-01-23 20:54:29 +000050
borenet@google.com6b5388e2013-01-23 20:54:29 +000051# String for matching the svn url of the try server inside codereview.settings.
52TRYSERVER_SVN_URL = 'TRYSERVER_SVN_URL: '
53
54# Strings used for matching svn config properties.
borenet@google.coma74302d2013-03-18 18:18:26 +000055URL_STR = 'URL'
56REPO_ROOT_STR = 'Repository Root'
borenet@google.com6b5388e2013-01-23 20:54:29 +000057
58
59def FindDepotTools():
60 """ Find depot_tools on the local machine and return its location. """
61 which_cmd = 'where' if os.name == 'nt' else 'which'
62 cmd = [which_cmd, 'gcl']
63 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
64 if proc.wait() != 0:
65 raise Exception('Couldn\'t find depot_tools in PATH!')
borenet@google.com6c55b512013-01-24 21:38:51 +000066 gcl = proc.communicate()[0].split('\n')[0].rstrip()
borenet@google.com6b5388e2013-01-23 20:54:29 +000067 depot_tools_dir = os.path.dirname(gcl)
68 return depot_tools_dir
69
70
borenet2e81e512014-06-05 07:32:15 -070071def GetCheckoutRoot():
72 """ Determine where the local checkout is rooted."""
73 cmd = ['git', 'rev-parse', '--show-toplevel']
74 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
75 stderr=subprocess.STDOUT)
76 if proc.wait() != 0:
77 raise Exception('Couldn\'t find checkout root!')
78 return os.path.basename(proc.communicate()[0])
borenet@google.com6b5388e2013-01-23 20:54:29 +000079
80
81def GetTryRepo():
borenet@google.com6f0f5b42014-01-09 21:41:39 +000082 """Determine the TRYSERVER_SVN_URL from the codereview.settings file."""
83 codereview_settings_file = os.path.join(os.path.dirname(__file__), os.pardir,
84 'codereview.settings')
85 with open(codereview_settings_file) as f:
86 for line in f:
87 if line.startswith(TRYSERVER_SVN_URL):
88 return line[len(TRYSERVER_SVN_URL):].rstrip()
borenet@google.com6b5388e2013-01-23 20:54:29 +000089 raise Exception('Couldn\'t determine the TRYSERVER_SVN_URL. Make sure it is '
borenet@google.com6f0f5b42014-01-09 21:41:39 +000090 'defined in the %s file.' % codereview_settings_file)
borenet@google.com6b5388e2013-01-23 20:54:29 +000091
92
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +000093def RetrieveTrybotList():
94 """Retrieve the list of known trybots from the checked-in buildbot
95 configuration."""
96 # Retrieve the slaves.cfg file from the repository.
borenet2e81e512014-06-05 07:32:15 -070097 slaves_cfg_text = retrieve_from_googlesource.get(SKIA_REPO, SLAVES_CFG_PATH)
borenet@google.com6b5388e2013-01-23 20:54:29 +000098
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +000099 # Execute the slaves.cfg file to obtain the list of slaves.
100 vars = {}
101 exec(slaves_cfg_text, vars)
102 slaves_cfg = vars['slaves']
103
104 # Pull the list of known builders from the slaves list.
105 trybots = set()
106 for slave in slaves_cfg:
107 for builder in slave['builder']:
108 if not builder.endswith(TRYBOT_SUFFIX):
109 trybots.add(builder)
110
111 return list(trybots), vars['cq_trybots']
borenet@google.com6b5388e2013-01-23 20:54:29 +0000112
113
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +0000114def ValidateArgs(argv, trybots, cq_trybots, is_svn=True):
borenet@google.com6b5388e2013-01-23 20:54:29 +0000115 """ Parse and validate command-line arguments. If the arguments are valid,
116 returns a tuple of (<changelist name>, <list of trybots>).
117
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +0000118 trybots: list of strings; A list of the known try builders.
119 cq_trybots: list of strings; Trybots who get run by the commit queue.
120 is_svn: bool; whether or not we're in an svn checkout.
borenet@google.com6b5388e2013-01-23 20:54:29 +0000121 """
borenet@google.com6b5388e2013-01-23 20:54:29 +0000122
borenet@google.coma5d621f2013-01-25 20:55:35 +0000123 class CollectedArgs(object):
124 def __init__(self, bots, changelist, revision):
125 self._bots = bots
126 self._changelist = changelist
127 self._revision = revision
128
129 @property
130 def bots(self):
131 for bot in self._bots:
132 yield bot
133
134 @property
135 def changelist(self):
136 return self._changelist
137
138 @property
139 def revision(self):
140 return self._revision
141
142 usage = (
143"""submit_try: Submit a try request.
144submit_try %s--bot <buildername> [<buildername> ...]
145
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000146-b, --bot Builder(s) or Alias on which to run the try. Required.
147 Allowed aliases: %s
borenet@google.coma5d621f2013-01-25 20:55:35 +0000148-h, --help Show this message.
149-r <revision#> Revision from which to run the try.
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000150-l, --list_bots List the available try builders and aliases and exit.
151""" % ('<changelist> ' if is_svn else '', ALL_ALIASES))
borenet@google.coma5d621f2013-01-25 20:55:35 +0000152
153 def Error(msg=None):
154 if msg:
155 print msg
156 print usage
157 sys.exit(1)
158
159 using_bots = None
160 changelist = None
161 revision = None
162
163 while argv:
164 arg = argv.pop(0)
165 if arg == '-h' or arg == '--help':
166 Error()
167 elif arg == '-l' or arg == '--list_bots':
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +0000168 format_args = ['\n '.join(sorted(trybots))] + \
169 ALL_ALIASES + \
170 ['\n '.join(sorted(cq_trybots))]
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000171 print (
172"""
173submit_try: Available builders:\n %s
174
175Can also use the following aliases to run on groups of builders-
176 %s: Will run against all trybots.
177 %s: Will run against all compile trybots.
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000178 %s: You will be prompted to enter a regex to select builders with.
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +0000179 %s: Will run against the same trybots as the commit queue:\n %s
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000180
181""" % tuple(format_args))
borenet@google.coma5d621f2013-01-25 20:55:35 +0000182 sys.exit(0)
borenet@google.com02009c72013-04-01 17:59:16 +0000183 elif arg == '-b' or arg == '--bot':
borenet@google.coma5d621f2013-01-25 20:55:35 +0000184 if using_bots:
185 Error('--bot specified multiple times.')
186 if len(argv) < 1:
187 Error('You must specify a builder with "--bot".')
188 using_bots = []
189 while argv and not argv[0].startswith('-'):
epoger@google.com50d68622013-04-03 18:35:35 +0000190 for bot in argv.pop(0).split(','):
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000191 if bot in ALL_ALIASES:
epoger@google.com50d68622013-04-03 18:35:35 +0000192 if using_bots:
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000193 Error('Cannot specify "%s" with additional builder names or '
194 'aliases.' % bot)
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000195 elif bot == COMPILE_BUILDERS:
borenet@google.com6fb77562013-07-12 18:11:04 +0000196 using_bots = [t for t in trybots if t.startswith('Build')]
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000197 elif bot == CQ_BUILDERS:
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +0000198 using_bots = cq_trybots
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000199 elif bot == REGEX:
200 while True:
201 regex = raw_input("Enter your trybot regex: ")
202 p = re.compile(regex)
203 using_bots = [t for t in trybots if p.match(t)]
204 print '\n\nTrybots that match your regex:\n%s\n\n' % '\n'.join(
205 using_bots)
206 if raw_input('Re-enter regex? [y,n]: ') == 'n':
207 break
epoger@google.com50d68622013-04-03 18:35:35 +0000208 break
209 else:
210 if not bot in trybots:
211 Error('Unrecognized builder: %s' % bot)
212 using_bots.append(bot)
borenet@google.coma5d621f2013-01-25 20:55:35 +0000213 elif arg == '-r':
214 if len(argv) < 1:
215 Error('You must specify a revision with "-r".')
216 revision = argv.pop(0)
217 else:
218 if changelist or not is_svn:
219 Error('Unknown argument: %s' % arg)
220 changelist = arg
221 if is_svn and not changelist:
222 Error('You must specify a changelist name.')
223 if not using_bots:
224 Error('You must specify one or more builders using --bot.')
borenet2e81e512014-06-05 07:32:15 -0700225 if len(using_bots) > LARGE_NUMBER_OF_BOTS:
226 are_you_sure = raw_input('Running a try on a large number of bots is very '
227 'expensive. You may be able to get enough '
228 'information by running on a smaller set of bots. '
229 'Are you sure you want to do this? [y,n]: ')
230 if are_you_sure != 'y':
231 Error()
borenet@google.coma5d621f2013-01-25 20:55:35 +0000232 return CollectedArgs(bots=using_bots, changelist=changelist,
233 revision=revision)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000234
235
borenet2e81e512014-06-05 07:32:15 -0700236def SubmitTryRequest(trybots, revision=None):
237 """ Submits a try request on the given list of trybots.
borenet@google.com6b5388e2013-01-23 20:54:29 +0000238
borenet2e81e512014-06-05 07:32:15 -0700239 Args:
240 trybots: list of strings; the names of the try builders to run.
241 revision: optional string; the revision from which to run the try.
borenet@google.com6b5388e2013-01-23 20:54:29 +0000242 """
borenet2e81e512014-06-05 07:32:15 -0700243 botlist = ','.join(['%s%s' % (bot, TRYBOT_SUFFIX) for bot in trybots])
244 # Find depot_tools. This is needed to import git_cl and trychange.
245 sys.path.append(FindDepotTools())
246 import git_cl
247 import trychange
borenet@google.com1a5e83a2014-01-14 18:03:10 +0000248
borenet2e81e512014-06-05 07:32:15 -0700249 cmd = [GIT, 'diff', git_cl.Changelist().GetUpstreamBranch(),
250 '--no-ext-diff']
251 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
252 git_data = proc.communicate()
253 if git_data[0] is None:
254 raise Exception('Failed to capture git diff!')
borenet@google.comd4ba6e72014-01-08 21:00:30 +0000255
borenet2e81e512014-06-05 07:32:15 -0700256 temp_dir = tempfile.mkdtemp()
257 try:
258 diff_file = os.path.join(temp_dir, 'patch.diff')
259 with open(diff_file, 'wb') as f:
260 f.write(git_data[0])
261 f.close()
commit-bot@chromium.org240196d2014-01-16 19:48:19 +0000262
borenet2e81e512014-06-05 07:32:15 -0700263 try_args = ['--use_svn',
264 '--svn_repo', GetTryRepo(),
265 '--root', GetCheckoutRoot(),
266 '--bot', botlist,
267 '--diff', diff_file,
268 ]
269 if revision:
270 try_args.extend(['-r', revision])
borenet@google.comd4ba6e72014-01-08 21:00:30 +0000271
borenet2e81e512014-06-05 07:32:15 -0700272 # Submit the try request.
273 trychange.TryChange(try_args, None, False)
274 finally:
275 shutil.rmtree(temp_dir)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000276
277
278def main():
279 # Retrieve the list of active try builders from the build master.
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +0000280 trybots, cq_trybots = RetrieveTrybotList()
borenet@google.com6b5388e2013-01-23 20:54:29 +0000281
282 # Determine if we're in an SVN checkout.
283 is_svn = os.path.isdir('.svn')
284
285 # Parse and validate the command-line arguments.
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +0000286 args = ValidateArgs(sys.argv[1:], trybots=trybots, cq_trybots=cq_trybots,
287 is_svn=is_svn)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000288
289 # Submit the try request.
borenet2e81e512014-06-05 07:32:15 -0700290 SubmitTryRequest(args.bots, args.revision)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000291
292
293if __name__ == '__main__':
epoger@google.com50d68622013-04-03 18:35:35 +0000294 sys.exit(main())