Skip to content

Commit 9be8871

Browse files
YasenTggainey
authored andcommitted
When no repo, use synchronous upload for rpm
1 parent eb1564b commit 9be8871

File tree

2 files changed

+92
-18
lines changed

2 files changed

+92
-18
lines changed

pulp-glue/pulp_glue/rpm/context.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import typing as t
23

34
from pulp_glue.common.context import (
@@ -154,6 +155,60 @@ def list_iterator(
154155
stats=stats,
155156
)
156157

158+
def upload(
159+
self,
160+
file: t.IO[bytes],
161+
chunk_size: int,
162+
repository: t.Optional[PulpRepositoryContext],
163+
**kwargs: t.Any,
164+
) -> t.Any:
165+
"""
166+
Create an RPM package by uploading a file.
167+
168+
This function is deprecated. The create call can handle the upload logic transparently.
169+
170+
Parameters:
171+
file: A file like object that supports `os.path.getsize`.
172+
chunk_size: Size of the chunks to upload independently.
173+
repository: Repository context to add the newly created content to.
174+
kwargs: Extra args specific to the content type, passed to the create call.
175+
176+
Returns:
177+
The result of the create task.
178+
"""
179+
self.needs_capability("upload")
180+
size = os.path.getsize(file.name)
181+
body: t.Dict[str, t.Any] = {**kwargs}
182+
183+
if not self.pulp_ctx.fake_mode:
184+
if chunk_size > size:
185+
# Small file: direct upload
186+
body["file"] = file
187+
else:
188+
# Large file: chunked upload
189+
if self.pulp_ctx.has_plugin(PluginRequirement("core", specifier=">=3.20.0")):
190+
from pulp_glue.core.context import PulpUploadContext
191+
192+
upload_href = PulpUploadContext(self.pulp_ctx).upload_file(file, chunk_size)
193+
body["upload"] = upload_href
194+
else:
195+
from pulp_glue.core.context import PulpArtifactContext
196+
197+
artifact_href = PulpArtifactContext(self.pulp_ctx).upload(file, chunk_size)
198+
body["artifact"] = artifact_href
199+
200+
# For rpm plugin >= 3.32.5, use synchronous upload endpoint when no repository is provided
201+
# For older versions, always use the create endpoint (backward compatibility)
202+
if repository is None and self.pulp_ctx.has_plugin(
203+
PluginRequirement("rpm", specifier=">=3.32.5")
204+
):
205+
return self.call("upload", body=body)
206+
207+
# Repository is specified or older rpm version: use create endpoint (async path)
208+
if repository is not None:
209+
body["repository"] = repository
210+
return self.create(body=body)
211+
157212

158213
class PulpRpmAdvisoryContext(PulpContentContext):
159214
PLUGIN = "rpm"

pulpcore/cli/rpm/content.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,17 @@ def upload(
389389
_("You must specify one (and only one) of --file or --directory.")
390390
)
391391

392-
# Sanity: If directory, repository required
392+
# Sanity: If using temp repository, both directory and repository are required
393393
final_dest_repo_ctx = kwargs["repository"]
394-
if directory and not final_dest_repo_ctx:
394+
if kwargs["use_temp_repository"] and not (directory and final_dest_repo_ctx):
395395
raise click.ClickException(
396-
_("You must specify a --repository to use --directory uploads.")
396+
_(
397+
"You must specify both --directory and --repository "
398+
"to use --use-temp-repository."
399+
)
397400
)
398401

399-
# Sanity: ignore publish|use_temp unless directory has been specified
402+
# Sanity: ignore publish|use_temp unless destination-repository has been specified
400403
use_tmp = final_dest_repo_ctx and kwargs["use_temp_repository"]
401404
do_publish = final_dest_repo_ctx and kwargs["publish"]
402405

@@ -416,10 +419,11 @@ def upload(
416419
)
417420
else:
418421
# Upload a directory-full of RPMs
422+
dest_repo_ctx = None
419423
try:
420424
dest_repo_ctx = _determine_upload_repository(final_dest_repo_ctx, pulp_ctx, use_tmp)
421425
result = _upload_rpms(entity_ctx, dest_repo_ctx, directory, chunk_size)
422-
if use_tmp:
426+
if use_tmp and dest_repo_ctx:
423427
result = _copy_to_final(dest_repo_ctx, final_dest_repo_ctx, pulp_ctx)
424428
finally:
425429
if use_tmp and dest_repo_ctx:
@@ -488,23 +492,36 @@ def _copy_to_final(
488492

489493
def _upload_rpms(
490494
entity_ctx: PulpContentContext,
491-
dest_repo_ctx: PulpRpmRepositoryContext,
495+
dest_repo_ctx: t.Optional[PulpRpmRepositoryContext],
492496
directory: t.Any,
493497
chunk_size: int,
494498
) -> t.Any:
495499
rpms_path = f"{directory}/*.rpm"
496500
rpm_names = glob.glob(rpms_path)
497501
if not rpm_names:
498502
raise click.ClickException(_("Directory {} has no .rpm files in it.").format(directory))
499-
click.echo(
500-
_(
501-
"About to upload {} files for {}.".format(
502-
len(rpm_names),
503-
dest_repo_ctx.entity["name"],
504-
)
505-
),
506-
err=True,
507-
)
503+
504+
# Build message based on whether we have a repository or not
505+
if dest_repo_ctx:
506+
click.echo(
507+
_(
508+
"About to upload {} files for {}.".format(
509+
len(rpm_names),
510+
dest_repo_ctx.entity["name"],
511+
)
512+
),
513+
err=True,
514+
)
515+
else:
516+
click.echo(
517+
_(
518+
"About to upload {} files from {}.".format(
519+
len(rpm_names),
520+
directory,
521+
)
522+
),
523+
err=True,
524+
)
508525
# Upload all *.rpm into the destination
509526
successful_uploads = 0
510527
for name in rpm_names:
@@ -525,9 +542,11 @@ def _upload_rpms(
525542

526543

527544
def _determine_upload_repository(
528-
final_dest_repo_ctx: PulpRpmRepositoryContext, pulp_ctx: PulpCLIContext, use_tmp: bool
529-
) -> PulpRpmRepositoryContext:
530-
if use_tmp:
545+
final_dest_repo_ctx: t.Optional[PulpRpmRepositoryContext],
546+
pulp_ctx: PulpCLIContext,
547+
use_tmp: bool,
548+
) -> t.Optional[PulpRpmRepositoryContext]:
549+
if use_tmp and final_dest_repo_ctx:
531550
dest_repo_ctx = PulpRpmRepositoryContext(pulp_ctx)
532551
body: t.Dict[str, t.Any] = {
533552
"name": f"uploadtmp_{final_dest_repo_ctx.entity['name']}_{uuid4()}",

0 commit comments

Comments
 (0)