Skip to content
Open
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/1391.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `blocklist` subcommand group to `pulp python repository` for managing blocklist entries (add, list, remove, show).
1 change: 1 addition & 0 deletions CHANGES/pulp-glue/1391.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `PulpPythonBlocklistEntryContext` for `pulp_python>=3.30.0`.
23 changes: 23 additions & 0 deletions pulp-glue/src/pulp_glue/python/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
EntityDefinition,
PluginRequirement,
PulpContentContext,
PulpContext,
PulpDistributionContext,
PulpEntityContext,
PulpPublicationContext,
PulpRemoteContext,
PulpRepositoryContext,
Expand Down Expand Up @@ -165,3 +167,24 @@ def preprocess_entity(self, body: EntityDefinition, partial: bool = False) -> En
def repair_metadata(self) -> t.Any:
self.needs_capability("repair_metadata")
return self.call("repair_metadata", parameters={self.HREF: self.pulp_href})


class PulpPythonBlocklistEntryContext(PulpEntityContext):
ENTITY = _("blocklist entry")
ENTITIES = _("blocklist entries")
HREF = "python_python_python_blocklist_entry_href"
ID_PREFIX = "repositories_python_python_blocklist_entries"
NEEDS_PLUGINS = [PluginRequirement("python", specifier=">=3.30.0")]
repository_ctx: PulpPythonRepositoryContext

def __init__(
self,
pulp_ctx: PulpContext,
repository_ctx: PulpPythonRepositoryContext,
) -> None:
super().__init__(pulp_ctx)
self.repository_ctx = repository_ctx

@property
def scope(self) -> dict[str, t.Any]:
return {self.repository_ctx.HREF: self.repository_ctx.pulp_href}
93 changes: 93 additions & 0 deletions src/pulpcore/cli/python/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from pulp_glue.common.context import (
EntityFieldDefinition,
PluginRequirement,
PulpEntityContext,
PulpRemoteContext,
PulpRepositoryContext,
)
from pulp_glue.common.i18n import get_translation
from pulp_glue.python.context import (
PulpPythonBlocklistEntryContext,
PulpPythonContentContext,
PulpPythonProvenanceContext,
PulpPythonRemoteContext,
Expand All @@ -28,6 +30,7 @@
list_command,
lookup_callback,
name_option,
pass_entity_context,
pass_pulp_context,
pass_repository_context,
pulp_group,
Expand Down Expand Up @@ -152,6 +155,96 @@ def repository() -> None:
)


@repository.group(needs_plugins=[PluginRequirement("python", specifier=">=3.30.0")])
@pass_repository_context
@pass_pulp_context
@click.pass_context
def blocklist(
ctx: click.Context,
pulp_ctx: PulpCLIContext,
repository_ctx: PulpRepositoryContext,
/,
) -> None:
"""
Manage blocklist entries for a Python repository.
"""
assert isinstance(repository_ctx, PulpPythonRepositoryContext)
ctx.obj = PulpPythonBlocklistEntryContext(pulp_ctx, repository_ctx)


_HELP_BLOCKLIST_NAME = _("Package name to block. Required when 'filename' is not provided.")
_HELP_BLOCKLIST_VERSION = _("Exact version to block. Only used when 'name' is set.")
_HELP_BLOCKLIST_FILENAME = _("Exact filename to block. Required when 'name' is not provided.")

blocklist_options = [
click.option("--name", help=_HELP_BLOCKLIST_NAME),
click.option("--version", help=_HELP_BLOCKLIST_VERSION),
click.option("--filename", help=_HELP_BLOCKLIST_FILENAME),
]
blocklist_lookup_options = [
pulp_option(
"--name",
help=_HELP_BLOCKLIST_NAME,
callback=lookup_callback("name"),
expose_value=False,
),
pulp_option(
"--version",
help=_HELP_BLOCKLIST_VERSION,
callback=lookup_callback("version"),
expose_value=False,
),
pulp_option(
"--filename",
help=_HELP_BLOCKLIST_FILENAME,
callback=lookup_callback("filename"),
expose_value=False,
),
href_option,
]

blocklist.add_command(
create_command(name="add", decorators=nested_lookup_options + blocklist_options)
)
blocklist.add_command(list_command(decorators=nested_lookup_options + blocklist_options))
blocklist.add_command(show_command(decorators=nested_lookup_options + blocklist_lookup_options))


@blocklist.command(name="remove")
@repository_href_option
@repository_lookup_option
@click.option("--name", help=_HELP_BLOCKLIST_NAME)
@click.option("--version", help=_HELP_BLOCKLIST_VERSION)
@click.option("--filename", help=_HELP_BLOCKLIST_FILENAME)
@href_option
@pass_entity_context
def blocklist_remove(
entity_ctx: PulpEntityContext,
/,
name: str | None,
version: str | None,
filename: str | None,
) -> None:
"""
Remove a blocklist entry.
"""
assert isinstance(entity_ctx, PulpPythonBlocklistEntryContext)
if version and filename:
raise click.ClickException(_("'version' cannot be used with 'filename'."))
if version and not name:
raise click.ClickException(_("'version' requires 'name' to be provided."))
if name and filename:
raise click.ClickException(_("Exactly one of 'name' or 'filename' must be provided."))

if name:
entity_ctx.entity = {"name": name}
if version:
entity_ctx.entity = {"version": version}
if filename:
entity_ctx.entity = {"filename": filename}
entity_ctx.delete()


@repository.command()
@name_option
@href_option
Expand Down
59 changes: 59 additions & 0 deletions tests/scripts/pulp_python/test_blocklist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

set -eu
# shellcheck source=tests/scripts/config.source
. "$(dirname "$(dirname "$(realpath "$0")")")"/config.source

pulp debug has-plugin --name "python" --specifier ">=3.30.0" || exit 23

cleanup() {
pulp python repository destroy --name "cli_test_python_blocklist" || true
}
trap cleanup EXIT

expect_succ pulp python repository create --name "cli_test_python_blocklist"

# Test adding a blocklist entry by package name
expect_succ pulp python repository blocklist add --repository "cli_test_python_blocklist" --name "pkg"
test "$(echo "$OUTPUT" | jq -r '.name')" = "pkg"
test "$(echo "$OUTPUT" | jq -r '.version')" = "null"
test "$(echo "$OUTPUT" | jq -r '.filename')" = "null"
ENTRY_HREF="$(echo "$OUTPUT" | jq -r '.pulp_href')"

# Test listing blocklist entries
expect_succ pulp python repository blocklist list --repository "cli_test_python_blocklist"
expect_succ test "$(echo "$OUTPUT" | jq -r '.|length')" = "1"
expect_succ pulp python repository blocklist list --repository "cli_test_python_blocklist" --name "pkg"
expect_succ test "$(echo "$OUTPUT" | jq -r '.|length')" = "1"
expect_succ pulp python repository blocklist list --repository "cli_test_python_blocklist" --name "nonexistent"
expect_succ test "$(echo "$OUTPUT" | jq -r '.|length')" = "0"

# Test showing a specific blocklist entry
expect_succ pulp python repository blocklist show --repository "cli_test_python_blocklist" --href "$ENTRY_HREF"
expect_succ test "$(echo "$OUTPUT" | jq -r '.name')" = "pkg"
expect_succ pulp python repository blocklist show --repository "cli_test_python_blocklist" --name "pkg"
expect_succ test "$(echo "$OUTPUT" | jq -r '.name')" = "pkg"

# Test remove validation
expect_fail pulp python repository blocklist remove --repository "cli_test_python_blocklist" --version "1.0" --filename "pkg-1.0.tar.gz"
expect_fail pulp python repository blocklist remove --repository "cli_test_python_blocklist" --version "1.0"
expect_fail pulp python repository blocklist remove --repository "cli_test_python_blocklist" --name "pkg" --filename "pkg-1.0.tar.gz"

# Test removing a blocklist entry by href
expect_succ pulp python repository blocklist remove --repository "cli_test_python_blocklist" --href "$ENTRY_HREF"
expect_succ pulp python repository blocklist list --repository "cli_test_python_blocklist"
expect_succ test "$(echo "$OUTPUT" | jq -r '.|length')" = "0"

# Test removing a blocklist entry by package name + version
expect_succ pulp python repository blocklist add --repository "cli_test_python_blocklist" --name "pkg" --version "2.0"
expect_succ pulp python repository blocklist remove --repository "cli_test_python_blocklist" --name "pkg" --version "2.0"
expect_succ pulp python repository blocklist list --repository "cli_test_python_blocklist"
expect_succ test "$(echo "$OUTPUT" | jq -r '.|length')" = "0"

# Test removing a blocklist entry by package filename
expect_succ pulp python repository blocklist add --repository "cli_test_python_blocklist" --filename "pkg-3.0.tar.gz"
expect_succ pulp python repository blocklist remove --repository "cli_test_python_blocklist" --filename "pkg-3.0.tar.gz"
expect_succ pulp python repository blocklist list --repository "cli_test_python_blocklist"
expect_succ test "$(echo "$OUTPUT" | jq -r '.|length')" = "0"

expect_succ pulp python repository destroy --name "cli_test_python_blocklist"
Loading