예제 #1
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.')

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

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

        data.update(new_pkg)
        return data
예제 #2
0
파일: package.py 프로젝트: pulp/pulp_rpm
    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 = Package.createrepo_to_dict(
                read_crpackage_from_artifact(data["artifact"]))
        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))

        new_pkg["location_href"] = (format_nevra_short(
            new_pkg["name"],
            new_pkg["epoch"],
            new_pkg["version"],
            new_pkg["release"],
            new_pkg["arch"],
        ) + ".rpm")
        if not data.get("relative_path"):
            data["relative_path"] = new_pkg["location_href"]

        data.update(new_pkg)
        return data