borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 1 | #!/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 | """ |
| 9 | submit_try: Submit a try request. |
| 10 | |
| 11 | This is a thin wrapper around the try request utilities in depot_tools which |
| 12 | adds some validation and supports both git and svn. |
| 13 | """ |
| 14 | |
| 15 | |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 16 | import httplib |
| 17 | import json |
| 18 | import os |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 19 | import re |
borenet@google.com | d4ba6e7 | 2014-01-08 21:00:30 +0000 | [diff] [blame^] | 20 | import shutil |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 21 | import subprocess |
borenet@google.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 22 | import svn |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 23 | import sys |
borenet@google.com | d4ba6e7 | 2014-01-08 21:00:30 +0000 | [diff] [blame^] | 24 | import tempfile |
borenet@google.com | fe7533e | 2013-03-11 20:09:40 +0000 | [diff] [blame] | 25 | |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 26 | import buildbot_globals |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | # Alias which can be used to run a try on every builder. |
| 30 | ALL_BUILDERS = 'all' |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 31 | # Alias which can be used to run a try on all compile builders. |
| 32 | COMPILE_BUILDERS = 'compile' |
| 33 | # Alias which can be used to run a try on all builders that are run in the CQ. |
| 34 | CQ_BUILDERS = 'cq' |
| 35 | # Alias which can be used to specify a regex to choose builders. |
| 36 | REGEX = 'regex' |
| 37 | |
| 38 | ALL_ALIASES = [ALL_BUILDERS, COMPILE_BUILDERS, CQ_BUILDERS, REGEX] |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 39 | |
| 40 | # Contact information for the build master. |
epoger@google.com | c192aa4 | 2013-08-21 17:35:59 +0000 | [diff] [blame] | 41 | SKIA_BUILD_MASTER_HOST = str(buildbot_globals.Get('public_master_host')) |
| 42 | SKIA_BUILD_MASTER_PORT = str(buildbot_globals.Get('public_external_port')) |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 43 | |
| 44 | # All try builders have this suffix. |
borenet@google.com | 99da601 | 2013-05-02 12:14:35 +0000 | [diff] [blame] | 45 | TRYBOT_SUFFIX = '-Trybot' |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 46 | |
| 47 | # Location of the codereview.settings file in the Skia repo. |
| 48 | SKIA_URL = 'skia.googlecode.com' |
| 49 | CODEREVIEW_SETTINGS = '/svn/codereview.settings' |
| 50 | |
| 51 | # String for matching the svn url of the try server inside codereview.settings. |
| 52 | TRYSERVER_SVN_URL = 'TRYSERVER_SVN_URL: ' |
| 53 | |
| 54 | # Strings used for matching svn config properties. |
borenet@google.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 55 | URL_STR = 'URL' |
| 56 | REPO_ROOT_STR = 'Repository Root' |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def 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.com | 6c55b51 | 2013-01-24 21:38:51 +0000 | [diff] [blame] | 66 | gcl = proc.communicate()[0].split('\n')[0].rstrip() |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 67 | depot_tools_dir = os.path.dirname(gcl) |
| 68 | return depot_tools_dir |
| 69 | |
| 70 | |
| 71 | def 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.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 78 | 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.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 82 | 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 | |
| 96 | def 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.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 109 | def RetrieveTrybotList(json_filename): |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 110 | """ 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.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 114 | SKIA_BUILD_MASTER_PORT) |
| 115 | connection.request('GET', '/json/%s' % json_filename) |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 116 | 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.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 125 | def ValidateArgs(argv, trybots, is_svn=True): |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 126 | """ 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.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 131 | |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 132 | 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. |
| 153 | submit_try %s--bot <buildername> [<buildername> ...] |
| 154 | |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 155 | -b, --bot Builder(s) or Alias on which to run the try. Required. |
| 156 | Allowed aliases: %s |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 157 | -h, --help Show this message. |
| 158 | -r <revision#> Revision from which to run the try. |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 159 | -l, --list_bots List the available try builders and aliases and exit. |
| 160 | """ % ('<changelist> ' if is_svn else '', ALL_ALIASES)) |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 161 | |
| 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.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 177 | format_args = ['\n '.join(sorted(trybots))] + ALL_ALIASES |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 178 | print ( |
| 179 | """ |
| 180 | submit_try: Available builders:\n %s |
| 181 | |
| 182 | Can 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.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 189 | sys.exit(0) |
borenet@google.com | 02009c7 | 2013-04-01 17:59:16 +0000 | [diff] [blame] | 190 | elif arg == '-b' or arg == '--bot': |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 191 | 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.com | 50d6862 | 2013-04-03 18:35:35 +0000 | [diff] [blame] | 197 | for bot in argv.pop(0).split(','): |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 198 | if bot in ALL_ALIASES: |
epoger@google.com | 50d6862 | 2013-04-03 18:35:35 +0000 | [diff] [blame] | 199 | if using_bots: |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 200 | Error('Cannot specify "%s" with additional builder names or ' |
| 201 | 'aliases.' % bot) |
| 202 | if bot == ALL_BUILDERS: |
borenet@google.com | 6fb7756 | 2013-07-12 18:11:04 +0000 | [diff] [blame] | 203 | 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.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 211 | elif bot == COMPILE_BUILDERS: |
borenet@google.com | 6fb7756 | 2013-07-12 18:11:04 +0000 | [diff] [blame] | 212 | using_bots = [t for t in trybots if t.startswith('Build')] |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 213 | 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.com | 50d6862 | 2013-04-03 18:35:35 +0000 | [diff] [blame] | 224 | break |
| 225 | else: |
| 226 | if not bot in trybots: |
| 227 | Error('Unrecognized builder: %s' % bot) |
| 228 | using_bots.append(bot) |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 229 | 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.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 243 | |
| 244 | |
| 245 | def 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.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 259 | botlist = ','.join(['%s%s' % (bot, TRYBOT_SUFFIX) for bot in args.bots]) |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 260 | if is_svn: |
borenet@google.com | 6c55b51 | 2013-01-24 21:38:51 +0000 | [diff] [blame] | 261 | 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.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 264 | '--bot', botlist] |
| 265 | if args.revision: |
| 266 | try_args.extend(['-r', args.revision]) |
borenet@google.com | 6c55b51 | 2013-01-24 21:38:51 +0000 | [diff] [blame] | 267 | 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.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 274 | else: |
borenet@google.com | d4ba6e7 | 2014-01-08 21:00:30 +0000 | [diff] [blame^] | 275 | # 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.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 303 | |
| 304 | |
| 305 | def main(): |
| 306 | # Retrieve the list of active try builders from the build master. |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 307 | trybots = RetrieveTrybotList(json_filename='trybots') |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 308 | |
| 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.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 313 | args = ValidateArgs(sys.argv[1:], trybots=trybots, is_svn=is_svn) |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 314 | |
| 315 | # Submit the try request. |
| 316 | SubmitTryRequest(args, is_svn=is_svn) |
| 317 | |
| 318 | |
| 319 | if __name__ == '__main__': |
epoger@google.com | 50d6862 | 2013-04-03 18:35:35 +0000 | [diff] [blame] | 320 | sys.exit(main()) |