Example #1
0
def is_distributed_item(item_public_id: PublicId) -> bool:
    """
    Check whether the item public id correspond to a package in the distribution.

    If the provided item has version 'latest', only the prefixes are compared.
    Otherwise, the function will try to match the exact version occurrence among the distributed packages.
    """
    if item_public_id.package_version.is_latest:
        return any(
            item_public_id.same_prefix(other)
            for other in DISTRIBUTED_PACKAGES)
    return item_public_id in DISTRIBUTED_PACKAGES
Example #2
0
def _check_package_public_id(source_path, item_type, item_id) -> None:
    # we load only based on item_name, hence also check item_version and item_author match.
    config = load_yaml(os.path.join(source_path, item_type + ".yaml"))
    item_author = config.get("author", "")
    item_name = config.get("name", "")
    item_version = config.get("version", "")
    actual_item_id = PublicId(item_author, item_name, item_version)
    if not actual_item_id.same_prefix(item_id) or (
            not item_id.package_version.is_latest
            and item_id.version != actual_item_id.version):
        raise click.ClickException(
            "Version, name or author does not match. Expected '{}', found '{}'"
            .format(item_id,
                    item_author + "/" + item_name + ":" + item_version))
Example #3
0
    def fetch(  # pylint: disable=arguments-differ
            self, public_id: PublicId) -> Optional[Item]:
        """
        Fetch an item associated with a public id.

        :param public_id: the public id.
        :return: an item, or None if the key is not present.
        """
        if public_id.package_version.is_latest:
            filtered_records: List[Tuple[PublicId, Item]] = list(
                filter(
                    lambda x: public_id.same_prefix(x[0]),
                    self._public_id_to_item.items(),
                ))
            if len(filtered_records) == 0:
                return None
            return max(filtered_records, key=itemgetter(0))[1]
        return self._public_id_to_item.get(public_id, None)
Example #4
0
def test_pubic_id_same_prefix():
    """Test PublicId.same_prefix"""
    same_1 = PublicId("author", "name", "0.1.0")
    same_2 = PublicId("author", "name", "0.1.1")
    different = PublicId("author", "different_name", "0.1.0")

    assert same_1.same_prefix(same_2)
    assert same_2.same_prefix(same_1)

    assert not different.same_prefix(same_1)
    assert not same_1.same_prefix(different)

    assert not different.same_prefix(same_2)
    assert not same_2.same_prefix(different)