diff --git a/dokimi/changelog.d/20261205_100000_runs-export-unlistable-dir.md b/dokimi/changelog.d/20261205_100000_runs-export-unlistable-dir.md new file mode 100644 index 000000000..195b82c47 --- /dev/null +++ b/dokimi/changelog.d/20261205_100000_runs-export-unlistable-dir.md @@ -0,0 +1,8 @@ + + +### Fixed + +- **`runs export` names a directory of the run it cannot list in its left-out note** — the walk that names the files `run.json` does not list skipped a directory it could not list (mode `0o111`) without a word, so the note was missing. It now prints `left out: 1 directory in the run directory that cannot be listed (first: ...)` after the file line. The bundle and the exit code are unchanged (#1618). diff --git a/dokimi/docs/runs.md b/dokimi/docs/runs.md index c098f4b2f..fbf2ad361 100644 --- a/dokimi/docs/runs.md +++ b/dokimi/docs/runs.md @@ -405,8 +405,10 @@ saved baseline main -> 20261123T101500Z-1a2b3c4d; before: none and every artifact it lists: the plan, the result and the run's own artifacts. An artifact that carries the `run_id` but that `run.json` does not list (saved outside the directory) is left out and named (`left out: 1 artifact ...`), and so is any other file in the run directory - that `run.json` does not list, such as a report (`left out: 1 file in the run directory ...`). - It refuses, and writes nothing to stdout, a + that `run.json` does not list, such as a report (`left out: 1 file in the run directory ...`); + a directory in the run directory that cannot be listed is named the same way (`left out: 1 + directory in the run directory that cannot be listed ...`), so a file in it is not silently + missing from that count. It refuses, and writes nothing to stdout, a run with no `run.json`, more than one, a run without exactly one plan and one result (the bundle is addressed by them), a listed file that is missing, is not a regular file or leads out of the run directory, anything an import would refuse (below), a `bundle.json` past its diff --git a/dokimi/dokimi/commands/runs_bundle.py b/dokimi/dokimi/commands/runs_bundle.py index e963e1a95..6ee5aa5b1 100644 --- a/dokimi/dokimi/commands/runs_bundle.py +++ b/dokimi/dokimi/commands/runs_bundle.py @@ -45,6 +45,10 @@ def cmd_runs_export(args: argparse.Namespace) -> None: if found.unlisted: print(f"left out: {_count(len(found.unlisted), 'file')} in the run directory that {bundle.RUN_FILE} " f"does not list (first: {plain(found.unlisted[0].as_posix())})", file=sys.stderr) + if found.unreadable: + n = len(found.unreadable) + print(f"left out: {n} {'directory' if n == 1 else 'directories'} in the run directory that cannot be " + f"listed (first: {plain(found.unreadable[0].as_posix())})", file=sys.stderr) def _import_root(args: argparse.Namespace) -> Path: diff --git a/dokimi/dokimi/runs/bundle.py b/dokimi/dokimi/runs/bundle.py index eeb452d1e..fd181d277 100644 --- a/dokimi/dokimi/runs/bundle.py +++ b/dokimi/dokimi/runs/bundle.py @@ -246,12 +246,13 @@ def write(bundle: Bundle, out: IO[bytes]) -> None: class Collected: """A run read for export: its bundle, its directory (relative to the root), the artifacts that carry its run_id but are not in it, and the other files of its directory it leaves out (run.json - lists neither).""" + lists neither), and the directories of it that could not be listed, so a file in one is not counted.""" bundle: Bundle directory: Path left_out: list[Path] unlisted: list[Path] + unreadable: list[Path] def resolve(root: Path, target: str) -> str: @@ -327,13 +328,21 @@ def collect(root: Path, run_id: str) -> Collected: "an import reads") held = {record.parent / path for path in bundle.files} left_out = [p for p in carried if p not in held] - return Collected(bundle, record.parent, left_out, _unlisted(directory, record.parent, held | set(left_out))) + unlisted, unreadable = _unlisted(directory, record.parent, held | set(left_out)) + return Collected(bundle, record.parent, left_out, unlisted, unreadable) -def _unlisted(directory: Path, prefix: Path, known: set[Path]) -> list[Path]: - """The files in ``directory`` (``prefix`` under the root) not in ``known``, by path.""" - found = (prefix / Path(top, name).relative_to(directory) for top, _, names in os.walk(directory) for name in names) - return sorted(path for path in found if path not in known) +def _unlisted(directory: Path, prefix: Path, known: set[Path]) -> tuple[list[Path], list[Path]]: + """The files in ``directory`` (``prefix`` under the root) not in ``known``, and the directories + of it that cannot be listed, each by path.""" + skipped: list[Path] = [] + + def note(exc: OSError) -> None: + skipped.append(prefix / Path(str(exc.filename)).relative_to(directory)) + + found = (prefix / Path(top, name).relative_to(directory) + for top, _, names in os.walk(directory, onerror=note) for name in names) + return sorted(path for path in found if path not in known), sorted(skipped) class _Capped(io.RawIOBase): diff --git a/dokimi/tests/test_runs_export_unlistable.py b/dokimi/tests/test_runs_export_unlistable.py new file mode 100644 index 000000000..5fa6acb89 --- /dev/null +++ b/dokimi/tests/test_runs_export_unlistable.py @@ -0,0 +1,52 @@ +# Copyright (C) 2026 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +"""`runs export` names a directory of the run it cannot list (#1618): a file in it is not silently +missing from the left-out count (docs/runs.md, "Moving a run").""" + +from __future__ import annotations + +from pathlib import Path + +from dokimi.runs import bundle +from tests.test_runs_bundle import RUN, _run, src # noqa: F401 (src is a fixture) +from tests.test_unlistable_sites import unlistable + + +def test_export_names_a_directory_of_the_run_it_cannot_list(src, monkeypatch, capsysbinary): # noqa: F811 + (src / "nightly-7" / "extra").mkdir() + (src / "nightly-7" / "extra" / "f.txt").write_text("hi\n", encoding="utf-8") + code, listable, err = _run(monkeypatch, capsysbinary, "export", RUN) + assert code == 0 + assert err.splitlines()[1:] == [ + "left out: 1 file in the run directory that run.json does not list (first: nightly-7/extra/f.txt)"] + + with unlistable(src / "nightly-7" / "extra"): + code, out, err = _run(monkeypatch, capsysbinary, "export", RUN) + + assert code == 0 + assert err.splitlines()[1:] == [ + "left out: 1 directory in the run directory that cannot be listed (first: nightly-7/extra)"] + assert out == listable + + +def test_export_counts_the_directories_of_the_run_it_cannot_list(src, monkeypatch, capsysbinary): # noqa: F811 + for name in ("a", "b"): + (src / "nightly-7" / name).mkdir() + with unlistable(src / "nightly-7" / "a"), unlistable(src / "nightly-7" / "b"): + found = bundle.collect(src, RUN) + code, _, err = _run(monkeypatch, capsysbinary, "export", RUN) + + assert found.unreadable == [Path("nightly-7/a"), Path("nightly-7/b")] + assert (code, err.splitlines()[1:]) == ( + 0, ["left out: 2 directories in the run directory that cannot be listed (first: nightly-7/a)"]) + + +def test_export_prints_no_directory_line_when_every_directory_lists(src, monkeypatch, capsysbinary): # noqa: F811 + (src / "nightly-7" / "extra").mkdir() + + code, _, err = _run(monkeypatch, capsysbinary, "export", RUN) + + assert code == 0 + assert "cannot be listed" not in err + assert bundle.collect(src, RUN).unreadable == []