Skip to content

Commit

Permalink
Fixed creating of repositories when importing content
Browse files Browse the repository at this point in the history
closes #872
  • Loading branch information
hstct committed Sep 21, 2023
1 parent a012301 commit 82532ed
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGES/872.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the ``create_repositories=True`` parameter for importing content.
146 changes: 132 additions & 14 deletions pulp_deb/app/modelresource.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from import_export import fields
from import_export.widgets import ForeignKeyWidget

from pulpcore.plugin.importexport import BaseContentResource
from pulpcore.plugin.modelresources import RepositoryResource
from pulp_deb.app.models import (
AptRepository,
GenericContent,
InstallerFileIndex,
Package,
Expand All @@ -13,7 +18,43 @@
)


class InstallerFileIndexResource(BaseContentResource):
class DebContentResource(BaseContentResource):
"""
Resource for import/export of deb content.
"""

def __init__(self, *args, **kwargs):
"""
Initialize the DebContentResource
"""
self.content_mapping = {}
super().__init__(*args, **kwargs)

def _add_to_mapping(self, repo, uuids):
if not uuids.exists():
return

self.content_mapping[repo.name] = list(map(str, uuids))

def set_up_queryset(self):
"""
Return Content for a RepositoryVersion while populating content_mapping.
Returns:
django.db.models.QuerySet: The Content to export for a RepositoryVersion.
"""
content = self.Meta.model.objects.filter(pk__in=self.repo_version.content).order_by(
"content_ptr_id"
)

self._add_to_mapping(
self.repo_version.repository, content.values_list("pulp_id", flat=True)
)

return content


class InstallerFileIndexResource(DebContentResource):
"""
Resource for import/export of apt_installerfileindex entities.
"""
Expand All @@ -23,7 +64,7 @@ class Meta:
import_id_fields = model.natural_key_fields()


class PackageResource(BaseContentResource):
class PackageResource(DebContentResource):
"""
Resource for import/export of apt_package entities.
"""
Expand All @@ -33,7 +74,7 @@ class Meta:
import_id_fields = model.natural_key_fields()


class InstallerPackageResource(BaseContentResource):
class InstallerPackageResource(DebContentResource):
"""
Resource for import/export of apt_installerpackage entities.
"""
Expand All @@ -43,7 +84,7 @@ class Meta:
import_id_fields = model.natural_key_fields()


class GenericContentResource(BaseContentResource):
class GenericContentResource(DebContentResource):
"""
Resource for import/export of apt_genericcontent entities.
"""
Expand All @@ -53,7 +94,7 @@ class Meta:
import_id_fields = model.natural_key_fields()


class PackageIndexResource(BaseContentResource):
class PackageIndexResource(DebContentResource):
"""
Resource for import/export of apt_packageindex entities.
"""
Expand All @@ -63,7 +104,7 @@ class Meta:
import_id_fields = model.natural_key_fields()


class ReleaseArchitectureResource(BaseContentResource):
class ReleaseArchitectureResource(DebContentResource):
"""
Resource for import/export of apt_releasearchitecture entities.
"""
Expand All @@ -73,7 +114,7 @@ class Meta:
import_id_fields = model.natural_key_fields()


class ReleaseComponentResource(BaseContentResource):
class ReleaseComponentResource(DebContentResource):
"""
Resource for import/export of apt_releasecomponent entities.
"""
Expand All @@ -83,7 +124,7 @@ class Meta:
import_id_fields = model.natural_key_fields()


class ReleaseFileResource(BaseContentResource):
class ReleaseFileResource(DebContentResource):
"""
Resource for import/export of apt_releasefile entities.
"""
Expand All @@ -93,17 +134,74 @@ class Meta:
import_id_fields = model.natural_key_fields()


class PackageReleaseComponentResource(BaseContentResource):
class PackageReleaseComponentResource(DebContentResource):
"""
Resource for import/export of apt_packagereleasecomponent entities.
"""

class ReleaseComponentForeignKeyWidget(ForeignKeyWidget):
"""
Class that lets us specify a multi-key link to ReleaseComponent.
Format to be used at import-row time is:
str(<release_component.distribution>|<release_component.component>)
"""

def render(self, value, obj):
"""Render formatted string to use as unique-identifier."""
rc_dist = obj.release_component.distribution
rc_comp = obj.release_component.component
return f"{rc_dist}|{rc_comp}"

class PackageForeignKeyWidget(ForeignKeyWidget):
"""
Class that lets us specify a multi-key link to Package.
Format to be used at import-row time is:
str(<package.relative_path>|<package.sha256>)
"""

def render(self, value, obj):
"""Render formatted string to use as unique-identifier."""
pkg_relative_path = obj.package.relative_path
pkg_sha256 = obj.package.sha256
return f"{pkg_relative_path}|{pkg_sha256}"

release_component = fields.Field(
column_name="release_component",
attribute="release_component",
widget=ReleaseComponentForeignKeyWidget(ReleaseComponent),
)

package = fields.Field(
column_name="package",
attribute="package",
widget=PackageForeignKeyWidget(Package),
)

def before_import_row(self, row, **kwargs):
"""
Finds and sets release_component using upstream_id.
Args:
row (tablib.Dataset row): incoming import-row representing a single PRC.
kwargs: args passed along from the import() call.
"""
super().before_import_row(row, **kwargs)

(rc_dist, rc_comp) = row["release_component"].split("|")
(pkg_relative_path, pkg_sha256) = row["package"].split("|")
rc = ReleaseComponent.objects.filter(distribution=rc_dist, component=rc_comp).first()
pkg = Package.objects.filter(relative_path=pkg_relative_path, sha256=pkg_sha256).first()
row["release_component"] = str(rc.pulp_id)
row["package"] = str(pkg.pulp_id)

class Meta:
model = PackageReleaseComponent
import_id_fields = model.natural_key_fields()


class ReleaseResource(BaseContentResource):
class ReleaseResource(DebContentResource):
"""
Resource for import/export of apt_release entities.
"""
Expand All @@ -113,15 +211,35 @@ class Meta:
import_id_fields = model.natural_key_fields()


class AptRepositoryResource(RepositoryResource):
"""
A resource for import/export Deb repository entities.
"""

def set_up_queryset(self):
"""
Set up a queryset for DebRepositories.
Returns:
A queryset containing one repository that will be exported.
"""
return AptRepository.objects.filter(pk=self.repo_version.repository)

class Meta:
model = AptRepository
exclude = RepositoryResource.Meta.exclude + ("most_recent_version",)


IMPORT_ORDER = [
AptRepositoryResource,
PackageResource,
InstallerPackageResource,
ReleaseResource,
InstallerFileIndexResource,
ReleaseArchitectureResource,
ReleaseComponentResource,
ReleaseFileResource,
PackageReleaseComponentResource,
ReleaseResource,
PackageResource,
InstallerPackageResource,
ReleaseFileResource,
PackageIndexResource,
GenericContentResource,
]
Loading

0 comments on commit 82532ed

Please sign in to comment.