/
acs4cmd.py
228 lines (194 loc) · 8.56 KB
/
acs4cmd.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
Copyright(c)2010 Internet Archive. Software license AGPL version 3.
Entry points - call from command line, or see:
queryresourceitems, upload, request and mint.
"""
from __future__ import print_function
import sys
import optparse
import acs4
import json
def main(argv):
class MyParser(optparse.OptionParser):
""" allows non-word-wrapped help epilog """
def format_epilog(self, formatter):
return self.epilog
parser = MyParser(usage='usage: %prog [options] SERVER ACTION',
version='%prog 0.1',
description='Interact with ACS. Action is one of "mint", "queryresourceitems", "upload" or "request". See examples below.',
epilog="""
Examples:
python acs4cmd.py server mint --distributor='uuid' --resource='uuid'
python acs4cmd.py server queryresourceitems
python acs4cmd.py server upload [filename] (or --datapath=/server/path/book.epub)
python acs4cmd.py server request api request_type
api is: (id is:)
DistributionRights (distributor + resource)
Distributor (distributor)
Fulfillment (fulfillment)
FulfillmentItem (fulfillment)
License (user + resource)
ResourceItem (resource + item)
ResourceKey (resource)
UserPublic (user)
request_type is: 'get count create delete update'
USE WITH CARE, this can break your acs4 install!
""")
parser.add_option('-p', '--password',
action='store',
help='ACS4 password')
parser.add_option('--permissions',
action='store',
help='xml file of ACS4 perms - for upload and request')
parser.add_option('-d', '--debug',
action='store_true',
help='Print debugging output')
parser.add_option('--dry_run',
action='store_true',
help='Don\'t post to server')
parser.add_option('--port',
action='store',
default=acs4.defaultport,
help='Server port to use (default 8080)')
group = optparse.OptionGroup(parser, "Request arguments",
"Arguments specific to 'request' actions")
group.add_option('--start',
action='store',
type='int',
default='0',
help='Start of items to fetch with request get')
group.add_option('--count',
action='store',
type='int',
default='0',
help='Count of items to fetch with request get')
parser.add_option_group(group)
group = optparse.OptionGroup(parser, "Upload arguments",
"Arguments specific to 'upload' action")
group.add_option('--datapath',
action='store',
help='server data path to use with upload')
group.add_option('--metadata',
action='store',
help='xml file of resource metadata - for upload.')
parser.add_option_group(group)
# also repeat these below, near 'dynamic'
request_arg_names = ['distributor',
'resource',
'distributionType',
'available',
'returnable',
'resourceItem',
'notifyURL',
'user',
]
for name in request_arg_names:
# 'available' - int
# user - needed?
if name in ('distributor', 'resource'):
parser.add_option('--' + name,
action='store',
metavar='UUID',
help=name + ' argument for request')
elif name == 'notifyURL':
parser.add_option('--' + name,
action='store',
metavar='URL',
help=name + ' argument for request')
else:
parser.add_option('--' + name,
action='store',
help=name + ' argument for request')
opts, args = parser.parse_args(argv)
if opts.permissions:
opts.permissions = open(opts.permissions).read()
if opts.debug:
acs4.debug = True
if opts.dry_run:
acs4.dry_run = True
if not opts.password:
parser.error('We think a password arg might be required')
if len(args) < 2:
parser.error('Please supply at least server and action args')
server = args[0]
action = args[1].lower()
actions = ['queryresourceitems', 'upload', 'request', 'mint']
if not action in actions:
parser.error('action arg should be one of ' + ', '.join(actions))
if action == 'queryresourceitems':
result = acs4.queryresourceitems(server, opts.password,
start=opts.start,
count=opts.count,
distributor=opts.distributor,
port=opts.port)
json.dump(result, sys.stdout, indent=4, sort_keys=True)
print()
elif action == 'upload':
fh = None
if len(args) == 3:
if opts.datapath is not None:
parser.error('--datapath (path to remote file)'
' and filename both supplied.')
fh = open(args[2])
elif len(args) == 2:
if opts.datapath is None:
parser.error('please supply filename or --datapath argument')
else:
parser.error('Wrong number of args supplied to upload action')
result = acs4.upload(server, fh, opts.password,
datapath=opts.datapath,
permissions=opts.permissions,
metadata=opts.metadata,
port=opts.port)
json.dump(result, sys.stdout, indent=4, sort_keys=True)
print()
elif action == 'request':
request_types = ['get', 'count', 'create', 'delete', 'update']
joined = ', '.join(request_types)
if len(args) != 4:
parser.error('For "request" action, please supply server,'
' "request", web_api, request_type - where web_api'
' is e.g. DistributionRights, and request_type'
' is one of ' + joined)
api = args[2]
request_type = args[3].lower()
if not request_type in request_types:
parser.error('Request type should be one of ' + joined)
request_args = {}
# TODO make this dynamic. But opts is an optparse.Values, and
# doesn't have __getitem__! request_args = dict([(name,
# opts[name]) for name in request_arg_names])
request_args = {
'distributor': opts.distributor,
'resource': opts.resource,
'distributionType': opts.distributionType,
'available': opts.available,
'returnable': opts.returnable,
'resourceItem': opts.resourceItem,
'notifyURL': opts.notifyURL,
'user': opts.user,
}
result = acs4.request(server, api, request_type, request_args,
opts.password,
start=opts.start,
count=opts.count,
permissions=opts.permissions,
port=opts.port)
json.dump(result, sys.stdout, indent=4, sort_keys=True)
print()
elif action == 'mint':
# XXX also opt for supplying name, sharedsecret? would skip
# rt to server.
if not opts.resource or not opts.distributor:
parser.error('Please supply --resource= and --distributor='
' arguments for mint')
distinfo = acs4.get_distributor_info(server, opts.password,
opts.distributor,
port=opts.port)
secret = distinfo['sharedSecret']
name = distinfo['name']
print(acs4.mint(server, secret, opts.resource, 'enterloan', name,
port=opts.port))
parser.destroy()
if __name__ == '__main__':
main(sys.argv[1:])