Skip to content
Merged
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
22 changes: 17 additions & 5 deletions src/format_bench/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ def _default_run_dir(root: Path, dataset_id: str) -> Path:
return root / "runs" / f"{dataset_id}-{stamp}"


def _create_missing_private_parents(destination: Path) -> None:
missing: list[Path] = []
parent = destination.parent
while not parent.exists():
missing.append(parent)
parent = parent.parent

# LLM contract: EXISTING_ANCESTOR -> PRIVATE_ANCESTORS_READY | FAILED.
# Create shallow-to-deep without parents=True so every new level requests
# 0700; any mkdir failure stops before the private run leaf is created.
for directory in reversed(missing):
directory.mkdir(mode=0o700)


def _safe_format_component(
value: object, label: str, *, extension: bool = False
) -> str:
Expand Down Expand Up @@ -143,11 +157,9 @@ def prepare_run(
destination = run_dir or _default_run_dir(root, dataset_id)
if run_dir is None:
destination.parent.mkdir(mode=0o700, exist_ok=True)
destination.mkdir(
mode=0o700,
parents=run_dir is not None,
exist_ok=False,
)
else:
_create_missing_private_parents(destination)
destination.mkdir(mode=0o700, exist_ok=False)
input_dir = destination / "input"
input_dir.mkdir(mode=0o700)

Expand Down
44 changes: 44 additions & 0 deletions tests/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,50 @@ def record_mode(
assert (run_dir / "manifest.json").is_file()


@pytest.mark.skipif(os.name != "posix", reason="POSIX directory-mode contract")
@pytest.mark.parametrize("mask", (0o002, 0o000))
def test_prepare_run_creates_each_missing_custom_ancestor_privately(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
mask: int,
) -> None:
root = Path(__file__).parents[1]
existing_parent = tmp_path / "existing"
existing_parent.mkdir(mode=0o700)
first_ancestor = existing_parent / "first"
second_ancestor = first_ancestor / "second"
run_dir = second_ancestor / "run"
created_levels = (first_ancestor, second_ancestor, run_dir)
requested_modes: dict[Path, int] = {}
real_mkdir = Path.mkdir

def record_mode(
path: Path,
mode: int = 0o777,
parents: bool = False,
exist_ok: bool = False,
) -> None:
if path in created_levels:
requested_modes[path] = mode
assert parents is False
real_mkdir(path, mode=mode, parents=parents, exist_ok=exist_ok)

monkeypatch.setattr(Path, "mkdir", record_mode)

previous_mask = os.umask(mask)
try:
prepared = prepare_run(root, DATASET, run_dir, fixture=True, selected=())
finally:
os.umask(previous_mask)

assert prepared == run_dir
assert requested_modes == {directory: 0o700 for directory in created_levels}
for directory in created_levels:
assert stat.S_IMODE(directory.stat().st_mode) == 0o700
assert (run_dir / "input" / "manifest.json").is_file()
assert (run_dir / "manifest.json").is_file()


@pytest.mark.skipif(os.name != "posix", reason="POSIX directory-mode contract")
@pytest.mark.parametrize("mask", (0o002, 0o000))
def test_prepare_run_creates_missing_default_parent_privately(
Expand Down
Loading