-
Notifications
You must be signed in to change notification settings - Fork 77
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
Fix requires_python metadata + add repair metadata command #779
Open
gerrod3
wants to merge
1
commit into
pulp:main
Choose a base branch
from
gerrod3:python-metadata-fix-script
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.
+261
−35
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 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,4 @@ | ||
Fixed `requires_python` field not being properly set on package upload. | ||
|
||
Run the new `pulpcore-manager repair-python-metadata` command with repositories containing affected | ||
packages to repair their metadata. |
Empty file.
Empty file.
118 changes: 118 additions & 0 deletions
118
pulp_python/app/management/commands/repair-python-metadata.py
This file contains 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,118 @@ | ||
import re | ||
import os | ||
from django.core.management import BaseCommand, CommandError | ||
from gettext import gettext as _ | ||
|
||
from django.conf import settings | ||
|
||
from pulpcore.plugin.util import extract_pk | ||
from pulp_python.app.models import PythonPackageContent, PythonRepository | ||
from pulp_python.app.utils import artifact_to_python_content_data | ||
|
||
|
||
def repair_metadata(content): | ||
""" | ||
Repairs the metadata for the passed in content queryset. | ||
:param content: The PythonPackageContent queryset. | ||
Return: number of content units that were repaired | ||
""" | ||
# TODO: Add on_demand content repair? | ||
os.chdir(settings.WORKING_DIRECTORY) | ||
content = content.select_related("pulp_domain") | ||
immediate_content = content.filter(contentartifact__artifact__isnull=False) | ||
batch = [] | ||
set_of_update_fields = set() | ||
total_repaired = 0 | ||
for package in immediate_content.prefetch_related('_artifacts').iterator(chunk_size=1000): | ||
new_data = artifact_to_python_content_data( | ||
package.filename, package._artifacts.get(), package.pulp_domain | ||
) | ||
changed = False | ||
for field, value in new_data.items(): | ||
if getattr(package, field) != value: | ||
setattr(package, field, value) | ||
set_of_update_fields.add(field) | ||
changed = True | ||
if changed: | ||
batch.append(package) | ||
if len(batch) == 1000: | ||
total_repaired += len(batch) | ||
PythonPackageContent.objects.bulk_update(batch, set_of_update_fields) | ||
batch = [] | ||
set_of_update_fields.clear() | ||
|
||
if len(batch) > 0: | ||
total_repaired += len(batch) | ||
PythonPackageContent.objects.bulk_update(batch, set_of_update_fields) | ||
|
||
return total_repaired | ||
|
||
|
||
def href_prn_list_handler(value): | ||
"""Common list parsing for a string of hrefs/prns.""" | ||
r = re.compile( | ||
rf""" | ||
(?:{settings.API_ROOT}(?:[-_a-zA-Z0-9]+/)?api/v3/repositories/python/python/[-a-f0-9]+/) | ||
|(?:prn:python\.pythonrepository:[-a-f0-9]+) | ||
""", | ||
re.VERBOSE | ||
) | ||
values = [] | ||
for v in value.split(","): | ||
if v: | ||
if match := r.match(v.strip()): | ||
values.append(match.group(0)) | ||
else: | ||
raise CommandError(f"Invalid href/prn: {v}") | ||
return values | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Management command to repair metadata of PythonPackageContent. | ||
""" | ||
|
||
help = _("Repair the metadata of PythonPackageContent stored in PythonRepositories") | ||
|
||
def add_arguments(self, parser): | ||
"""Set up arguments.""" | ||
parser.add_argument( | ||
"--repositories", | ||
type=href_prn_list_handler, | ||
required=False, | ||
help=_( | ||
"List of PythonRepository hrefs/prns whose content's metadata will be repaired. " | ||
"Leave blank to include all repositories in all domains. Mutually exclusive " | ||
"with domain." | ||
), | ||
) | ||
parser.add_argument( | ||
"--domain", | ||
default=None, | ||
required=False, | ||
help=_( | ||
"The pulp domain to gather the repositories from if specified. Mutually" | ||
" exclusive with repositories." | ||
), | ||
) | ||
|
||
def handle(self, *args, **options): | ||
"""Implement the command.""" | ||
domain = options.get("domain") | ||
repository_hrefs = options.get("repositories") | ||
if domain and repository_hrefs: | ||
raise CommandError(_("--domain and --repositories are mutually exclusive")) | ||
|
||
repositories = PythonRepository.objects.all() | ||
if repository_hrefs: | ||
repos_ids = [extract_pk(r) for r in repository_hrefs] | ||
repositories = repositories.filter(pk__in=repos_ids) | ||
elif domain: | ||
repositories = repositories.filter(pulp_domain__name=domain) | ||
|
||
content_set = set() | ||
for repository in repositories: | ||
content_set.update(repository.latest_version().content.values_list("pk", flat=True)) | ||
content = PythonPackageContent.objects.filter(pk__in=content_set) | ||
num_repaired = repair_metadata(content) | ||
print(f"{len(content_set)} packages processed, {num_repaired} package metadata repaired.") |
This file contains 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 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 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 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 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.
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.
This data repair is not meant to unblock a migration / update, right?
In that case I'd say a best effort approach is just fine. We never really own the on-demand content anyway.
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.
No it's not meant to unblock, but I have a feeling that a lot of packages in users' repositories contain bad metadata. Like when we do a sync (which are default on-demand) currently all the non-latest versions of a package potentially get incorrect metadata because the majority of the available metadata (the
info
section) is from the latest package. There is a way to get the correct metadata for each version, but it adds an API call for each version, so I've never implemented it.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.
How bad is it? Is it documented?
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.
It's not too hard, it's this api: https://docs.pypi.org/api/json/. You've seen it around, I've been using it in some of my scripts (oci-images check updates script for example)