-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.py
545 lines (455 loc) · 17.6 KB
/
cli.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# -*- coding: utf-8 -*-
#
# cli.py
#
# Copyright (c) Nicholas J. Radcliffe 2009-2011 and other authors specified
# in the AUTHOR
# Licence terms in LICENCE.
import shutil
import sys
import types
from optparse import OptionParser, OptionGroup
from itertools import chain, imap
from fdblib import (
FluidDB,
O,
Credentials,
get_credentials_file,
get_typed_tag_value,
path_style,
Print,
toStr,
uprint,
version,
DEFAULT_ENCODING,
STATUS,
DADGAD_ID,
HTTP_TIMEOUT,
SANDBOX_PATH,
FLUIDDB_PATH,
)
import ls
import flags
HTTP_METHODS = ['GET', 'PUT', 'POST', 'DELETE', 'HEAD']
ARGLESS_COMMANDS = ['COUNT', 'TAGS', 'LS', 'PWD', 'PWN', 'WHOAMI']
USAGE = u"""
Tag objects:
fdb tag -a 'DADGAD' tuning rating=10
fdb tag -i %s /njr/tuning /njr/rating=10
fdb tag -q 'about = "DADGAD"' tuning rating=10
Untag objects:
fdb untag -a 'DADGAD' /njr/tuning rating
fdb untag -i %s
fdb untag -q 'about = "DADGAD"' tuning rating
Fetch objects and show tags
fdb show -a 'DADGAD' /njr/tuning /njr/rating
fdb show -i %s tuning rating
fdb show -q 'about = "DADGAD"' tuning rating
Count objects matching query:
fdb count -q 'has fluiddb/users/username'
Get tags on objects and their values:
fdb tags -a 'DADGAD'
fdb tags -i %s
Miscellaneous:
fdb whoami prints username for authenticated user
fdb pwd / fdb pwn prints root namespace of authenticated user
fdb su fdbuser set fdb to use user credentials for fdbuser
Run Tests:
fdb test runs all tests
fdb testcli tests command line interface only
fdb testdb tests core FluidDB interface only
fdb testutil runs tests not requiring FluidDB access
Raw HTTP GET:
fdb get /tags/njr/google
fdb get /permissions/tags/njr/rating action=delete
(use POST/PUT/DELETE/HEAD at your peril; currently untested.)
""" % (DADGAD_ID, DADGAD_ID, DADGAD_ID, DADGAD_ID)
USAGE_FI = u"""
Tag objects:
fdb tag -a 'DADGAD' njr/tuning njr/rating=10
fdb tag -i %s njr/tuning njr/rating=10
fdb tag -q 'about = "DADGAD"' tuning njr/rating=10
Untag objects:
fdb untag -a 'DADGAD' njr/tuning njr/rating
fdb untag -i %s
fdb untag -q 'about = "DADGAD"' njr/tuning njr/rating
Fetch objects and show tags
fdb show -a 'DADGAD' njr/tuning njr/rating
fdb show -i %s njr/tuning njr/rating
fdb show -q 'about = "DADGAD"' njr/tuning njr/rating
Count objects matching query:
fdb count -q 'has fluiddb/users/username'
Get tags on objects and their values:
fdb tags -a 'DADGAD'
fdb tags -i %s
Miscellaneous:
fdb whoami prints username for authenticated user
fdb pwd / fdb pwn prints root namespace of authenticated user
fdb su fdbuser set fdb to use user credentials for fdbuser
Run Tests:
fdb test (runs all tests)
fdb testcli (tests command line interface only)
fdb testdb (tests core FluidDB interface only)
fdb testutil (runs tests not requiring FluidDB access)
Raw HTTP GET:
fdb get /tags/njr/google
fdb get /permissions/tags/njr/rating action=delete
(use POST/PUT/DELETE/HEAD at your peril; currently untested.)
""" % (DADGAD_ID, DADGAD_ID, DADGAD_ID, DADGAD_ID)
class ModeError(Exception):
pass
class TooFewArgsForHTTPError(Exception):
pass
class UnrecognizedHTTPMethodError(Exception):
pass
class TagValue:
def __init__(self, name, value=None):
self.name = name
self.value = value
def __unicode__(self):
return (u'Tag "%s", value "%s" of type %s'
% (self.name, toStr(self.value), toStr(type(self.value))))
def error_code(n):
code = STATUS.__dict__
for key in code:
if n == code[key]:
return unicode('%d (%s)' % (n, key.replace('_', ' ')))
return unicode(n)
def execute_tag_command(objs, db, tags, options):
tags = form_tag_value_pairs(tags)
actions = {
u'id': db.tag_object_by_id,
u'about': db.tag_object_by_about,
}
for obj in objs:
description = describe_by_mode(obj.specifier, obj.mode)
for tag in tags:
o = actions[obj.mode](obj.specifier, tag.name, tag.value,
inPref=True)
if o == 0:
if options.verbose:
Print(u'Tagged object %s with %s'
% (description,
formatted_tag_value(tag.name, tag.value)))
else:
warning(u'Failed to tag object %s with %s'
% (description, tag.name))
warning(u'Error code %s' % error_code(o))
def execute_untag_command(objs, db, tags, options):
actions = {
'id': db.untag_object_by_id,
'about': db.untag_object_by_about,
}
for obj in objs:
description = describe_by_mode(obj.specifier, obj.mode)
for tag in tags:
o = actions[obj.mode](obj.specifier, tag, inPref=True)
if o == 0:
if options.verbose:
Print('Removed tag %s from object %s\n'
% (tag, description))
else:
warning(u'Failed to remove tag %s from object %s'
% (tag, description))
warning(u'Error code %s' % error_code(o))
def execute_show_command(objs, db, tags, options):
actions = {
u'id': db.get_tag_value_by_id,
u'about': db.get_tag_value_by_about,
}
for obj in objs:
description = describe_by_mode(obj.specifier, obj.mode)
Print(u'Object %s:' % description)
for tag in tags:
fulltag = db.abs_tag_path(tag, inPref=True)
outtag = db.abs_tag_path(tag, inPref=True, outPref=True)
if tag == u'/id':
if obj.mode == u'about':
o = db.query(u'fluiddb/about = "%s"' % obj.specifier)
if type(o) == types.IntType: # error
status, v = o, None
else:
status, v = STATUS.OK, o[0]
else:
status, v = STATUS.OK, obj.specifier
else:
status, v = actions[obj.mode](obj.specifier, tag, inPref=True)
if status == STATUS.OK:
Print(u' %s' % formatted_tag_value(outtag, v))
elif status == STATUS.NOT_FOUND:
Print(u' %s' % cli_bracket(u'tag %s not present' % outtag))
else:
Print(cli_bracket(u'error code %s getting tag %s'
% (error_code(status), outtag)))
def execute_tags_command(objs, db, options):
for obj in objs:
description = describe_by_mode(obj.specifier, obj.mode)
Print(u'Object %s:' % description)
id = (db.create_object(obj.specifier).id if obj.mode == u'about'
else obj.specifier)
for tag in db.get_object_tags_by_id(id):
fulltag = u'/%s' % tag
outtag = u'/%s' % tag if db.unixStyle else tag
status, v = db.get_tag_value_by_id(id, fulltag)
if status == STATUS.OK:
Print(u' %s' % formatted_tag_value(outtag, v))
elif status == STATUS.NOT_FOUND:
Print(u' %s' % cli_bracket(u'tag %s not present' % outtag))
else:
Print(cli_bracket(u'error code %s getting tag %s'
% (error_code(status), uttag)))
def execute_whoami_command(db):
Print(db.credentials.username)
def execute_su_command(db, args):
source = get_credentials_file(username=args[0])
dest = get_credentials_file()
shutil.copyfile(source, dest)
db = FluidDB(Credentials(filename=dest))
username = db.credentials.username
file = args[0].decode(DEFAULT_ENCODING)
extra = u'' if args[0] == username else (u' (file %s)' % file)
Print(u'Credentials set to user %s%s.' % (username, extra))
def execute_http_request(action, args, db, options):
"""Executes a raw HTTP command (GET, PUT, POST, DELETE or HEAD)
as specified on the command line."""
method = action.upper()
if method not in HTTP_METHODS:
raise UnrecognizedHTTPMethodError(u'Only supported HTTP methods are'
u'%s and %s' % (' '.join(HTTP_METHODS[:-1], HTTP_METHODS[-1])))
if len(args) == 0:
raise TooFewArgsForHTTPError(u'HTTP command %s requires a URI'
% method)
uri = args[0]
tags = form_tag_value_pairs(args[1:])
if method == u'PUT':
body = {tags[0].tag: tags[0].value}
tags = tags[1:]
else:
body = None
hash = {}
for pair in tags:
hash[pair.name] = pair.value
status, result = db.call(method, uri, body, hash)
Print(u'Status: %d' % status)
Print(u'Result: %s' % toStr(result))
def describe_by_mode(specifier, mode):
"""mode can be a string (about, id or query) or a flags object
with flags.about, flags.query and flags.id"""
if mode == u'about':
return describe_by_about(specifier)
elif mode == u'id':
return describe_by_id(specifier)
elif mode == u'query':
return describe_by_id(specifier)
raise ModeError(u'Bad Mode')
def describe_by_about(specifier):
return u'with about="%s"' % specifier
def describe_by_id(specifier):
return specifier
def formatted_tag_value(tag, value):
if value == None:
return tag
elif type(value) in types.StringTypes:
return u'%s = "%s"' % (tag, value)
else:
return u'%s = %s' % (tag, toStr(value))
def form_tag_value_pairs(tags):
pairs = []
for tag in tags:
eqPos = tag.find('=')
if eqPos == -1:
pairs.append(TagValue(tag, None))
else:
t = tag[:eqPos]
v = get_typed_tag_value(tag[eqPos + 1:])
pairs.append(TagValue(t, v))
return pairs
def warning(msg):
# sys.stderr.write(u'%s\n' % msg)
Print(u'%s\n' % msg)
def fail(msg):
warning(msg)
raise Exception, msg
def nothing_to_do():
Print(u'Nothing to do.')
raise Exception, msg
def cli_bracket(s):
return u'(%s)' % s
def get_ids_or_fail(query, db):
ids = db.query(query)
if type(ids) == types.IntType:
fail(u'Query failed')
else: # list of ids
Print(u'%s matched' % plural(len(ids), u'object'))
return ids
def plural(n, s, pl=None, str=False, justTheWord=False):
"""Returns a string like '23 fields' or '1 field' where the
number is n, the stem is s and the plural is either stem + 's'
or stem + pl (if provided)."""
smallints = [u'zero', u'one', u'two', u'three', u'four', u'five',
u'six', u'seven', u'eight', u'nine', u'ten']
if pl == None:
pl = u's'
if str and n < 10 and n >= 0:
strNum = smallints[n]
else:
strNum = int(n)
if n == 1:
if justTheWord:
return s
else:
return (u'%s %s' % (strNum, s))
else:
if justTheWord:
return u'%s%s' % (s, pl)
else:
return (u'%s %s%s' % (strNum, s, pl))
def parse_args(args=None):
if args is None:
args = [a.decode(DEFAULT_ENCODING) for a in sys.argv[1:]]
if Credentials().unixStyle:
usage = USAGE_FI if '-F' in args else USAGE
else:
usage = USAGE if '-U' in args else USAGE_FI
else:
usage = USAGE_FI
parser = OptionParser(usage=usage)
general = OptionGroup(parser, 'General options')
general.add_option('-a', '--about', action='append', default=[],
help='used to specify objects by about tag')
general.add_option('-i', '--id', action='append', default=[],
help='used to specify objects by ID')
general.add_option('-q', '--query', action='append', default=[],
help='used to specify objects with a FluidDB query')
general.add_option('-v', '--verbose', action='store_true', default=False,
help='encourages FDB to report what it\'s doing (verbose mode)')
general.add_option('-D', '--debug', action='store_true', default=False,
help='enables debug mode (more output)')
general.add_option('-T', '--timeout', type='float', default=HTTP_TIMEOUT,
metavar='n', help='sets the HTTP timeout to n seconds')
general.add_option('-U', '--unixstylepaths', action='store_true',
default=False,
help='Forces unix-style paths for tags and namespaces.')
general.add_option('-u', '--user', action='append', default=[],
help='used to specify a different user (credentials file)')
general.add_option('-F', '--fluidinfostylepaths', action='store_true',
default=False,
help='Forces Fluidinfo--style paths for tags and namespaces.')
general.add_option('-V', '--version', action='store_true',
default=False,
help='Report version number.')
general.add_option('-R', '--recurse', action='store_true',
default=False,
help='recursive (for ls and rm).')
general.add_option('-l', '--long', action='store_true',
default=False,
help='long listing (for ls).')
general.add_option('-L', '--longer', action='store_true',
default=False,
help='longer listing (for ls).')
general.add_option('-g', '--group', action='store_true',
default=False,
help='long listing with groups (for ls).')
general.add_option('-d', '--namespace', action='store_true',
default=False,
help='don\'t list namespace; just name of namespace.')
general.add_option('-P', '--policy', action='store_true',
default=False,
help='policy (i.e. default permission).')
general.add_option('-n', '--ns', action='store_true',
default=False,
help='don\'t list namespace; just name of namespace.')
general.add_option('-2', '--hightestverbosity', action='store_true',
default=False,
help='don\'t list namespace; just name of namespace.')
parser.add_option_group(general)
other = OptionGroup(parser, 'Other flags')
other.add_option('-s', '--sandbox', action='store_const',
dest='hostname', const=SANDBOX_PATH,
help='use the sandbox at http://sandbox.fluidinfo.com')
other.add_option('--hostname', default=FLUIDDB_PATH, dest='hostname',
help=('use the specified host (which should start http:// or '
'https://; http:// will be added if it doesn\'t) default '
'is %default'))
parser.add_option_group(other)
options, args = parser.parse_args(args)
if args == []:
action = 'version' if options.version else 'help'
else:
action, args = args[0], args[1:]
return action, args, options, parser
def execute_command_line(action, args, options, parser, user=None, pwd=None):
credentials = (Credentials(user or options.user[0], pwd)
if (user or options.user) else None)
if not action == 'ls':
db = FluidDB(host=options.hostname, credentials=credentials,
debug=options.debug, unixStylePaths=path_style(options))
ids_from_queries = chain(*imap(lambda q: get_ids_or_fail(q, db),
options.query))
ids = chain(options.id, ids_from_queries)
command_list = [
'help',
'version',
'commands',
'tag',
'untag',
'show',
'tags',
'ls',
'perms',
'pwd',
'pwn',
'test',
'testcli',
'testdb',
'testapi',
'whoami',
'su',
]
objs = [O({'mode': 'about', 'specifier': a}) for a in options.about] + \
[O({'mode': 'id', 'specifier': id}) for id in ids]
if action == 'version' or options.version:
Print('fdb %s' % version())
if action == 'version':
return
if action == 'help':
Print(USAGE if db.unixStyle else USAGE_FI)
elif action == 'commands':
Print(' '.join(command_list))
elif action not in command_list:
Print('Unrecognized command %s' % action)
elif (action.upper() not in HTTP_METHODS + ARGLESS_COMMANDS
and not args):
Print('Too few arguments for action %s' % action)
elif action == 'count':
Print('Total: %s' % (flags.Plural(len(objs), 'object')))
elif action == 'tags':
execute_tags_command(objs, db, options)
elif action in ('tag', 'untag', 'show'):
if not (options.about or options.query or options.id):
Print('You must use -q, -a or -i with %s' % action)
return
tags = args
if len(tags) == 0 and action != 'count':
nothing_to_do()
actions = {
'tag': execute_tag_command,
'untag': execute_untag_command,
'show': execute_show_command,
}
command = actions[action]
command(objs, db, args, options)
elif action == 'ls':
ls.execute_ls_command(objs, args, options, credentials)
elif action == 'chmod':
ls.execute_chmod_command(objs, args, options, credentials)
elif action == 'perms':
ls.execute_perms_command(objs, args, options, credentials)
elif action in ('pwd', 'pwn', 'whoami'):
execute_whoami_command(db)
elif action == 'su':
execute_su_command(db, args)
elif action in ['get', 'put', 'post', 'delete']:
execute_http_request(action, args, db, options)
else:
Print('Unrecognized command %s' % action)