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 | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 20 | import subprocess |
borenet@google.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 21 | import svn |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 22 | import sys |
borenet@google.com | fe7533e | 2013-03-11 20:09:40 +0000 | [diff] [blame] | 23 | |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame^] | 24 | import buildbot_globals |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | # Alias which can be used to run a try on every builder. |
| 28 | ALL_BUILDERS = 'all' |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 29 | # Alias which can be used to run a try on all compile builders. |
| 30 | COMPILE_BUILDERS = 'compile' |
| 31 | # Alias which can be used to run a try on all builders that are run in the CQ. |
| 32 | CQ_BUILDERS = 'cq' |
| 33 | # Alias which can be used to specify a regex to choose builders. |
| 34 | REGEX = 'regex' |
| 35 | |
| 36 | ALL_ALIASES = [ALL_BUILDERS, COMPILE_BUILDERS, CQ_BUILDERS, REGEX] |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 37 | |
| 38 | # Contact information for the build master. |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame^] | 39 | SKIA_BUILD_MASTER_HOST = str(buildbot_globals.Get('master_host')) |
| 40 | SKIA_BUILD_MASTER_PORT = str(buildbot_globals.Get('external_port')) |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 41 | |
| 42 | # All try builders have this suffix. |
borenet@google.com | 99da601 | 2013-05-02 12:14:35 +0000 | [diff] [blame] | 43 | TRYBOT_SUFFIX = '-Trybot' |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 44 | |
| 45 | # Location of the codereview.settings file in the Skia repo. |
| 46 | SKIA_URL = 'skia.googlecode.com' |
| 47 | CODEREVIEW_SETTINGS = '/svn/codereview.settings' |
| 48 | |
| 49 | # String for matching the svn url of the try server inside codereview.settings. |
| 50 | TRYSERVER_SVN_URL = 'TRYSERVER_SVN_URL: ' |
| 51 | |
| 52 | # Strings used for matching svn config properties. |
borenet@google.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 53 | URL_STR = 'URL' |
| 54 | REPO_ROOT_STR = 'Repository Root' |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | def 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.com | 6c55b51 | 2013-01-24 21:38:51 +0000 | [diff] [blame] | 64 | gcl = proc.communicate()[0].split('\n')[0].rstrip() |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 65 | depot_tools_dir = os.path.dirname(gcl) |
| 66 | return depot_tools_dir |
| 67 | |
| 68 | |
| 69 | def 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.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 76 | 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.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 80 | 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 | |
| 94 | def GetTryRepo(): |
| 95 | """ Determine the TRYSERVER_SVN_URL from the codereview.settings file in the |
| 96 | Skia repo. """ |
| 97 | connection = httplib.HTTPConnection(SKIA_URL) |
| 98 | connection.request('GET', CODEREVIEW_SETTINGS) |
| 99 | content = connection.getresponse().read() |
| 100 | for line in content.split('\n'): |
| 101 | if line.startswith(TRYSERVER_SVN_URL): |
| 102 | return line[len(TRYSERVER_SVN_URL):].rstrip() |
| 103 | raise Exception('Couldn\'t determine the TRYSERVER_SVN_URL. Make sure it is ' |
| 104 | 'defined in the %s file.' % CODEREVIEW_SETTINGS) |
| 105 | |
| 106 | |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 107 | def RetrieveTrybotList(json_filename): |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 108 | """ Retrieve the list of known trybots from the build master, stripping |
| 109 | TRYBOT_SUFFIX from the name. """ |
| 110 | trybots = [] |
| 111 | connection = httplib.HTTPConnection(SKIA_BUILD_MASTER_HOST, |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 112 | SKIA_BUILD_MASTER_PORT) |
| 113 | connection.request('GET', '/json/%s' % json_filename) |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 114 | response = connection.getresponse() |
| 115 | builders = json.load(response) |
| 116 | |
| 117 | for builder in builders: |
| 118 | if builder.endswith(TRYBOT_SUFFIX): |
| 119 | trybots.append(builder[:-len(TRYBOT_SUFFIX)]) |
| 120 | return trybots |
| 121 | |
| 122 | |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 123 | def ValidateArgs(argv, trybots, is_svn=True): |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 124 | """ Parse and validate command-line arguments. If the arguments are valid, |
| 125 | returns a tuple of (<changelist name>, <list of trybots>). |
| 126 | |
| 127 | trybots: A list of the known try builders. |
| 128 | """ |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 129 | |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 130 | class CollectedArgs(object): |
| 131 | def __init__(self, bots, changelist, revision): |
| 132 | self._bots = bots |
| 133 | self._changelist = changelist |
| 134 | self._revision = revision |
| 135 | |
| 136 | @property |
| 137 | def bots(self): |
| 138 | for bot in self._bots: |
| 139 | yield bot |
| 140 | |
| 141 | @property |
| 142 | def changelist(self): |
| 143 | return self._changelist |
| 144 | |
| 145 | @property |
| 146 | def revision(self): |
| 147 | return self._revision |
| 148 | |
| 149 | usage = ( |
| 150 | """submit_try: Submit a try request. |
| 151 | submit_try %s--bot <buildername> [<buildername> ...] |
| 152 | |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 153 | -b, --bot Builder(s) or Alias on which to run the try. Required. |
| 154 | Allowed aliases: %s |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 155 | -h, --help Show this message. |
| 156 | -r <revision#> Revision from which to run the try. |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 157 | -l, --list_bots List the available try builders and aliases and exit. |
| 158 | """ % ('<changelist> ' if is_svn else '', ALL_ALIASES)) |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 159 | |
| 160 | def Error(msg=None): |
| 161 | if msg: |
| 162 | print msg |
| 163 | print usage |
| 164 | sys.exit(1) |
| 165 | |
| 166 | using_bots = None |
| 167 | changelist = None |
| 168 | revision = None |
| 169 | |
| 170 | while argv: |
| 171 | arg = argv.pop(0) |
| 172 | if arg == '-h' or arg == '--help': |
| 173 | Error() |
| 174 | elif arg == '-l' or arg == '--list_bots': |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 175 | format_args = ['\n '.join(trybots)] + ALL_ALIASES |
| 176 | print ( |
| 177 | """ |
| 178 | submit_try: Available builders:\n %s |
| 179 | |
| 180 | Can also use the following aliases to run on groups of builders- |
| 181 | %s: Will run against all trybots. |
| 182 | %s: Will run against all compile trybots. |
| 183 | %s: Will run against the same trybots as the commit queue. |
| 184 | %s: You will be prompted to enter a regex to select builders with. |
| 185 | |
| 186 | """ % tuple(format_args)) |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 187 | sys.exit(0) |
borenet@google.com | 02009c7 | 2013-04-01 17:59:16 +0000 | [diff] [blame] | 188 | elif arg == '-b' or arg == '--bot': |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 189 | if using_bots: |
| 190 | Error('--bot specified multiple times.') |
| 191 | if len(argv) < 1: |
| 192 | Error('You must specify a builder with "--bot".') |
| 193 | using_bots = [] |
| 194 | while argv and not argv[0].startswith('-'): |
epoger@google.com | 50d6862 | 2013-04-03 18:35:35 +0000 | [diff] [blame] | 195 | for bot in argv.pop(0).split(','): |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 196 | if bot in ALL_ALIASES: |
epoger@google.com | 50d6862 | 2013-04-03 18:35:35 +0000 | [diff] [blame] | 197 | if using_bots: |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 198 | Error('Cannot specify "%s" with additional builder names or ' |
| 199 | 'aliases.' % bot) |
| 200 | if bot == ALL_BUILDERS: |
borenet@google.com | 6fb7756 | 2013-07-12 18:11:04 +0000 | [diff] [blame] | 201 | are_you_sure = raw_input('Running a try on every bot is very ' |
| 202 | 'expensive. You may be able to get ' |
| 203 | 'enough information by running on a ' |
| 204 | 'smaller set of bots. Are you sure you ' |
| 205 | 'want to run your try job on all of the ' |
| 206 | 'trybots? [y,n]: ') |
| 207 | if are_you_sure == 'y': |
| 208 | using_bots = trybots |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 209 | elif bot == COMPILE_BUILDERS: |
borenet@google.com | 6fb7756 | 2013-07-12 18:11:04 +0000 | [diff] [blame] | 210 | using_bots = [t for t in trybots if t.startswith('Build')] |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 211 | elif bot == CQ_BUILDERS: |
| 212 | using_bots = RetrieveTrybotList(json_filename='cqtrybots') |
| 213 | elif bot == REGEX: |
| 214 | while True: |
| 215 | regex = raw_input("Enter your trybot regex: ") |
| 216 | p = re.compile(regex) |
| 217 | using_bots = [t for t in trybots if p.match(t)] |
| 218 | print '\n\nTrybots that match your regex:\n%s\n\n' % '\n'.join( |
| 219 | using_bots) |
| 220 | if raw_input('Re-enter regex? [y,n]: ') == 'n': |
| 221 | break |
epoger@google.com | 50d6862 | 2013-04-03 18:35:35 +0000 | [diff] [blame] | 222 | break |
| 223 | else: |
| 224 | if not bot in trybots: |
| 225 | Error('Unrecognized builder: %s' % bot) |
| 226 | using_bots.append(bot) |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 227 | elif arg == '-r': |
| 228 | if len(argv) < 1: |
| 229 | Error('You must specify a revision with "-r".') |
| 230 | revision = argv.pop(0) |
| 231 | else: |
| 232 | if changelist or not is_svn: |
| 233 | Error('Unknown argument: %s' % arg) |
| 234 | changelist = arg |
| 235 | if is_svn and not changelist: |
| 236 | Error('You must specify a changelist name.') |
| 237 | if not using_bots: |
| 238 | Error('You must specify one or more builders using --bot.') |
| 239 | return CollectedArgs(bots=using_bots, changelist=changelist, |
| 240 | revision=revision) |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 241 | |
| 242 | |
| 243 | def SubmitTryRequest(args, is_svn=True): |
| 244 | """ Submits a try request for the given changelist on the given list of |
| 245 | trybots. |
| 246 | |
| 247 | args: Object whose properties are derived from command-line arguments. If |
| 248 | is_svn is True, it should contain: |
| 249 | - changelist: string; the name of the changelist to try. |
| 250 | - bot: list of strings; the names of the try builders to run. |
| 251 | - revision: optional, int; the revision number from which to run the try. |
| 252 | If is_svn is False, it should contain: |
| 253 | - bot: list of strings; the names of the try builders to run. |
| 254 | - revision: optional, int; the revision number from which to run the try. |
| 255 | is_svn: boolean; are we in an SVN repo? |
| 256 | """ |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 257 | 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] | 258 | if is_svn: |
borenet@google.com | 6c55b51 | 2013-01-24 21:38:51 +0000 | [diff] [blame] | 259 | gcl_cmd = 'gcl.bat' if os.name == 'nt' else 'gcl' |
| 260 | try_args = [gcl_cmd, 'try', args.changelist, |
| 261 | '--root', GetCheckoutRoot(is_svn), |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 262 | '--bot', botlist] |
| 263 | if args.revision: |
| 264 | try_args.extend(['-r', args.revision]) |
borenet@google.com | 6c55b51 | 2013-01-24 21:38:51 +0000 | [diff] [blame] | 265 | print ' '.join(try_args) |
| 266 | proc = subprocess.Popen(try_args, stdout=subprocess.PIPE, |
| 267 | stderr=subprocess.STDOUT) |
| 268 | if proc.wait() != 0: |
| 269 | raise Exception('Failed to submit try request: %s' % ( |
| 270 | proc.communicate()[0])) |
| 271 | print proc.communicate()[0] |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 272 | else: |
borenet@google.com | 6c55b51 | 2013-01-24 21:38:51 +0000 | [diff] [blame] | 273 | # First, find depot_tools. This is needed to import trychange. |
| 274 | sys.path.append(FindDepotTools()) |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 275 | import trychange |
| 276 | try_args = ['--use_svn', |
| 277 | '--svn_repo', GetTryRepo(), |
| 278 | '--root', GetCheckoutRoot(is_svn), |
| 279 | '--bot', botlist] |
| 280 | if args.revision: |
| 281 | try_args.extend(['-r', args.revision]) |
| 282 | trychange.TryChange(try_args, None, False) |
| 283 | |
| 284 | |
| 285 | def main(): |
| 286 | # Retrieve the list of active try builders from the build master. |
rmistry@google.com | f5c4fc8 | 2013-04-09 11:46:46 +0000 | [diff] [blame] | 287 | trybots = RetrieveTrybotList(json_filename='trybots') |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 288 | |
| 289 | # Determine if we're in an SVN checkout. |
| 290 | is_svn = os.path.isdir('.svn') |
| 291 | |
| 292 | # Parse and validate the command-line arguments. |
borenet@google.com | a5d621f | 2013-01-25 20:55:35 +0000 | [diff] [blame] | 293 | args = ValidateArgs(sys.argv[1:], trybots=trybots, is_svn=is_svn) |
borenet@google.com | 6b5388e | 2013-01-23 20:54:29 +0000 | [diff] [blame] | 294 | |
| 295 | # Submit the try request. |
| 296 | SubmitTryRequest(args, is_svn=is_svn) |
| 297 | |
| 298 | |
| 299 | if __name__ == '__main__': |
epoger@google.com | 50d6862 | 2013-04-03 18:35:35 +0000 | [diff] [blame] | 300 | sys.exit(main()) |