Repository navigation
Add a paginated Python repository package catalog #1451
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
gerrod3
wants to merge
3
commits into
pulp:main
Choose a base branch
from
gerrod3:t3/take-over-pr-1359
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.
+824
−24
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| 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,49 @@ | ||
| # 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. `${REPO_PK}` 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. The selected version | ||
| must belong to this repository and be complete. | ||
|
|
||
| Filter names with `name_normalized`, `name_normalized__icontains`, or | ||
| `name_normalized__istartswith`. Use `ordering=-name_normalized` to list names | ||
| in descending order. |
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,99 @@ | ||
| """Helpers for the repository package list.""" | ||
|
|
||
| from collections import defaultdict | ||
|
|
||
| from django.db.models import Case, IntegerField, Value, When | ||
| from packaging.version import InvalidVersion, Version | ||
|
|
||
| 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 python_packages_in_version(repository_version): | ||
| """Python package content contained in `repository_version`.""" | ||
| content = PythonPackageContent.objects.all() | ||
| if repository_version is None: | ||
| return content.none() | ||
| return repository_version.get_content(content) | ||
|
|
||
|
|
||
| def distinct_package_names_qs(content_qs): | ||
| """One row per distinct `name_normalized`, ordered for stable pagination.""" | ||
| return ( | ||
| content_qs.values("name_normalized") | ||
| .distinct() | ||
| .order_by(*(content_qs.query.order_by or ("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) | ||
| .defer("description", "classifiers", "requires_dist", "project_urls", "license_file") | ||
| .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
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.