Skip to content

Commit

Permalink
Remove custom code used to deal with package metadata and package con…
Browse files Browse the repository at this point in the history
…tent.
  • Loading branch information
decko committed Feb 13, 2025
1 parent 8aea30b commit 68d783b
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 134 deletions.
9 changes: 0 additions & 9 deletions pulp_npm/app/content/__init__.py

This file was deleted.

11 changes: 0 additions & 11 deletions pulp_npm/app/content/handler.py

This file was deleted.

49 changes: 49 additions & 0 deletions pulp_npm/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
http://docs.pulpproject.org/en/3.0/nightly/plugins/plugin-writer/index.html
"""

import json
from logging import getLogger

from aiohttp.web_response import Response
from django.conf import settings
from django.db import models


from pulpcore.plugin.models import (
Content,
Remote,
Expand All @@ -17,6 +21,7 @@
)

from pulpcore.plugin.util import get_domain_pk
from .utils import urlpath_sanitize

logger = getLogger(__name__)

Expand Down Expand Up @@ -96,3 +101,47 @@ class NpmDistribution(Distribution):

class Meta:
default_related_name = "%(app_label)s_%(model_name)s"

def content_handler(self, path):
data = {}

repository_version = self.repository_version
if not repository_version:
repository_version = self.repository.latest_version()

content = repository_version.content
packages = Package.objects.filter(name=path, pk__in=content)

if not packages:
return None

if packages.count():
data["name"] = path
data["versions"] = {}
versions = []

for package in packages:
prefix_url = "{}/".format(
urlpath_sanitize(
settings.CONTENT_ORIGIN,
settings.CONTENT_PATH_PREFIX,
self.pulp_domain.name,
self.base_path,
)
)

tarball_url = f"{prefix_url}{package.relative_path.split('/')[-1]}"

version = {
package.version: {
"_id": f"{package.name}@{package.version}",
"dist": {"tarball": tarball_url},
}
}
versions.append(package.version)
data["versions"].update(version)

data["dist-tags"] = {"latest": max(versions)}

serialized_data = json.dumps(data)
return Response(body=serialized_data)
25 changes: 0 additions & 25 deletions pulp_npm/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
http://docs.pulpproject.org/en/3.0/nightly/plugins/plugin-writer/index.html
"""

from gettext import gettext as _

from django.conf import settings
from rest_framework import serializers

from pulpcore.plugin import models as core_models
from pulpcore.plugin import serializers as platform

from .utils import pulp_npm_content_path
from . import models


Expand Down Expand Up @@ -106,32 +102,11 @@ class Meta:
model = models.NpmRepository


class NpmBaseURLField(serializers.CharField):
"""
Field for the base_url field pointing to the npm content app.
"""

def to_representation(self, value):
"""
Field representation.
"""
base_path = value
origin = settings.CONTENT_ORIGIN
prefix = pulp_npm_content_path()
return "/".join((origin.strip("/"), prefix.strip("/"), base_path.lstrip("/")))


class NpmDistributionSerializer(platform.DistributionSerializer):
"""
Serializer for NPM Distributions.
"""

base_url = NpmBaseURLField(
source="base_path",
read_only=True,
help_text=_("The URL for accessing the universe API as defined by this distribution."),
)

class Meta:
fields = platform.DistributionSerializer.Meta.fields
model = models.NpmDistribution
6 changes: 0 additions & 6 deletions pulp_npm/app/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
"""
Check `Plugin Writer's Guide`_ for more details.
.. _Plugin Writer's Guide:
http://docs.pulpproject.org/en/3.0/nightly/plugins/plugin-writer/index.html
"""
10 changes: 1 addition & 9 deletions pulp_npm/app/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
from django.conf import settings
from django.urls import path

from pulp_npm.app.viewsets import PublishedPackageViewSet


PACKAGE_API_ROOT = getattr(settings, "PACKAGE_API_ROOT", "pulp_npm/packages/<path:name>/")

urlpatterns = [path(PACKAGE_API_ROOT, PublishedPackageViewSet.as_view())]
urlpatterns = []
21 changes: 15 additions & 6 deletions pulp_npm/app/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
from django.conf import settings
def urlpath_sanitize(*args):
"""
Join an arbitrary number of strings into a /-separated path.
Replaces uses of urljoin() that don't want/need urljoin's subtle semantics.
def pulp_npm_content_path():
"""Get base cotent path from configuration."""
components = settings.CONTENT_PATH_PREFIX.split("/")
components[1] = "pulp_npm"
return "/".join(components)
Returns: single string provided arguments separated by single-slashes
Args:
Arbitrary list of arguments to be join()ed
"""
segments = []
for a in args + ("",):
stripped = a.strip("/")
if stripped:
segments.append(stripped)
return "/".join(segments)
64 changes: 0 additions & 64 deletions pulp_npm/app/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
"""
Check `Plugin Writer's Guide`_ for more details.
.. _Plugin Writer's Guide:
http://docs.pulpproject.org/en/3.0/nightly/plugins/plugin-writer/index.html
"""

from django.conf import settings

from django.db import transaction
from drf_spectacular.utils import extend_schema

from rest_framework import status
from rest_framework.decorators import action
from rest_framework.views import APIView
from rest_framework.response import Response

from pulpcore.plugin import viewsets as core
Expand All @@ -24,7 +14,6 @@
from pulpcore.plugin.tasking import dispatch

from . import models, serializers, tasks
from .utils import pulp_npm_content_path


class PackageFilter(core.ContentFilter):
Expand Down Expand Up @@ -177,56 +166,3 @@ class NpmDistributionViewSet(core.DistributionViewSet):
endpoint_name = "npm"
queryset = models.NpmDistribution.objects.all()
serializer_class = serializers.NpmDistributionSerializer


class PublishedPackageViewSet(APIView):
"""
ViewSet for Published packages.
"""

authentication_classes = []
permission_classes = []

def get(self, request, name):
"""
Return a published package.
"""
data = {}

distributions = models.NpmDistribution.objects.all()
for distribution in distributions:
repository_version = distribution.repository_version
if not repository_version:
repository_version = distribution.repository.latest_version()
content = repository_version.content
packages = models.Package.objects.filter(name=name, pk__in=content)

if packages.count():
data["name"] = name
data["versions"] = {}
versions = []

for package in packages:
origin = settings.CONTENT_ORIGIN
prefix = pulp_npm_content_path()
tarball_url = "/".join(
(
origin.strip("/"),
prefix.strip("/"),
distribution.base_path.strip("/"),
package.relative_path.split("/")[-1],
)
)

version = {
package.version: {
"_id": f"{package.name}@{package.version}",
"dist": {"tarball": tarball_url},
}
}
versions.append(package.version)
data["versions"].update(version)

data["dist-tags"] = {"latest": max(versions)}

return Response(data)
2 changes: 1 addition & 1 deletion pulp_npm/tests/functional/api/test_download_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_download_content(
fixture_hash = hashlib.sha256(http_get(urljoin(NPM_FIXTURE_URL, unit_path))).hexdigest()

pulp_hash = hashlib.sha256(
http_get(urljoin(distribution.base_url + "/", unit_path.split("/")[-1]))
http_get(urljoin(distribution.base_url, unit_path.split("/")[-1]))
).hexdigest()

assert fixture_hash == pulp_hash
4 changes: 1 addition & 3 deletions pulp_npm/tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ def npm_remote(npm_remote_factory):
def get_npm_content_paths(npm_bindings):
"""Build closure for fetching content from a repository.
:param content_field: The name of a field on a RepositoryVersion, which
contains a dict of content types and the URL at which to view content.
:returns: A closure which returns content from the specified field.
:returns: A closure which returns content.
"""

def _get_npm_content_paths(repo, version_href=None):
Expand Down

0 comments on commit 68d783b

Please sign in to comment.