66import typing as t
77import warnings
88from contextlib import ExitStack
9+ from pathlib import Path
910
1011from packaging .specifiers import SpecifierSet
1112
@@ -248,6 +249,7 @@ def __init__(
248249 fake_mode : bool = False ,
249250 verify_ssl : bool | str | None = None ,
250251 verify : bool | str | None = None , # Deprecated
252+ chunk_size : int | None = None ,
251253 ) -> None :
252254 self ._api : OpenAPI | None = None
253255 self ._api_root : str = api_root
@@ -273,6 +275,7 @@ def __init__(
273275 self .fake_mode : bool = fake_mode
274276 if self .fake_mode :
275277 self ._api_kwargs ["dry_run" ] = True
278+ self .chunk_size = chunk_size
276279
277280 @classmethod
278281 def from_config_files (
@@ -1565,6 +1568,7 @@ def __init__(
15651568 repository_ctx : PulpRepositoryContext | None = None ,
15661569 ):
15671570 super ().__init__ (pulp_ctx , pulp_href = pulp_href , entity = entity )
1571+ assert (repository_ctx is None ) or (repository_ctx .pulp_ctx is pulp_ctx )
15681572 self .repository_ctx = repository_ctx
15691573
15701574 def list (self , limit : int , offset : int , parameters : dict [str , t .Any ]) -> list [t .Any ]:
@@ -1578,6 +1582,29 @@ def find(self, **kwargs: t.Any) -> t.Any:
15781582 kwargs ["repository_version" ] = self .repository_ctx .entity ["latest_version_href" ]
15791583 return super ().find (** kwargs )
15801584
1585+ def _prepare_upload (
1586+ self ,
1587+ body : EntityDefinition ,
1588+ file : t .IO [bytes ],
1589+ chunk_size : int | None ,
1590+ ) -> None :
1591+ _chunk_size : int | None = chunk_size or self .pulp_ctx .chunk_size
1592+ size = os .path .getsize (file .name )
1593+ if not self .pulp_ctx .fake_mode : # Skip the uploading part in fake_mode
1594+ if _chunk_size is None or _chunk_size > size :
1595+ body ["file" ] = file
1596+ elif self .pulp_ctx .has_plugin (PluginRequirement ("core" , specifier = ">=3.20.0" )):
1597+ self .needs_capability ("upload" )
1598+ from pulp_glue .core .context import PulpUploadContext
1599+
1600+ upload_href = PulpUploadContext (self .pulp_ctx ).upload_file (file , _chunk_size )
1601+ body ["upload" ] = upload_href
1602+ else :
1603+ from pulp_glue .core .context import PulpArtifactContext
1604+
1605+ artifact_href = PulpArtifactContext (self .pulp_ctx ).upload (file , _chunk_size )
1606+ body ["artifact" ] = artifact_href
1607+
15811608 def create (
15821609 self ,
15831610 body : EntityDefinition ,
@@ -1589,24 +1616,10 @@ def create(
15891616 file = body .pop ("file" , None )
15901617 chunk_size : int | None = body .pop ("chunk_size" , None )
15911618 if file :
1592- if isinstance (file , str ):
1619+ if isinstance (file , str | Path ):
15931620 file = open (file , "rb" )
15941621 cleanup .enter_context (file )
1595- size = os .path .getsize (file .name )
1596- if not self .pulp_ctx .fake_mode : # Skip the uploading part in fake_mode
1597- if chunk_size is None or chunk_size > size :
1598- body ["file" ] = file
1599- elif self .pulp_ctx .has_plugin (PluginRequirement ("core" , specifier = ">=3.20.0" )):
1600- self .needs_capability ("upload" )
1601- from pulp_glue .core .context import PulpUploadContext
1602-
1603- upload_href = PulpUploadContext (self .pulp_ctx ).upload_file (file , chunk_size )
1604- body ["upload" ] = upload_href
1605- else :
1606- from pulp_glue .core .context import PulpArtifactContext
1607-
1608- artifact_href = PulpArtifactContext (self .pulp_ctx ).upload (file , chunk_size )
1609- body ["artifact" ] = artifact_href
1622+ self ._prepare_upload (body , file , chunk_size )
16101623 if self .repository_ctx is not None :
16111624 body ["repository" ] = self .repository_ctx
16121625 return super ().create (body = body , parameters = parameters , non_blocking = non_blocking )
@@ -1618,7 +1631,7 @@ def delete(self, non_blocking: bool = False) -> None:
16181631 def upload (
16191632 self ,
16201633 file : t .IO [bytes ],
1621- chunk_size : int ,
1634+ chunk_size : int | None ,
16221635 repository : PulpRepositoryContext | None ,
16231636 ** kwargs : t .Any ,
16241637 ) -> t .Any :
@@ -1629,31 +1642,18 @@ def upload(
16291642
16301643 Parameters:
16311644 file: A file like object that supports `os.path.getsize`.
1632- chunk_size: Size of the chunks to upload independently.
1645+ chunk_size: Size of the chunks to upload independently. `None` to disable chunking.
16331646 repository: Repository context to add the newly created content to.
16341647 kwargs: Extra args specific to the content type, passed to the create call.
16351648
16361649 Returns:
16371650 The result of the create task.
16381651 """
16391652 self .needs_capability ("upload" )
1640- size = os .path .getsize (file .name )
16411653 body : dict [str , t .Any ] = {** kwargs }
1642- if not self .pulp_ctx .fake_mode : # Skip the uploading part in fake_mode
1643- if chunk_size > size :
1644- body ["file" ] = file
1645- elif self .pulp_ctx .has_plugin (PluginRequirement ("core" , specifier = ">=3.20.0" )):
1646- from pulp_glue .core .context import PulpUploadContext
1647-
1648- upload_href = PulpUploadContext (self .pulp_ctx ).upload_file (file , chunk_size )
1649- body ["upload" ] = upload_href
1650- else :
1651- from pulp_glue .core .context import PulpArtifactContext
1652-
1653- artifact_href = PulpArtifactContext (self .pulp_ctx ).upload (file , chunk_size )
1654- body ["artifact" ] = artifact_href
1655- if repository :
1656- body ["repository" ] = repository
1654+ self ._prepare_upload (body , file , chunk_size )
1655+ if repository is not None :
1656+ body ["repository" ] = repository
16571657 return self .create (body = body )
16581658
16591659
0 commit comments