2012-01-24 15:44:51 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#-*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Use of this source code is governed by a BSD-style license
|
|
|
|
# that can be found in the LICENSE file in the root of the source
|
|
|
|
# tree. An additional intellectual property rights grant can be found
|
|
|
|
# in the file PATENTS. All contributing project authors may
|
|
|
|
# be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
|
|
|
"""This script request an access token from the appengine running the dashboard.
|
|
|
|
|
|
|
|
The script is intended to be run manually whenever we wish to change which
|
|
|
|
dashboard administrator we act on behalf of when running the
|
|
|
|
track_coverage.py script. For example, this will be useful if the current
|
|
|
|
dashboard administrator leaves the project.
|
|
|
|
|
|
|
|
This script should be run on the build bot which runs the track_coverage.py
|
|
|
|
script. This script will present a link during its execution, which the new
|
|
|
|
administrator should follow and then click approve on the web page that
|
|
|
|
appears. The new administrator should have admin rights on the coverage
|
2012-02-01 11:59:23 +01:00
|
|
|
dashboard, otherwise track_coverage.py will not work.
|
2012-01-24 15:44:51 +01:00
|
|
|
|
|
|
|
If successful, this script will write the access token to a file access.token
|
|
|
|
in the current directory, which later can be read by track_coverage.py.
|
|
|
|
The token is stored in string form (as reported by the web server) using the
|
|
|
|
shelve module.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'phoglund@webrtc.org (Patrik Höglund)'
|
|
|
|
|
|
|
|
import shelve
|
|
|
|
import sys
|
|
|
|
import urlparse
|
|
|
|
import oauth2 as oauth
|
|
|
|
|
2012-02-01 11:59:23 +01:00
|
|
|
import constants
|
2012-01-24 15:44:51 +01:00
|
|
|
|
2012-02-06 11:55:12 +01:00
|
|
|
|
2012-01-24 15:44:51 +01:00
|
|
|
class FailedToRequestPermissionException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def _ensure_token_response_is_200(response, queried_url, token_type):
|
|
|
|
if response.status != 200:
|
|
|
|
raise FailedToRequestPermissionException('Failed to request %s from %s: '
|
|
|
|
'received status %d, reason %s.' %
|
|
|
|
(token_type,
|
|
|
|
queried_url,
|
|
|
|
response.status,
|
|
|
|
response.reason))
|
|
|
|
|
2012-02-01 11:59:23 +01:00
|
|
|
|
2012-01-24 15:44:51 +01:00
|
|
|
def _request_unauthorized_token(consumer, request_token_url):
|
2012-02-01 11:59:23 +01:00
|
|
|
"""Requests the initial token from the dashboard service.
|
2012-01-24 15:44:51 +01:00
|
|
|
|
|
|
|
Given that the response from the server is correct, we will return a
|
|
|
|
dictionary containing oauth_token and oauth_token_secret mapped to the
|
|
|
|
token and secret value, respectively.
|
|
|
|
"""
|
|
|
|
client = oauth.Client(consumer)
|
|
|
|
|
|
|
|
try:
|
|
|
|
response, content = client.request(request_token_url, 'POST')
|
|
|
|
except AttributeError as error:
|
|
|
|
# This catch handler is here since we'll get very confusing messages
|
|
|
|
# if the target server is down for some reason.
|
2012-02-01 11:59:23 +01:00
|
|
|
raise FailedToRequestPermissionException('Failed to request token: '
|
|
|
|
'the dashboard is likely down.',
|
2012-01-24 15:44:51 +01:00
|
|
|
error)
|
|
|
|
|
|
|
|
_ensure_token_response_is_200(response, request_token_url,
|
2012-02-01 11:59:23 +01:00
|
|
|
'unauthorized token')
|
2012-01-24 15:44:51 +01:00
|
|
|
|
|
|
|
return dict(urlparse.parse_qsl(content))
|
|
|
|
|
|
|
|
|
|
|
|
def _ask_user_to_authorize_us(unauthorized_token):
|
|
|
|
"""This function will block until the user enters y + newline."""
|
|
|
|
print 'Go to the following link in your browser:'
|
2012-02-01 11:59:23 +01:00
|
|
|
print '%s?oauth_token=%s' % (constants.AUTHORIZE_TOKEN_URL,
|
2012-01-24 15:44:51 +01:00
|
|
|
unauthorized_token['oauth_token'])
|
|
|
|
|
|
|
|
accepted = 'n'
|
|
|
|
while accepted.lower() != 'y':
|
|
|
|
accepted = raw_input('Have you authorized me yet? (y/n) ')
|
|
|
|
|
|
|
|
|
|
|
|
def _request_access_token(consumer, unauthorized_token):
|
|
|
|
token = oauth.Token(unauthorized_token['oauth_token'],
|
|
|
|
unauthorized_token['oauth_token_secret'])
|
|
|
|
client = oauth.Client(consumer, token)
|
2012-02-01 11:59:23 +01:00
|
|
|
response, content = client.request(constants.ACCESS_TOKEN_URL, 'POST')
|
2012-01-24 15:44:51 +01:00
|
|
|
|
2012-02-01 11:59:23 +01:00
|
|
|
_ensure_token_response_is_200(response, constants.ACCESS_TOKEN_URL,
|
|
|
|
'access token')
|
2012-01-24 15:44:51 +01:00
|
|
|
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
|
|
|
def _write_access_token_to_file(access_token, filename):
|
|
|
|
output = shelve.open(filename)
|
|
|
|
output['access_token'] = access_token
|
|
|
|
output.close()
|
|
|
|
|
|
|
|
print 'Wrote the access token to the file %s.' % filename
|
|
|
|
|
|
|
|
|
|
|
|
def _main():
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print ('Usage: %s <consumer secret>.\n\nThe consumer secret is an OAuth '
|
|
|
|
'concept and is obtained from the appengine running the dashboard.' %
|
|
|
|
sys.argv[0])
|
|
|
|
return
|
|
|
|
|
|
|
|
consumer_secret = sys.argv[1]
|
2012-02-01 11:59:23 +01:00
|
|
|
consumer = oauth.Consumer(constants.CONSUMER_KEY, consumer_secret)
|
2012-01-24 15:44:51 +01:00
|
|
|
|
2012-02-01 11:59:23 +01:00
|
|
|
unauthorized_token = _request_unauthorized_token(consumer,
|
|
|
|
constants.REQUEST_TOKEN_URL)
|
2012-01-24 15:44:51 +01:00
|
|
|
|
|
|
|
_ask_user_to_authorize_us(unauthorized_token)
|
|
|
|
|
|
|
|
access_token_string = _request_access_token(consumer, unauthorized_token)
|
|
|
|
|
2012-02-01 11:59:23 +01:00
|
|
|
_write_access_token_to_file(access_token_string, constants.ACCESS_TOKEN_FILE)
|
2012-01-24 15:44:51 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
_main()
|