|
| 1 | +import logging |
| 2 | +import uuid |
| 3 | +from gettext import gettext as _ |
| 4 | + |
| 5 | +from django.db.models.query import QuerySet |
| 6 | +from pulpcore.plugin.constants import TASK_STATES |
| 7 | +from pulpcore.plugin.models import ProgressReport |
| 8 | + |
| 9 | +from pulp_python.app.models import PythonPackageContent, PythonRepository |
| 10 | +from pulp_python.app.utils import artifact_to_python_content_data |
| 11 | + |
| 12 | +log = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +def repair(repository_pk: uuid.UUID) -> None: |
| 16 | + """ |
| 17 | + Repairs metadata of all packages for the specified repository. |
| 18 | +
|
| 19 | + Args: |
| 20 | + repository_pk (uuid.UUID): The primary key of the repository to repair. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + None |
| 24 | + """ |
| 25 | + repository = PythonRepository.objects.get(pk=repository_pk) |
| 26 | + |
| 27 | + log.info( |
| 28 | + _( |
| 29 | + "Repairing packages' metadata for the latest version of repository {}." |
| 30 | + ).format(repository.name) |
| 31 | + ) |
| 32 | + content_set = repository.latest_version().content.values_list("pk", flat=True) |
| 33 | + content = PythonPackageContent.objects.filter(pk__in=content_set) |
| 34 | + num_repaired = repair_metadata(content) |
| 35 | + progress_report = ProgressReport( |
| 36 | + message=_("Repaired packages' metadata"), |
| 37 | + code="repair.metadata", |
| 38 | + state=TASK_STATES.COMPLETED, |
| 39 | + total=len(content_set), |
| 40 | + done=num_repaired, |
| 41 | + ) |
| 42 | + progress_report.save() |
| 43 | + |
| 44 | + |
| 45 | +def repair_metadata(content: QuerySet[PythonPackageContent]) -> int: |
| 46 | + """ |
| 47 | + Repairs metadata for a queryset of PythonPackageContent objects. |
| 48 | +
|
| 49 | + Args: |
| 50 | + content (QuerySet[PythonPackageContent]): The queryset of items to repair. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + int: The number of packages that were repaired. |
| 54 | + """ |
| 55 | + # TODO: Add on_demand content repair |
| 56 | + immediate_content = content.filter(contentartifact__artifact__isnull=False) |
| 57 | + |
| 58 | + batch = [] |
| 59 | + set_of_update_fields = set() |
| 60 | + total_repaired = 0 |
| 61 | + |
| 62 | + for package in immediate_content.prefetch_related("_artifacts").iterator( |
| 63 | + chunk_size=1000 |
| 64 | + ): |
| 65 | + new_data = artifact_to_python_content_data( |
| 66 | + package.filename, package._artifacts.get(), package.pulp_domain |
| 67 | + ) |
| 68 | + changed = False |
| 69 | + for field, value in new_data.items(): |
| 70 | + if getattr(package, field) != value: |
| 71 | + setattr(package, field, value) |
| 72 | + set_of_update_fields.add(field) |
| 73 | + changed = True |
| 74 | + if changed: |
| 75 | + batch.append(package) |
| 76 | + if len(batch) == 1000: |
| 77 | + total_repaired += len(batch) |
| 78 | + PythonPackageContent.objects.bulk_update(batch, set_of_update_fields) |
| 79 | + batch = [] |
| 80 | + set_of_update_fields.clear() |
| 81 | + |
| 82 | + if batch: |
| 83 | + total_repaired += len(batch) |
| 84 | + PythonPackageContent.objects.bulk_update(batch, set_of_update_fields) |
| 85 | + |
| 86 | + return total_repaired |
0 commit comments