import functools
import collections


def group_artifacts_by_provider(artifacts_with_providers):
    """
    Return a dictionary with providers as keys and the set of artifact to check
    for each one.

    Given::

        [(artifact1, (provider1, provider2)),
         (artifact2, (provider2)),
         (artifact3, (provider1, provider3))]

    Returns::

        {provider1: {artifact1, artifact3},
         provider2: {artifact1, artifact2},
         provider3: {artifact3}}

    """
    def _group_by_provider(acc, entry):
        artifact, providers = entry
        for cprov in providers:
            acc[cprov].add(artifact)
        return acc

    return functools.reduce(
        _group_by_provider,
        artifacts_with_providers,
        collections.defaultdict(set))