コード例 #1
0
def interpret(cmd, arguments, parts, shorthands, outdir):
    """Perform command required by user."""
    if cmd == 'help':
        print 'Available commands:'
        for name, desc in commands:
            print '    %s%s' % (name.ljust(28), desc)
    elif cmd == 'tree':
        if len(arguments) != 1:
            print 'Wrong number of parameters!'
        else:
            part = check_valid_part(arguments[0], parts, shorthands)
            if part is not None:
                print '-' * 10
                print utils.tree_folder(part.root)
                print utils.tree_folder(part.lost)
                print '-' * 10
    elif cmd == 'bodyfile':
        if len(arguments) != 2:
            print 'Wrong number of parameters!'
        else:
            part = check_valid_part(arguments[0], parts, shorthands)
            if part is not None:
                contents = ['# ---' + repr(part) + '---', '# Full paths'
                            ] + utils.bodyfile_folder(part.root) + [
                                '# \n# Orphaned files'
                            ] + utils.bodyfile_folder(part.lost)
                fname = os.path.join(outdir, arguments[1])
                try:
                    with codecs.open(fname, 'w', encoding='utf8') as outfile:
                        outfile.write('\n'.join(contents))
                        print 'Saved body file to %s' % fname
                except IOError:
                    print 'Cannot open file %s for output!' % fname
    elif cmd == 'csv':
        if len(arguments) != 2:
            print 'Wrong number of parameters!'
        else:
            part = check_valid_part(arguments[0], parts, shorthands)
            if part is not None:
                contents = utils.csv_part(part)
                fname = os.path.join(outdir, arguments[1])
                try:
                    with codecs.open(fname, 'w', encoding='utf8') as outfile:
                        outfile.write('\n'.join(contents))
                        print 'Saved CSV file to %s' % fname
                except IOError:
                    print 'Cannot open file %s for output!' % fname
    elif cmd == 'tikzplot':
        if len(arguments) not in (1, 2):
            print 'Wrong number of parameters!'
        else:
            part = check_valid_part(arguments[0], parts, shorthands)
            if part is not None:
                if len(arguments) > 1:
                    fname = os.path.join(outdir, arguments[1])
                    try:
                        with codecs.open(fname, 'w') as outfile:
                            outfile.write(utils.tikz_part(part) + '\n')
                            print 'Saved Tikz code to %s' % fname
                    except IOError:
                        print 'Cannot open file %s for output!' % fname
                else:
                    print utils.tikz_part(part)
    elif cmd == 'restore':
        if len(arguments) != 2:
            print 'Wrong number of parameters!'
        else:
            partid = arguments[0]
            part = check_valid_part(partid, parts, shorthands)
            if part is not None:
                index = arguments[1]
                partition_dir = os.path.join(outdir, 'Partition' + str(partid))
                myfile = None
                try:
                    indexi = int(index)
                except ValueError:
                    indexi = index
                for i in [index, indexi]:
                    myfile = part.get(i, myfile)
                if myfile is None:
                    print 'The index is not valid'
                else:
                    logic.recursive_restore(myfile, part, partition_dir)
    elif cmd == 'locate':
        if len(arguments) != 2:
            print 'Wrong number of parameters!'
        else:
            part = check_valid_part(arguments[0], parts, shorthands)
            if part is not None:
                text = arguments[1]
                results = utils.locate(part, text)
                for node, path in results:
                    desc = (' [GHOST]' if node.is_ghost else
                            ' [DELETED]' if node.is_deleted else '')
                    print "[%s]: %s%s" % (node.index, path, desc)
    elif cmd == 'merge':
        if len(arguments) != 2:
            print 'Wrong number of parameters!'
        else:
            part1 = check_valid_part(arguments[0],
                                     parts,
                                     shorthands,
                                     rebuild=False)
            part2 = check_valid_part(arguments[1],
                                     parts,
                                     shorthands,
                                     rebuild=False)
            if None in (part1, part2):
                return
            if part1.fs_type != part2.fs_type:
                print 'Cannot merge partitions with types (%s, %s)' % (
                    part1.fs_type, part2.fs_type)
                return
            print 'Merging partitions...'
            utils.merge(part1, part2)
            position = int(arguments[1])
            _, par = shorthands[position]
            del shorthands[position]
            del parts[par]
            try:
                rebuilt.remove(par)
            except:
                pass
            print 'There are now %d partitions.' % (len(parts), )
    elif cmd == 'recoverable':
        list_parts(parts, shorthands, lambda x: x.recoverable)
    elif cmd == 'other':
        list_parts(parts, shorthands, lambda x: not x.recoverable)
    elif cmd == 'allparts':
        list_parts(parts, shorthands, lambda x: True)
    elif cmd == 'quit':
        exit(0)
    else:
        print 'Unknown command.'
コード例 #2
0
ファイル: main.py プロジェクト: risusanto/RecuperaBit
def interpret(cmd, arguments, parts, shorthands, outdir):
    """Perform command required by user."""
    if cmd == 'help':
        print 'Available commands:'
        for name, desc in commands:
            print '    %s%s' % (name.ljust(28), desc)
    elif cmd == 'tree':
        if len(arguments) != 1:
            print 'Wrong number of parameters!'
        else:
            part = check_valid_part(arguments[0], parts, shorthands)
            if part is not None:
                print '-'*10
                print utils.tree_folder(part.root)
                print utils.tree_folder(part.lost)
                print '-'*10
    elif cmd == 'bodyfile':
        if len(arguments) != 2:
            print 'Wrong number of parameters!'
        else:
            part = check_valid_part(arguments[0], parts, shorthands)
            if part is not None:
                contents = [
                    '# ---' + repr(part) + '---',
                    '# Full paths'
                ] + utils.bodyfile_folder(part.root) + [
                    '# \n# Orphaned files'
                ] + utils.bodyfile_folder(part.lost)
                fname = os.path.join(outdir, arguments[1])
                try:
                    with codecs.open(fname, 'w', encoding='utf8') as outfile:
                        outfile.write('\n'.join(contents))
                        print 'Saved body file to %s' % fname
                except IOError:
                    print 'Cannot open file %s for output!' % fname
    elif cmd == 'csv':
        if len(arguments) != 2:
            print 'Wrong number of parameters!'
        else:
            part = check_valid_part(arguments[0], parts, shorthands)
            if part is not None:
                contents = utils.csv_part(part)
                fname = os.path.join(outdir, arguments[1])
                try:
                    with codecs.open(fname, 'w', encoding='utf8') as outfile:
                        outfile.write(
                            '\n'.join(contents)
                        )
                        print 'Saved CSV file to %s' % fname
                except IOError:
                    print 'Cannot open file %s for output!' % fname
    elif cmd == 'tikzplot':
        if len(arguments) not in (1, 2):
            print 'Wrong number of parameters!'
        else:
            part = check_valid_part(arguments[0], parts, shorthands)
            if part is not None:
                if len(arguments) > 1:
                    fname = os.path.join(outdir, arguments[1])
                    try:
                        with codecs.open(fname, 'w') as outfile:
                            outfile.write(utils.tikz_part(part) + '\n')
                            print 'Saved Tikz code to %s' % fname
                    except IOError:
                        print 'Cannot open file %s for output!' % fname
                else:
                    print utils.tikz_part(part)
    elif cmd == 'restore':
        if len(arguments) != 2:
            print 'Wrong number of parameters!'
        else:
            partid = arguments[0]
            part = check_valid_part(partid, parts, shorthands)
            if part is not None:
                index = arguments[1]
                partition_dir = os.path.join(outdir, 'Partition' + str(partid))
                myfile = None
                try:
                    indexi = int(index)
                except ValueError:
                    indexi = index
                for i in [index, indexi]:
                    myfile = part.get(i, myfile)
                if myfile is None:
                    print 'The index is not valid'
                else:
                    logic.recursive_restore(myfile, part, partition_dir)
    elif cmd == 'locate':
        if len(arguments) != 2:
            print 'Wrong number of parameters!'
        else:
            part = check_valid_part(arguments[0], parts, shorthands)
            if part is not None:
                text = arguments[1]
                results = utils.locate(part, text)
                for node, path in results:
                    desc = (
                        ' [GHOST]' if node.is_ghost else
                        ' [DELETED]' if node.is_deleted else ''
                    )
                    print "[%s]: %s%s" % (node.index, path, desc)
    elif cmd == 'traceback':
        if len(arguments) != 2:
            print 'Wrong number of parameters!'
        else:
            partid = arguments[0]
            part = check_valid_part(partid, parts, shorthands)
            if part is not None:
                index = arguments[1]
                myfile = None
                try:
                    indexi = int(index)
                except ValueError:
                    indexi = index
                for i in [index, indexi]:
                    myfile = part.get(i, myfile)
                if myfile is None:
                    print 'The index is not valid'
                else:
                    while myfile is not None:
                        print "[{}] {}".format(myfile.index, myfile.full_path(part))
                        myfile = part.get(myfile.parent)
    elif cmd == 'merge':
        if len(arguments) != 2:
            print 'Wrong number of parameters!'
        else:
            part1 = check_valid_part(arguments[0], parts, shorthands, rebuild=False)
            part2 = check_valid_part(arguments[1], parts, shorthands, rebuild=False)
            if None in (part1, part2):
                return
            if part1.fs_type != part2.fs_type:
                print 'Cannot merge partitions with types (%s, %s)' % (part1.fs_type, part2.fs_type)
                return
            print 'Merging partitions...'
            utils.merge(part1, part2)
            source_position = int(arguments[1])
            destination_position = int(arguments[0])
            _, par_source = shorthands[source_position]
            _, par_destination = shorthands[destination_position]
            del shorthands[source_position]
            del parts[par_source]
            for par in (par_source, par_destination):
                try:
                    rebuilt.remove(par)
                except:
                    pass
            print 'There are now %d partitions.' % (len(parts), )
    elif cmd == 'recoverable':
        list_parts(parts, shorthands, lambda x: x.recoverable)
    elif cmd == 'other':
        list_parts(parts, shorthands, lambda x: not x.recoverable)
    elif cmd == 'allparts':
        list_parts(parts, shorthands, lambda x: True)
    elif cmd == 'quit':
        exit(0)
    else:
        print 'Unknown command.'