forked from pulp/pulp_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepair.py
More file actions
198 lines (169 loc) · 6.84 KB
/
repair.py
File metadata and controls
198 lines (169 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import logging
from collections import defaultdict
from gettext import gettext as _
from itertools import groupby
from uuid import UUID
from django.db.models import Prefetch
from django.db.models.query import QuerySet
from pulp_python.app.models import PythonPackageContent, PythonRepository
from pulp_python.app.utils import (
artifact_to_python_content_data,
fetch_json_release_metadata,
parse_metadata,
)
from pulpcore.plugin.models import ContentArtifact, ProgressReport
from pulpcore.plugin.util import get_domain
log = logging.getLogger(__name__)
BULK_SIZE = 1000
def repair(repository_pk: UUID) -> None:
"""
Repairs metadata of all packages for the specified repository.
Args:
repository_pk (UUID): The primary key of the repository to repair.
Returns:
None
"""
repository = PythonRepository.objects.get(pk=repository_pk)
log.info(
_(
"Repairing packages' metadata for the latest version of repository {}."
).format(repository.name)
)
content_set = repository.latest_version().content.values_list("pk", flat=True)
content = PythonPackageContent.objects.filter(pk__in=content_set)
num_repaired, pkgs_not_repaired = repair_metadata(content)
log.info(
_(
"{} packages' metadata repaired. Not repaired packages due to either "
"inaccessible URL or mismatched sha256: {}."
).format(num_repaired, pkgs_not_repaired)
)
def repair_metadata(content: QuerySet[PythonPackageContent]) -> tuple[int, set[str]]:
"""
Repairs metadata for a queryset of PythonPackageContent objects
and updates the progress report.
Args:
content (QuerySet[PythonPackageContent]): The queryset of items to repair.
Returns:
tuple[int, set[str]]: A tuple containing:
- The number of packages that were repaired.
- A set of packages' PKs that were not repaired.
"""
immediate_content = (
content.filter(contentartifact__artifact__isnull=False)
.distinct()
.prefetch_related("_artifacts")
)
on_demand_content = (
content.filter(contentartifact__artifact__isnull=True)
.distinct()
.prefetch_related(
Prefetch(
"contentartifact_set",
queryset=ContentArtifact.objects.prefetch_related("remoteartifact_set"),
)
)
.order_by("name", "version")
)
domain = get_domain()
batch = []
set_of_update_fields = set()
total_repaired = 0
# Keep track of on-demand packages that were not repaired
pkgs_not_repaired = set()
progress_report = ProgressReport(
message="Repairing packages' metadata",
code="repair.metadata",
total=content.count(),
)
progress_report.save()
with progress_report:
for package in progress_report.iter(
immediate_content.iterator(chunk_size=BULK_SIZE)
):
new_data = artifact_to_python_content_data(
package.filename, package._artifacts.get(), domain
)
total_repaired += update_package_if_needed(
package, new_data, batch, set_of_update_fields
)
# For on-demand content, we expect that:
# 1. PythonPackageContent always has correct name and version
# 2. RemoteArtifact always has correct sha256
for (name, version), group in groupby(
on_demand_content.iterator(chunk_size=BULK_SIZE),
key=lambda x: (x.name, x.version),
):
group_set = set(group)
grouped_by_url = defaultdict(list)
for package in group_set:
for ra in package.contentartifact_set.get().remoteartifact_set.all():
grouped_by_url[ra.remote.url].append((package, ra))
# Prioritize the URL that can serve the most packages
for url, pkg_ra_pairs in sorted(
grouped_by_url.items(), key=lambda x: len(x[1]), reverse=True
):
if not group_set:
break # No packages left to repair, move onto the next group
remotes = set([pkg_ra[1].remote for pkg_ra in pkg_ra_pairs])
try:
json_data = fetch_json_release_metadata(name, version, remotes)
except Exception:
continue
for package, ra in pkg_ra_pairs:
if package not in group_set:
continue # Package was already repaired
# Extract data only for the specific distribution being checked
dist_data = None
for dist in json_data["urls"]:
if ra.sha256 == dist["digests"]["sha256"]:
dist_data = dist
break
if not dist_data:
continue
new_data = parse_metadata(json_data["info"], version, dist_data)
new_data.pop("url") # url belongs to RemoteArtifact
total_repaired += update_package_if_needed(
package, new_data, batch, set_of_update_fields
)
group_set.remove(package)
progress_report.increment()
# Store and track the unrepaired packages after all URLs are processed
pkgs_not_repaired.update([p.pk for p in group_set])
progress_report.increase_by(len(group_set))
if batch:
total_repaired += len(batch)
PythonPackageContent.objects.bulk_update(batch, set_of_update_fields)
return total_repaired, pkgs_not_repaired
def update_package_if_needed(
package: PythonPackageContent,
new_data: dict,
batch: list[PythonPackageContent],
set_of_update_fields: set[str],
) -> int:
"""
Compares the current package data with new data and updates the package
if needed ("batch" and "set_of_update_fields" are updated in-place).
Args:
package: Package to check and update.
new_data: A dict of new field values to compare against the package.
batch: A list of packages that were updated.
set_of_update_fields: A set of package field names that were updated.
Returns:
The count of repaired packages (increments in multiples of BULK_SIZE only).
"""
total_repaired = 0
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) == BULK_SIZE:
PythonPackageContent.objects.bulk_update(batch, set_of_update_fields)
total_repaired += BULK_SIZE
batch.clear()
set_of_update_fields.clear()
return total_repaired