Repository navigation
feat: add HTTP catalog for Python packages and repository metrics #1359
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
TenSt
wants to merge
1
commit into
pulp:main
Choose a base branch
from
TenSt:stepan/1358-extend-python-api
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added a repository endpoint that lists Python packages with limit/offset pagination. |
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
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,44 @@ | ||
| # Browse the package catalog | ||
|
|
||
| The package list returns **one row per package name**. It defaults to the | ||
| repository's latest complete version. Pass `repository_version` (HREF or PRN) | ||
| to read an older snapshot. `{pulp_id}` is the repository UUID. | ||
|
|
||
| ## List packages | ||
|
|
||
| ```bash | ||
| http GET "${BASE_ADDR}/pulp/api/v3/repositories/python/python/${REPO_PK}/packages/?limit=10" | ||
| ``` | ||
|
|
||
| `count` is the number of distinct packages, not files. Each row is one package | ||
| name. `summary`, `description`, `author`, `author_email`, and `license` come | ||
| from the newest version. `versions` has one entry per stored version string, | ||
| newest first, with that version's `license_expression`. A wheel and an sdist | ||
| of the same version are one entry. A rebuild such as `1.0.0+test.1` stays its | ||
| own entry because the stored version differs. | ||
|
|
||
| ```json | ||
| { | ||
| "count": 1, | ||
| "next": null, | ||
| "previous": null, | ||
| "results": [ | ||
| { | ||
| "name": "shelf-reader", | ||
| "name_normalized": "shelf-reader", | ||
| "summary": "A small example package", | ||
| "description": "", | ||
| "author": "", | ||
| "author_email": "", | ||
| "license": "", | ||
| "versions": [ | ||
| {"version": "0.1", "license_expression": ""} | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| Results are ordered by `name_normalized`. Within a package, versions are newest | ||
| first. Page with `limit` and `offset` to read the whole catalog. Pass | ||
| `repository_version` when the export must stay on one snapshot. |
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,186 @@ | ||
| """Helpers for the repository package list.""" | ||
|
|
||
| from collections import defaultdict | ||
| from urllib.parse import urlparse | ||
|
|
||
| from django.core.exceptions import FieldError | ||
| from django.core.exceptions import ValidationError as DjangoValidationError | ||
| from django.db.models import Case, IntegerField, Q, Value, When | ||
| from django.urls import Resolver404, resolve | ||
| from packaging.version import InvalidVersion, Version | ||
|
|
||
| from pulpcore.plugin.models import Repository, RepositoryContent, RepositoryVersion | ||
| from pulpcore.plugin.util import resolve_prn | ||
|
|
||
| from pulp_python.app.models import PythonPackageContent | ||
|
|
||
|
|
||
| def _version_sort_key(version): | ||
| """PEP 440 sort key. Use with ``reverse=True`` for newest first. | ||
|
|
||
| Invalid versions sort after all valid ones when ``reverse=True``. | ||
| """ | ||
| if not version: | ||
| return (-1, "") | ||
| try: | ||
| return (0, Version(version)) | ||
| except InvalidVersion: | ||
| return (-1, str(version)) | ||
|
|
||
|
|
||
| def _in_version_bounds_q(repository_version, prefix=""): | ||
| """Version-added/removed bounds for rows present in ``repository_version``. | ||
|
|
||
| ``prefix`` is empty for ``RepositoryContent`` and ``in_repo__`` for the | ||
| filtered membership join. Both catalog paths use this helper. | ||
| """ | ||
| return Q(**{f"{prefix}version_added__number__lte": repository_version.number}) & ( | ||
| Q(**{f"{prefix}version_removed__isnull": True}) | ||
| | Q(**{f"{prefix}version_removed__number__gt": repository_version.number}) | ||
| ) | ||
|
TenSt marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def memberships_in_version(repository_version): | ||
| """RepositoryContent rows contained in ``repository_version``. | ||
|
|
||
| A subquery against this queryset keeps the content-id list in the database. | ||
| ``RepositoryVersion.content`` inlines ``content_ids`` as one bound UUID per | ||
| content unit whenever that array is shorter than 65535. | ||
| """ | ||
| return RepositoryContent.objects.filter( | ||
| repository_id=repository_version.repository_id, | ||
| ).filter(_in_version_bounds_q(repository_version)) | ||
|
|
||
|
|
||
| def python_packages_in_version(repository_version): | ||
| """Python package content contained in ``repository_version``.""" | ||
| if repository_version is None: | ||
| return PythonPackageContent.objects.none() | ||
| content_ids = ( | ||
| memberships_in_version(repository_version) | ||
| .filter(content__pulp_type=PythonPackageContent.get_pulp_type()) | ||
| .order_by() | ||
| .values("content_id") | ||
| ) | ||
| return PythonPackageContent.objects.filter(pk__in=content_ids) | ||
|
|
||
|
|
||
| def resolve_repository_version(href): | ||
|
TenSt marked this conversation as resolved.
|
||
| """Load a repository version without selecting ``content_ids``. | ||
|
|
||
| Accepts a repository-version or repository HREF/PRN. Domain-prefixed hrefs | ||
| include ``pulp_domain`` in the resolved kwargs; that field lives on the | ||
| repository, not ``RepositoryVersion``. | ||
| """ | ||
| if not href: | ||
| raise ValueError("No value supplied for repository version.") | ||
|
|
||
| if href.startswith("prn:"): | ||
| model, pk = resolve_prn(href) | ||
| found_kwargs = {"pk": pk} | ||
| else: | ||
| try: | ||
| match = resolve(urlparse(href).path) | ||
| except Resolver404: | ||
| raise ValueError(f"URI not valid: {href}") from None | ||
| model = match.func.cls.queryset.model | ||
| found_kwargs = match.kwargs | ||
|
|
||
| if "pk" in found_kwargs: | ||
| lookup = {"pk": found_kwargs["pk"]} | ||
| else: | ||
| lookup = {} | ||
| for key, value in found_kwargs.items(): | ||
| if key.endswith("_pk"): | ||
| lookup[f"{key[:-3]}__pk"] = value | ||
| elif key == "pulp_domain": | ||
| if hasattr(model, "pulp_domain"): | ||
| lookup["pulp_domain__name"] = value | ||
| elif key in ("api_root", "version"): | ||
| continue | ||
| else: | ||
| lookup[key] = value | ||
|
|
||
| qs = model.objects.all() | ||
| if issubclass(model, RepositoryVersion): | ||
| qs = qs.defer("content_ids") | ||
| try: | ||
| obj = qs.get(**lookup) | ||
| except model.DoesNotExist: | ||
| raise ValueError(f"URI {href} not found.") from None | ||
| except (model.MultipleObjectsReturned, DjangoValidationError, FieldError): | ||
| raise ValueError(f"URI {href} is not a valid repository version.") from None | ||
|
|
||
| if isinstance(obj, RepositoryVersion): | ||
| return obj | ||
| if isinstance(obj, Repository): | ||
| try: | ||
| return obj.versions.complete().defer("content_ids").latest() | ||
| except RepositoryVersion.DoesNotExist: | ||
| return None | ||
| raise ValueError("Must be a repository version.") | ||
|
|
||
|
|
||
| def distinct_package_names_qs(content_qs): | ||
| """One row per distinct ``name_normalized``, ordered for stable pagination.""" | ||
| return content_qs.order_by().values("name_normalized").distinct().order_by("name_normalized") | ||
|
|
||
|
|
||
| def assemble_package_index(content_qs, name_rows, repository_version): | ||
| """One catalog row per package name, with one content unit per stored version. | ||
|
|
||
| Wheel and sdist of the same ``(name_normalized, version)`` collapse to one | ||
| row. An sdist is preferred; otherwise the newest ``pulp_created`` is kept. | ||
| Version strings are the values stored on the content unit, newest first. | ||
| Summary, description, author, author email, and license are taken from the | ||
| newest version because they do not vary by version. | ||
| """ | ||
| if not name_rows or repository_version is None: | ||
| return [] | ||
|
|
||
| names = [row["name_normalized"] for row in name_rows] | ||
| by_name = defaultdict(list) | ||
| # DISTINCT ON keeps the first row for each name and stored version. | ||
| units = ( | ||
| content_qs.filter(name_normalized__in=names) | ||
| .order_by( | ||
| "name_normalized", | ||
| "version", | ||
| Case( | ||
| When(packagetype="sdist", then=Value(0)), | ||
| default=Value(1), | ||
| output_field=IntegerField(), | ||
| ), | ||
| "-pulp_created", | ||
| "pk", | ||
| ) | ||
| .distinct("name_normalized", "version") | ||
| ) | ||
| for unit in units: | ||
| by_name[unit.name_normalized].append(unit) | ||
|
|
||
| result = [] | ||
| for row in name_rows: | ||
| normalized = row["name_normalized"] | ||
| versions = sorted( | ||
| by_name.get(normalized, []), | ||
| key=lambda unit: (_version_sort_key(unit.version), unit.filename), | ||
| reverse=True, | ||
| ) | ||
| source = versions[0] if versions else None | ||
| result.append( | ||
| { | ||
| "name": source.name if source else normalized, | ||
| "name_normalized": normalized, | ||
| "summary": source.summary if source else "", | ||
| "description": source.description if source else "", | ||
| "author": source.author if source else "", | ||
| "author_email": source.author_email if source else "", | ||
| "license": source.license if source else "", | ||
| "versions": [ | ||
| {"version": unit.version, "license_expression": unit.license_expression} | ||
| for unit in versions | ||
| ], | ||
| } | ||
| ) | ||
| return result | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.