Skip to content

Commit

Permalink
Published repo structured by alpha
Browse files Browse the repository at this point in the history
Packages are stored under 'Packages' directory and in underlying
directories starting with first letter of package.

re #4445
https://pulp.plan.io/issues/4445
  • Loading branch information
pavelpicka committed Dec 2, 2019
1 parent 3708978 commit c3bbfd1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES/4445.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
User can specify publication type to have packages sorter by alphabetical directory order.
4 changes: 4 additions & 0 deletions pulp_rpm/app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,7 @@
DESC_BY_LANG='desc_by_lang',
NAME_BY_LANG='name_by_lang'
)

PUBLICATION_TYPE = SimpleNamespace(
ALPHA='alpha'
)
10 changes: 9 additions & 1 deletion pulp_rpm/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,16 @@ class RpmPublicationSerializer(PublicationSerializer):
A Serializer for RpmPublication.
"""

publication_type = serializers.CharField(
help_text=_("Type of publication."),
required=False,
default=None
)

class Meta:
fields = PublicationSerializer.Meta.fields
fields = PublicationSerializer.Meta.fields + (
'publication_type',
)
model = RpmPublication


Expand Down
35 changes: 28 additions & 7 deletions pulp_rpm/app/tasks/publishing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from pulpcore.plugin.tasking import WorkingDirectory

from pulp_rpm.app.constants import PUBLICATION_TYPE

from pulp_rpm.app.models import (
DistributionTree,
Modulemd,
Expand Down Expand Up @@ -111,7 +113,7 @@ def get_packages(self, contents):

return packages

def populate(self):
def populate(self, publication_type):
"""
Populate a publication.
Expand Down Expand Up @@ -166,20 +168,28 @@ def populate(self):

for package in all_packages.distinct():
for content_artifact in package.contentartifact_set.all():
if publication_type == PUBLICATION_TYPE.ALPHA:
relative_path = os.path.join(
"Packages", package.name[0].lower(),
content_artifact.relative_path
)
else:
relative_path = content_artifact.relative_path
self.published_artifacts.append(PublishedArtifact(
relative_path=content_artifact.relative_path,
relative_path=relative_path,
publication=self.publication,
content_artifact=content_artifact)
)

PublishedArtifact.objects.bulk_create(self.published_artifacts)


def publish(repository_version_pk):
def publish(repository_version_pk, publication_type):
"""
Create a Publication based on a RepositoryVersion.
Args:
publication_type (str): Publication type.
repository_version_pk (str): Create a publication from this repository version.
"""
repository_version = RepositoryVersion.objects.get(pk=repository_version_pk)
Expand All @@ -192,12 +202,14 @@ def publish(repository_version_pk):
with WorkingDirectory():
with RpmPublication.create(repository_version) as publication:
publication_data = PublicationData(publication)
publication_data.populate()
publication_data.populate(publication_type)

packages = publication_data.packages

# Main repo
create_repomd_xml(packages, publication, publication_data.repomdrecords)
create_repomd_xml(
packages, publication, publication_data.repomdrecords, publication_type
)

for sub_repo in publication_data.sub_repos:
name = sub_repo[0]
Expand All @@ -206,7 +218,8 @@ def publish(repository_version_pk):
create_repomd_xml(packages, publication, extra_repomdrecords, name)


def create_repomd_xml(packages, publication, extra_repomdrecords, sub_folder=None):
def create_repomd_xml(packages, publication, extra_repomdrecords,
publication_type, sub_folder=None):
"""
Creates a repomd.xml file.
Expand All @@ -215,6 +228,7 @@ def create_repomd_xml(packages, publication, extra_repomdrecords, sub_folder=Non
publication(pulpcore.plugin.models.Publication): the publication
extra_repomdrecords(list): list with data relative to repo metadata files
sub_folder(str): name of the folder for sub repos
publication_type(str): type of published hierarchy
"""
cwd = os.getcwd()
Expand Down Expand Up @@ -253,7 +267,14 @@ def create_repomd_xml(packages, publication, extra_repomdrecords, sub_folder=Non
# Process all packages
for package in packages:
pkg = package.to_createrepo_c()
pkg.location_href = package.contentartifact_set.first().relative_path
if publication_type == PUBLICATION_TYPE.ALPHA:
relative_path = os.path.join(
"Packages", pkg.name[0].lower(),
package.contentartifact_set.first().relative_path
)
pkg.location_href = relative_path
else:
pkg.location_href = package.contentartifact_set.first().relative_path
pri_xml.add_pkg(pkg)
fil_xml.add_pkg(pkg)
oth_xml.add_pkg(pkg)
Expand Down
4 changes: 3 additions & 1 deletion pulp_rpm/app/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,14 @@ def create(self, request):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
repository_version = serializer.validated_data.get('repository_version')
publication_type = serializer.validated_data.get('publication_type')

result = enqueue_with_reservation(
tasks.publish,
[repository_version.repository],
kwargs={
'repository_version_pk': repository_version.pk
'repository_version_pk': repository_version.pk,
'publication_type': publication_type
}
)
return OperationPostponedResponse(result, request)
Expand Down

0 comments on commit c3bbfd1

Please sign in to comment.