fix: create parent directories for targets in subfolders

Build targets with subfolder paths (e.g. img/file.jpg) failed because
parent directories did not exist. Create them on demand in
_build_single_target before dispatching to providers.

Also clean up empty subdirectories in the clean command after removing
target files.
This commit is contained in:
Konstantin Fickel 2026-02-21 11:21:51 +01:00
parent 8f0e49ee6f
commit d22b524182
Signed by: kfickel
GPG key ID: A793722F9933C1A5
2 changed files with 17 additions and 0 deletions

View file

@ -145,6 +145,9 @@ async def _build_single_target(
provider_index: dict[str, Provider], provider_index: dict[str, Provider],
) -> None: ) -> None:
"""Build a single target by dispatching to the appropriate provider.""" """Build a single target by dispatching to the appropriate provider."""
# Ensure parent directories exist for targets in subfolders.
(project_dir / target_name).parent.mkdir(parents=True, exist_ok=True)
target_cfg = config.targets[target_name] target_cfg = config.targets[target_name]
if isinstance(target_cfg, DownloadTargetConfig): if isinstance(target_cfg, DownloadTargetConfig):

View file

@ -194,12 +194,26 @@ def clean() -> None:
state_name = state_filename(_project_name(config_path)) state_name = state_filename(_project_name(config_path))
removed = 0 removed = 0
dirs_to_check: set[Path] = set()
for target_name in config.targets: for target_name in config.targets:
target_path = project_dir / target_name target_path = project_dir / target_name
if target_path.exists(): if target_path.exists():
target_path.unlink() target_path.unlink()
click.echo(click.style(" rm ", fg="red") + target_name) click.echo(click.style(" rm ", fg="red") + target_name)
removed += 1 removed += 1
# Track parent dirs that may now be empty.
parent = target_path.parent
if parent != project_dir:
dirs_to_check.add(parent)
# Remove empty subdirectories left behind, bottom-up.
for d in sorted(dirs_to_check, key=lambda p: len(p.parts), reverse=True):
while d != project_dir and d.exists() and not any(d.iterdir()):
d.rmdir()
click.echo(
click.style(" rm ", fg="red") + str(d.relative_to(project_dir)) + "/"
)
d = d.parent
state_path = project_dir / state_name state_path = project_dir / state_name
if state_path.exists(): if state_path.exists():