from __future__ import annotations from pathlib import Path import subprocess class MustangError(RuntimeError): pass def _run(jar: Path, args: list[str], logger) -> subprocess.CompletedProcess[str]: if not jar.exists(): raise MustangError(f"Mustang jar not found: {jar}") cmd = ["java", "-Xmx1G", "-Dfile.encoding=UTF-8", "-jar", str(jar)] + args logger.debug("Running Mustang: %s", " ".join(cmd)) proc = subprocess.run(cmd, capture_output=True, text=True) logger.debug("Mustang stdout:\n%s", proc.stdout.strip()) logger.debug("Mustang stderr:\n%s", proc.stderr.strip()) return proc def combine_facturx(jar: Path, source_pdf: Path, source_xml: Path, out_pdf: Path, logger, profile: str = "E") -> Path: out_pdf.parent.mkdir(parents=True, exist_ok=True) proc = _run(jar, [ "--action", "combine", "--source", str(source_pdf), "--source-xml", str(source_xml), "--out", str(out_pdf), "--profile", profile ], logger) if proc.returncode != 0: raise MustangError(f"Mustang combine failed (rc={proc.returncode}). See logs for details.") return out_pdf def validate(jar: Path, source: Path, out_report: Path, logger, no_notices: bool = True) -> Path: out_report.parent.mkdir(parents=True, exist_ok=True) args = [] if no_notices: args += ["--no-notices"] args += ["--action", "validate", "--source", str(source)] proc = _run(jar, args, logger) if proc.returncode != 0: raise MustangError(f"Mustang validate failed (rc={proc.returncode}). See logs for details.") content = proc.stdout.strip() or "empty stdout from Mustang; check logs" out_report.write_text(content, encoding="utf-8") return out_report