Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish custom/unknown repository metadata #1440

Merged
merged 1 commit into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/5432.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sync and Publish custom/unknown repository metadata.
111 changes: 77 additions & 34 deletions pulp_rpm/app/tasks/publishing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from gettext import gettext as _
import logging
import shutil

import createrepo_c as cr

Expand All @@ -15,13 +16,79 @@

from pulpcore.plugin.tasking import WorkingDirectory

from pulp_rpm.app.models import Package, RpmPublication, UpdateRecord
from pulp_rpm.app.models import Package, RepoMetadataFile, RpmPublication, UpdateRecord

log = logging.getLogger(__name__)

REPODATA_PATH = 'repodata'


class PublicationData:
"""
Encapsulates data relative to publication.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include the description of attributes (packages, repomdrecords)

Attributes:
publication (pulpcore.plugin.models.Publication): A Publication to populate.
packages (pulp_rpm.models.Package): A list of published packages.
repomdrecords (list): A list of tuples with repomdrecords data.

"""

def __init__(self, publication):
"""
Setting Publication data.

Args:
publication (pulpcore.plugin.models.Publication): A Publication to populate.

"""
self.publication = publication
self.packages = []
self.repomdrecords = []

def prepare_metadata_files(self):
"""
Copies metadata files from the Artifact storage.

"""
publication = self.publication
repo_metadata_files = RepoMetadataFile.objects.filter(
pk__in=publication.repository_version.content).prefetch_related('contentartifact_set')

for repo_metadata_file in repo_metadata_files:
content_artifact = repo_metadata_file.contentartifact_set.get()
current_file = content_artifact.artifact.file.file
path = content_artifact.relative_path.split("/")[-1]
if repo_metadata_file.checksum in path:
path = path.split("-")[-1]
with open(path, "wb") as new_file:
shutil.copyfileobj(current_file, new_file)
self.repomdrecords.append((repo_metadata_file.data_type, new_file.name, None))

def populate(self):
"""
Populate a publication.

Create published artifacts for a publication.

"""
publication = self.publication

self.packages = Package.objects.filter(pk__in=publication.repository_version.content).\
prefetch_related('contentartifact_set')
published_artifacts = []

for package in self.packages:
for content_artifact in package.contentartifact_set.all():
published_artifacts.append(PublishedArtifact(
relative_path=content_artifact.relative_path,
publication=publication,
content_artifact=content_artifact)
)

PublishedArtifact.objects.bulk_create(published_artifacts)


def update_record_xml(update_record):
"""
Return xml for an UpdateRecord.
Expand Down Expand Up @@ -96,7 +163,9 @@ def publish(repository_version_pk):

with WorkingDirectory():
with RpmPublication.create(repository_version) as publication:
packages = populate(publication)
publication_data = PublicationData(publication)
publication_data.populate()
packages = publication_data.packages

# Prepare metadata files
repomd_path = os.path.join(os.getcwd(), "repomd.xml")
Expand Down Expand Up @@ -143,13 +212,17 @@ def publish(repository_version_pk):

repomd = cr.Repomd()

repomdrecords = (("primary", pri_xml_path, pri_db),
publication_data.prepare_metadata_files()

repomdrecords = [("primary", pri_xml_path, pri_db),
("filelists", fil_xml_path, fil_db),
("other", oth_xml_path, oth_db),
("primary_db", pri_db_path, None),
("filelists_db", fil_db_path, None),
("other_db", oth_db_path, None),
("updateinfo", upd_xml_path, None))
("updateinfo", upd_xml_path, None)]

repomdrecords.extend(publication_data.repomdrecords)

sqlite_files = ("primary_db", "filelists_db", "other_db")
for name, path, db_to_update in repomdrecords:
Expand Down Expand Up @@ -184,33 +257,3 @@ def publish(repository_version_pk):
file=File(open(os.path.basename(repomd_path), 'rb'))
)
metadata.save()


def populate(publication):
"""
Populate a publication.

Create published artifacts for a publication.

Args:
publication (pulpcore.plugin.models.Publication): A Publication to populate.

Returns:
packages (pulp_rpm.models.Package): A list of published packages.

"""
packages = Package.objects.filter(pk__in=publication.repository_version.content).\
prefetch_related('contentartifact_set')
published_artifacts = []

for package in packages:
for content_artifact in package.contentartifact_set.all():
published_artifacts.append(PublishedArtifact(
relative_path=content_artifact.relative_path,
publication=publication,
content_artifact=content_artifact)
)

PublishedArtifact.objects.bulk_create(published_artifacts)

return packages