-
Notifications
You must be signed in to change notification settings - Fork 1
Add support for supplying custom SimpleRepository definitions to the CLI #27
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
Open
pelson
wants to merge
1
commit into
main
Choose a base branch
from
feature/entrypoint-for-repo-definition
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
|
|
||
| import argparse | ||
| from contextlib import asynccontextmanager | ||
| import importlib | ||
| import logging | ||
| import os | ||
| from pathlib import Path | ||
|
|
@@ -54,6 +55,49 @@ def get_netrc_path() -> typing.Optional[Path]: | |
| return None | ||
|
|
||
|
|
||
| def load_repository_from_spec(spec: str, *, http_client: httpx.AsyncClient) -> SimpleRepository: | ||
| """ | ||
| Load a repository from a specification string. | ||
|
|
||
| The spec can be: | ||
| - An HTTP/HTTPS URL (e.g., "https://pypi.org/simple/") | ||
| - An existing filesystem directory (e.g., "/path/to/packages") | ||
| - A Python entrypoint specification (e.g., "mymodule:create_repo") | ||
|
|
||
| For entrypoint specifications: | ||
| - The format is "module.path:callable" | ||
| - The callable, invoked with no arguments, must return a SimpleRepository instance | ||
| """ | ||
| # Check if it's an HTTP URL | ||
| if is_url(spec): | ||
| return HttpRepository(url=spec, http_client=http_client) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know why this isn't cached... ? |
||
|
|
||
| # Check if it's an existing filesystem path | ||
| path = Path(spec) | ||
| if path.exists() and path.is_dir(): | ||
| return LocalRepository(path) | ||
|
|
||
| # Try to load as Python entrypoint | ||
| if ":" not in spec: | ||
| raise ValueError( | ||
| f"Invalid repository specification: '{spec}'. " | ||
| "Must be an HTTP URL, file path, or entrypoint (module:callable)", | ||
| ) | ||
|
|
||
| module_path, attr_name = spec.rsplit(":", 1) | ||
| module = importlib.import_module(module_path) | ||
| obj = getattr(module, attr_name) | ||
| # Call it and verify the result | ||
| result = obj() | ||
| if not isinstance(result, SimpleRepository): | ||
| raise TypeError( | ||
| f"Entrypoint '{spec}' must return a SimpleRepository instance, " | ||
| f"got {type(result).__name__}", | ||
| ) | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| def configure_parser(parser: argparse.ArgumentParser) -> None: | ||
| parser.description = "Run a Python Package Index" | ||
|
|
||
|
|
@@ -66,7 +110,7 @@ def configure_parser(parser: argparse.ArgumentParser) -> None: | |
| ) | ||
| parser.add_argument( | ||
| "repository_url", metavar="repository-url", type=str, nargs="+", | ||
| help="Repository URL (http/https) or local directory path", | ||
| help="Repository URL (http/https), local directory path, or Python entrypoint (module:callable)", | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -76,17 +120,8 @@ def create_repository( | |
| http_client: httpx.AsyncClient, | ||
| ) -> SimpleRepository: | ||
| base_repos: list[SimpleRepository] = [] | ||
| repo: SimpleRepository | ||
| for repo_url in repository_urls: | ||
| if is_url(repo_url): | ||
| repo = HttpRepository( | ||
| url=repo_url, | ||
| http_client=http_client, | ||
| ) | ||
| else: | ||
| repo = LocalRepository( | ||
| index_path=Path(repo_url), | ||
| ) | ||
| for repo_spec in repository_urls: | ||
| repo = load_repository_from_spec(repo_spec, http_client=http_client) | ||
| base_repos.append(repo) | ||
|
|
||
| if len(base_repos) > 1: | ||
|
|
||
54 changes: 54 additions & 0 deletions
54
simple_repository_server/tests/unit/test_entrypoint_loading.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Copyright (C) 2023, CERN | ||
| # This software is distributed under the terms of the MIT | ||
| # licence, copied verbatim in the file "LICENSE". | ||
| # In applying this license, CERN does not waive the privileges and immunities | ||
| # granted to it by virtue of its status as Intergovernmental Organization | ||
| # or submit itself to any jurisdiction. | ||
|
|
||
| from pathlib import Path | ||
| from unittest import mock | ||
|
|
||
| import httpx | ||
| import pytest | ||
| from simple_repository.components.core import SimpleRepository | ||
| from simple_repository.components.http import HttpRepository | ||
| from simple_repository.components.local import LocalRepository | ||
|
|
||
| from simple_repository_server.__main__ import load_repository_from_spec | ||
|
|
||
| repo_dir = Path(__file__).parent | ||
|
|
||
|
|
||
| def _test_repo_factory() -> SimpleRepository: | ||
| # Test helper: factory function that returns a repository | ||
| return LocalRepository(index_path=repo_dir) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def http_client(): | ||
| return mock.Mock(spec=httpx.AsyncClient) | ||
|
|
||
|
|
||
| def test_load_http_url(http_client): | ||
| """HTTP URLs should create HttpRepository""" | ||
| repo = load_repository_from_spec("https://pypi.org/simple/", http_client=http_client) | ||
| assert isinstance(repo, HttpRepository) | ||
| assert repo._source_url == "https://pypi.org/simple/" | ||
|
|
||
|
|
||
| def test_load_existing_path(tmp_path, http_client): | ||
| """Existing directory paths should create LocalRepository""" | ||
| test_dir = tmp_path / "packages" | ||
| test_dir.mkdir() | ||
|
|
||
| repo = load_repository_from_spec(str(test_dir), http_client=http_client) | ||
| assert isinstance(repo, LocalRepository) | ||
| assert repo._index_path == test_dir | ||
|
|
||
|
|
||
| def test_load_entrypoint(http_client): | ||
| """Entrypoint spec should load and call the factory""" | ||
| spec = "simple_repository_server.tests.unit.test_entrypoint_loading:_test_repo_factory" | ||
| repo = load_repository_from_spec(spec, http_client=http_client) | ||
| assert isinstance(repo, LocalRepository) | ||
| assert repo._index_path == repo_dir |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be nice to be able to specify direct needs, I'm thinking about adding a cool-off/quarantine thing: