|
| 1 | +import logging |
| 2 | +import shutil |
| 3 | +import tempfile |
1 | 4 | from gettext import gettext as _ |
2 | 5 | from django.conf import settings |
| 6 | +from django.db.utils import IntegrityError |
3 | 7 | from packaging.requirements import Requirement |
4 | 8 | from rest_framework import serializers |
5 | 9 |
|
|
8 | 12 | from pulpcore.plugin.util import get_domain |
9 | 13 |
|
10 | 14 | from pulp_python.app import models as python_models |
11 | | -from pulp_python.app.utils import artifact_to_python_content_data |
| 15 | +from pulp_python.app.utils import ( |
| 16 | + DIST_EXTENSIONS, |
| 17 | + artifact_to_python_content_data, |
| 18 | + get_project_metadata_from_file, |
| 19 | +) |
| 20 | + |
| 21 | +log = logging.getLogger(__name__) |
12 | 22 |
|
13 | 23 |
|
14 | 24 | class PythonRepositorySerializer(core_serializers.RepositorySerializer): |
@@ -215,7 +225,7 @@ class PythonPackageContentSerializer(core_serializers.SingleArtifactContentUploa |
215 | 225 | required=False, |
216 | 226 | allow_blank=True, |
217 | 227 | help_text=_( |
218 | | - "A string stating the markup syntax (if any) used in the distribution’s" |
| 228 | + "A string stating the markup syntax (if any) used in the distribution's" |
219 | 229 | " description, so that tools can intelligently render the description." |
220 | 230 | ), |
221 | 231 | ) |
@@ -357,6 +367,61 @@ class Meta: |
357 | 367 | model = python_models.PythonPackageContent |
358 | 368 |
|
359 | 369 |
|
| 370 | +class PythonPackageContentUploadSerializer(PythonPackageContentSerializer): |
| 371 | + """ |
| 372 | + A serializer for requests to synchronously upload Python packages. |
| 373 | + """ |
| 374 | + |
| 375 | + class Meta(PythonPackageContentSerializer.Meta): |
| 376 | + # This API does not support uploading to a repository or using a custom relative_path |
| 377 | + fields = tuple( |
| 378 | + f |
| 379 | + for f in PythonPackageContentSerializer.Meta.fields |
| 380 | + if f not in ["repository", "relative_path"] |
| 381 | + ) |
| 382 | + model = python_models.PythonPackageContent |
| 383 | + # Name used for the OpenAPI request object |
| 384 | + ref_name = "PythonPackageContentUpload" |
| 385 | + |
| 386 | + def validate(self, data): |
| 387 | + file = data.pop("file") |
| 388 | + |
| 389 | + for ext, packagetype in DIST_EXTENSIONS.items(): |
| 390 | + if file.name.endswith(ext): |
| 391 | + break |
| 392 | + else: |
| 393 | + raise serializers.ValidationError( |
| 394 | + _( |
| 395 | + "Extension on {} is not a valid python extension " |
| 396 | + "(.whl, .exe, .egg, .tar.gz, .tar.bz2, .zip)" |
| 397 | + ).format(file.name) |
| 398 | + ) |
| 399 | + |
| 400 | + artifact = core_models.Artifact.init_and_validate(file) |
| 401 | + try: |
| 402 | + artifact.save() |
| 403 | + except IntegrityError: |
| 404 | + artifact = core_models.Artifact.objects.get( |
| 405 | + sha256=artifact.sha256, pulp_domain=get_domain() |
| 406 | + ) |
| 407 | + artifact.touch() |
| 408 | + log.info(f"Artifact for {file.name} already existed in database") |
| 409 | + |
| 410 | + filename = file.name |
| 411 | + with tempfile.NamedTemporaryFile("wb", dir=".", suffix=filename) as temp_file: |
| 412 | + shutil.copyfileobj(artifact.file, temp_file) |
| 413 | + temp_file.flush() |
| 414 | + metadata = get_project_metadata_from_file(temp_file.name) |
| 415 | + |
| 416 | + data["artifact"] = artifact |
| 417 | + data["sha256"] = artifact.sha256 |
| 418 | + data["relative_path"] = file.name |
| 419 | + data.update(vars(metadata)) |
| 420 | + # Overwrite filename from metadata |
| 421 | + data["filename"] = filename |
| 422 | + return data |
| 423 | + |
| 424 | + |
360 | 425 | class MinimalPythonPackageContentSerializer(PythonPackageContentSerializer): |
361 | 426 | """ |
362 | 427 | A Serializer for PythonPackageContent. |
|
0 commit comments