From 2fe9829b6f1e1e9a48ebe4d776d3219e84486661 Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Sat, 1 Jun 2024 17:23:56 +0100 Subject: [PATCH 1/5] Update PodCLI.create() to use Iterable and Mapping types --- .../components/pod/cli_wrapper.py | 66 +++++++++---------- tests/python_on_whales/components/test_pod.py | 4 +- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/python_on_whales/components/pod/cli_wrapper.py b/python_on_whales/components/pod/cli_wrapper.py index d476ac4b..15ac8a6d 100644 --- a/python_on_whales/components/pod/cli_wrapper.py +++ b/python_on_whales/components/pod/cli_wrapper.py @@ -259,17 +259,17 @@ def create( self, name: Optional[str] = None, *, - add_hosts: List[Tuple[str, str]] = [], + add_hosts: Iterable[Tuple[str, str]] = (), cgroup_parent: Optional[str] = None, cpus: Optional[float] = None, - cpuset_cpus: Optional[List[int]] = None, - devices: List[str] = [], - device_read_bps: List[str] = [], - dns: List[str] = [], - dns_options: List[str] = [], - dns_search: List[str] = [], + cpuset_cpus: Optional[Iterable[int]] = None, + devices: Iterable[str] = (), + device_read_bps: Iterable[str] = (), + dns: Iterable[str] = (), + dns_options: Iterable[str] = (), + dns_search: Iterable[str] = (), exit_policy: Optional[str] = None, - gidmaps: List[Tuple[int, int, int]] = [], + gidmaps: Iterable[Tuple[int, int, int]] = (), hostname: Optional[str] = None, infra: Optional[bool] = None, infra_command: Optional[str] = None, @@ -280,33 +280,33 @@ def create( infra_name: Optional[str] = None, ip: Optional[str] = None, ip6: Optional[str] = None, - labels: Dict[str, str] = {}, - label_files: List[ValidPath] = [], + labels: Mapping[str, str] = {}, + label_files: Iterable[ValidPath] = (), mac_address: Optional[str] = None, memory: Union[int, str, None] = None, - networks: List[ + networks: Iterable[ python_on_whales.components.network.cli_wrapper.ValidNetwork - ] = [], - network_aliases: List[str] = [], + ] = (), + network_aliases: Iterable[str] = (), no_hosts: bool = False, pid: Optional[str] = None, pod_id_file: Optional[ValidPath] = None, - publish: List[ValidPortMapping] = [], + publish: Iterable[ValidPortMapping] = (), replace: bool = False, restart: Optional[str] = None, - security_options: List[str] = [], - share: Optional[List[str]] = None, + security_options: Iterable[str] = (), + share: Optional[Iterable[str]] = None, shm_size: Optional[Union[int, str]] = None, subgidname: Optional[str] = None, subuidname: Optional[str] = None, - sysctl: Dict[str, str] = {}, - uidmaps: List[Tuple[int, int, int]] = [], + sysctl: Mapping[str, str] = {}, + uidmaps: Iterable[Tuple[int, int, int]] = (), userns: Optional[str] = None, uts: Optional[str] = None, - volumes: List[VolumeDefinition] = [], - volumes_from: List[ + volumes: Iterable[VolumeDefinition] = (), + volumes_from: Iterable[ python_on_whales.components.container.cli_wrapper.ValidContainer - ] = [], + ] = (), ) -> Pod: """Creates a pod, but does not start it. @@ -401,19 +401,19 @@ def create( full_cmd = self.docker_cmd + ["pod", "create"] full_cmd.add_simple_arg("--name", name) - full_cmd.add_args_iterable_or_single( + full_cmd.add_args_iterable( "--add-host", [f"{host}:{ip}" for host, ip in add_hosts] ) full_cmd.add_simple_arg("--cgroup-parent", cgroup_parent) full_cmd.add_simple_arg("--cpus", cpus) full_cmd.add_simple_arg("--cpuset-cpus", join_if_not_none(cpuset_cpus)) - full_cmd.add_args_iterable_or_single("--device", devices) - full_cmd.add_args_iterable_or_single("--device-read-bps", device_read_bps) - full_cmd.add_args_iterable_or_single("--dns", dns) - full_cmd.add_args_iterable_or_single("--dns-option", dns_options) - full_cmd.add_args_iterable_or_single("--dns-search", dns_search) + full_cmd.add_args_iterable("--device", devices) + full_cmd.add_args_iterable("--device-read-bps", device_read_bps) + full_cmd.add_args_iterable("--dns", dns) + full_cmd.add_args_iterable("--dns-option", dns_options) + full_cmd.add_args_iterable("--dns-search", dns_search) full_cmd.add_simple_arg("--exit-policy", exit_policy) - full_cmd.add_args_iterable_or_single("--gidmap", [":".join(x) for x in gidmaps]) + full_cmd.add_args_iterable("--gidmap", [":".join(x) for x in gidmaps]) full_cmd.add_simple_arg("--hostname", hostname) if infra is not None: @@ -426,27 +426,27 @@ def create( full_cmd.add_simple_arg("--ip", ip) full_cmd.add_simple_arg("--ip6", ip6) - full_cmd.add_args_iterable_or_single("--label", format_mapping_for_cli(labels)) + full_cmd.add_args_mapping("--label", labels) full_cmd.add_args_iterable_or_single("--label-file", label_files) full_cmd.add_simple_arg("--mac-address", mac_address) full_cmd.add_simple_arg("--memory", memory) full_cmd.add_args_iterable_or_single("--network", networks) - full_cmd.add_args_iterable_or_single("--network-alias", network_aliases) + full_cmd.add_args_iterable("--network-alias", network_aliases) full_cmd.add_flag("--no-hosts", no_hosts) full_cmd.add_simple_arg("--pid", pid) full_cmd.add_simple_arg("--pod-id-file", pod_id_file) - full_cmd.add_args_iterable_or_single( + full_cmd.add_args_iterable( "-p", [format_port_arg(p) for p in publish] ) full_cmd.add_flag("--replace", replace) full_cmd.add_simple_arg("--restart", restart) - full_cmd.add_args_iterable_or_single("--security-opt", security_options) + full_cmd.add_args_iterable("--security-opt", security_options) if share is not None: full_cmd.add_simple_arg("--share", ",".join(share)) full_cmd.add_simple_arg("--shm-size", shm_size) full_cmd.add_simple_arg("--subgidname", subgidname) full_cmd.add_simple_arg("--subuidname", subuidname) - full_cmd.add_args_iterable_or_single("--sysctl", format_mapping_for_cli(sysctl)) + full_cmd.add_args_mapping("--sysctl", sysctl) full_cmd.add_args_iterable_or_single("--uidmap", [":".join(x) for x in uidmaps]) full_cmd.add_simple_arg("--userns", userns) full_cmd.add_simple_arg("--uts", uts) diff --git a/tests/python_on_whales/components/test_pod.py b/tests/python_on_whales/components/test_pod.py index c583ded3..035c713a 100644 --- a/tests/python_on_whales/components/test_pod.py +++ b/tests/python_on_whales/components/test_pod.py @@ -192,9 +192,9 @@ def test_create_share_arg(podman_client: DockerClient): "ubuntu", ["readlink", "/proc/self"], pod=pod ) assert output == "1" - with podman_client.pod.create(pod_name, share=["pid"]) as pod: + with podman_client.pod.create(pod_name, share=["pid", "ipc"]) as pod: pod.start() # start the infra container (PID 1) - assert pod.shared_namespaces == ["pid"] + assert pod.shared_namespaces == ["pid", "ipc"] output = podman_client.container.run( "ubuntu", ["readlink", "/proc/self"], pod=pod ) From d1156b7d70d07896a27126e0dca31c2a29922779 Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Sat, 8 Jun 2024 17:42:01 +0100 Subject: [PATCH 2/5] Fix black --- python_on_whales/components/pod/cli_wrapper.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/python_on_whales/components/pod/cli_wrapper.py b/python_on_whales/components/pod/cli_wrapper.py index 15ac8a6d..33ade240 100644 --- a/python_on_whales/components/pod/cli_wrapper.py +++ b/python_on_whales/components/pod/cli_wrapper.py @@ -435,9 +435,7 @@ def create( full_cmd.add_flag("--no-hosts", no_hosts) full_cmd.add_simple_arg("--pid", pid) full_cmd.add_simple_arg("--pod-id-file", pod_id_file) - full_cmd.add_args_iterable( - "-p", [format_port_arg(p) for p in publish] - ) + full_cmd.add_args_iterable("-p", [format_port_arg(p) for p in publish]) full_cmd.add_flag("--replace", replace) full_cmd.add_simple_arg("--restart", restart) full_cmd.add_args_iterable("--security-opt", security_options) From e4e228c278811a7c4e16cb78b034f77a7c095466 Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Sat, 31 Aug 2024 22:22:34 +0100 Subject: [PATCH 3/5] Run ruff --- python_on_whales/components/pod/cli_wrapper.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python_on_whales/components/pod/cli_wrapper.py b/python_on_whales/components/pod/cli_wrapper.py index 33ade240..09cd4937 100644 --- a/python_on_whales/components/pod/cli_wrapper.py +++ b/python_on_whales/components/pod/cli_wrapper.py @@ -4,7 +4,6 @@ from datetime import datetime, timedelta from typing import ( Any, - Dict, Iterable, List, Mapping, From fff4338aee94666531d40fd74d4296a93250fb9d Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Sat, 31 Aug 2024 22:24:30 +0100 Subject: [PATCH 4/5] Switch from Sequence to Iterable --- .../components/pod/cli_wrapper.py | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/python_on_whales/components/pod/cli_wrapper.py b/python_on_whales/components/pod/cli_wrapper.py index 09cd4937..7891600d 100644 --- a/python_on_whales/components/pod/cli_wrapper.py +++ b/python_on_whales/components/pod/cli_wrapper.py @@ -8,7 +8,6 @@ List, Mapping, Optional, - Sequence, Tuple, TypedDict, Union, @@ -473,10 +472,10 @@ def exists(self, pod: ValidPod) -> bool: def inspect(self, x: ValidPod, /) -> Pod: ... @overload - def inspect(self, x: Sequence[ValidPod], /) -> List[Pod]: ... + def inspect(self, x: Iterable[ValidPod], /) -> List[Pod]: ... def inspect( - self, x: Union[ValidPod, Sequence[ValidPod]], / + self, x: Union[ValidPod, Iterable[ValidPod]], / ) -> Union[Pod, List[Pod]]: """Creates a `python_on_whales.Pod` object. @@ -495,7 +494,7 @@ def inspect( def kill( self, - x: Union[ValidPod, Sequence[ValidPod]], + x: Union[ValidPod, Iterable[ValidPod]], /, *, signal: Optional[Union[int, str]] = None, @@ -615,7 +614,7 @@ def logs( else: return "".join(x[1].decode() for x in iterator) - def pause(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> None: + def pause(self, x: Union[ValidPod, Iterable[ValidPod]], /) -> None: """Pauses one or more pods Parameters: @@ -638,7 +637,7 @@ def prune(self) -> None: def remove( self, - x: Union[ValidPod, Sequence[ValidPod]], + x: Union[ValidPod, Iterable[ValidPod]], /, *, force: bool = False, @@ -668,7 +667,7 @@ def remove( full_cmd.extend([str(p) for p in pods]) run(full_cmd) - def restart(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> None: + def restart(self, x: Union[ValidPod, Iterable[ValidPod]], /) -> None: """Restarts one or more pods Parameters: @@ -684,7 +683,7 @@ def restart(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> None: full_cmd.extend([str(p) for p in pods]) run(full_cmd) - def start(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> None: + def start(self, x: Union[ValidPod, Iterable[ValidPod]], /) -> None: """Starts one or more pods Parameters: @@ -700,7 +699,7 @@ def start(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> None: full_cmd.extend([str(p) for p in pods]) run(full_cmd) - def stats(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> List[PodStats]: + def stats(self, x: Union[ValidPod, Iterable[ValidPod]], /) -> List[PodStats]: """Get pods resource usage statistics The data unit is the byte. @@ -728,7 +727,7 @@ def stats(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> List[PodStats]: def stop( self, - x: Union[ValidPod, Sequence[ValidPod]], + x: Union[ValidPod, Iterable[ValidPod]], /, *, time: Optional[int] = None, @@ -756,7 +755,7 @@ def top(self, pod: ValidPod): Not yet implemented""" raise NotImplementedError - def unpause(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> None: + def unpause(self, x: Union[ValidPod, Iterable[ValidPod]], /) -> None: """Unpauses one or more pods Parameters: From 46ba37fac8a0598b39d6e5f2f8eb031edb79ff19 Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Sat, 31 Aug 2024 22:34:19 +0100 Subject: [PATCH 5/5] Fix test passing the wrong type --- tests/python_on_whales/components/test_pod.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python_on_whales/components/test_pod.py b/tests/python_on_whales/components/test_pod.py index 035c713a..41697be7 100644 --- a/tests/python_on_whales/components/test_pod.py +++ b/tests/python_on_whales/components/test_pod.py @@ -119,7 +119,7 @@ def test_create_devices_arg(podman_client: DockerClient): def test_create_dns_arg(podman_client: DockerClient): pod_name = random_name() - with podman_client.pod.create(pod_name, dns="1.2.3.4") as pod: + with podman_client.pod.create(pod_name, dns=["1.2.3.4"]) as pod: output = podman_client.container.run( "ubuntu", ["cat", "/etc/resolv.conf"], pod=pod )