import argparse import sys from collections.abc import Sequence from pathlib import Path from config import EXPORT_DIR from copienator import ( EvaluationWorkspace, ExitCode, evaluation_parser, execute, workspace_from_args, ) from platform_utils import replace_with_link_or_copy def export_directory( workspace: EvaluationWorkspace, source_dir_name: str, ) -> ExitCode: workspace.require_directories(source_dir_name) source_dir = workspace.root / source_dir_name sync_dir = Path(EXPORT_DIR).expanduser() / workspace.name sync_dir.mkdir(parents=True, exist_ok=True) subdirs = [directory for directory in source_dir.iterdir() if directory.is_dir()] if source_dir_name == "BGnot" and subdirs: all_start_with_copie = all(directory.name.startswith("Copie") for directory in subdirs) if not all_start_with_copie: subdirs = [directory for directory in subdirs if not directory.name.startswith("Copie")] missing_outputs = 0 for subdir in subdirs: concat_file = subdir / "Concat.pdf" if not concat_file.is_file(): print(f"Warning: file not found: {concat_file}", file=sys.stderr) missing_outputs += 1 continue destination = sync_dir / f"{subdir.name}.pdf" method = replace_with_link_or_copy(concat_file, destination, prefer="hardlink") print(f"Exported: {destination} ({method})") return ExitCode.PARTIAL if missing_outputs else ExitCode.SUCCESS def build_parser() -> argparse.ArgumentParser: parser = evaluation_parser("Export annotated PDFs to the tablet directory.") parser.add_argument("--refaire", action="store_true", help="Process only copies/labels defined in refaire.json") return parser def run(workspace: EvaluationWorkspace, *, refaire: bool = False) -> ExitCode: return export_directory(workspace, "BRnot" if refaire else "BGnot") def main(argv: Sequence[str] | None = None) -> int: parser = build_parser() return execute( parser, argv, lambda args: run(workspace_from_args(args), refaire=args.refaire), ) if __name__ == "__main__": raise SystemExit(main())