def whitelist_licenses_policy(policy_name: str, allowed_licenses: set):
    """A policy factory for making license-based whitelist policies.

    To apply in project, include the function returned from this factory
    in the ilst returned by the `get_policies` function implemented in the
    project `YSettings` file.

    The factory returns a policy function named
    `whitelist_{policy_name}_licenses` that applies to targets with
    `policy_name` in their policies list.
    The returned policy asserts that all licenses contained in the target
    (including through explicit & implicit dependencies) are in the whitelist
    defined by `allowed_licenses`.

    See example in tests/errors.
    """

    def policy_func(build_context, target):
        """whitelist_{policy_name}_licenses policy function.

        Return error message (string) if policy for `target` is violated,
        otherwise return `None`.
        """
        if policy_name in target.props.policies:
            licenses = set(target.props.license)
            for dep in build_context.generate_all_deps(target):
                licenses.update(dep.props.license)
            licenses.difference_update(allowed_licenses)
            if licenses:
                return 'Invalid licenses for {} policy: {}'.format(
                    policy_name, ', '.join(sorted(licenses)))
        return None

    policy_func.__name__ = 'whitelist_{}_licenses'.format(policy_name)
    return policy_func