Standardisation des scripts
This commit is contained in:
@@ -1,44 +1,65 @@
|
||||
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(base_dir, source_dir_name):
|
||||
base_dir = Path(base_dir).expanduser().resolve()
|
||||
source_dir = base_dir / source_dir_name
|
||||
sync_dir = Path(EXPORT_DIR).expanduser() / base_dir.name
|
||||
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)
|
||||
|
||||
if not source_dir.is_dir():
|
||||
print(f"Erreur : le sous-dossier {source_dir} n'existe pas.")
|
||||
return
|
||||
|
||||
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"Attention : le fichier {concat_file} est introuvable.")
|
||||
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__":
|
||||
parser = argparse.ArgumentParser(description="Move to tablette folder.")
|
||||
parser.add_argument("dir", help="The directory to process")
|
||||
parser.add_argument("--refaire", action="store_true", help="Process only copies/labels defined in refaire.json")
|
||||
|
||||
args = parser.parse_args()
|
||||
if args.refaire:
|
||||
export_directory(args.dir, "BRnot")
|
||||
sys.exit(0)
|
||||
export_directory(args.dir, "BGnot")
|
||||
raise SystemExit(main())
|
||||
|
||||
Reference in New Issue
Block a user