86956b4c6e9a18801c0618d37337b844e2d925ac
[WebKit-https.git] / Websites / perf.webkit.org / tools / util.py
1 import json
2 import sys
3 import urllib2
4
5
6 def submit_commits(commits, dashboard_url, slave_name, slave_password):
7     try:
8         payload = json.dumps({
9             'slaveName': slave_name,
10             'slavePassword': slave_password,
11             'commits': commits,
12         })
13         request = urllib2.Request(dashboard_url + '/api/report-commits')
14         request.add_header('Content-Type', 'application/json')
15         request.add_header('Content-Length', len(payload))
16
17         output = urllib2.urlopen(request, payload).read()
18         try:
19             result = json.loads(output)
20         except Exception, error:
21             raise Exception(error, output)
22
23         if result.get('status') != 'OK':
24             raise Exception(result)
25     except Exception as error:
26         sys.exit('Failed to submit commits: %s' % str(error))
27
28
29 def text_content(element):
30     text = ''
31     for child in element.childNodes:
32         if child.nodeType == child.TEXT_NODE:
33             text += child.data
34         else:
35             text += text_content(child)
36     return text
37
38
39 HTTP_AUTH_HANDLERS = {
40     'basic': urllib2.HTTPBasicAuthHandler,
41     'digest': urllib2.HTTPDigestAuthHandler,
42 }
43
44
45 def setup_auth(server):
46     auth = server.get('auth')
47     if not auth:
48         return
49
50     password_manager = urllib2.HTTPPasswordMgr()
51     password_manager.add_password(realm=auth['realm'], uri=server['url'], user=auth['username'], passwd=auth['password'])
52     auth_handler = HTTP_AUTH_HANDLERS[auth['type']](password_manager)
53     urllib2.install_opener(urllib2.build_opener(auth_handler))