Enhance logging and progress handling in SSH operations

- Updated the `_log_default` function to handle messages starting with `\r` for in-place updates.
- Introduced `split_ssh_stream` to differentiate between line and progress events in SSH output.
- Implemented `feed_ssh_log` to dispatch log messages based on event type.
- Refactored `_stream_pty_output` to utilize the new logging and event handling functions.
- Enhanced the `log` function in `term.py` to support progress updates, ensuring a cleaner output experience.
- Updated `DownloadProgress` classes in `civitai_fetch.py` and `llamacpp_fetch.py` to print progress in place, improving user feedback during downloads.
This commit is contained in:
Leonid Pershin
2026-08-21 07:20:28 +03:00
parent 4186d0bcf1
commit aca8d5e990
7 changed files with 196 additions and 29 deletions
+9 -7
View File
@@ -47,7 +47,7 @@ def progress_line(
class DownloadProgress:
"""Print size + speed about once per second (SSH readline-friendly)."""
"""Print size + speed ~1Hz; \\r in place, newline on finish."""
def __init__(self, label: str, total: int | None) -> None:
self.label = label
@@ -67,14 +67,16 @@ class DownloadProgress:
self._emit()
def finish(self) -> None:
self._emit()
# Final line with newline so the next log stays below the bar.
self._emit(final=True)
def _emit(self) -> None:
def _emit(self, *, final: bool = False) -> None:
elapsed = max(time.monotonic() - self.t0, 0.001)
print(
progress_line(self.label, self.done, self.total, self.done / elapsed),
flush=True,
)
line = progress_line(self.label, self.done, self.total, self.done / elapsed)
if final:
print(line, flush=True)
else:
print(line, end="\r", flush=True)
def sha256_path(path: Path) -> str:
+7 -6
View File
@@ -64,14 +64,15 @@ class DownloadProgress:
self._emit()
def finish(self) -> None:
self._emit()
self._emit(final=True)
def _emit(self) -> None:
def _emit(self, *, final: bool = False) -> None:
elapsed = max(time.monotonic() - self.t0, 0.001)
print(
progress_line(self.label, self.done, self.total, self.done / elapsed),
flush=True,
)
line = progress_line(self.label, self.done, self.total, self.done / elapsed)
if final:
print(line, flush=True)
else:
print(line, end="\r", flush=True)
def download(url: str, dest: Path, headers: dict[str, str], *, label: str) -> None:
+3 -1
View File
@@ -96,7 +96,9 @@ def pull_stream(name: str, label: str) -> None:
if now - last_print >= 1.0 or done >= total:
elapsed = max(now - t0, 0.001)
speed = done / elapsed
print(progress_line(label, done, total, speed), flush=True)
line = progress_line(label, done, total, speed)
end = "\n" if done >= total else "\r"
print(line, end=end, flush=True)
last_print = now
elif status and status not in {"success"} and now - last_print >= 2.0:
print(f"{label} {status}", flush=True)