blob: a2d46fc14fd3aa6e0e2952e37a47fbeee4820f86 [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
epoger@google.comfd040112013-08-20 16:21:55 +000026import buildbot_globals
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
38ALL_ALIASES = [ALL_BUILDERS, COMPILE_BUILDERS, CQ_BUILDERS, REGEX]
borenet@google.com6b5388e2013-01-23 20:54:29 +000039
borenet@google.com1a5e83a2014-01-14 18:03:10 +000040GIT = 'git.bat' if os.name == 'nt' else 'git'
41
borenet@google.com6b5388e2013-01-23 20:54:29 +000042# Contact information for the build master.
epoger@google.comc192aa42013-08-21 17:35:59 +000043SKIA_BUILD_MASTER_HOST = str(buildbot_globals.Get('public_master_host'))
44SKIA_BUILD_MASTER_PORT = str(buildbot_globals.Get('public_external_port'))
borenet@google.com6b5388e2013-01-23 20:54:29 +000045
46# All try builders have this suffix.
borenet@google.com99da6012013-05-02 12:14:35 +000047TRYBOT_SUFFIX = '-Trybot'
borenet@google.com6b5388e2013-01-23 20:54:29 +000048
borenet@google.com6b5388e2013-01-23 20:54:29 +000049# String for matching the svn url of the try server inside codereview.settings.
50TRYSERVER_SVN_URL = 'TRYSERVER_SVN_URL: '
51
52# Strings used for matching svn config properties.
borenet@google.coma74302d2013-03-18 18:18:26 +000053URL_STR = 'URL'
54REPO_ROOT_STR = 'Repository Root'
borenet@google.com6b5388e2013-01-23 20:54:29 +000055
56
57def FindDepotTools():
58 """ Find depot_tools on the local machine and return its location. """
59 which_cmd = 'where' if os.name == 'nt' else 'which'
60 cmd = [which_cmd, 'gcl']
61 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
62 if proc.wait() != 0:
63 raise Exception('Couldn\'t find depot_tools in PATH!')
borenet@google.com6c55b512013-01-24 21:38:51 +000064 gcl = proc.communicate()[0].split('\n')[0].rstrip()
borenet@google.com6b5388e2013-01-23 20:54:29 +000065 depot_tools_dir = os.path.dirname(gcl)
66 return depot_tools_dir
67
68
69def GetCheckoutRoot(is_svn=True):
70 """ Determine where the local checkout is rooted.
71
72 is_svn: boolean; whether we're in an SVN checkout. If False, assume we're in
73 a git checkout.
74 """
75 if is_svn:
borenet@google.coma74302d2013-03-18 18:18:26 +000076 repo = svn.Svn(os.curdir)
77 svn_info = repo.GetInfo()
78 url = svn_info.get(URL_STR, None)
79 repo_root = svn_info.get(REPO_ROOT_STR, None)
borenet@google.com6b5388e2013-01-23 20:54:29 +000080 if not url or not repo_root:
81 raise Exception('Couldn\'t find checkout root!')
82 if url == repo_root:
83 return 'svn'
84 return url[len(repo_root)+1:]
85 else:
86 cmd = ['git', 'rev-parse', '--show-toplevel']
87 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
88 stderr=subprocess.STDOUT)
89 if proc.wait() != 0:
90 raise Exception('Couldn\'t find checkout root!')
91 return os.path.basename(proc.communicate()[0])
92
93
94def GetTryRepo():
borenet@google.com6f0f5b42014-01-09 21:41:39 +000095 """Determine the TRYSERVER_SVN_URL from the codereview.settings file."""
96 codereview_settings_file = os.path.join(os.path.dirname(__file__), os.pardir,
97 'codereview.settings')
98 with open(codereview_settings_file) as f:
99 for line in f:
100 if line.startswith(TRYSERVER_SVN_URL):
101 return line[len(TRYSERVER_SVN_URL):].rstrip()
borenet@google.com6b5388e2013-01-23 20:54:29 +0000102 raise Exception('Couldn\'t determine the TRYSERVER_SVN_URL. Make sure it is '
borenet@google.com6f0f5b42014-01-09 21:41:39 +0000103 'defined in the %s file.' % codereview_settings_file)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000104
105
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000106def RetrieveTrybotList(json_filename):
borenet@google.com6b5388e2013-01-23 20:54:29 +0000107 """ Retrieve the list of known trybots from the build master, stripping
108 TRYBOT_SUFFIX from the name. """
109 trybots = []
110 connection = httplib.HTTPConnection(SKIA_BUILD_MASTER_HOST,
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000111 SKIA_BUILD_MASTER_PORT)
112 connection.request('GET', '/json/%s' % json_filename)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000113 response = connection.getresponse()
114 builders = json.load(response)
115
116 for builder in builders:
117 if builder.endswith(TRYBOT_SUFFIX):
118 trybots.append(builder[:-len(TRYBOT_SUFFIX)])
119 return trybots
120
121
borenet@google.coma5d621f2013-01-25 20:55:35 +0000122def ValidateArgs(argv, trybots, is_svn=True):
borenet@google.com6b5388e2013-01-23 20:54:29 +0000123 """ Parse and validate command-line arguments. If the arguments are valid,
124 returns a tuple of (<changelist name>, <list of trybots>).
125
126 trybots: A list of the known try builders.
127 """
borenet@google.com6b5388e2013-01-23 20:54:29 +0000128
borenet@google.coma5d621f2013-01-25 20:55:35 +0000129 class CollectedArgs(object):
130 def __init__(self, bots, changelist, revision):
131 self._bots = bots
132 self._changelist = changelist
133 self._revision = revision
134
135 @property
136 def bots(self):
137 for bot in self._bots:
138 yield bot
139
140 @property
141 def changelist(self):
142 return self._changelist
143
144 @property
145 def revision(self):
146 return self._revision
147
148 usage = (
149"""submit_try: Submit a try request.
150submit_try %s--bot <buildername> [<buildername> ...]
151
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000152-b, --bot Builder(s) or Alias on which to run the try. Required.
153 Allowed aliases: %s
borenet@google.coma5d621f2013-01-25 20:55:35 +0000154-h, --help Show this message.
155-r <revision#> Revision from which to run the try.
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000156-l, --list_bots List the available try builders and aliases and exit.
157""" % ('<changelist> ' if is_svn else '', ALL_ALIASES))
borenet@google.coma5d621f2013-01-25 20:55:35 +0000158
159 def Error(msg=None):
160 if msg:
161 print msg
162 print usage
163 sys.exit(1)
164
165 using_bots = None
166 changelist = None
167 revision = None
168
169 while argv:
170 arg = argv.pop(0)
171 if arg == '-h' or arg == '--help':
172 Error()
173 elif arg == '-l' or arg == '--list_bots':
borenet@google.com4b897fa2013-12-02 20:27:16 +0000174 format_args = ['\n '.join(sorted(trybots))] + ALL_ALIASES
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000175 print (
176"""
177submit_try: Available builders:\n %s
178
179Can also use the following aliases to run on groups of builders-
180 %s: Will run against all trybots.
181 %s: Will run against all compile trybots.
182 %s: Will run against the same trybots as the commit queue.
183 %s: You will be prompted to enter a regex to select builders with.
184
185""" % tuple(format_args))
borenet@google.coma5d621f2013-01-25 20:55:35 +0000186 sys.exit(0)
borenet@google.com02009c72013-04-01 17:59:16 +0000187 elif arg == '-b' or arg == '--bot':
borenet@google.coma5d621f2013-01-25 20:55:35 +0000188 if using_bots:
189 Error('--bot specified multiple times.')
190 if len(argv) < 1:
191 Error('You must specify a builder with "--bot".')
192 using_bots = []
193 while argv and not argv[0].startswith('-'):
epoger@google.com50d68622013-04-03 18:35:35 +0000194 for bot in argv.pop(0).split(','):
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000195 if bot in ALL_ALIASES:
epoger@google.com50d68622013-04-03 18:35:35 +0000196 if using_bots:
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000197 Error('Cannot specify "%s" with additional builder names or '
198 'aliases.' % bot)
199 if bot == ALL_BUILDERS:
borenet@google.com6fb77562013-07-12 18:11:04 +0000200 are_you_sure = raw_input('Running a try on every bot is very '
201 'expensive. You may be able to get '
202 'enough information by running on a '
203 'smaller set of bots. Are you sure you '
204 'want to run your try job on all of the '
205 'trybots? [y,n]: ')
206 if are_you_sure == 'y':
207 using_bots = trybots
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000208 elif bot == COMPILE_BUILDERS:
borenet@google.com6fb77562013-07-12 18:11:04 +0000209 using_bots = [t for t in trybots if t.startswith('Build')]
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000210 elif bot == CQ_BUILDERS:
211 using_bots = RetrieveTrybotList(json_filename='cqtrybots')
212 elif bot == REGEX:
213 while True:
214 regex = raw_input("Enter your trybot regex: ")
215 p = re.compile(regex)
216 using_bots = [t for t in trybots if p.match(t)]
217 print '\n\nTrybots that match your regex:\n%s\n\n' % '\n'.join(
218 using_bots)
219 if raw_input('Re-enter regex? [y,n]: ') == 'n':
220 break
epoger@google.com50d68622013-04-03 18:35:35 +0000221 break
222 else:
223 if not bot in trybots:
224 Error('Unrecognized builder: %s' % bot)
225 using_bots.append(bot)
borenet@google.coma5d621f2013-01-25 20:55:35 +0000226 elif arg == '-r':
227 if len(argv) < 1:
228 Error('You must specify a revision with "-r".')
229 revision = argv.pop(0)
230 else:
231 if changelist or not is_svn:
232 Error('Unknown argument: %s' % arg)
233 changelist = arg
234 if is_svn and not changelist:
235 Error('You must specify a changelist name.')
236 if not using_bots:
237 Error('You must specify one or more builders using --bot.')
238 return CollectedArgs(bots=using_bots, changelist=changelist,
239 revision=revision)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000240
241
242def SubmitTryRequest(args, is_svn=True):
243 """ Submits a try request for the given changelist on the given list of
244 trybots.
245
246 args: Object whose properties are derived from command-line arguments. If
247 is_svn is True, it should contain:
248 - changelist: string; the name of the changelist to try.
249 - bot: list of strings; the names of the try builders to run.
250 - revision: optional, int; the revision number from which to run the try.
251 If is_svn is False, it should contain:
252 - bot: list of strings; the names of the try builders to run.
253 - revision: optional, int; the revision number from which to run the try.
254 is_svn: boolean; are we in an SVN repo?
255 """
borenet@google.coma5d621f2013-01-25 20:55:35 +0000256 botlist = ','.join(['%s%s' % (bot, TRYBOT_SUFFIX) for bot in args.bots])
borenet@google.com6b5388e2013-01-23 20:54:29 +0000257 if is_svn:
borenet@google.com6c55b512013-01-24 21:38:51 +0000258 gcl_cmd = 'gcl.bat' if os.name == 'nt' else 'gcl'
259 try_args = [gcl_cmd, 'try', args.changelist,
260 '--root', GetCheckoutRoot(is_svn),
borenet@google.com6b5388e2013-01-23 20:54:29 +0000261 '--bot', botlist]
262 if args.revision:
263 try_args.extend(['-r', args.revision])
borenet@google.com6c55b512013-01-24 21:38:51 +0000264 print ' '.join(try_args)
265 proc = subprocess.Popen(try_args, stdout=subprocess.PIPE,
266 stderr=subprocess.STDOUT)
267 if proc.wait() != 0:
268 raise Exception('Failed to submit try request: %s' % (
269 proc.communicate()[0]))
270 print proc.communicate()[0]
borenet@google.com6b5388e2013-01-23 20:54:29 +0000271 else:
borenet@google.com1a5e83a2014-01-14 18:03:10 +0000272 # Find depot_tools. This is needed to import git_cl and trychange.
273 sys.path.append(FindDepotTools())
274 import git_cl
275 import trychange
276
277 cmd = [GIT, 'diff', git_cl.Changelist().GetUpstreamBranch(),
278 '--no-ext-diff']
borenet@google.comd4ba6e72014-01-08 21:00:30 +0000279 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
280 if proc.wait() != 0:
281 raise Exception('Failed to capture git diff!')
282
283 temp_dir = tempfile.mkdtemp()
284 try:
285 diff_file = os.path.join(temp_dir, 'patch.diff')
286 with open(diff_file, 'wb') as f:
287 f.write(proc.communicate()[0])
288
borenet@google.comd4ba6e72014-01-08 21:00:30 +0000289 try_args = ['--use_svn',
290 '--svn_repo', GetTryRepo(),
291 '--root', GetCheckoutRoot(is_svn),
292 '--bot', botlist,
293 '--diff', diff_file,
294 ]
295 if args.revision:
296 try_args.extend(['-r', args.revision])
297
298 # Submit the try request.
299 trychange.TryChange(try_args, None, False)
300 finally:
301 shutil.rmtree(temp_dir)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000302
303
304def main():
305 # Retrieve the list of active try builders from the build master.
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000306 trybots = RetrieveTrybotList(json_filename='trybots')
borenet@google.com6b5388e2013-01-23 20:54:29 +0000307
308 # Determine if we're in an SVN checkout.
309 is_svn = os.path.isdir('.svn')
310
311 # Parse and validate the command-line arguments.
borenet@google.coma5d621f2013-01-25 20:55:35 +0000312 args = ValidateArgs(sys.argv[1:], trybots=trybots, is_svn=is_svn)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000313
314 # Submit the try request.
315 SubmitTryRequest(args, is_svn=is_svn)
316
317
318if __name__ == '__main__':
epoger@google.com50d68622013-04-03 18:35:35 +0000319 sys.exit(main())