Beispiel #1
0
Datei: wheel.py Projekt: dwt/pip
def pkg_resources_distribution_for_wheel(wheel_zip, name, location):
    # type: (ZipFile, str, str) -> Distribution
    """Get a pkg_resources distribution given a wheel.

    :raises UnsupportedWheel: on any errors
    """
    info_dir, _ = parse_wheel(wheel_zip, name)

    metadata_files = [
        p for p in wheel_zip.namelist() if p.startswith(f"{info_dir}/")
    ]

    metadata_text = {}  # type: Dict[str, bytes]
    for path in metadata_files:
        _, metadata_name = path.split("/", 1)

        try:
            metadata_text[metadata_name] = read_wheel_metadata_file(
                wheel_zip, path)
        except UnsupportedWheel as e:
            raise UnsupportedWheel("{} has an invalid wheel, {}".format(
                name, str(e)))

    metadata = WheelMetadata(metadata_text, location)

    return DistInfoDistribution(location=location,
                                metadata=metadata,
                                project_name=name)
Beispiel #2
0
def pkg_resources_distribution_for_wheel(wheel_zip, name, location):
    # type: (ZipFile, str, str) -> Distribution
    """Get a pkg_resources distribution given a wheel.

    :raises UnsupportedWheel: on any errors
    """
    info_dir, _ = parse_wheel(wheel_zip, name)

    metadata_files = [
        p for p in wheel_zip.namelist() if p.startswith(f"{info_dir}/")
    ]

    metadata_text = {}  # type: Dict[str, bytes]
    for path in metadata_files:
        # If a flag is set, namelist entries may be unicode in Python 2.
        # We coerce them to native str type to match the types used in the rest
        # of the code. This cannot fail because unicode can always be encoded
        # with UTF-8.
        full_path = ensure_str(path)
        _, metadata_name = full_path.split("/", 1)

        try:
            metadata_text[metadata_name] = read_wheel_metadata_file(
                wheel_zip, full_path)
        except UnsupportedWheel as e:
            raise UnsupportedWheel("{} has an invalid wheel, {}".format(
                name, str(e)))

    metadata = WheelMetadata(metadata_text, location)

    return DistInfoDistribution(location=location,
                                metadata=metadata,
                                project_name=name)
def test_dict_metadata_works():
    name = "simple"
    version = "0.1.0"
    require_a = "a==1.0"
    require_b = "b==1.1; extra == 'also_b'"
    requires = [require_a, require_b, "c==1.2; extra == 'also_c'"]
    extras = ["also_b", "also_c"]
    requires_python = ">=3"

    metadata = Message()
    metadata["Name"] = name
    metadata["Version"] = version
    for require in requires:
        metadata["Requires-Dist"] = require
    for extra in extras:
        metadata["Provides-Extra"] = extra
    metadata["Requires-Python"] = requires_python

    inner_metadata = DictMetadata({
        "METADATA": ensure_binary(metadata.as_string())
    })
    dist = DistInfoDistribution(
        location="<in-memory>", metadata=inner_metadata, project_name=name
    )

    assert name == dist.project_name
    assert version == dist.version
    assert set(extras) == set(dist.extras)
    assert [Requirement.parse(require_a)] == dist.requires([])
    assert [
        Requirement.parse(require_a), Requirement.parse(require_b)
    ] == dist.requires(["also_b"])
    assert metadata.as_string() == get_metadata(dist).as_string()
    assert requires_python == get_requires_python(dist)