Flake8 fixes

This commit is contained in:
Joe Rickerby
2020-07-10 13:04:50 +01:00
parent 44e3198880
commit 5842a75927
2 changed files with 11 additions and 9 deletions
+9 -6
View File
@@ -7,7 +7,7 @@ import sys
import uuid import uuid
from os import PathLike from os import PathLike
from pathlib import Path, PurePath from pathlib import Path, PurePath
from typing import Any, IO, Dict, List, Optional, Sequence, Union from typing import IO, Dict, List, Optional, Sequence, Union
class DockerContainer: class DockerContainer:
@@ -78,17 +78,17 @@ class DockerContainer:
# a container is running and the host filesystem is # a container is running and the host filesystem is
# mounted. https://github.com/moby/moby/issues/38995 # mounted. https://github.com/moby/moby/issues/38995
# Use `docker exec` instead. # Use `docker exec` instead.
quote = lambda p: shlex.quote(str(p))
if from_path.is_dir(): if from_path.is_dir():
self.call(['mkdir', '-p', to_path]) self.call(['mkdir', '-p', to_path])
subprocess.run( subprocess.run(
f'tar cf - . | docker exec -i {self.name} tar -xC {quote(to_path)} -f -', f'tar cf - . | docker exec -i {self.name} tar -xC {shell_quote(to_path)} -f -',
shell=True, shell=True,
check=True, check=True,
cwd=from_path) cwd=from_path)
else: else:
subprocess.run( subprocess.run(
f'cat {quote(from_path)} | docker exec -i {self.name} sh -c "cat > {quote(to_path)}"', f'cat {shell_quote(from_path)} | docker exec -i {self.name} sh -c "cat > {shell_quote(to_path)}"',
shell=True, shell=True,
check=True) check=True)
@@ -96,9 +96,8 @@ class DockerContainer:
# note: we assume from_path is a dir # note: we assume from_path is a dir
to_path.mkdir(parents=True, exist_ok=True) to_path.mkdir(parents=True, exist_ok=True)
quote = lambda p: shlex.quote(str(p))
subprocess.run( subprocess.run(
f'docker exec -i {self.name} tar -cC {quote(from_path)} -f - . | tar -xf -', f'docker exec -i {self.name} tar -cC {shell_quote(from_path)} -f - . | tar -xf -',
shell=True, shell=True,
check=True, check=True,
cwd=to_path cwd=to_path
@@ -184,3 +183,7 @@ class DockerContainer:
def environment_executor(self, command: str, environment: Dict[str, str]) -> str: def environment_executor(self, command: str, environment: Dict[str, str]) -> str:
# used as an EnvironmentExecutor to evaluate commands and capture output # used as an EnvironmentExecutor to evaluate commands and capture output
return self.call(shlex.split(command), env=environment) return self.call(shlex.split(command), env=environment)
def shell_quote(path: PurePath) -> str:
return shlex.quote(str(path))
+2 -3
View File
@@ -1,10 +1,9 @@
from pathlib import Path, PurePath
import platform import platform
import random import random
import shutil import shutil
import subprocess import subprocess
import textwrap import textwrap
from uuid import uuid4 from pathlib import Path, PurePath
import pytest import pytest
@@ -117,6 +116,7 @@ def test_file_operations(tmp_path: Path):
output = container.call(['cat', dst_file], capture_output=True) output = container.call(['cat', dst_file], capture_output=True)
assert test_binary_data == bytes(output, encoding='utf8', errors='surrogateescape') assert test_binary_data == bytes(output, encoding='utf8', errors='surrogateescape')
@pytest.mark.docker @pytest.mark.docker
def test_dir_operations(tmp_path: Path): def test_dir_operations(tmp_path: Path):
with DockerContainer(DEFAULT_IMAGE) as container: with DockerContainer(DEFAULT_IMAGE) as container:
@@ -145,4 +145,3 @@ def test_dir_operations(tmp_path: Path):
container.copy_out(dst_dir, new_test_dir) container.copy_out(dst_dir, new_test_dir)
assert test_binary_data == (new_test_dir / 'test.dat').read_bytes() assert test_binary_data == (new_test_dir / 'test.dat').read_bytes()