-
Notifications
You must be signed in to change notification settings - Fork 119
/
package.py
335 lines (305 loc) · 9.58 KB
/
package.py
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import logging
import traceback
from gettext import gettext as _
from rest_framework import serializers
from rest_framework.exceptions import NotAcceptable
from pulpcore.plugin.serializers import (
ContentChecksumSerializer,
SingleArtifactContentUploadSerializer,
)
from pulp_rpm.app.models import Package
from pulp_rpm.app.shared_utils import _prepare_package
log = logging.getLogger(__name__)
class PackageSerializer(SingleArtifactContentUploadSerializer, ContentChecksumSerializer):
"""
A Serializer for Package.
Add serializers for the new fields defined in Package and add those fields to the Meta class
keeping fields from the parent class as well. Provide help_text.
"""
name = serializers.CharField(
help_text=_("Name of the package"),
read_only=True,
)
epoch = serializers.CharField(
help_text=_("The package's epoch"),
allow_blank=True,
required=False,
read_only=True,
)
version = serializers.CharField(
help_text=_("The version of the package. For example, '2.8.0'"),
read_only=True,
)
release = serializers.CharField(
help_text=_("The release of a particular version of the package. e.g. '1.el7' or '3.f24'"),
read_only=True,
)
arch = serializers.CharField(
help_text=_(
"The target architecture for a package." "For example, 'x86_64', 'i686', or 'noarch'"
),
read_only=True,
)
pkgId = serializers.CharField(
help_text=_("Checksum of the package file"),
read_only=True,
)
checksum_type = serializers.CharField(
help_text=_("Type of checksum, e.g. 'sha256', 'md5'"),
read_only=True,
)
summary = serializers.CharField(
help_text=_("Short description of the packaged software"),
allow_blank=True,
required=False,
read_only=True,
)
description = serializers.CharField(
help_text=_("In-depth description of the packaged software"),
allow_blank=True,
required=False,
read_only=True,
)
url = serializers.CharField(
help_text=_("URL with more information about the packaged software"),
allow_blank=True,
required=False,
read_only=True,
)
changelogs = serializers.JSONField(
help_text=_("Changelogs that package contains"),
default="[]",
required=False,
read_only=True,
)
files = serializers.JSONField(
help_text=_("Files that package contains"),
default="[]",
required=False,
read_only=True,
)
requires = serializers.JSONField(
help_text=_("Capabilities the package requires"),
default="[]",
required=False,
read_only=True,
)
provides = serializers.JSONField(
help_text=_("Capabilities the package provides"),
default="[]",
required=False,
read_only=True,
)
conflicts = serializers.JSONField(
help_text=_("Capabilities the package conflicts"),
default="[]",
required=False,
read_only=True,
)
obsoletes = serializers.JSONField(
help_text=_("Capabilities the package obsoletes"),
default="[]",
required=False,
read_only=True,
)
suggests = serializers.JSONField(
help_text=_("Capabilities the package suggests"),
default="[]",
required=False,
read_only=True,
)
enhances = serializers.JSONField(
help_text=_("Capabilities the package enhances"),
default="[]",
required=False,
read_only=True,
)
recommends = serializers.JSONField(
help_text=_("Capabilities the package recommends"),
default="[]",
required=False,
read_only=True,
)
supplements = serializers.JSONField(
help_text=_("Capabilities the package supplements"),
default="[]",
required=False,
read_only=True,
)
location_base = serializers.CharField(
help_text=_("Base location of this package"),
allow_blank=True,
required=False,
read_only=True,
)
location_href = serializers.CharField(
help_text=_("Relative location of package to the repodata"),
read_only=True,
)
rpm_buildhost = serializers.CharField(
help_text=_("Hostname of the system that built the package"),
allow_blank=True,
required=False,
read_only=True,
)
rpm_group = serializers.CharField(
help_text=_("RPM group (See: http://fedoraproject.org/wiki/RPMGroups)"),
allow_blank=True,
required=False,
read_only=True,
)
rpm_license = serializers.CharField(
help_text=_("License term applicable to the package software (GPLv2, etc.)"),
allow_blank=True,
required=False,
read_only=True,
)
rpm_packager = serializers.CharField(
help_text=_("Person or persons responsible for creating the package"),
allow_blank=True,
required=False,
read_only=True,
)
rpm_sourcerpm = serializers.CharField(
help_text=_("Name of the source package (srpm) the package was built from"),
allow_blank=True,
required=False,
read_only=True,
)
rpm_vendor = serializers.CharField(
help_text=_("Name of the organization that produced the package"),
allow_blank=True,
required=False,
read_only=True,
)
rpm_header_start = serializers.IntegerField(
help_text=_("First byte of the header"),
read_only=True,
)
rpm_header_end = serializers.IntegerField(
help_text=_("Last byte of the header"),
read_only=True,
)
is_modular = serializers.BooleanField(
help_text=_("Flag to identify if the package is modular"),
required=False,
read_only=True,
)
size_archive = serializers.IntegerField(
help_text=_("Size, in bytes, of the archive portion of the original package file"),
read_only=True,
)
size_installed = serializers.IntegerField(
help_text=_("Total size, in bytes, of every file installed by this package"),
read_only=True,
)
size_package = serializers.IntegerField(
help_text=_("Size, in bytes, of the package"),
read_only=True,
)
time_build = serializers.IntegerField(
help_text=_("Time the package was built in seconds since the epoch"),
read_only=True,
)
time_file = serializers.IntegerField(
help_text=_(
"The 'file' time attribute in the primary XML - "
"file mtime in seconds since the epoch."
),
read_only=True,
)
def deferred_validate(self, data):
"""
Validate the rpm package data.
Args:
data (dict): Data to be validated
Returns:
dict: Data that has been validated
"""
data = super().deferred_validate(data)
# export META from rpm and prepare dict as saveable format
try:
new_pkg = _prepare_package(data["artifact"], data["relative_path"])
except OSError:
log.info(traceback.format_exc())
raise NotAcceptable(detail="RPM file cannot be parsed for metadata")
attrs = {key: new_pkg[key] for key in Package.natural_key_fields()}
package = Package.objects.filter(**attrs)
if package.exists():
keywords = (
"name",
"epoch",
"version",
"release",
"arch",
"checksum_type",
"pkgId",
)
error_data = ", ".join(
["=".join(item) for item in new_pkg.items() if item[0] in keywords]
)
package.get().touch()
raise serializers.ValidationError(
_("There is already a package with: {values}.").format(values=error_data)
)
data.update(new_pkg)
return data
class Meta:
fields = (
ContentChecksumSerializer.Meta.fields
+ SingleArtifactContentUploadSerializer.Meta.fields
+ (
"name",
"epoch",
"version",
"release",
"arch",
"pkgId",
"checksum_type",
"summary",
"description",
"url",
"changelogs",
"files",
"requires",
"provides",
"conflicts",
"obsoletes",
"suggests",
"enhances",
"recommends",
"supplements",
"location_base",
"location_href",
"rpm_buildhost",
"rpm_group",
"rpm_license",
"rpm_packager",
"rpm_sourcerpm",
"rpm_vendor",
"rpm_header_start",
"rpm_header_end",
"is_modular",
"size_archive",
"size_installed",
"size_package",
"time_build",
"time_file",
)
)
model = Package
class MinimalPackageSerializer(PackageSerializer):
"""
A minimal serializer for RPM packages.
"""
class Meta:
fields = SingleArtifactContentUploadSerializer.Meta.fields + (
"name",
"epoch",
"version",
"release",
"arch",
"pkgId",
"checksum_type",
)
model = Package