-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloaddata.py
More file actions
59 lines (41 loc) · 1.56 KB
/
Copy pathloaddata.py
File metadata and controls
59 lines (41 loc) · 1.56 KB
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
#!/usr/bin/python3
import base64
import hashlib
import hmac
import os
import glob
import json
import requests
import sys
def get_service_token():
domain = os.environ.get('SERVICE_DOMAIN')
secret = os.environ.get('SERVICE_AUTH_SECRET')
key = hashlib.sha1(b'trood.signer' + secret.encode('utf-8')).digest()
signature = hmac.new(key, msg=domain.encode('utf-8'), digestmod=hashlib.sha1).digest()
signature = base64.urlsafe_b64encode(signature).strip(b'=')
token = str('%s:%s' % (domain, signature))
return "Service {}".format(token)
if __name__ == "__main__":
verbose = False
if len(sys.argv) > 1:
if '-v' in sys.argv:
verbose = True
fixtures_path = sys.argv[1]
def apply_fixture(name, data):
response = requests.post(
'http://127.0.0.1:8000/custodian/data/{}'.format(name),
json=data,
headers={'Authorization': get_service_token()}
)
if response.status_code != 200:
print('Fixture {} not uploaded.'.format(name))
if verbose:
print(response.status_code)
print(json.dumps(response.json(), indent=4))
return
print('Fixture: {} uploaded.'.format(name))
fixture_files = sorted(glob.glob(fixtures_path))
for f in fixture_files:
object_name = '_'.join(os.path.basename(f).split('.')[1].split('_')[1:])
fixture_data = json.load(open(f))
apply_fixture(object_name, fixture_data)