Skip to content

Commit cdcf644

Browse files
committed
Rework - second version
1 parent 96c64f6 commit cdcf644

3 files changed

Lines changed: 60 additions & 104 deletions

File tree

pulp-glue/src/pulp_glue/python/context.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,23 @@ def preprocess_entity(self, body: EntityDefinition, partial: bool = False) -> En
7373
if "repository" not in body and "publication" in body:
7474
body["repository"] = None
7575

76-
version = body.pop("version", None)
77-
if version is not None:
78-
repository_href = body.pop("repository")
79-
body["repository_version"] = f"{repository_href}versions/{version}/"
76+
if self.pulp_ctx.has_plugin(PluginRequirement("python", specifier=">=3.21.0")):
77+
version = body.pop("version", None)
78+
if version is not None:
79+
if repository_href := body.pop("repository", None):
80+
body["repository_version"] = f"{repository_href}versions/{version}/"
81+
body["repository"] = None
82+
else:
83+
current_entity = self.entity
84+
if repository_href := current_entity.get("repository"):
85+
body["repository_version"] = f"{repository_href}versions/{version}/"
86+
body["repository"] = None
87+
elif repository_version_href := current_entity.get("repository_version"):
88+
repository_href = repository_version_href.partition("versions")[0]
89+
body["repository_version"] = f"{repository_href}versions/{version}/"
90+
elif "repository" in body:
91+
body["repository_version"] = None
92+
8093
return body
8194

8295

Lines changed: 10 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
import typing as t
2-
31
import click
42

5-
from pulp_glue.common.context import (
6-
EntityDefinition,
7-
EntityFieldDefinition,
8-
PluginRequirement,
9-
PulpEntityContext,
10-
)
3+
from pulp_glue.common.context import PluginRequirement
114
from pulp_glue.common.i18n import get_translation
125
from pulp_glue.python.context import (
136
PulpPythonDistributionContext,
@@ -27,13 +20,13 @@
2720
label_command,
2821
list_command,
2922
name_option,
30-
pass_entity_context,
3123
pass_pulp_context,
3224
pulp_group,
3325
pulp_labels_option,
3426
pulp_option,
3527
resource_option,
3628
show_command,
29+
update_command,
3730
)
3831

3932
translation = get_translation(__package__)
@@ -47,7 +40,6 @@
4740
context_table={"python:python": PulpPythonRepositoryContext},
4841
help=_(
4942
"Repository to be used for auto-distributing."
50-
" When used with --version, this will create repository_version instead."
5143
" When set, this will unset the 'publication'."
5244
" Specified as '[[<plugin>:]<type>:]<name>' or as href."
5345
),
@@ -86,7 +78,11 @@ def distribution(ctx: click.Context, pulp_ctx: PulpCLIContext, /, distribution_t
8678
pulp_option(
8779
"--version",
8880
type=int,
89-
help=_("A repository version number, leave blank for latest."),
81+
help=_(
82+
"A repository version number, leave blank for latest."
83+
" When used with --repository, this will select a repository version instead."
84+
" Repository will no longer be auto-distributed."
85+
),
9086
needs_plugins=[PluginRequirement("python", specifier=">=3.21.0")],
9187
),
9288
content_guard_option,
@@ -110,77 +106,8 @@ def distribution(ctx: click.Context, pulp_ctx: PulpCLIContext, /, distribution_t
110106
distribution.add_command(list_command(decorators=distribution_filter_options))
111107
distribution.add_command(show_command(decorators=lookup_options))
112108
distribution.add_command(create_command(decorators=create_options))
109+
distribution.add_command(
110+
update_command(decorators=lookup_options + update_options + [click.option("--base-path")])
111+
)
113112
distribution.add_command(destroy_command(decorators=lookup_options))
114113
distribution.add_command(label_command(decorators=nested_lookup_options))
115-
116-
117-
def apply_decorators(decorators_list: list[t.Callable[..., t.Any]]) -> t.Callable[..., t.Any]:
118-
def decorator(func: t.Callable[..., t.Any]) -> t.Callable[..., t.Any]:
119-
for d in decorators_list:
120-
func = d(func)
121-
return func
122-
123-
return decorator
124-
125-
126-
@distribution.command()
127-
@apply_decorators(lookup_options + update_options + [click.option("--base-path")])
128-
@pass_entity_context
129-
def update(
130-
distribution_ctx: PulpEntityContext,
131-
/,
132-
publication: str | None,
133-
repository: EntityFieldDefinition,
134-
version: int | None,
135-
content_guard: EntityFieldDefinition,
136-
allow_uploads: bool | None,
137-
remote: EntityFieldDefinition,
138-
pulp_labels: dict[str, str] | None,
139-
base_path: str | None,
140-
) -> None:
141-
"""
142-
Update a Python distribution.
143-
"""
144-
assert isinstance(distribution_ctx, PulpPythonDistributionContext)
145-
146-
dist_body: EntityDefinition = distribution_ctx.entity
147-
body: EntityDefinition = dict()
148-
149-
if publication is not None:
150-
body["publication"] = publication
151-
if content_guard is not None:
152-
body["content_guard"] = content_guard
153-
if allow_uploads is not None:
154-
body["allow_uploads"] = allow_uploads
155-
if remote is not None:
156-
body["remote"] = remote
157-
if pulp_labels is not None:
158-
body["pulp_labels"] = pulp_labels
159-
if base_path is not None:
160-
body["base_path"] = base_path
161-
162-
if repository is not None and isinstance(repository, PulpPythonRepositoryContext):
163-
repo = repository.entity
164-
if version is not None:
165-
if dist_body.get("repository"):
166-
distribution_ctx.update(body={"repository": ""}, non_blocking=True)
167-
body["repository_version"] = f"{repo['versions_href']}{version}/"
168-
else:
169-
if dist_body.get("repository_version"):
170-
distribution_ctx.update(body={"repository_version": ""}, non_blocking=True)
171-
body["repository"] = repo["pulp_href"]
172-
elif version is not None:
173-
if dist_body.get("repository"):
174-
distribution_ctx.update(body={"repository": ""}, non_blocking=True)
175-
body["repository_version"] = f"{dist_body['repository']}versions/{version}/"
176-
elif dist_body.get("repository_version"):
177-
repository_href = dist_body["repository_version"].partition("versions")[0]
178-
body["repository_version"] = f"{repository_href}versions/{version}/"
179-
else:
180-
raise click.ClickException(
181-
_(
182-
"Distribution doesn't have a repository set, "
183-
"please specify the repository to use with --repository"
184-
)
185-
)
186-
distribution_ctx.update(body=body)

tests/scripts/pulp_python/test_distribution.sh

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,39 @@ expect_succ pulp python distribution update \
4545
expect_succ pulp python distribution destroy --distribution "cli_test_python_distro"
4646

4747
# Test repository_version functionality
48-
pulp debug has-plugin --name "python" --specifier ">=3.21.0" || exit 0
48+
if pulp debug has-plugin --name "python" --specifier ">=3.21.0"; then
49+
expect_succ pulp python distribution create \
50+
--name "cli_test_python_distro_repo_version" \
51+
--base-path "cli_test_python_distro_repo_version" \
52+
--repository "cli_test_python_distribution_repository" \
53+
--version 0
54+
expect_succ pulp python distribution show --distribution "cli_test_python_distro_repo_version"
55+
echo "$OUTPUT" | jq -e '.repository_version | contains("/versions/0/")'
56+
echo "$OUTPUT" | jq -e '.repository == null'
4957

50-
expect_succ pulp python distribution create \
51-
--name "cli_test_python_distro_repo_version" \
52-
--base-path "cli_test_python_distro_repo_version" \
53-
--repository "cli_test_python_distribution_repository" \
54-
--version 0
55-
expect_succ pulp python distribution show --distribution "cli_test_python_distro_repo_version"
56-
echo "$OUTPUT" | jq -e '.repository_version | contains("/versions/0/")'
57-
echo "$OUTPUT" | jq -e '.repository == null'
58+
expect_succ pulp python distribution update \
59+
--distribution "cli_test_python_distro_repo_version" \
60+
--repository "cli_test_python_distribution_repository" \
61+
--version 1
62+
expect_succ pulp python distribution show --distribution "cli_test_python_distro_repo_version"
63+
echo "$OUTPUT" | jq -e '.repository_version | contains("/versions/1/")'
64+
echo "$OUTPUT" | jq -e '.repository == null'
5865

59-
expect_succ pulp python distribution update \
60-
--distribution "cli_test_python_distro_repo_version" \
61-
--repository "cli_test_python_distribution_repository" \
62-
--version 1
63-
expect_succ pulp python distribution show --distribution "cli_test_python_distro_repo_version"
64-
echo "$OUTPUT" | jq -e '.repository_version | contains("/versions/1/")'
65-
echo "$OUTPUT" | jq -e '.repository == null'
66+
expect_succ pulp python distribution update \
67+
--distribution "cli_test_python_distro_repo_version" \
68+
--version 0
69+
expect_succ pulp python distribution show --distribution "cli_test_python_distro_repo_version"
70+
echo "$OUTPUT" | jq -e '.repository_version | contains("/versions/0/")'
71+
echo "$OUTPUT" | jq -e '.repository == null'
72+
73+
expect_succ pulp python distribution update \
74+
--distribution "cli_test_python_distro_repo_version" \
75+
--repository "cli_test_python_distribution_repository"
76+
expect_succ pulp python distribution show --distribution "cli_test_python_distro_repo_version"
77+
echo "$OUTPUT" | jq -e '.repository_version == null'
78+
echo "$OUTPUT" | jq -e '.repository != null'
6679

67-
expect_succ pulp python distribution destroy --distribution "cli_test_python_distro_repo_version"
80+
expect_succ pulp python distribution destroy --distribution "cli_test_python_distro_repo_version"
81+
else
82+
echo "Python plugin version >=3.21.0 not available, skipping repository_version tests"
83+
fi

0 commit comments

Comments
 (0)