-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap_tests.py
106 lines (83 loc) · 3.08 KB
/
bootstrap_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from textwrap import dedent
import os
from configobj import ConfigObj
from validate import Validator
import webbrowser
import soundcloud
config = {
'CONFIG_NAME': "test.ini",
'CLIENT_ID': None,
'CLIENT_SECRET': None,
'TOKEN_URI': None,
'REDIRECT_URI': None,
'API_HOST': None
}
CONFIGSPEC=dedent("""
[api]
api_host=string(default=api.soundcloud.com)
client_id=string
client_secret=string
token_uri=string
redirect_uri=string
[proxy]
use_proxy=boolean(default=false)
proxy=string(default=http://127.0.0.1:10000/)
[logging]
test_logger=string(default=ERROR)
api_logger=string(default=ERROR)
[test]
run_interactive_tests=boolean(default=false)
""")
def load_config():
"""
Loads the configuration by looking from
- the environment variable soundcloud_CONFIG
- the installation location upwards until it finds test.ini
- the current working directory upwards until it finds test.ini
Raises an error if there is no config found
"""
config_name = config['CONFIG_NAME']
name = None
if "soundcloud_CONFIG" in os.environ:
if os.path.exists(os.environ["soundcloud_CONFIG"]):
name = os.environ["soundcloud_CONFIG"]
def search_for_config(current):
while current:
name = os.path.join(current, config_name)
if os.path.exists(name):
return name
new_current = os.path.dirname(current)
if new_current == current:
return
current = new_current
if name is None:
name = search_for_config(os.path.dirname(__file__))
if name is None:
name = search_for_config(os.getcwd())
if not name:
raise Exception("No test configuration file found!")
parser = ConfigObj(name, configspec=CONFIGSPEC.split("\n"))
val = Validator()
if not parser.validate(val):
raise Exception("Config file validation error")
api = parser['api']
config['API_HOST'] = api.get('api_host')
config['CLIENT_ID'] = api.get('client_id')
config['CLIENT_SECRET'] = api.get('client_secret')
config['TOKEN_URI'] = api.get('token_uri')
config['REDIRECT_URI'] = api.get('redirect_uri')
load_config()
authenticator = soundcloud.OAuth2Authenticator( config['CLIENT_ID'],
config['CLIENT_SECRET'],
config['REDIRECT_URI'])
connect_url = authenticator.construct_connect_url()
webbrowser.open(connect_url)
authorization_code = raw_input("After connecting, please enter the code parameter in the browser's address bar (code=...): ")
authenticator = soundcloud.OAuth2Authenticator( config['CLIENT_ID'],
config['CLIENT_SECRET'],
config['REDIRECT_URI'],
None,
authorization_code)
token = authenticator.fetch_access_token()
print "Done! Make sure to add this line to your test.ini under [api]:"
print "access_token=%s" % token