Exemplo n.º 1
0
    def create(self, request):
        """
        Create a new Package from a request.
        """
        try:
            artifact = self.get_resource(request.data['_artifact'], Artifact)
        except KeyError:
            raise serializers.ValidationError(detail={'_artifact': _('This field is required')})

        try:
            filename = request.data['filename']
        except KeyError:
            raise serializers.ValidationError(detail={'filename': _('This field is required')})

        try:
            new_pkg = _prepare_package(artifact, filename)
            new_pkg['_artifact'] = request.data['_artifact']
            new_pkg['relative_path'] = request.data.get('relative_path', '')
        except OSError:
            return Response('RPM file cannot be parsed for metadata.',
                            status=status.HTTP_406_NOT_ACCEPTABLE)

        serializer = self.get_serializer(data=new_pkg)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        headers = self.get_success_headers(request.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
Exemplo n.º 2
0
    def create(self, request):
        """
        Create a new Package from a request.
        """
        try:
            artifact = self.get_resource(request.data['_artifact'], Artifact)
        except KeyError:
            raise serializers.ValidationError(
                detail={'_artifact': _('This field is required')})

        try:
            relative_path = request.data['relative_path']
        except KeyError:
            raise serializers.ValidationError(
                detail={'relative_path': _('This field is required')})

        try:
            new_pkg = _prepare_package(artifact, relative_path)
            new_pkg['_artifact'] = request.data['_artifact']
            new_pkg['relative_path'] = relative_path
        except OSError:
            return Response('RPM file cannot be parsed for metadata.',
                            status=status.HTTP_406_NOT_ACCEPTABLE)

        serializer = self.get_serializer(data=new_pkg)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        headers = self.get_success_headers(request.data)
        return Response(serializer.data,
                        status=status.HTTP_201_CREATED,
                        headers=headers)
Exemplo n.º 3
0
def one_shot_upload(artifact, repository=None):
    """
    One shot upload for RPM package.

    Args:
        artifact: validated artifact for a file
        repository: repository to extend with new pkg
        path: artifact href
    """
    filename = os.path.basename(artifact.file.path)

    # export META from rpm and prepare dict as saveable format
    try:
        new_pkg = _prepare_package(artifact, filename)
    except OSError:
        raise OSError('RPM file cannot be parsed for metadata.')

    pkg, created = Package.objects.get_or_create(**new_pkg)

    ContentArtifact.objects.create(
        artifact=artifact,
        content=pkg,
        relative_path=filename
    )

    if repository:
        content_to_add = Package.objects.filter(pkgId=pkg.pkgId)

        # create new repo version with uploaded package
        with RepositoryVersion.create(repository) as new_version:
            new_version.add_content(content_to_add)
Exemplo n.º 4
0
    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:
            raise NotAcceptable(
                detail='RPM file cannot be parsed for metadata.')

        package = Package.objects.filter(**new_pkg)

        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
            ])

            raise serializers.ValidationError(
                _("There is already a package with: {values}.").format(
                    values=error_data))

        data.update(new_pkg)
        return data
Exemplo n.º 5
0
def one_shot_upload(artifact, filename, repository=None):
    """
    One shot upload for RPM package.

    Args:
        artifact: validated artifact for a file
        filename : name of file
        repository: repository to extend with new pkg
    """
    # export META from rpm and prepare dict as saveable format
    try:
        new_pkg = _prepare_package(artifact, filename)
    except OSError:
        raise OSError('RPM file cannot be parsed for metadata.')

    pkg, created = Package.objects.get_or_create(**new_pkg)

    if not created:
        raise OSError('RPM package {} already exists.'.format(pkg.filename))

    ContentArtifact.objects.create(
        artifact=artifact,
        content=pkg,
        relative_path=filename
    )

    resource = CreatedResource(content_object=pkg)
    resource.save()

    if repository:
        content_to_add = Package.objects.filter(pkgId=pkg.pkgId)

        # create new repo version with uploaded package
        with RepositoryVersion.create(repository) as new_version:
            new_version.add_content(content_to_add)
Exemplo n.º 6
0
    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
Exemplo n.º 7
0
def one_shot_upload(artifact_pk, filename, repository_pk=None):
    """
    One shot upload for RPM package.

    Args:
        artifact_pk: validated artifact for a file
        filename : name of file
        repository_pk: repository to extend with new pkg
    """
    artifact = Artifact.objects.get(pk=artifact_pk)

    # export META from rpm and prepare dict as saveable format
    try:
        new_pkg = _prepare_package(artifact, filename)
    except OSError:
        raise OSError('RPM file cannot be parsed for metadata.')

    pkg, created = Package.objects.get_or_create(**new_pkg)

    if not created:
        raise OSError('RPM package {} already exists.'.format(pkg.filename))

    ContentArtifact.objects.create(
        artifact=artifact,
        content=pkg,
        relative_path=filename
    )

    resource = CreatedResource(content_object=pkg)
    resource.save()

    if repository_pk:
        repository = Repository.objects.get(pk=repository_pk)
        content_to_add = Package.objects.filter(pkgId=pkg.pkgId)

        # create new repo version with uploaded package
        with RepositoryVersion.create(repository) as new_version:
            new_version.add_content(content_to_add)