|
| 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): |
@@ -207,15 +218,15 @@ class PythonPackageContentSerializer(core_serializers.SingleArtifactContentUploa |
207 | 218 | required=False, |
208 | 219 | allow_blank=True, |
209 | 220 | help_text=_( |
210 | | - "The Python version(s) that the distribution is guaranteed to be " "compatible with." |
| 221 | + "The Python version(s) that the distribution is guaranteed to be compatible with." |
211 | 222 | ), |
212 | 223 | ) |
213 | 224 | # Version 2.1 |
214 | 225 | description_content_type = serializers.CharField( |
215 | 226 | required=False, |
216 | 227 | allow_blank=True, |
217 | 228 | help_text=_( |
218 | | - "A string stating the markup syntax (if any) used in the distribution’s" |
| 229 | + "A string stating the markup syntax (if any) used in the distribution's" |
219 | 230 | " description, so that tools can intelligently render the description." |
220 | 231 | ), |
221 | 232 | ) |
@@ -256,7 +267,7 @@ class PythonPackageContentSerializer(core_serializers.SingleArtifactContentUploa |
256 | 267 | ) |
257 | 268 | packagetype = serializers.CharField( |
258 | 269 | help_text=_( |
259 | | - "The type of the distribution package " "(e.g. sdist, bdist_wheel, bdist_egg, etc)" |
| 270 | + "The type of the distribution package (e.g. sdist, bdist_wheel, bdist_egg, etc)" |
260 | 271 | ), |
261 | 272 | read_only=True, |
262 | 273 | ) |
@@ -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. |
@@ -503,7 +578,7 @@ class PythonPublicationSerializer(core_serializers.PublicationSerializer): |
503 | 578 |
|
504 | 579 | distributions = core_serializers.DetailRelatedField( |
505 | 580 | help_text=_( |
506 | | - "This publication is currently being hosted as configured by these " "distributions." |
| 581 | + "This publication is currently being hosted as configured by these distributions." |
507 | 582 | ), |
508 | 583 | source="distribution_set", |
509 | 584 | view_name="pythondistributions-detail", |
|
0 commit comments