blob: 81c5dd00ee90253045cd82a6ec89915568c0069c [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
40# Contact information for the build master.
epoger@google.comc192aa42013-08-21 17:35:59 +000041SKIA_BUILD_MASTER_HOST = str(buildbot_globals.Get('public_master_host'))
42SKIA_BUILD_MASTER_PORT = str(buildbot_globals.Get('public_external_port'))
borenet@google.com6b5388e2013-01-23 20:54:29 +000043
44# All try builders have this suffix.
borenet@google.com99da6012013-05-02 12:14:35 +000045TRYBOT_SUFFIX = '-Trybot'
borenet@google.com6b5388e2013-01-23 20:54:29 +000046
47# Location of the codereview.settings file in the Skia repo.
48SKIA_URL = 'skia.googlecode.com'
49CODEREVIEW_SETTINGS = '/svn/codereview.settings'
50
51# 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
71def GetCheckoutRoot(is_svn=True):
72 """ Determine where the local checkout is rooted.
73
74 is_svn: boolean; whether we're in an SVN checkout. If False, assume we're in
75 a git checkout.
76 """
77 if is_svn:
borenet@google.coma74302d2013-03-18 18:18:26 +000078 repo = svn.Svn(os.curdir)
79 svn_info = repo.GetInfo()
80 url = svn_info.get(URL_STR, None)
81 repo_root = svn_info.get(REPO_ROOT_STR, None)
borenet@google.com6b5388e2013-01-23 20:54:29 +000082 if not url or not repo_root:
83 raise Exception('Couldn\'t find checkout root!')
84 if url == repo_root:
85 return 'svn'
86 return url[len(repo_root)+1:]
87 else:
88 cmd = ['git', 'rev-parse', '--show-toplevel']
89 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
90 stderr=subprocess.STDOUT)
91 if proc.wait() != 0:
92 raise Exception('Couldn\'t find checkout root!')
93 return os.path.basename(proc.communicate()[0])
94
95
96def GetTryRepo():
97 """ Determine the TRYSERVER_SVN_URL from the codereview.settings file in the
98 Skia repo. """
99 connection = httplib.HTTPConnection(SKIA_URL)
100 connection.request('GET', CODEREVIEW_SETTINGS)
101 content = connection.getresponse().read()
102 for line in content.split('\n'):
103 if line.startswith(TRYSERVER_SVN_URL):
104 return line[len(TRYSERVER_SVN_URL):].rstrip()
105 raise Exception('Couldn\'t determine the TRYSERVER_SVN_URL. Make sure it is '
106 'defined in the %s file.' % CODEREVIEW_SETTINGS)
107
108
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000109def RetrieveTrybotList(json_filename):
borenet@google.com6b5388e2013-01-23 20:54:29 +0000110 """ Retrieve the list of known trybots from the build master, stripping
111 TRYBOT_SUFFIX from the name. """
112 trybots = []
113 connection = httplib.HTTPConnection(SKIA_BUILD_MASTER_HOST,
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000114 SKIA_BUILD_MASTER_PORT)
115 connection.request('GET', '/json/%s' % json_filename)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000116 response = connection.getresponse()
117 builders = json.load(response)
118
119 for builder in builders:
120 if builder.endswith(TRYBOT_SUFFIX):
121 trybots.append(builder[:-len(TRYBOT_SUFFIX)])
122 return trybots
123
124
borenet@google.coma5d621f2013-01-25 20:55:35 +0000125def ValidateArgs(argv, trybots, is_svn=True):
borenet@google.com6b5388e2013-01-23 20:54:29 +0000126 """ Parse and validate command-line arguments. If the arguments are valid,
127 returns a tuple of (<changelist name>, <list of trybots>).
128
129 trybots: A list of the known try builders.
130 """
borenet@google.com6b5388e2013-01-23 20:54:29 +0000131
borenet@google.coma5d621f2013-01-25 20:55:35 +0000132 class CollectedArgs(object):
133 def __init__(self, bots, changelist, revision):
134 self._bots = bots
135 self._changelist = changelist
136 self._revision = revision
137
138 @property
139 def bots(self):
140 for bot in self._bots:
141 yield bot
142
143 @property
144 def changelist(self):
145 return self._changelist
146
147 @property
148 def revision(self):
149 return self._revision
150
151 usage = (
152"""submit_try: Submit a try request.
153submit_try %s--bot <buildername> [<buildername> ...]
154
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000155-b, --bot Builder(s) or Alias on which to run the try. Required.
156 Allowed aliases: %s
borenet@google.coma5d621f2013-01-25 20:55:35 +0000157-h, --help Show this message.
158-r <revision#> Revision from which to run the try.
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000159-l, --list_bots List the available try builders and aliases and exit.
160""" % ('<changelist> ' if is_svn else '', ALL_ALIASES))
borenet@google.coma5d621f2013-01-25 20:55:35 +0000161
162 def Error(msg=None):
163 if msg:
164 print msg
165 print usage
166 sys.exit(1)
167
168 using_bots = None
169 changelist = None
170 revision = None
171
172 while argv:
173 arg = argv.pop(0)
174 if arg == '-h' or arg == '--help':
175 Error()
176 elif arg == '-l' or arg == '--list_bots':
borenet@google.com4b897fa2013-12-02 20:27:16 +0000177 format_args = ['\n '.join(sorted(trybots))] + ALL_ALIASES
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000178 print (
179"""
180submit_try: Available builders:\n %s
181
182Can also use the following aliases to run on groups of builders-
183 %s: Will run against all trybots.
184 %s: Will run against all compile trybots.
185 %s: Will run against the same trybots as the commit queue.
186 %s: You will be prompted to enter a regex to select builders with.
187
188""" % tuple(format_args))
borenet@google.coma5d621f2013-01-25 20:55:35 +0000189 sys.exit(0)
borenet@google.com02009c72013-04-01 17:59:16 +0000190 elif arg == '-b' or arg == '--bot':
borenet@google.coma5d621f2013-01-25 20:55:35 +0000191 if using_bots:
192 Error('--bot specified multiple times.')
193 if len(argv) < 1:
194 Error('You must specify a builder with "--bot".')
195 using_bots = []
196 while argv and not argv[0].startswith('-'):
epoger@google.com50d68622013-04-03 18:35:35 +0000197 for bot in argv.pop(0).split(','):
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000198 if bot in ALL_ALIASES:
epoger@google.com50d68622013-04-03 18:35:35 +0000199 if using_bots:
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000200 Error('Cannot specify "%s" with additional builder names or '
201 'aliases.' % bot)
202 if bot == ALL_BUILDERS:
borenet@google.com6fb77562013-07-12 18:11:04 +0000203 are_you_sure = raw_input('Running a try on every bot is very '
204 'expensive. You may be able to get '
205 'enough information by running on a '
206 'smaller set of bots. Are you sure you '
207 'want to run your try job on all of the '
208 'trybots? [y,n]: ')
209 if are_you_sure == 'y':
210 using_bots = trybots
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000211 elif bot == COMPILE_BUILDERS:
borenet@google.com6fb77562013-07-12 18:11:04 +0000212 using_bots = [t for t in trybots if t.startswith('Build')]
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000213 elif bot == CQ_BUILDERS:
214 using_bots = RetrieveTrybotList(json_filename='cqtrybots')
215 elif bot == REGEX:
216 while True:
217 regex = raw_input("Enter your trybot regex: ")
218 p = re.compile(regex)
219 using_bots = [t for t in trybots if p.match(t)]
220 print '\n\nTrybots that match your regex:\n%s\n\n' % '\n'.join(
221 using_bots)
222 if raw_input('Re-enter regex? [y,n]: ') == 'n':
223 break
epoger@google.com50d68622013-04-03 18:35:35 +0000224 break
225 else:
226 if not bot in trybots:
227 Error('Unrecognized builder: %s' % bot)
228 using_bots.append(bot)
borenet@google.coma5d621f2013-01-25 20:55:35 +0000229 elif arg == '-r':
230 if len(argv) < 1:
231 Error('You must specify a revision with "-r".')
232 revision = argv.pop(0)
233 else:
234 if changelist or not is_svn:
235 Error('Unknown argument: %s' % arg)
236 changelist = arg
237 if is_svn and not changelist:
238 Error('You must specify a changelist name.')
239 if not using_bots:
240 Error('You must specify one or more builders using --bot.')
241 return CollectedArgs(bots=using_bots, changelist=changelist,
242 revision=revision)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000243
244
245def SubmitTryRequest(args, is_svn=True):
246 """ Submits a try request for the given changelist on the given list of
247 trybots.
248
249 args: Object whose properties are derived from command-line arguments. If
250 is_svn is True, it should contain:
251 - changelist: string; the name of the changelist to try.
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 If is_svn is False, it should contain:
255 - bot: list of strings; the names of the try builders to run.
256 - revision: optional, int; the revision number from which to run the try.
257 is_svn: boolean; are we in an SVN repo?
258 """
borenet@google.coma5d621f2013-01-25 20:55:35 +0000259 botlist = ','.join(['%s%s' % (bot, TRYBOT_SUFFIX) for bot in args.bots])
borenet@google.com6b5388e2013-01-23 20:54:29 +0000260 if is_svn:
borenet@google.com6c55b512013-01-24 21:38:51 +0000261 gcl_cmd = 'gcl.bat' if os.name == 'nt' else 'gcl'
262 try_args = [gcl_cmd, 'try', args.changelist,
263 '--root', GetCheckoutRoot(is_svn),
borenet@google.com6b5388e2013-01-23 20:54:29 +0000264 '--bot', botlist]
265 if args.revision:
266 try_args.extend(['-r', args.revision])
borenet@google.com6c55b512013-01-24 21:38:51 +0000267 print ' '.join(try_args)
268 proc = subprocess.Popen(try_args, stdout=subprocess.PIPE,
269 stderr=subprocess.STDOUT)
270 if proc.wait() != 0:
271 raise Exception('Failed to submit try request: %s' % (
272 proc.communicate()[0]))
273 print proc.communicate()[0]
borenet@google.com6b5388e2013-01-23 20:54:29 +0000274 else:
borenet@google.comd4ba6e72014-01-08 21:00:30 +0000275 # Create the diff file.
276 cmd = ['git.bat' if os.name == 'nt' else 'git', 'diff', 'origin/master']
277 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
278 if proc.wait() != 0:
279 raise Exception('Failed to capture git diff!')
280
281 temp_dir = tempfile.mkdtemp()
282 try:
283 diff_file = os.path.join(temp_dir, 'patch.diff')
284 with open(diff_file, 'wb') as f:
285 f.write(proc.communicate()[0])
286
287 # Find depot_tools. This is needed to import trychange.
288 sys.path.append(FindDepotTools())
289 import trychange
290 try_args = ['--use_svn',
291 '--svn_repo', GetTryRepo(),
292 '--root', GetCheckoutRoot(is_svn),
293 '--bot', botlist,
294 '--diff', diff_file,
295 ]
296 if args.revision:
297 try_args.extend(['-r', args.revision])
298
299 # Submit the try request.
300 trychange.TryChange(try_args, None, False)
301 finally:
302 shutil.rmtree(temp_dir)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000303
304
305def main():
306 # Retrieve the list of active try builders from the build master.
rmistry@google.comf5c4fc82013-04-09 11:46:46 +0000307 trybots = RetrieveTrybotList(json_filename='trybots')
borenet@google.com6b5388e2013-01-23 20:54:29 +0000308
309 # Determine if we're in an SVN checkout.
310 is_svn = os.path.isdir('.svn')
311
312 # Parse and validate the command-line arguments.
borenet@google.coma5d621f2013-01-25 20:55:35 +0000313 args = ValidateArgs(sys.argv[1:], trybots=trybots, is_svn=is_svn)
borenet@google.com6b5388e2013-01-23 20:54:29 +0000314
315 # Submit the try request.
316 SubmitTryRequest(args, is_svn=is_svn)
317
318
319if __name__ == '__main__':
epoger@google.com50d68622013-04-03 18:35:35 +0000320 sys.exit(main())