Ejemplo n.º 1
0
def execute_command_line(action, args, options, parser, user=None, pwd=None,
                         unixPaths=None, docbase=None, saveOut=False,
                         rawargs=None, cache=None, raiseErrors=False):
    credentials = (Credentials(user or options.user[0], pwd)
                   if (user or options.user) else None)
    unixPaths = (path_style(options) if path_style(options) is not None
                                     else unixPaths)
    db = ls.ExtendedFluidinfo(host=options.hostname, credentials=credentials,
                              debug=options.debug,
                              unixStylePaths=unixPaths,
                              saveOut=saveOut, cache=cache)
    quiet = (action == 'get')

    command_list = [
        'help',
        'version',
        'commands',
        'tag',
        'untag',
        'show',
        'get',
        'tags',
        'count',
        'ls',
        'rm',
        'perms',
        'pwd',
        'pwn',
        'rmdir',
        'rmns',
        'touch',
        'mkns',
        'mkdir',
        'amazon',
        'test',
        'testcli',
        'testdb',
        'testapi',
        'whoami',
        'su',
        'abouttag',
        'about',
        'normalize',
        'seq',
        'listseq',
        'mkseq',
        'alias',
        'unalias',
        'init',
        'search',
        'showcache',
        'sync',
        'quit',
        'exit',
    ]
    command_list.sort()

    # Expand aliases
    alias = db.cache.get_alias(action)
    if alias:
        words = cline.CScanSplit(alias, ' \t', quotes='"\'').words
        loc = rawargs.index(action)
        args = words + rawargs[:loc] + rawargs[loc + 1:]
        action, args, options, parser = parse_args(args)
        if options.verbose or options.debug:
            db.Print('Expanded to %s %s' % (action, u'   '.join(args)))
            db.Print('  with Query %s' % (options.query))

    ids_from_queries = chain(*imap(lambda q: get_ids_or_fail(q, db,
                                                             quiet=quiet),
        options.query))
    ids = chain(options.id, ids_from_queries)
    if options.anon:
        o = db.create_object()
        if type(o) == int:
           raise CommandError(u'Error Status: %d' % o) 
        else:
            ids = [o.id]
    objs = [O(about=a) for a in options.about] + [O(id=id) for id in ids]

    if action == 'version' or options.version:
        db.Print('fish %s' % version())
        if action == 'version':
            return
    
    try:
        retval = None
        if action == 'help':
            if args and args[0] in command_list:
                base = docbase or sys.path[0]
                path = os.path.join(base, 'doc/build/text/%s.txt' % args[0])
                f = open(path)
                s = f.read()
                db.Print(s.decode('UTF-8'))
                f.close()
            else:
                db.Print(USAGE_FISH if db.unixStyle else USAGE_FI)
        elif action == 'commands':
            db.Print(' '.join(command_list))
        elif action not in command_list:
            raise CommandError('Unrecognized command %s' % action)        
        elif (action.lower() not in ARGLESS_COMMANDS
              and not args and not options.anon):
            raise CommandError('Too few arguments for action %s' % action)
        elif action == 'count':
            db.Print('Total: %s' % (flags.Plural(len(objs), 'object')))
        elif action in ('tags', 'tag', 'untag', 'show', 'get'):
            if not (options.about or options.query or options.id
                    or options.anon):
                if args:
                    spec = args[0]
                    if re.match(UUID_RE, spec):
                        objs = [O(id=spec)]
                        options.id = [spec]
                    else:
                        objs = [O(about=spec)]
                        options.about = [spec]
                    args = args[1:]
                else:
                    raise CommandError('You must use -q or specify an about '
                                       'tag or object ID for the %s command.'
                                       % action)
                                       
            if action == 'tags':
                execute_tags_command(objs, db, options)
            elif objs:
                tags = args
                if len(tags) == 0 and action != 'count':
                    db.nothing_to_do('No tags specified')
                actions = {
                    'tag': execute_tag_command,
                    'untag': execute_untag_command,
                    'show': execute_show_command,
                    'get': execute_show_command,
                }
                command = actions[action]
                retval = command(objs, db, args, options, action)
        elif action == 'ls':
            ls.execute_ls_command(db, objs, args, options, credentials,
                                  unixPaths)
        elif action == 'rm':
            ls.execute_rm_command(db, objs, args, options, credentials,
                                  unixPaths)
        elif action in ('rmdir', 'rmns'):
            raise CommandError(u'Use rm to remove namespaces as well as tags')
        elif action == 'chmod':
            ls.execute_chmod_command(db, objs, args, options, credentials,
                                     unixPaths)
        elif action == 'perms':
            ls.execute_perms_command(db, objs, args, options, credentials,
                                     unixPaths)
        elif action in ('pwd', 'pwn', 'whoami'):
            execute_whoami_command(db)
        elif action == 'touch':
            execute_touch_command(db, args, options)
        elif action in ('mkns', 'mkdir'):
            execute_mkns_command(db, args, options)
        elif action == 'su':
            execute_su_command(db, args)
        elif action == 'amazon':
            execute_amazon_command(db, args)
        elif action in ('abouttag', 'about'):
            execute_abouttag_command(db, args)
        elif action == 'normalize':
            execute_normalize_command(db, args)
        elif action == 'seq':
            execute_seq_command(db, args, options)
        elif action == 'listseq':
            execute_listseq_command(db, args, options)
        elif action == 'mkseq':
            execute_mkseq_command(db, args, options)
        elif action == 'alias':
            execute_alias_command(db, args, options)
        elif action == 'unalias':
            execute_unalias_command(db, args, options)
        elif action == 'showcache':
            execute_showcache_command(db, args, options)
        elif action == 'sync':
            execute_sync_command(db, args, options)
        elif action == 'init':
            execute_init_command(db, args, options)
        elif action == 'search':
            execute_search_command(db, args, options)
        elif action in ('quit', 'exit'):
            pass
        else:
            raise CommandError('Unrecognized command %s' % action)
    except Exception, e:
        if raiseErrors:
            raise
        elif options.debug:
            db.Print(unicode(traceback.format_exc()))
        db.Print(u'Fish failure:\n  %s' % unicode(e))
Ejemplo n.º 2
0
Archivo: cli.py Proyecto: njr0/fdb
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)