1 # Copyright 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.
5 """Post a try job to the Try server to produce build. It communicates
6 to server by directly connecting via HTTP.
17 class ServerAccessError(Exception):
19 return '%s\nSorry, cannot connect to server.' % self
.args
[0]
22 def PostTryJob(url_params
):
23 """Sends a build request to the server using the HTTP protocol.
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.
34 True if the request is posted successfully. Otherwise throws an exception.
36 # Parse url parameters to be sent to Try server.
37 if not url_params
.get('host'):
38 raise ValueError('Hostname of server to connect is missing.')
39 if not url_params
.get('port'):
40 raise ValueError('Port of server to connect is missing.')
41 if not url_params
.get('revision'):
42 raise ValueError('Missing revision details. Please specify revision'
44 if not url_params
.get('bot'):
45 raise ValueError('Missing bot details. Please specify bot information.')
47 # Pop 'host' and 'port' to avoid passing them as query params.
48 url
= 'http://%s:%s/send_try_patch' % (url_params
.pop('host'),
49 url_params
.pop('port'))
51 print 'Sending by HTTP'
52 query_params
= '&'.join('%s=%s' % (k
, v
) for k
, v
in url_params
.iteritems())
53 print 'url: %s?%s' % (url
, query_params
)
57 print 'Opening connection...'
58 connection
= urllib2
.urlopen(url
, urllib
.urlencode(url_params
))
59 print 'Done, request sent to server to produce build.'
61 raise ServerAccessError('%s is unaccessible. Reason: %s' % (url
, e
))
63 raise ServerAccessError('%s is unaccessible.' % url
)
64 response
= connection
.read()
65 print 'Received %s from server' % response
67 raise ServerAccessError('%s is unaccessible. Got:\n%s' % (url
, response
))
71 def _GetQueryParams(options
):
72 """Parses common query parameters which will be passed to PostTryJob.
75 options: The options object parsed from the command line.
78 A dictionary consists of query parameters.
80 values
= {'host': options
.host
,
86 values
['email'] = options
.email
88 values
['revision'] = options
.revision
90 values
['root'] = options
.root
92 values
['bot'] = options
.bot
94 values
['patch'] = options
.patch
99 """Parses the command line for posting build request."""
100 usage
= ('%prog [options]\n'
101 'Post a build request to the try server for the given revision.\n')
102 parser
= optparse
.OptionParser(usage
=usage
)
103 parser
.add_option('-H', '--host',
104 help='Host address of the try server.')
105 parser
.add_option('-P', '--port', type='int',
106 help='HTTP port of the try server.')
107 parser
.add_option('-u', '--user', default
=getpass
.getuser(),
109 help='Owner user name [default: %default]')
110 parser
.add_option('-e', '--email',
111 default
=os
.environ
.get('TRYBOT_RESULTS_EMAIL_ADDRESS',
112 os
.environ
.get('EMAIL_ADDRESS')),
113 help='Email address where to send the results. Use either '
114 'the TRYBOT_RESULTS_EMAIL_ADDRESS environment '
115 'variable or EMAIL_ADDRESS to set the email address '
116 'the try bots report results to [default: %default]')
117 parser
.add_option('-n', '--name',
118 default
= 'try_job_http',
119 help='Descriptive name of the try job')
120 parser
.add_option('-b', '--bot',
121 help=('IMPORTANT: specify ONE builder per run is supported.'
122 'Run script for each builders separately.'))
123 parser
.add_option('-r', '--revision',
124 help='Revision to use for the try job; default: the '
125 'revision will be determined by the try server; see '
126 'its waterfall for more info')
127 parser
.add_option('--root',
128 help='Root to use for the patch; base subdirectory for '
129 'patch created in a subdirectory')
130 parser
.add_option('--patch',
131 help='Patch information.')
136 parser
= _GenParser()
137 options
, args
= parser
.parse_args()
139 raise ServerAccessError('Please use the --host option to specify the try '
140 'server host to connect to.')
142 raise ServerAccessError('Please use the --port option to specify the try '
143 'server port to connect to.')
144 params
= _GetQueryParams(options
)
148 if __name__
== '__main__':
149 sys
.exit(Main(sys
.argv
))