Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions concore_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ Stops all running concore processes. Prompts for confirmation before proceeding.
concore stop
```

### `concore logs <study_dir> [node]`

Shows the output (`concoreout.txt`) that each node of a built study has written.

**Options:**
- `-n, --lines N` - Lines to show per node (default: 20)
- `-f, --follow` - Keep printing new output until Ctrl+C

**Example:**
```bash
concore logs out # last 20 lines of every node
concore logs out CZ -n 100 # one node
concore logs out -f # follow all nodes live
```

## Development Workflow

1. **Create a new project**
Expand Down
26 changes: 26 additions & 0 deletions concore_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .commands.stop import stop_all
from .commands.inspect import inspect_workflow
from .commands.watch import watch_study
from .commands.logs import logs_study
from .commands.doctor import doctor_check
from .commands.setup import setup_concore
from . import __version__
Expand Down Expand Up @@ -181,6 +182,31 @@ def watch(study_dir, interval, once):
sys.exit(1)


@cli.command()
@click.argument("study_dir", type=click.Path(exists=True))
@click.argument("node", required=False, default=None)
@click.option(
"-n",
"--lines",
default=20,
show_default=True,
type=click.IntRange(min=0),
help="Lines to show per node",
)
@click.option(
"-f", "--follow", is_flag=True, help="Keep printing new output (Ctrl+C to stop)"
)
def logs(study_dir, node, lines, follow):
"""Show the output of a study's nodes (concoreout.txt)"""
try:
ok = logs_study(study_dir, node, lines, follow, console)
if not ok:
sys.exit(1)
except Exception as e:
console.print(f"[red]Error:[/red] {str(e)}")
sys.exit(1)


@cli.command()
def doctor():
"""Check system readiness for running concore studies"""
Expand Down
96 changes: 96 additions & 0 deletions concore_cli/commands/logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import time
from pathlib import Path

from .watch import _find_nodes

LOG_NAME = "concoreout.txt"
TAIL_CHUNK = 1024 * 1024
POLL_SECONDS = 0.5


def _tail(path, lines):
"""Return the last `lines` lines of a text file without loading all of it."""
with open(path, "rb") as f:
f.seek(0, 2)
size = f.tell()
f.seek(max(0, size - TAIL_CHUNK))
data = f.read()
text = data.decode("utf-8", errors="replace")
if lines <= 0:
return []
return text.splitlines()[-lines:]


def _emit(console, line, prefix=None):
text = f"[{prefix}] {line}" if prefix else line
console.print(text, markup=False, highlight=False)


def logs_study(study_dir, node, lines, follow, console):
study_path = Path(study_dir).resolve()
if not study_path.is_dir():
console.print(f"[red]Error:[/red] '{study_dir}' is not a directory")
return False

nodes = sorted(_find_nodes(study_path), key=lambda item: item[0])

if node is not None:
if node not in [name for name, _ in nodes]:
known = ", ".join(name for name, _ in nodes) or "none"
console.print(f"[red]Error:[/red] no node '{node}' in {study_path}")
console.print(f"[dim]Nodes: {known}[/dim]")
return False
selected = [(n, p) for n, p in nodes if n == node]
else:
selected = nodes

selected = [(n, p / LOG_NAME) for n, p in selected if (p / LOG_NAME).is_file()]
if not selected:
console.print(
f"[yellow]No {LOG_NAME} found.[/yellow] "
"[dim]Run the study first (concore run).[/dim]"
)
return True

single = len(selected) == 1
offsets = {}
for name, path in selected:
if not single:
console.print(f"[bold cyan]== {name} ==[/bold cyan]")
for line in _tail(path, lines):
_emit(console, line, None if single else name)
offsets[name] = path.stat().st_size

if not follow:
return True

console.print("[dim]Following — Ctrl+C to stop[/dim]")
pending = {name: b"" for name, _ in selected}
try:
while True:
for name, path in selected:
try:
size = path.stat().st_size
except OSError:
continue
if size < offsets[name]: # file was truncated by a new run
offsets[name] = 0
pending[name] = b""
if size == offsets[name]:
continue
with open(path, "rb") as f:
f.seek(offsets[name])
chunk = f.read()
offsets[name] += len(chunk)
buf = pending[name] + chunk
*complete, pending[name] = buf.split(b"\n")
for raw in complete:
_emit(
console,
raw.decode("utf-8", errors="replace").rstrip("\r"),
None if single else name,
)
time.sleep(POLL_SECONDS)
except KeyboardInterrupt:
console.print("\n[yellow]Stopped following.[/yellow]")
return True
98 changes: 98 additions & 0 deletions tests/test_logs_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import threading
import time

from click.testing import CliRunner

from concore_cli.cli import cli


def _study(tmp_path, nodes):
study = tmp_path / "study"
(study / "src").mkdir(parents=True)
for name, text in nodes.items():
node = study / name
(node / "in1").mkdir(parents=True)
(node / "out1").mkdir()
if text is not None:
(node / "concoreout.txt").write_text(text)
return study


def test_logs_shows_every_node(tmp_path):
study = _study(tmp_path, {"CZ": "a1\na2\n", "PZ": "b1\n"})
result = CliRunner().invoke(cli, ["logs", str(study)])
assert result.exit_code == 0
assert "== CZ ==" in result.output and "[CZ] a2" in result.output
assert "== PZ ==" in result.output and "[PZ] b1" in result.output


def test_logs_single_node_has_no_prefix(tmp_path):
study = _study(tmp_path, {"CZ": "a1\na2\n", "PZ": "b1\n"})
result = CliRunner().invoke(cli, ["logs", str(study), "CZ"])
assert result.exit_code == 0
assert "a2" in result.output
assert "b1" not in result.output
assert "[CZ]" not in result.output


def test_logs_lines_limits_output(tmp_path):
study = _study(tmp_path, {"CZ": "".join(f"line{i}\n" for i in range(50))})
result = CliRunner().invoke(cli, ["logs", str(study), "CZ", "-n", "3"])
assert result.exit_code == 0
assert "line49" in result.output and "line47" in result.output
assert "line46" not in result.output


def test_logs_does_not_interpret_markup(tmp_path):
study = _study(tmp_path, {"CZ": "value [red]x[/red] [1, 2]\n"})
result = CliRunner().invoke(cli, ["logs", str(study), "CZ"])
assert result.exit_code == 0
assert "[red]x[/red] [1, 2]" in result.output


def test_logs_unknown_node_lists_known_nodes(tmp_path):
study = _study(tmp_path, {"CZ": "a\n"})
result = CliRunner().invoke(cli, ["logs", str(study), "NOPE"])
assert result.exit_code == 1
assert "NOPE" in result.output and "CZ" in result.output


def test_logs_before_first_run_is_not_an_error(tmp_path):
study = _study(tmp_path, {"CZ": None})
result = CliRunner().invoke(cli, ["logs", str(study)])
assert result.exit_code == 0
assert "No concoreout.txt found" in result.output


def test_logs_missing_directory(tmp_path):
result = CliRunner().invoke(cli, ["logs", str(tmp_path / "nope")])
assert result.exit_code != 0


def test_logs_follow_prints_new_lines(tmp_path, monkeypatch):
study = _study(tmp_path, {"CZ": "old\n"})
log = study / "CZ" / "concoreout.txt"

def append_then_stop():
time.sleep(0.3)
with open(log, "a") as f:
f.write("new1\nnew2\n")
time.sleep(0.2)

real_sleep = time.sleep
calls = {"n": 0}

def fake_sleep(seconds):
calls["n"] += 1
if calls["n"] > 4:
raise KeyboardInterrupt
real_sleep(0.2)

monkeypatch.setattr("concore_cli.commands.logs.time.sleep", fake_sleep)
t = threading.Thread(target=append_then_stop)
t.start()
result = CliRunner().invoke(cli, ["logs", str(study), "CZ", "-f"])
t.join()
assert result.exit_code == 0
assert "old" in result.output
assert "new1" in result.output and "new2" in result.output
Loading