| 1 |
"""I/O utilities for con-duct.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
from collections.abc import Iterable |
| 5 |
import os |
| 6 |
import subprocess |
| 7 |
import sys |
| 8 |
import threading |
| 9 |
import time |
| 10 |
from typing import IO, Any, TextIO |
| 11 |
from con_duct._models import LogPaths, Outputs |
| 12 |
|
| 13 |
|
| 14 |
class TailPipe: |
| 15 |
"""TailPipe simultaneously streams to an output stream (stdout or stderr) and a specified file.""" |
| 16 |
|
| 17 |
TAIL_CYCLE_TIME = 0.01 |
| 18 |
|
| 19 |
def __init__(self, file_path: str, buffer: IO[bytes]) -> None: |
| 20 |
self.file_path = file_path |
| 21 |
self.buffer = buffer |
| 22 |
self.stop_event: threading.Event | None = None |
| 23 |
self.infile: IO[bytes] | None = None |
| 24 |
self.thread: threading.Thread | None = None |
| 25 |
|
| 26 |
def start(self) -> None: |
| 27 |
self.stop_event = threading.Event() |
| 28 |
self.infile = open(self.file_path, "rb") |
| 29 |
self.thread = threading.Thread(target=self._tail, daemon=True) |
| 30 |
self.thread.start() |
| 31 |
|
| 32 |
def fileno(self) -> int: |
| 33 |
assert self.infile is not None |
| 34 |
return self.infile.fileno() |
| 35 |
|
| 36 |
def _catch_up(self) -> None: |
| 37 |
assert self.infile is not None |
| 38 |
data = self.infile.read() |
| 39 |
if data: |
| 40 |
self.buffer.write(data) |
| 41 |
self.buffer.flush() |
| 42 |
|
| 43 |
def _tail(self) -> None: |
| 44 |
assert self.stop_event is not None |
| 45 |
try: |
| 46 |
while not self.stop_event.is_set(): |
| 47 |
self._catch_up() |
| 48 |
time.sleep(TailPipe.TAIL_CYCLE_TIME) |
| 49 |
|
| 50 |
self._catch_up() |
| 51 |
except Exception: |
| 52 |
raise |
| 53 |
finally: |
| 54 |
self.buffer.flush() |
| 55 |
|
| 56 |
def close(self) -> None: |
| 57 |
assert self.stop_event is not None |
| 58 |
assert self.thread is not None |
| 59 |
assert self.infile is not None |
| 60 |
self.stop_event.set() |
| 61 |
self.thread.join() |
| 62 |
self.infile.close() |
| 63 |
|
| 64 |
|
| 65 |
def prepare_outputs( |
| 66 |
capture_outputs: Outputs, |
| 67 |
outputs: Outputs, |
| 68 |
log_paths: LogPaths, |
| 69 |
) -> tuple[TextIO | TailPipe | int | None, TextIO | TailPipe | int | None]: |
| 70 |
stdout: TextIO | TailPipe | int | None |
| 71 |
stderr: TextIO | TailPipe | int | None |
| 72 |
|
| 73 |
if capture_outputs.has_stdout(): |
| 74 |
if outputs.has_stdout(): |
| 75 |
stdout = TailPipe(log_paths.stdout, buffer=sys.stdout.buffer) |
| 76 |
stdout.start() |
| 77 |
else: |
| 78 |
stdout = open(log_paths.stdout, "w") |
| 79 |
elif outputs.has_stdout(): |
| 80 |
stdout = None |
| 81 |
else: |
| 82 |
stdout = subprocess.DEVNULL |
| 83 |
|
| 84 |
if capture_outputs.has_stderr(): |
| 85 |
if outputs.has_stderr(): |
| 86 |
stderr = TailPipe(log_paths.stderr, buffer=sys.stderr.buffer) |
| 87 |
stderr.start() |
| 88 |
else: |
| 89 |
stderr = open(log_paths.stderr, "w") |
| 90 |
elif outputs.has_stderr(): |
| 91 |
stderr = None |
| 92 |
else: |
| 93 |
stderr = subprocess.DEVNULL |
| 94 |
return stdout, stderr |
| 95 |
|
| 96 |
|
| 97 |
def safe_close_files(file_list: Iterable[Any]) -> None: |
| 98 |
for f in file_list: |
| 99 |
try: |
| 100 |
f.close() |
| 101 |
except Exception: |
| 102 |
pass |
| 103 |
|
| 104 |
|
| 105 |
def remove_files(log_paths: LogPaths, assert_empty: bool = False) -> None: |
| 106 |
for _, file_path in log_paths: |
| 107 |
if os.path.exists(file_path): |
| 108 |
if assert_empty: |
| 109 |
assert os.stat(file_path).st_size == 0 |
| 110 |
os.remove(file_path) |