Skip to content

Commit

Permalink
feat: add internal Podman.cp() helper
Browse files Browse the repository at this point in the history
Add a simple helper function to the Podman class to wrap the "podman cp"
behaviour. There are a couple of things added on top of it:
- to ease its usage, in addition to the actual container ID, it is
  possible to specify "$C" (as literal string) as ID, which will be
  replaced dynamically by the right ID
- any other ID is rejected, as the helper is supposed to copy only for
  the container represented by the specific class instance

Signed-off-by: Pino Toscano <[email protected]>
  • Loading branch information
ptoscano committed Jul 16, 2024
1 parent 11e560d commit 06e0546
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pytest_client_tools/podman.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ def __init__(
self._hostname = hostname
self._port_mappings = port_mappings

def _replace_container_name(self, path):
if path.startswith("$C:"):
return self._running_id + path[2:]
elif path.startswith(f"{self._running_id}:"):
return path
elif ":" in path:
raise ValueError("referring to a different podman container")
return path

@property
def running_id(self):
return self._running_id
Expand Down Expand Up @@ -90,3 +99,21 @@ def stop(self):
stderr=subprocess.PIPE,
)
self._running_id = None

@requires_running
def cp(self, src, dest):
actual_src = self._replace_container_name(src)
actual_dest = self._replace_container_name(dest)

args = [
"podman",
"cp",
actual_src,
actual_dest,
]
subprocess.run(
args,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

0 comments on commit 06e0546

Please sign in to comment.