def _artifact_from_data(raw_data): tmpfile = PulpTemporaryUploadedFile("tmpfile", "application/octet-stream", len(raw_data), "", "") tmpfile.write(raw_data) artifact_serializer = ArtifactSerializer(data={"file": tmpfile}) artifact_serializer.is_valid(raise_exception=True) return artifact_serializer.save()
def _artifact_from_data(raw_data): tmpfile = PulpTemporaryUploadedFile("", "application/octet-stream", len(raw_data), "", "") tmpfile.write(raw_data) for hasher in Artifact.DIGEST_FIELDS: tmpfile.hashers[hasher].update(raw_data) artifact = Artifact() artifact.file = tmpfile artifact.size = tmpfile.size for hasher in Artifact.DIGEST_FIELDS: setattr(artifact, hasher, tmpfile.hashers[hasher].hexdigest()) artifact.save() return artifact
def general_create_from_temp_file(app_label, serializer_name, temp_file_pk, *args, **kwargs): """ Create a model instance from contents stored in a temporary file. A task which executes this function takes the ownership of a temporary file and deletes it afterwards. This function calls the function general_create() to create a model instance. """ temp_file = PulpTemporaryFile.objects.get(pk=temp_file_pk) data = kwargs.pop("data", {}) data["file"] = PulpTemporaryUploadedFile.from_file(temp_file.file) general_create(app_label, serializer_name, data=data, *args, **kwargs) temp_file.delete()
def general_create_from_temp_file(app_label, serializer_name, *args, **kwargs): """ Create a model instance from contents stored in a temporary Artifact. A caller should always pass the dictionary "data", as a keyword argument, containing the href to the temporary Artifact. Otherwise, the function does nothing. This function calls the function general_create() to create a model instance. Data passed to that function already contains a serialized artifact converted to PulpTemporaryUploadFile that will be deleted afterwards. """ data = kwargs.pop("data", None) if data and "artifact" in data: named_model_view_set = get_plugin_config( app_label).viewsets_module.NamedModelViewSet artifact = named_model_view_set.get_resource(data.pop("artifact"), Artifact) data["file"] = PulpTemporaryUploadedFile.from_file(artifact.file) general_create(app_label, serializer_name, data=data, *args, **kwargs) artifact.delete()
def create_from_file(cls, file, publication, relative_path=None): """ Creates PublishedMetadata along with Artifact, ContentArtifact, and PublishedArtifact. Args: file (django.core.files.File): an open File that contains metadata publication (pulpcore.app.models.Publication): The publication in which the PublishedMetadata is included. relative_path (str): relative path at which the Metadata is published at. If None, the name of the 'file' is used. Returns: PublishedMetadata (pulpcore.app.models.PublishedMetadata): A saved instance of PublishedMetadata. """ with transaction.atomic(): artifact = Artifact.init_and_validate( file=PulpTemporaryUploadedFile.from_file(file)) try: with transaction.atomic(): artifact.save() except IntegrityError: artifact = Artifact.objects.get(sha256=artifact.sha256) if not relative_path: relative_path = file.name content = cls(relative_path=relative_path, publication=publication) content.save() ca = ContentArtifact(relative_path=relative_path, content=content, artifact=artifact) ca.save() pa = PublishedArtifact(relative_path=relative_path, content_artifact=ca, publication=publication) pa.save() return content