Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pypi repository on the local disk (file:// urls) #538

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions conda_lock/_vendor/requests-file.LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 121 additions & 0 deletions conda_lock/_vendor/requests_file.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions conda_lock/_vendor/requests_file.pyi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions conda_lock/_vendor/vendor.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions conda_lock/interfaces/vendored_poetry.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from conda_lock._vendor.poetry.config.config import Config
from conda_lock._vendor.poetry.core.packages import Dependency as PoetryDependency
from conda_lock._vendor.poetry.core.packages import Package as PoetryPackage
from conda_lock._vendor.poetry.core.packages import (
Expand All @@ -9,16 +10,22 @@
from conda_lock._vendor.poetry.installation.chooser import Chooser
from conda_lock._vendor.poetry.installation.operations.uninstall import Uninstall
from conda_lock._vendor.poetry.puzzle import Solver as PoetrySolver
from conda_lock._vendor.poetry.repositories.legacy_repository import LegacyRepository
from conda_lock._vendor.poetry.repositories.pool import Pool
from conda_lock._vendor.poetry.repositories.pypi_repository import PyPiRepository
from conda_lock._vendor.poetry.repositories.repository import Repository
from conda_lock._vendor.poetry.utils.env import Env
from conda_lock._vendor.poetry.utils.helpers import get_cert, get_client_cert


__all__ = [
"get_cert",
"get_client_cert",
"Chooser",
"Config",
"Env",
"Factory",
"LegacyRepository",
"PoetryDependency",
"PoetryPackage",
"PoetryProjectPackage",
Expand Down
6 changes: 6 additions & 0 deletions conda_lock/interfaces/vendored_requests_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from conda_lock._vendor.requests_file import FileAdapter # type: ignore


__all__ = [
"FileAdapter",
]
45 changes: 37 additions & 8 deletions conda_lock/pypi_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
from pathlib import Path
from posixpath import expandvars
from typing import TYPE_CHECKING, Dict, List, Optional
from urllib.parse import urldefrag, urlparse, urlsplit, urlunsplit
from urllib.parse import urldefrag, urlsplit, urlunsplit

import requests

from clikit.api.io.flags import VERY_VERBOSE
from clikit.io import ConsoleIO, NullIO
from packaging.tags import compatible_tags, cpython_tags, mac_platforms

from conda_lock.interfaces.vendored_poetry import (
Chooser,
Config,
Env,
Factory,
LegacyRepository,
PoetryDependency,
PoetryPackage,
PoetryProjectPackage,
Expand All @@ -24,7 +28,10 @@
PyPiRepository,
Repository,
Uninstall,
get_cert,
get_client_cert,
)
from conda_lock.interfaces.vendored_requests_file import FileAdapter
from conda_lock.lockfile import apply_categories
from conda_lock.lockfile.v2prelim.models import (
DependencySource,
Expand Down Expand Up @@ -384,6 +391,28 @@ def solve_pypi(
return {dep.name: dep for dep in requirements}


class CondaLockLegacyRepository(LegacyRepository):
@property
def session(self) -> requests.Session:
_session = super().session
_session.mount("file://", FileAdapter())
return _session


def create_legacy_repository(
source: Dict[str, str], auth_config: Config
) -> LegacyRepository:
name = source["name"]
url = source["url"]
return CondaLockLegacyRepository(
name,
url,
config=auth_config,
cert=get_cert(auth_config, name),
client_cert=get_client_cert(auth_config, name),
)


def _prepare_repositories_pool(
allow_pypi_requests: bool, pip_repositories: Optional[List[PipRepository]] = None
) -> Pool:
Expand All @@ -397,13 +426,13 @@ def _prepare_repositories_pool(
"""
factory = Factory()
config = factory.create_config()
repos = [
factory.create_legacy_repository(
{"name": pip_repository.name, "url": expandvars(pip_repository.url)},
config,
)
for pip_repository in pip_repositories or []
] + [

legacy_repos = []
for pip_repository in pip_repositories or []:
repo_cfg = {"name": pip_repository.name, "url": expandvars(pip_repository.url)}
legacy_repo = create_legacy_repository(repo_cfg, config)
legacy_repos.append(legacy_repo)
repos = legacy_repos + [
factory.create_legacy_repository(
{"name": source[0], "url": source[1]["url"]}, config
)
Expand Down
13 changes: 13 additions & 0 deletions conda_lock/scripts/vendor_poetry/patches/requests-file.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git b/conda_lock/_vendor/requests_file.py a/conda_lock/_vendor/requests_file.py
index d9bf5b8..e850ed0 100644
--- b/conda_lock/_vendor/requests_file.py
+++ a/conda_lock/_vendor/requests_file.py
@@ -82,6 +82,8 @@ class FileAdapter(BaseAdapter):
if path_drive and not os.path.splitdrive(path):
path = os.sep + os.path.join(path_drive, *path_parts)

+ if os.path.isdir(path):
+ path = os.path.join(path, "index.html")
# Use io.open since we need to add a release_conn method, and
# methods can't be added to file objects in python 2.
resp.raw = io.open(path, "rb")
11 changes: 11 additions & 0 deletions tests/test-local-pip-repository/environment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
channels:
- conda-forge
pip-repositories:
- file://$PYPI_FILE_URL/
dependencies:
- python
- pip:
- fake-private-package==1.0.0
- six
platforms:
- linux-64
109 changes: 109 additions & 0 deletions tests/test_local_pip_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import shutil

from pathlib import Path
from urllib.parse import urlparse

import pytest

from conda_lock.conda_lock import DEFAULT_LOCKFILE_NAME, run_lock
from conda_lock.lockfile import parse_conda_lock_file
from tests.test_conda_lock import clone_test_dir

# This is a fixture, so we need to import it:
from tests.test_pip_repositories import private_package_tar # pyright: ignore


_PRIVATE_REPO_ROOT_INDEX = """<!DOCTYPE html>
<html>
<body>
<a href="fake-private-package/index.html">fake-private-package</a>
</body>
</html>
"""

_PRIVATE_REPO_PACKAGE_INDEX = """<!DOCTYPE html>
<html>
<body>
<a href="file://$PYPI_FILE_URL/fake-private-package/fake-private-package-1.0.0.tar.gz">fake-private-package-1.0.0.tar.gz</a>
</body>
</html>
"""


@pytest.fixture
def local_private_pypi(private_package_tar: Path, tmp_path: Path): # noqa: F811
"""
Create a local package index with the following directory structure:
tmp_path
└── repo
├── fake-private-package
│ ├── fake-private-package-1.0.0.tar.gz
│ └── index.html
└── index.html
"""
repo_dir = tmp_path / "repo"
pkg_dir = repo_dir / "fake-private-package"
pkg_dir.mkdir(parents=True, exist_ok=True)

tar_path = pkg_dir / private_package_tar.name
shutil.copy(private_package_tar, tar_path)

with open(repo_dir / "index.html", "w") as repo_index:
repo_index.write(_PRIVATE_REPO_ROOT_INDEX)

_repo_package_index_html = _PRIVATE_REPO_PACKAGE_INDEX.replace(
"$PYPI_FILE_URL", str(repo_dir)
)
with open(pkg_dir / "index.html", "w") as pkg_index:
pkg_index.write(_repo_package_index_html)
return repo_dir


@pytest.fixture
def local_pip_repository_environment_file(
local_private_pypi: Path, tmp_path: Path
) -> Path:
environment_file = (
clone_test_dir("test-local-pip-repository", tmp_path) / "environment.yaml"
)
templated_env_yml = environment_file.read_text()
env_yml = templated_env_yml.replace("$PYPI_FILE_URL", str(local_private_pypi))
environment_file.write_text(env_yml)
return environment_file


def test_it_installs_packages_from_private_pip_repository_on_local_disk(
monkeypatch: "pytest.MonkeyPatch",
conda_exe: str,
tmp_path: Path,
local_pip_repository_environment_file: Path,
):
# WHEN I create the lockfile
monkeypatch.chdir(tmp_path)
environment_file = local_pip_repository_environment_file
run_lock([environment_file], conda_exe=conda_exe)

# THEN the lockfile is generated correctly
lockfile_path = tmp_path / DEFAULT_LOCKFILE_NAME
assert lockfile_path.exists(), list(tmp_path.iterdir())
lockfile = parse_conda_lock_file(lockfile_path)
lockfile_content = lockfile_path.read_text(encoding="utf-8")
packages = {package.name: package for package in lockfile.package}

# AND the private package is in the lockfile
private_package = packages.get("fake-private-package")
assert private_package, lockfile_content

# AND the private package was installed from the local repository
assert private_package.url.startswith("file://")

# AND the six package is in the lockfile
package = packages.get("six")
assert package, lockfile_content

package_url = urlparse(package.url)
# AND the package was sourced from pypi
assert package_url.hostname == "files.pythonhosted.org", (
"Package was fetched from incorrect host. See full lock-file:\n"
+ lockfile_content
)
Loading