-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix requires_python metadata + add repair metadata command
fixes: #773
- Loading branch information
Showing
10 changed files
with
250 additions
and
35 deletions.
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.
107 changes: 107 additions & 0 deletions
107
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,107 @@ | ||
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.""" | ||
h = rf"(?:{settings.API_ROOT}(?:[-_a-zA-Z0-9]+/)?api/v3/repositories/python/python/[-a-f0-9]+/)" | ||
p = r"(?:prn:python\.pythonrepository:[-a-f0-9]+)" | ||
r = rf"{h}|{p}" | ||
return re.findall(r, value) | ||
|
||
|
||
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
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,78 @@ | ||
import pytest | ||
import subprocess | ||
|
||
from pulp_python.tests.functional.constants import PYTHON_EGG_FILENAME | ||
|
||
|
||
@pytest.fixture | ||
def create_content_direct(python_bindings): | ||
def _create(artifact_filename, filename, content_data): | ||
commands = ( | ||
"from pulpcore.plugin.models import Artifact, ContentArtifact; " | ||
"from pulpcore.plugin.util import get_url; " | ||
"from pulp_python.app.models import PythonPackageContent; " | ||
f"a = Artifact.init_and_validate('{artifact_filename}'); " | ||
"a.save(); " | ||
f"c = PythonPackageContent(sha256=a.sha256, filename={filename!r}, **{content_data!r}); " # noqa: E501 | ||
"c.save(); " | ||
f"ca = ContentArtifact(artifact=a, content=c, relative_path={filename!r}); " | ||
"ca.save(); " | ||
"print(get_url(c))" | ||
) | ||
process = subprocess.run(["pulpcore-manager", "shell", "-c", commands], capture_output=True) | ||
|
||
assert process.returncode == 0 | ||
content_href = process.stdout.decode().strip() | ||
return python_bindings.ContentPackagesApi.read(content_href) | ||
|
||
return _create | ||
|
||
|
||
@pytest.fixture | ||
def move_to_repository(python_bindings, monitor_task): | ||
def _move(repo_href, content_hrefs): | ||
body = {"add_content_units": content_hrefs} | ||
task = monitor_task(python_bindings.RepositoriesPythonApi.modify(repo_href, body).task) | ||
assert len(task.created_resources) == 1 | ||
return python_bindings.RepositoriesPythonApi.read(repo_href) | ||
|
||
return _move | ||
|
||
|
||
def test_metadata_repair_command( | ||
create_content_direct, | ||
python_file, | ||
python_repo, | ||
move_to_repository, | ||
python_bindings, | ||
delete_orphans_pre, | ||
): | ||
"""Test pulpcore-manager repair-python-metadata command.""" | ||
data = { | ||
"name": "shelf-reader", | ||
# Wrong metadata | ||
"version": "0.2", | ||
"packagetype": "bdist", | ||
"requires_python": ">=3.8", | ||
"author": "ME", | ||
} | ||
content = create_content_direct(python_file, PYTHON_EGG_FILENAME, data) | ||
for field, wrong_value in data.items(): | ||
if field == "python_version": | ||
continue | ||
assert getattr(content, field) == wrong_value | ||
|
||
move_to_repository(python_repo.pulp_href, [content.pulp_href]) | ||
process = subprocess.run( | ||
["pulpcore-manager", "repair-python-metadata", "--repositories", python_repo.pulp_href], | ||
capture_output=True | ||
) | ||
assert process.returncode == 0 | ||
output = process.stdout.decode().strip() | ||
assert output == "1 packages processed, 1 package metadata repaired." | ||
|
||
content = python_bindings.ContentPackagesApi.read(content.pulp_href) | ||
assert content.version == "0.1" | ||
assert content.packagetype == "sdist" | ||
assert content.requires_python == "" # technically null | ||
assert content.author == "Austin Macdonald" |