Skip to content

Commit 06525c0

Browse files
committed
Allow writing binary call output to file.
1 parent 8b3ef91 commit 06525c0

2 files changed

Lines changed: 43 additions & 7 deletions

File tree

cachebin/cachebin.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from shutil import rmtree
66
from stat import S_IXGRP, S_IXOTH, S_IXUSR
77
from subprocess import PIPE, Popen
8-
from typing import Callable
8+
from typing import Callable, overload
99

1010
import py7zr
1111
import requests
@@ -162,9 +162,31 @@ def get_binary_path(self, command: str | None = None) -> Path:
162162
raise FileNotFoundError(f"Binary '{binary_path}' does not exist.")
163163
return binary_path
164164

165+
@overload
165166
def call(
166-
self, command: str | None = None, arguments: list[str] | None = None, working_directory: Path | str = Path(".")
167-
) -> str:
167+
self,
168+
command: str | None = ...,
169+
arguments: list[str] | None = ...,
170+
working_directory: Path | str = ...,
171+
output_file_path: None = ...,
172+
) -> str: ...
173+
174+
@overload
175+
def call(
176+
self,
177+
command: str | None = ...,
178+
arguments: list[str] | None = ...,
179+
working_directory: Path | str = ...,
180+
output_file_path: str | Path = ...,
181+
) -> None: ...
182+
183+
def call(
184+
self,
185+
command: str | None = None,
186+
arguments: list[str] | None = None,
187+
working_directory: Path | str = Path.cwd(),
188+
output_file_path: str | Path | None = None,
189+
) -> str | None:
168190
"""
169191
Calls the binary with the specified command and arguments.
170192
@@ -188,16 +210,29 @@ def call(
188210

189211
if arguments is None:
190212
arguments = []
213+
214+
if output_file_path is not None:
215+
output_file_path = Path(output_file_path)
216+
if not output_file_path.is_absolute():
217+
output_file_path = working_directory / output_file_path
218+
output_stream = open(output_file_path, "w")
191219
process = Popen(
192-
[str(binary_path)] + arguments, stdout=PIPE, stderr=PIPE, creationflags=creation_flag, cwd=working_directory
220+
[str(binary_path)] + arguments,
221+
stdout=PIPE if output_file_path is None else output_stream,
222+
stderr=PIPE,
223+
creationflags=creation_flag,
224+
cwd=working_directory,
193225
)
194226
stdout, stderr = process.communicate()
195227
if process.returncode != 0:
196228
raise RuntimeError(
197229
f"Command '{binary_path} {' '.join(arguments)}' failed with error:\n"
198230
f"{stderr.decode('utf-8')}\n{stdout.decode('utf-8')}"
199231
)
200-
return stdout.decode("utf-8")
232+
if output_file_path is None:
233+
return stdout.decode("utf-8")
234+
output_stream.close()
235+
return None
201236

202237
def clear_cache(self) -> None:
203238
"""

test/test_cachebin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ def test_tinytex():
1717
version = "v2025.05"
1818
tinytex = tinytex_manager.get_version(version)
1919
assert version in tinytex.call("tlmgr", ["--version"])
20-
tlmgr_list = tinytex.call("tlmgr", ["info", "--only-installed"]).splitlines()
21-
tlmgr_list = [line[2 : line.find(": ")] for line in tlmgr_list]
20+
tinytex.call(
21+
"tlmgr", ["info", "--only-installed"], output_file_path=tinytex.extraction_directory_path / "tlmgr_info.txt"
22+
)
2223
tinytex.clear_cache()
2324

2425

0 commit comments

Comments
 (0)