|
| 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 | +from pulpcore.plugin.util import get_domain |
| 9 | + |
| 10 | +from pulp_python.app.models import PythonPackageContent, PythonRepository |
| 11 | +from pulp_python.app.utils import artifact_to_python_content_data |
| 12 | + |
| 13 | +log = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +def repair(repository_pk: uuid.UUID) -> None: |
| 17 | + """ |
| 18 | + Repairs metadata of all packages for the specified repository. |
| 19 | +
|
| 20 | + Args: |
| 21 | + repository_pk (uuid.UUID): The primary key of the repository to repair. |
| 22 | +
|
| 23 | + Returns: |
| 24 | + None |
| 25 | + """ |
| 26 | + repository = PythonRepository.objects.get(pk=repository_pk) |
| 27 | + |
| 28 | + log.info( |
| 29 | + _( |
| 30 | + "Repairing packages' metadata for the latest version of repository {}." |
| 31 | + ).format(repository.name) |
| 32 | + ) |
| 33 | + content_set = repository.latest_version().content.values_list("pk", flat=True) |
| 34 | + content = PythonPackageContent.objects.filter(pk__in=content_set) |
| 35 | + |
| 36 | + progress_report = ProgressReport( |
| 37 | + message=_("Repairing packages' metadata"), |
| 38 | + code="repair.metadata", |
| 39 | + state=TASK_STATES.RUNNING, |
| 40 | + total=len(content_set), |
| 41 | + done=0, |
| 42 | + ) |
| 43 | + num_repaired = repair_metadata(content, progress_report) |
| 44 | + progress_report.state = TASK_STATES.COMPLETED |
| 45 | + progress_report.save(update_fields=["state"]) |
| 46 | + |
| 47 | + log.info(_("{} packages' metadata repaired.").format(num_repaired)) |
| 48 | + |
| 49 | + |
| 50 | +def repair_metadata( |
| 51 | + content: QuerySet[PythonPackageContent], progress_report: ProgressReport |
| 52 | +) -> int: |
| 53 | + """ |
| 54 | + Repairs metadata for a queryset of PythonPackageContent objects |
| 55 | + and updates the progress report after every 100 items processed. |
| 56 | +
|
| 57 | + Args: |
| 58 | + content (QuerySet[PythonPackageContent]): The queryset of items to repair. |
| 59 | + progress_report (ProgressReport): The progress report instance to update. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + int: The number of packages that were repaired. |
| 63 | + """ |
| 64 | + # TODO: Add on_demand content repair |
| 65 | + immediate_content = content.filter(contentartifact__artifact__isnull=False) |
| 66 | + domain = get_domain() |
| 67 | + |
| 68 | + batch = [] |
| 69 | + set_of_update_fields = set() |
| 70 | + total_repaired = 0 |
| 71 | + |
| 72 | + i = 0 |
| 73 | + for i, package in enumerate( |
| 74 | + immediate_content.prefetch_related("_artifacts").iterator(chunk_size=1000), |
| 75 | + start=1, |
| 76 | + ): |
| 77 | + new_data = artifact_to_python_content_data( |
| 78 | + package.filename, package._artifacts.get(), domain |
| 79 | + ) |
| 80 | + changed = False |
| 81 | + for field, value in new_data.items(): |
| 82 | + if getattr(package, field) != value: |
| 83 | + setattr(package, field, value) |
| 84 | + set_of_update_fields.add(field) |
| 85 | + changed = True |
| 86 | + if changed: |
| 87 | + batch.append(package) |
| 88 | + if len(batch) == 1000: |
| 89 | + total_repaired += len(batch) |
| 90 | + PythonPackageContent.objects.bulk_update(batch, set_of_update_fields) |
| 91 | + batch = [] |
| 92 | + set_of_update_fields.clear() |
| 93 | + if i % 100 == 0: |
| 94 | + progress_report.done = i |
| 95 | + progress_report.save(update_fields=["done"]) |
| 96 | + |
| 97 | + if batch: |
| 98 | + total_repaired += len(batch) |
| 99 | + PythonPackageContent.objects.bulk_update(batch, set_of_update_fields) |
| 100 | + if i % 100 != 0: |
| 101 | + progress_report.done = i |
| 102 | + progress_report.save(update_fields=["done"]) |
| 103 | + |
| 104 | + return total_repaired |
0 commit comments