Skip to content

Commit 5c43924

Browse files
committed
Handle duplicate filenames across multiple content
1 parent 32cf7aa commit 5c43924

1 file changed

Lines changed: 43 additions & 11 deletions

File tree

pulpcore/cli/python/repository.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import typing as t
22

33
import click
4+
import schema as s
45
from pulp_glue.common.context import (
56
EntityFieldDefinition,
67
PluginRequirement,
@@ -15,14 +16,17 @@
1516
)
1617

1718
from pulp_cli.generic import (
19+
GroupOption,
1820
PulpCLIContext,
1921
create_command,
2022
create_content_json_callback,
2123
destroy_command,
2224
href_option,
25+
json_callback,
2326
label_command,
2427
label_select_option,
2528
list_command,
29+
load_file_wrapper,
2630
name_option,
2731
pass_pulp_context,
2832
pass_repository_context,
@@ -62,10 +66,31 @@ def _content_callback(
6266
if value:
6367
pulp_ctx = ctx.find_object(PulpCLIContext)
6468
assert pulp_ctx is not None
65-
ctx.obj = PulpPythonContentContext(pulp_ctx, entity={"filename": value})
69+
ctx.obj = PulpPythonContentContext(pulp_ctx, entity=value)
6670
return value
6771

6872

73+
CONTENT_LIST_SCHEMA = s.Schema([{"sha256": str, "filename": s.And(str, len)}])
74+
75+
76+
@load_file_wrapper
77+
def _content_list_callback(
78+
ctx: click.Context, param: click.Parameter, value: t.Optional[str]
79+
) -> t.Any:
80+
if value is None:
81+
return None
82+
83+
result = json_callback(ctx, param, value)
84+
try:
85+
return CONTENT_LIST_SCHEMA.validate(result)
86+
except s.SchemaError as e:
87+
raise click.ClickException(
88+
_("Validation of '{parameter}' failed: {error}").format(
89+
parameter=param.name, error=str(e)
90+
)
91+
)
92+
93+
6994
@pulp_group()
7095
@click.option(
7196
"-t",
@@ -97,20 +122,27 @@ def repository(ctx: click.Context, pulp_ctx: PulpCLIContext, /, repo_type: str)
97122
pulp_labels_option,
98123
]
99124
create_options = update_options + [click.option("--name", required=True)]
100-
package_option = click.option(
101-
"--filename",
102-
callback=_content_callback,
103-
expose_value=False,
104-
help=_("Filename of the python package"),
125+
package_options = [
126+
click.option("--sha256", cls=GroupOption, expose_value=False, group=["filename"]),
127+
click.option(
128+
"--filename",
129+
callback=_content_callback,
130+
expose_value=False,
131+
cls=GroupOption,
132+
group=["sha256"],
133+
help=_("Filename of the python package"),
134+
),
135+
]
136+
content_json_callback = create_content_json_callback(
137+
PulpPythonContentContext, schema=CONTENT_LIST_SCHEMA
105138
)
106-
content_json_callback = create_content_json_callback(PulpPythonContentContext)
107139
modify_options = [
108140
click.option(
109141
"--add-content",
110142
callback=content_json_callback,
111143
help=_(
112144
"""JSON string with a list of objects to add to the repository.
113-
Each object should have the key: "filename"
145+
Each object must contain the following keys: "sha256", "filename".
114146
The argument prefixed with the '@' can be the path to a JSON file with a list of objects."""
115147
),
116148
),
@@ -119,7 +151,7 @@ def repository(ctx: click.Context, pulp_ctx: PulpCLIContext, /, repo_type: str)
119151
callback=content_json_callback,
120152
help=_(
121153
"""JSON string with a list of objects to remove from the repository.
122-
Each object should have the key: "filename"
154+
Each object must contain the following keys: "sha256", "filename".
123155
The argument prefixed with the '@' can be the path to a JSON file with a list of objects."""
124156
),
125157
),
@@ -136,8 +168,8 @@ def repository(ctx: click.Context, pulp_ctx: PulpCLIContext, /, repo_type: str)
136168
repository.add_command(
137169
repository_content_command(
138170
contexts={"package": PulpPythonContentContext},
139-
add_decorators=[package_option],
140-
remove_decorators=[package_option],
171+
add_decorators=package_options,
172+
remove_decorators=package_options,
141173
modify_decorators=modify_options,
142174
base_default_plugin="python",
143175
base_default_type="python",

0 commit comments

Comments
 (0)