Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Post a try job to the Try server to produce build. It communicates | |
| 6 to server by directly connecting via HTTP. | |
| 7 """ | |
| 8 | |
| 9 import getpass | |
| 10 import optparse | |
| 11 import os | |
| 12 import sys | |
| 13 import urllib | |
| 14 import urllib2 | |
| 15 | |
| 16 | |
| 17 class ServerAccessError(Exception): | |
| 18 def __str__(self): | |
| 19 return '%s\nSorry, cannot connect to server.' % self.args[0] | |
| 20 | |
| 21 | |
| 22 def PostTryJob(url_params): | |
| 23 """Sends a build request to the server using the HTTP protocol. | |
| 24 | |
| 25 Args: | |
| 26 url_params: A dictionary of query parameters to be sent in the request. | |
| 27 In order to post build request to try server, this dictionary | |
| 28 should contain information for following keys: | |
| 29 'host': Hostname of the try server. | |
| 30 'port': Port of the try server. | |
| 31 'revision': SVN Revision to build. | |
| 32 'bot': Name of builder bot which would be used. | |
| 33 Returns: | |
| 34 True if the request is posted successfully. Otherwise throws an exception. | |
| 35 """ | |
| 36 # Parse url parameters to be sent to Try server. | |
| 37 if not url_params.get('host'): | |
|
tonyg
2014/03/07 02:41:09
These missing param checks should probably raise a
prasadv
2014/03/07 17:54:53
Done.
| |
| 38 raise ServerAccessError('Hostname of server to connect is missing.') | |
| 39 if not url_params.get('port'): | |
| 40 raise ServerAccessError('Port of server to connect is missing.') | |
| 41 if not url_params.get('revision'): | |
| 42 raise ServerAccessError('Missing revision details. Please specify revision' | |
| 43 ' information.') | |
| 44 if not url_params.get('bot'): | |
| 45 raise ServerAccessError('Missing bot details. Please specify bot' | |
| 46 ' information.') | |
| 47 | |
| 48 url = 'http://%s:%s/send_try_patch' % (url_params['host'], url_params['port']) | |
| 49 | |
| 50 print 'Sending by HTTP' | |
| 51 query_params = '&'.join('%s=%s' % (k, v) for k, v in url_params.iteritems()) | |
| 52 print 'url: %s?%s' % (url, query_params) | |
| 53 | |
| 54 connection = None | |
| 55 try: | |
| 56 print 'Opening connection...' | |
| 57 connection = urllib2.urlopen(url, urllib.urlencode(url_params)) | |
| 58 print 'Done, request sent to server to produce build.' | |
| 59 except IOError, e: | |
| 60 raise ServerAccessError('%s is unaccessible. Reason: %s' % (url, e)) | |
| 61 if not connection: | |
| 62 raise ServerAccessError('%s is unaccessible.' % url) | |
| 63 response = connection.read() | |
| 64 print 'Received %s from server' % response | |
| 65 if response != 'OK': | |
| 66 raise ServerAccessError('%s is unaccessible. Got:\n%s' % (url, response)) | |
| 67 return True | |
| 68 | |
| 69 | |
| 70 def _GetQueryParams(options): | |
| 71 """Parses common query parameters which will be passed to PostTryJob. | |
| 72 | |
| 73 Args: | |
| 74 options: The options object parsed from the command line. | |
| 75 | |
| 76 Returns: | |
| 77 A dictionary consists of query parameters. | |
| 78 """ | |
| 79 values = {'host': options.host, | |
| 80 'port': options.port, | |
| 81 'user': options.user, | |
| 82 'name': options.name | |
| 83 } | |
| 84 if options.email: | |
| 85 values['email'] = options.email | |
| 86 if options.revision: | |
| 87 values['revision'] = options.revision | |
| 88 if options.root: | |
| 89 values['root'] = options.root | |
| 90 if options.bot: | |
| 91 values['bot'] = options.bot | |
| 92 if options.patch: | |
| 93 values['patch'] = options.patch | |
| 94 return values | |
| 95 | |
| 96 | |
| 97 def _GenParser(): | |
| 98 """Parses the command line for posting build request.""" | |
| 99 usage = ('%prog [options]\n' | |
| 100 'Post a build request to the try server for the given revision.\n') | |
| 101 parser = optparse.OptionParser(usage=usage) | |
| 102 parser.add_option('-H', '--host', | |
| 103 help='Host address') | |
| 104 parser.add_option('-P', '--port', type='int', | |
| 105 help='HTTP port') | |
| 106 parser.add_option('-u', '--user', default=getpass.getuser(), | |
| 107 dest='user', | |
| 108 help='Owner user name [default: %default]') | |
| 109 parser.add_option('-e', '--email', | |
| 110 default=os.environ.get('TRYBOT_RESULTS_EMAIL_ADDRESS', | |
| 111 os.environ.get('EMAIL_ADDRESS')), | |
| 112 help='Email address where to send the results. Use either ' | |
| 113 'the TRYBOT_RESULTS_EMAIL_ADDRESS environment ' | |
| 114 'variable or EMAIL_ADDRESS to set the email address ' | |
| 115 'the try bots report results to [default: %default]') | |
| 116 parser.add_option('-n', '--name', | |
| 117 default= 'try_job_http', | |
| 118 help='Descriptive name of the try job') | |
| 119 parser.add_option('-b', '--bot', | |
| 120 help=('IMPORTANT: specify ONE builder per run is supported.' | |
| 121 'Run script for each builders separately.')) | |
| 122 parser.add_option('-r', '--revision', | |
| 123 help='Revision to use for the try job; default: the ' | |
| 124 'revision will be determined by the try server; see ' | |
| 125 'its waterfall for more info') | |
| 126 parser.add_option('--root', | |
| 127 help='Root to use for the patch; base subdirectory for ' | |
| 128 'patch created in a subdirectory') | |
| 129 parser.add_option('--patch', | |
| 130 help='Patch information.') | |
| 131 return parser | |
| 132 | |
| 133 | |
| 134 def Main(argv): | |
| 135 parser = _GenParser() | |
| 136 options, args = parser.parse_args() | |
| 137 if not options.host: | |
| 138 raise ServerAccessError('Please use the --host option to specify the try ' | |
| 139 'server host to connect to.') | |
| 140 if not options.port: | |
| 141 raise ServerAccessError('Please use the --port option to specify the try ' | |
| 142 'server port to connect to.') | |
| 143 params = _GetQueryParams(options) | |
| 144 PostTryJob(params) | |
| 145 | |
| 146 | |
| 147 if __name__ == '__main__': | |
| 148 sys.exit(Main(sys.argv)) | |
| OLD | NEW |