Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / tools / post_perf_builder_job.py
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.
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'):
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'
43                      ' information.')
44   if not url_params.get('bot'):
45     raise ValueError('Missing bot details. Please specify bot information.')
46
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'))
50
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)
54
55   connection = None
56   try:
57     print 'Opening connection...'
58     connection = urllib2.urlopen(url, urllib.urlencode(url_params))
59     print 'Done, request sent to server to produce build.'
60   except IOError, e:
61     raise ServerAccessError('%s is unaccessible. Reason: %s' % (url, e))
62   if not connection:
63     raise ServerAccessError('%s is unaccessible.' % url)
64   response = connection.read()
65   print 'Received %s from server' % response
66   if response != 'OK':
67     raise ServerAccessError('%s is unaccessible. Got:\n%s' % (url, response))
68   return True
69
70
71 def _GetQueryParams(options):
72   """Parses common query parameters which will be passed to PostTryJob.
73
74   Args:
75     options: The options object parsed from the command line.
76
77   Returns:
78     A dictionary consists of query parameters.
79   """
80   values = {'host': options.host,
81             'port': options.port,
82             'user': options.user,
83             'name': options.name
84            }
85   if options.email:
86     values['email'] = options.email
87   if options.revision:
88     values['revision'] = options.revision
89   if options.root:
90     values['root'] = options.root
91   if options.bot:
92     values['bot'] = options.bot
93   if options.patch:
94     values['patch'] = options.patch
95   return values
96
97
98 def _GenParser():
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(),
108                     dest='user',
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.')
132   return parser
133
134
135 def Main(argv):
136   parser = _GenParser()
137   options, args = parser.parse_args()
138   if not options.host:
139     raise ServerAccessError('Please use the --host option to specify the try '
140         'server host to connect to.')
141   if not options.port:
142     raise ServerAccessError('Please use the --port option to specify the try '
143         'server port to connect to.')
144   params = _GetQueryParams(options)
145   PostTryJob(params)
146
147
148 if __name__ == '__main__':
149   sys.exit(Main(sys.argv))