-
Notifications
You must be signed in to change notification settings - Fork 7
/
uploadChanges.py
118 lines (103 loc) · 4.45 KB
/
uploadChanges.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
107
108
109
110
111
112
113
114
115
116
117
import argparse
import json
import os
import requests
"""
This script uploads content to the defined register
./prodRegister
This reqires an authentication token and userID and structured
content.
The structured content is taken from the command line argument which
shall consist of a dictionary with the keys:
'PUT', 'POST'
and each key shall provide a list of .ttl files to upload to prodRegister
based on the relative path of the .ttl file.
"""
def authenticate(session, base, userid, pss):
auth = session.post('{}/system/security/apilogin'.format(base),
data={'userid':userid,
'password':pss})
if not auth.status_code == 200:
raise ValueError('auth failed')
return session
def parse_uploads(uploads):
result = json.loads(uploads)
if set(result.keys()) != set(('PUT', 'POST')):
raise ValueError("Uploads inputs should have keys"
" set(('PUT', 'POST')) only, not:\n"
"{}".format(result.keys()))
return result
def post(session, url, payload, targeturl):
headers={'Content-type':'text/turtle', 'charset':'utf-8'}
response = session.get(url, headers=headers)
if response.status_code != 200:
raise ValueError('Cannot POST to {}, it does not exist.'.format(url))
params = {'status':'experimental'}
res = session.post(url, headers=headers, data=payload.encode("utf-8"), params=params)
if res.status_code != 201:
print('POST failed with {}\n{}'.format(res.status_code, res.reason))
def put(session, url, payload):
headers={'Content-type':'text/turtle', 'charset':'utf-8'}
response = session.get(url, headers=headers)
if response.status_code != 200:
raise ValueError('Cannot PUT to {}, it does not exist.'.format(url))
res = session.put(url, headers=headers, data=payload.encode("utf-8"))
print('\t' + str(res.status_code))
if res.status_code != 204:
print('\t' + res.text)
def put_non_member(session, url, payload):
headers={'Content-type':'text/turtle', 'charset':'utf-8'}
response = session.get(url, headers=headers)
if response.status_code != 200:
raise ValueError('Cannot PUT to {}, it does not exist.'.format(url))
res = session.put(url + '?non-member-properties', headers=headers,
data=payload.encode("utf-8"))
print('\t' + str(res.status_code))
if res.status_code != 204:
print('\t' + res.text)
def post_uploads(session, rootURL, uploads):
for postfile in uploads:
with open('.{}'.format(postfile), 'r', encoding="utf-8") as pf:
pdata = pf.read()
# post, so remove last part of identity, this is in the payload
targetrelID = postfile.replace('.ttl', '')
relID = '/'.join(postfile.split('/')[:-1])
url = '{}{}'.format(rootURL, relID)
targeturl = '{}{}'.format(rootURL, targetrelID)
print(url)
post(session, url, pdata, targeturl)
def put_uploads(session, rootURL, uploads):
for putfile in uploads:
with open('.{}'.format(putfile), 'r', encoding="utf-8") as pf:
pdata = pf.read()
if os.path.basename(putfile).startswith('_'):
putfile = os.path.join(os.path.dirname(putfile),
os.path.basename(putfile).lstrip('_'))
relID = putfile.replace('.ttl', '')
url = '{}{}'.format(rootURL, relID)
print(url)
if os.path.exists('.{}'.format(putfile.replace('.ttl', ''))):
# then it is a register, so, only non-member-properties can be PUT
put_non_member(session, url, pdata)
else:
put(session, url, pdata)
if __name__ == '__main__':
with open('prodRegister', 'r', encoding='utf-8') as fh:
rooturl = fh.read().split('\n')[0].replace('http://', 'https://')
print('Running upload with respect to {}'.format(rooturl))
parser = argparse.ArgumentParser()
parser.add_argument('user_id')
parser.add_argument("passcode")
parser.add_argument('uploads')
args = parser.parse_args()
session = requests.Session()
if os.path.exists(args.uploads):
with open(args.uploads, 'r') as ups:
uploads = ups.read()
else:
uploads = args.uploads
uploads = parse_uploads(uploads)
session = authenticate(session, rooturl, args.user_id, args.passcode)
print(uploads)
post_uploads(session, rooturl, uploads['POST'])
put_uploads(session, rooturl, uploads['PUT'])