Exemplo n.º 1
0
def print_source_groups(source, level, xset, opts):
    """
    Print groups from a source, a level of verbosity and an optional
    nodeset acting as a filter.
    """
    # list groups of some specified nodes?
    if opts.all or xset or opts.and_nodes or opts.sub_nodes or opts.xor_nodes:
        # When some node sets are provided as argument, the list command
        # retrieves node groups these nodes belong to, thanks to the
        # groups() method.
        # Note: stdin support is enabled when '-' is found.
        groups = xset.groups(source, opts.groupbase)
        for group, (gnodes, inodes) in groups.iteritems():
            if level == 1:
                print group
            elif level == 2:
                print "%s %s" % (group, inodes)
            else:
                print "%s %s %d/%d" % (group, inodes, len(inodes),
                                       len(gnodes))
    else:
        # "raw" group list when no argument at all
        for group in grouplist(source):
            if source and not opts.groupbase:
                nsgroup = "@%s:%s" % (source, group)
            else:
                nsgroup = "@%s" % group
            if level == 1:
                print nsgroup
            else:
                nodes = NodeSet(nsgroup)
                if level == 2:
                    print "%s %s" % (nsgroup, nodes)
                else:
                    print "%s %s %d" % (nsgroup, nodes, len(nodes))
Exemplo n.º 2
0
def command_list(options, xset):
    """List (-l/-ll/-lll) command handler."""
    list_level = options.list
    # list groups of some specified nodes?
    if options.all or xset or \
        options.and_nodes or options.sub_nodes or options.xor_nodes:
        # When some node sets are provided as argument, the list command
        # retrieves node groups these nodes belong to, thanks to the
        # groups() method (new in 1.6). Note: stdin support is enabled
        # when the '-' special character is encountered.
        groups = xset.groups(options.groupsource, options.groupbase)
        for group, (gnodes, inodes) in groups.iteritems():
            if list_level == 1:         # -l
                print group
            elif list_level == 2:       # -ll
                print "%s %s" % (group, inodes)
            else:                       # -lll
                print "%s %s %d/%d" % (group, inodes, len(inodes), \
                                       len(gnodes))
        return
    # "raw" group list when no argument at all
    for group in grouplist(options.groupsource):
        if options.groupsource and not options.groupbase:
            nsgroup = "@%s:%s" % (options.groupsource, group)
        else:
            nsgroup = "@%s" % group
        if list_level == 1:         # -l
            print nsgroup
        else:
            nodes = NodeSet(nsgroup)
            if list_level == 2:     # -ll
                print "%s %s" % (nsgroup, nodes)
            else:                   # -lll
                print "%s %s %d" % (nsgroup, nodes, len(nodes))
Exemplo n.º 3
0
def command_list(options, xset):
    """List (-l/-ll/-lll) command handler."""
    list_level = options.list
    # list groups of some specified nodes?
    if options.all or xset or \
        options.and_nodes or options.sub_nodes or options.xor_nodes:
        # When some node sets are provided as argument, the list command
        # retrieves node groups these nodes belong to, thanks to the
        # groups() method (new in 1.6). Note: stdin support is enabled
        # when the '-' special character is encountered.
        groups = xset.groups(options.groupsource, options.groupbase)
        for group, (gnodes, inodes) in groups.iteritems():
            if list_level == 1:  # -l
                print group
            elif list_level == 2:  # -ll
                print "%s %s" % (group, inodes)
            else:  # -lll
                print "%s %s %d/%d" % (group, inodes, len(inodes), \
                                       len(gnodes))
        return
    # "raw" group list when no argument at all
    for group in grouplist(options.groupsource):
        if options.groupsource and not options.groupbase:
            nsgroup = "@%s:%s" % (options.groupsource, group)
        else:
            nsgroup = "@%s" % group
        if list_level == 1:  # -l
            print nsgroup
        else:
            nodes = NodeSet(nsgroup)
            if list_level == 2:  # -ll
                print "%s %s" % (nsgroup, nodes)
            else:  # -lll
                print "%s %s %d" % (nsgroup, nodes, len(nodes))
Exemplo n.º 4
0
def print_source_groups(source, level, xset, opts):
    """
    Print groups from a source, a level of verbosity and an optional
    nodeset acting as a filter.
    """
    # list groups of some specified nodes?
    if opts.all or xset or opts.and_nodes or opts.sub_nodes or opts.xor_nodes:
        # When some node sets are provided as argument, the list command
        # retrieves node groups these nodes belong to, thanks to the
        # groups() method.
        # Note: stdin support is enabled when '-' is found.
        groups = xset.groups(source, opts.groupbase)
        for group, (gnodes, inodes) in groups.iteritems():
            if level == 1:
                print group
            elif level == 2:
                print "%s %s" % (group, inodes)
            else:
                print "%s %s %d/%d" % (group, inodes, len(inodes), len(gnodes))
    else:
        # "raw" group list when no argument at all
        for group in grouplist(source):
            if source and not opts.groupbase:
                nsgroup = "@%s:%s" % (source, group)
            else:
                nsgroup = "@%s" % group
            if level == 1:
                print nsgroup
            else:
                nodes = NodeSet(nsgroup)
                if level == 2:
                    print "%s %s" % (nsgroup, nodes)
                else:
                    print "%s %s %d" % (nsgroup, nodes, len(nodes))
Exemplo n.º 5
0
def nodeset():
    """script subroutine"""
    class_set = NodeSet
    usage = "%prog [COMMAND] [OPTIONS] [ns1 [-ixX] ns2|...]"

    parser = OptionParser(usage)
    parser.install_nodeset_commands()
    parser.install_nodeset_operations()
    parser.install_nodeset_options()
    (options, args) = parser.parse_args()

    if options.debug:
        STD_GROUP_RESOLVER.set_verbosity(1)

    # Check for command presence
    cmdcount = int(options.count) + int(options.expand) + \
               int(options.fold) + int(bool(options.list)) + \
               int(options.regroup) + int(options.groupsources)
    if not cmdcount:
        parser.error("No command specified.")
    elif cmdcount > 1:
        parser.error("Multiple commands not allowed.")

    if options.rangeset:
        class_set = RangeSet

    if options.all or options.regroup:
        assert class_set == NodeSet, "-a/-r only supported in NodeSet mode"

    if options.groupsource and not options.quiet and \
       (class_set == RangeSet or options.groupsources):
        print >> sys.stderr, "WARNING: option group source \"%s\" ignored" \
                                % options.groupsource

    # The list command doesn't need any NodeSet, check for it first.
    if options.list > 0:
        list_level = options.list
        for group in grouplist(options.groupsource):
            if options.groupsource and not options.groupbase:
                nsgroup = "@%s:%s" % (options.groupsource, group)
            else:
                nsgroup = "@%s" % group
            if list_level == 1:
                print nsgroup
            else:
                nodes = NodeSet(nsgroup)
                if list_level == 2:     # -ll ?
                    print "%s %s" % (nsgroup, nodes)
                else:                   # -lll ?
                    print "%s %s %d" % (nsgroup, nodes, len(nodes))
        return
    # Also, the groupsources command simply lists group sources.
    elif options.groupsources:
        if options.quiet:
            dispdefault = ""    # don't show (default) if quiet is set
        else:
            dispdefault = " (default)"
        for src in STD_GROUP_RESOLVER.sources():
            print "%s%s" % (src, dispdefault)
            dispdefault = ""
        return

    # We want -s <groupsource> to act as a substition of default groupsource
    # (ie. it's not necessary to prefix group names by this group source).
    if options.groupsource:
        STD_GROUP_RESOLVER.default_sourcename = options.groupsource

    # Instantiate RangeSet or NodeSet object
    xset = class_set(autostep=options.autostep)

    if options.all:
        # Include all nodes from external node groups support.
        xset.update(NodeSet.fromall()) # uses default_sourcename
    elif not args:
        # No need to specify '-' to read stdin if no argument at all.
        process_stdin(xset.update, xset.__class__, options.autostep)

    # Apply first operations (before first non-option)
    for nodes in options.and_nodes:
        if nodes == '-':
            process_stdin(xset.intersection_update, xset.__class__,
                          options.autostep)
        else:
            xset.intersection_update(class_set(nodes,
                                               autostep=options.autostep))
    for nodes in options.sub_nodes:
        if nodes == '-':
            process_stdin(xset.difference_update, xset.__class__,
                          options.autostep)
        else:
            xset.difference_update(class_set(nodes, autostep=options.autostep))
    for nodes in options.xor_nodes:
        if nodes == '-':
            process_stdin(xset.symmetric_difference_update, xset.__class__,
                          options.autostep)
        else:
            xset.symmetric_difference_update(class_set(nodes, \
                                             autostep=options.autostep))

    # Finish xset computing from args
    compute_nodeset(xset, args, options.autostep)

    # Interprate special characters (may raise SyntaxError)
    separator = eval('\'%s\'' % options.separator, {"__builtins__":None}, {})

    if options.slice_rangeset:
        _xset = class_set()
        for sli in RangeSet(options.slice_rangeset).slices(False):
            _xset.update(xset[sli])
        xset = _xset

    # Display result according to command choice
    if options.expand:
        xsubres = separator.join
    elif options.fold:
        xsubres = lambda x: x
    elif options.regroup:
        xsubres = lambda x: x.regroup(options.groupsource, \
                                      noprefix=options.groupbase)
    else:
        xsubres = len

    if not xset or options.maxsplit <= 1:
        print xsubres(xset)
    else:
        for xsubset in xset.split(options.maxsplit):
            print xsubres(xsubset)