|
| 1 | +import logging |
| 2 | +import os |
1 | 3 | from gettext import gettext as _ |
2 | 4 | from django.conf import settings |
| 5 | +from django.db.utils import IntegrityError |
3 | 6 | from packaging.requirements import Requirement |
4 | 7 | from rest_framework import serializers |
5 | 8 |
|
|
8 | 11 | from pulpcore.plugin.util import get_domain |
9 | 12 |
|
10 | 13 | from pulp_python.app import models as python_models |
11 | | -from pulp_python.app.utils import artifact_to_python_content_data |
| 14 | +from pulp_python.app.utils import ( |
| 15 | + DIST_EXTENSIONS, |
| 16 | + artifact_to_python_content_data, |
| 17 | + get_project_metadata_from_file, |
| 18 | + parse_project_metadata, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +log = logging.getLogger(__name__) |
12 | 23 |
|
13 | 24 |
|
14 | 25 | class PythonRepositorySerializer(core_serializers.RepositorySerializer): |
@@ -357,6 +368,70 @@ class Meta: |
357 | 368 | model = python_models.PythonPackageContent |
358 | 369 |
|
359 | 370 |
|
| 371 | +class PythonPackageContentUploadSerializer(PythonPackageContentSerializer): |
| 372 | + """ |
| 373 | + A serializer for requests to synchronously upload Python packages. |
| 374 | + """ |
| 375 | + |
| 376 | + def validate(self, data): |
| 377 | + """ |
| 378 | + Validates an uploaded Python package file, extracts its metadata, |
| 379 | + and creates or retrieves an associated Artifact. |
| 380 | +
|
| 381 | + Returns updated data with artifact and metadata details. |
| 382 | + """ |
| 383 | + file = data.pop("file") |
| 384 | + filename = file.name |
| 385 | + |
| 386 | + for ext, packagetype in DIST_EXTENSIONS.items(): |
| 387 | + if filename.endswith(ext): |
| 388 | + break |
| 389 | + else: |
| 390 | + raise serializers.ValidationError( |
| 391 | + _( |
| 392 | + "Extension on {} is not a valid python extension " |
| 393 | + "(.whl, .exe, .egg, .tar.gz, .tar.bz2, .zip)" |
| 394 | + ).format(filename) |
| 395 | + ) |
| 396 | + |
| 397 | + # Replace the incorrect file name in the file path with the original file name |
| 398 | + original_filepath = file.file.name |
| 399 | + path_to_file, tmp_str = original_filepath.rsplit("/", maxsplit=1) |
| 400 | + tmp_str = tmp_str.split(".", maxsplit=1)[0] # Remove e.g. ".upload.gz" suffix |
| 401 | + new_filepath = f"{path_to_file}/{tmp_str}{filename}" |
| 402 | + os.rename(original_filepath, new_filepath) |
| 403 | + |
| 404 | + metadata = get_project_metadata_from_file(new_filepath) |
| 405 | + artifact = core_models.Artifact.init_and_validate(new_filepath) |
| 406 | + try: |
| 407 | + artifact.save() |
| 408 | + except IntegrityError: |
| 409 | + artifact = core_models.Artifact.objects.get( |
| 410 | + sha256=artifact.sha256, pulp_domain=get_domain() |
| 411 | + ) |
| 412 | + artifact.touch() |
| 413 | + log.info(f"Artifact for {file.name} already existed in database") |
| 414 | + |
| 415 | + data["artifact"] = artifact |
| 416 | + data["sha256"] = artifact.sha256 |
| 417 | + data["relative_path"] = filename |
| 418 | + data.update(parse_project_metadata(vars(metadata))) |
| 419 | + # Overwrite filename from metadata |
| 420 | + data["filename"] = filename |
| 421 | + return data |
| 422 | + |
| 423 | + class Meta(PythonPackageContentSerializer.Meta): |
| 424 | + # This API does not support uploading to a repository or using a custom relative_path |
| 425 | + fields = tuple( |
| 426 | + f |
| 427 | + for f in PythonPackageContentSerializer.Meta.fields |
| 428 | + if f not in ["repository", "relative_path"] |
| 429 | + ) |
| 430 | + model = python_models.PythonPackageContent |
| 431 | + # Name used for the OpenAPI request object |
| 432 | + ref_name = "PythonPackageContentUpload" |
| 433 | + |
| 434 | + |
360 | 435 | class MinimalPythonPackageContentSerializer(PythonPackageContentSerializer): |
361 | 436 | """ |
362 | 437 | A Serializer for PythonPackageContent. |
|
0 commit comments