Skip to content

Commit 0510a43

Browse files
committed
Refactor container.exec_run usages
1 parent f2b42fd commit 0510a43

File tree

12 files changed

+36
-36
lines changed

12 files changed

+36
-36
lines changed

docs/maintaining/tagging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ and `markdown_piece(container)` method returns a piece of the build manifest.
8484
:start-at: class AptPackagesManifest
8585
```
8686

87-
- `quoted_output(container, cmd)` simply runs the command inside a container using `DockerRunner.run_simple_command` and wraps it to triple quotes to create a valid markdown piece.
87+
- `quoted_output(container, cmd)` simply runs the command inside a container using `DockerRunner.exec_cmd` and wraps it to triple quotes to create a valid markdown piece.
8888
It also adds the command which was run to the markdown piece.
8989
- `manifests/` subdirectory contains all the manifests.
9090
- `apps/write_manifest.py` is a Python executable to create the build manifest and history line for an image.

docs/maintaining/tagging_examples/docker_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
from tagging.utils.docker_runner import DockerRunner
44

55
with DockerRunner("ubuntu") as container:
6-
DockerRunner.run_simple_command(container, cmd="env", print_result=True)
6+
DockerRunner.exec_cmd(container, cmd="env", print_output=True)

tagging/manifests/conda_environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def markdown_piece(container: Container) -> MarkdownPiece:
1313
return MarkdownPiece(
1414
title="## Python Packages",
1515
sections=[
16-
DockerRunner.run_simple_command(container, "python --version"),
16+
DockerRunner.exec_cmd(container, "python --version"),
1717
quoted_output(container, "conda info"),
1818
quoted_output(container, "mamba info"),
1919
quoted_output(container, "mamba list"),

tagging/taggers/ubuntu_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class UbuntuVersionTagger(TaggerInterface):
1010
@staticmethod
1111
def tag_value(container: Container) -> str:
12-
os_release = DockerRunner.run_simple_command(
12+
os_release = DockerRunner.exec_cmd(
1313
container,
1414
"cat /etc/os-release",
1515
).split("\n")

tagging/taggers/versions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88

99
def _get_program_version(container: Container, program: str) -> str:
10-
return DockerRunner.run_simple_command(container, cmd=f"{program} --version")
10+
return DockerRunner.exec_cmd(container, cmd=f"{program} --version")
1111

1212

1313
def _get_pip_package_version(container: Container, package: str) -> str:
1414
PIP_VERSION_PREFIX = "Version: "
1515

16-
package_info = DockerRunner.run_simple_command(
16+
package_info = DockerRunner.exec_cmd(
1717
container,
1818
cmd=f"pip show {package}",
19-
print_result=False,
19+
print_output=False,
2020
)
2121
version_line = package_info.split("\n")[1]
2222
assert version_line.startswith(PIP_VERSION_PREFIX)

tagging/utils/docker_runner.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ def __exit__(
4343
LOGGER.info(f"Container {self.container.name} removed")
4444

4545
@staticmethod
46-
def run_simple_command(
47-
container: Container, cmd: str, print_result: bool = True
48-
) -> str:
46+
def exec_cmd(container: Container, cmd: str, print_output: bool = True) -> str:
4947
LOGGER.info(f"Running cmd: '{cmd}' on container: {container}")
50-
out = container.exec_run(cmd)
51-
result = out.output.decode().rstrip()
52-
assert isinstance(result, str)
53-
if print_result:
54-
LOGGER.info(f"Command result: {result}")
55-
assert out.exit_code == 0, f"Command: {cmd} failed"
56-
return result
48+
exec_result = container.exec_run(cmd)
49+
output = exec_result.output.decode().rstrip()
50+
assert isinstance(output, str)
51+
if print_output:
52+
LOGGER.info(f"Command output: {output}")
53+
assert exec_result.exit_code == 0, f"Command: {cmd} failed"
54+
return output

tagging/utils/quoted_output.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
def quoted_output(container: Container, cmd: str) -> str:
9-
cmd_output = DockerRunner.run_simple_command(container, cmd, print_result=False)
9+
cmd_output = DockerRunner.exec_cmd(container, cmd, print_output=False)
1010
# For example, `mamba info` adds redundant empty lines
1111
cmd_output = cmd_output.strip("\n")
1212
# For example, R packages list contains trailing backspaces

tests/by_image/base-notebook/test_container_options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def test_nb_user_change(container: TrackedContainer) -> None:
4747
)
4848
command = f'stat -c "%F %U %G" /home/{nb_user}/.jupyter'
4949
expected_output = f"directory {nb_user} users"
50-
cmd = running_container.exec_run(command, workdir=f"/home/{nb_user}")
51-
output = cmd.output.decode().strip("\n")
50+
exec_result = running_container.exec_run(command, workdir=f"/home/{nb_user}")
51+
output = exec_result.output.decode().strip("\n")
5252
assert (
5353
output == expected_output
5454
), f"Hidden folder .jupyter was not copied properly to {nb_user} home folder. stat: {output}, expected {expected_output}"

tests/by_image/base-notebook/test_start_container.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ def test_tini_entrypoint(
7878
LOGGER.info(f"Test that {command} is launched as PID {pid} ...")
7979
running_container = container.run_detached(tty=True)
8080
# Select the PID 1 and get the corresponding command
81-
cmd = running_container.exec_run(f"ps -p {pid} -o comm=")
82-
output = cmd.output.decode().strip("\n")
81+
exec_result = running_container.exec_run(f"ps -p {pid} -o comm=")
82+
output = exec_result.output.decode().strip("\n")
8383
assert "ERROR" not in output
8484
assert "WARNING" not in output
8585
assert output == command, f"{command} shall be launched as pid {pid}, got {output}"

tests/by_image/docker-stacks-foundation/test_user_options.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ def test_nb_user_change(container: TrackedContainer) -> None:
6060
LOGGER.info(f"Checking {nb_user} id ...")
6161
command = "id"
6262
expected_output = f"uid=1000({nb_user}) gid=100(users) groups=100(users)"
63-
cmd = running_container.exec_run(command, user=nb_user, workdir=f"/home/{nb_user}")
64-
output = cmd.output.decode().strip("\n")
63+
exec_result = running_container.exec_run(
64+
command, user=nb_user, workdir=f"/home/{nb_user}"
65+
)
66+
output = exec_result.output.decode().strip("\n")
6567
assert output == expected_output, f"Bad user {output}, expected {expected_output}"
6668

6769
LOGGER.info(f"Checking if {nb_user} owns his home folder ...")
6870
command = f'stat -c "%U %G" /home/{nb_user}/'
6971
expected_output = f"{nb_user} users"
70-
cmd = running_container.exec_run(command, workdir=f"/home/{nb_user}")
71-
output = cmd.output.decode().strip("\n")
72+
exec_result = running_container.exec_run(command, workdir=f"/home/{nb_user}")
73+
output = exec_result.output.decode().strip("\n")
7274
assert (
7375
output == expected_output
7476
), f"Bad owner for the {nb_user} home folder {output}, expected {expected_output}"
@@ -78,8 +80,8 @@ def test_nb_user_change(container: TrackedContainer) -> None:
7880
)
7981
command = f'stat -c "%F %U %G" /home/{nb_user}/work'
8082
expected_output = f"directory {nb_user} users"
81-
cmd = running_container.exec_run(command, workdir=f"/home/{nb_user}")
82-
output = cmd.output.decode().strip("\n")
83+
exec_result = running_container.exec_run(command, workdir=f"/home/{nb_user}")
84+
output = exec_result.output.decode().strip("\n")
8385
assert (
8486
output == expected_output
8587
), f"Folder work was not copied properly to {nb_user} home folder. stat: {output}, expected {expected_output}"

0 commit comments

Comments
 (0)