Esempio n. 1
0
def get_created_direct_url(result: TestPipResult,
                           pkg: str) -> Optional[DirectUrl]:
    direct_url_path = get_created_direct_url_path(result, pkg)
    if direct_url_path:
        with open(direct_url_path) as f:
            return DirectUrl.from_json(f.read())
    return None
Esempio n. 2
0
def get_created_direct_url(result, pkg):
    direct_url_metadata_re = re.compile(pkg + r"-[\d\.]+\.dist-info." +
                                        DIRECT_URL_METADATA_NAME + r"$")
    for filename in result.files_created:
        if direct_url_metadata_re.search(filename):
            direct_url_path = result.test_env.base_path / filename
            with open(direct_url_path) as f:
                return DirectUrl.from_json(f.read())
    return None
Esempio n. 3
0
 def __init__(
     self,
     link: Link,
     persistent: bool,
 ):
     self.link = link
     self.persistent = persistent
     self.origin: Optional[DirectUrl] = None
     origin_direct_url_path = Path(
         self.link.file_path).parent / ORIGIN_JSON_NAME
     if origin_direct_url_path.exists():
         self.origin = DirectUrl.from_json(
             origin_direct_url_path.read_text())
Esempio n. 4
0
 def record_download_origin(cache_dir: str,
                            download_info: DirectUrl) -> None:
     origin_path = Path(cache_dir) / ORIGIN_JSON_NAME
     if origin_path.is_file():
         origin = DirectUrl.from_json(origin_path.read_text())
         # TODO: use DirectUrl.equivalent when https://github.com/pypa/pip/pull/10564
         # is merged.
         if origin.url != download_info.url:
             logger.warning(
                 "Origin URL %s in cache entry %s does not match download URL %s. "
                 "This is likely a pip bug or a cache corruption issue.",
                 origin.url,
                 cache_dir,
                 download_info.url,
             )
     origin_path.write_text(download_info.to_json(), encoding="utf-8")
Esempio n. 5
0
def test_freeze_pep610_editable(script: PipTestEnvironment) -> None:
    """
    Test that a package installed with a direct_url.json with editable=true
    is correctly frozeon as editable.
    """
    pkg_path = _create_test_package(script, name="testpkg")
    result = script.pip("install", pkg_path)
    direct_url_path = get_created_direct_url_path(result, "testpkg")
    assert direct_url_path
    # patch direct_url.json to simulate an editable install
    with open(direct_url_path) as f:
        direct_url = DirectUrl.from_json(f.read())
    assert isinstance(direct_url.info, DirInfo)
    direct_url.info.editable = True
    with open(direct_url_path, "w") as f:
        f.write(direct_url.to_json())
    result = script.pip("freeze")
    assert "# Editable Git install with no remote (testpkg==0.1)" in result.stdout
def dist_get_direct_url(dist):
    # type: (Distribution) -> Optional[DirectUrl]
    """Obtain a DirectUrl from a pkg_resource.Distribution.

    Returns None if the distribution has no `direct_url.json` metadata,
    or if `direct_url.json` is invalid.
    """
    if not dist.has_metadata(DIRECT_URL_METADATA_NAME):
        return None
    try:
        return DirectUrl.from_json(dist.get_metadata(DIRECT_URL_METADATA_NAME))
    except (DirectUrlValidationError, json.JSONDecodeError, UnicodeDecodeError) as e:
        logger.warning(
            "Error parsing %s for %s: %s",
            DIRECT_URL_METADATA_NAME,
            dist.project_name,
            e,
        )
        return None
Esempio n. 7
0
def test_list_pep610_editable(script: PipTestEnvironment) -> None:
    """
    Test that a package installed with a direct_url.json with editable=true
    is correctly listed as editable.
    """
    pkg_path = _create_test_package(script.scratch_path, name="testpkg")
    result = script.pip("install", pkg_path)
    direct_url_path = get_created_direct_url_path(result, "testpkg")
    assert direct_url_path
    # patch direct_url.json to simulate an editable install
    with open(direct_url_path) as f:
        direct_url = DirectUrl.from_json(f.read())
    assert isinstance(direct_url.info, DirInfo)
    direct_url.info.editable = True
    with open(direct_url_path, "w") as f:
        f.write(direct_url.to_json())
    result = script.pip("list", "--format=json")
    for item in json.loads(result.stdout):
        if item["name"] == "testpkg":
            assert item["editable_project_location"]
            break
    else:
        assert False, "package 'testpkg' not found in pip list result"
Esempio n. 8
0
    def direct_url(self) -> Optional[DirectUrl]:
        """Obtain a DirectUrl from this distribution.

        Returns None if the distribution has no `direct_url.json` metadata,
        or if `direct_url.json` is invalid.
        """
        try:
            content = self.read_text(DIRECT_URL_METADATA_NAME)
        except FileNotFoundError:
            return None
        try:
            return DirectUrl.from_json(content)
        except (
                UnicodeDecodeError,
                json.JSONDecodeError,
                DirectUrlValidationError,
        ) as e:
            logger.warning(
                "Error parsing %s for %s: %s",
                DIRECT_URL_METADATA_NAME,
                self.canonical_name,
                e,
            )
            return None
Esempio n. 9
0
def test_from_json():
    json = '{"url": "file:///home/user/project", "dir_info": {}}'
    direct_url = DirectUrl.from_json(json)
    assert direct_url.url == "file:///home/user/project"
    assert direct_url.info.editable is False
Esempio n. 10
0
def test_from_json() -> None:
    json = '{"url": "file:///home/user/project", "dir_info": {}}'
    direct_url = DirectUrl.from_json(json)
    assert direct_url.url == "file:///home/user/project"
    assert isinstance(direct_url.info, DirInfo)
    assert direct_url.info.editable is False