Skip to content

Commit 3724666

Browse files
committed
Promote filter_bytes
1 parent 0fba174 commit 3724666

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

nancy/__init__.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,36 @@ def command_to_str(
9292
return b"$" + name + args_string + input_string
9393

9494

95+
def filter_bytes(
96+
input: Optional[bytes],
97+
exe_path: Path,
98+
exe_args: list[bytes]
99+
) -> bytes:
100+
"""Run an external command passing `input` on stdin.
101+
102+
Args:
103+
input (Optional[bytes]): passed to `stdin`
104+
exe_path (Path): filesystem `Path` of the command to run
105+
exe_args (list[bytes]): arguments to the command
106+
107+
Returns:
108+
bytes: stdout of the command
109+
"""
110+
debug(f"Running {exe_path} {b' '.join(exe_args)}")
111+
try:
112+
res = subprocess.run(
113+
[exe_path.resolve(strict=True)] + exe_args,
114+
capture_output=True,
115+
check=True,
116+
input=input,
117+
)
118+
return res.stdout
119+
except subprocess.CalledProcessError as err:
120+
if err.stderr is not None:
121+
print(err.stderr.decode("iso-8859-1"), file=sys.stderr)
122+
die(f"Error code {err.returncode} running: {' '.join(map(str, err.cmd))}")
123+
124+
95125
class Trees:
96126
"""The state that is constant for a whole invocation of Nancy.
97127
@@ -366,35 +396,6 @@ def exe_arg(exe_arg: bytes):
366396
return Path(exe_path_str)
367397
raise ValueError(f"cannot find program '{exe_name}'")
368398

369-
def filter_bytes(
370-
input: Optional[bytes],
371-
exe_path: Path,
372-
exe_args: list[bytes]
373-
) -> bytes:
374-
"""Run an external command passing `input` on stdin.
375-
376-
Args:
377-
input (Optional[bytes]): passed to `stdin`
378-
exe_path (Path): filesystem `Path` of the command to run
379-
exe_args (list[bytes]): arguments to the command
380-
381-
Returns:
382-
bytes: stdout of the command
383-
"""
384-
debug(f"Running {exe_path} {b' '.join(exe_args)}")
385-
try:
386-
res = subprocess.run(
387-
[exe_path.resolve(strict=True)] + exe_args,
388-
capture_output=True,
389-
check=True,
390-
input=input,
391-
)
392-
return res.stdout
393-
except subprocess.CalledProcessError as err:
394-
if err.stderr is not None:
395-
print(err.stderr.decode("iso-8859-1"), file=sys.stderr)
396-
die(f"Error code {err.returncode} running: {' '.join(map(str, err.cmd))}")
397-
398399
def file_arg(filename: bytes) -> tuple[Optional[Path], bytes]:
399400
file = None
400401
contents = b""

0 commit comments

Comments
 (0)