def rollup_resources(resources):
    """Rollup resources together: if a resource include multiple
    separate ones (i.e. is a rollup) and all the separate ones are
    included the rollup will be used instead.
    """
    potential_rollups = {}
    for resource in resources:
        for rollup in resource.rollups:
            s = potential_rollups.setdefault((rollup.library, rollup.
                relpath), set())
            s.add((resource.library, resource.relpath))
    result = set()
    for resource in resources:
        superseders = []
        for rollup in resource.rollups:
            s = potential_rollups[rollup.library, rollup.relpath]
            if len(s) == len(rollup.supersedes):
                superseders.append(rollup)
        if superseders:
            superseders = sorted(superseders, key=lambda i: len(i.supersedes))
            result.add(superseders[-1])
        else:
            result.add(resource)
    return result
