Spaces:
Sleeping
Sleeping
Download services/lo_runner.py from DerXanity/farient-deckcheck: direct link, hf CLI and curl.
- Browser
- Download file 3.34 kB
-
https://huggingface.co/spaces/DerXanity/farient-deckcheck/resolve/main/services/lo_runner.py
- Command line
-
hf download hf://spaces/DerXanity/farient-deckcheck/services/lo_runner.py
-
curl -L -o lo_runner.py https://huggingface.co/spaces/DerXanity/farient-deckcheck/resolve/main/services/lo_runner.py
3.34 kB
| """ | |
| lo_runner.py — soffice subprocess wrapper with timeout and isolated UserInstallation. | |
| Each call generates a UUID and uses -env:UserInstallation=file:///tmp/lo_<uuid> | |
| to avoid lock-file collisions when multiple render_deck() calls run concurrently. | |
| """ | |
| from __future__ import annotations | |
| import logging | |
| import os | |
| import shutil | |
| import signal | |
| import subprocess | |
| import uuid | |
| from pathlib import Path | |
| logger = logging.getLogger(__name__) | |
| SOFFICE_TIMEOUT_SECONDS = 60 | |
| def soffice_available() -> bool: | |
| """Return True if soffice is on $PATH.""" | |
| return shutil.which("soffice") is not None | |
| def convert_pptx_to_pdf(pptx_path: Path, out_dir: Path) -> Path: | |
| """ | |
| Convert a .pptx file to a single PDF using LibreOffice headless. | |
| Each invocation creates an isolated UserInstallation directory at | |
| /tmp/lo_<uuid> to prevent concurrent lock-file collisions. | |
| Parameters | |
| ---------- | |
| pptx_path : Path | |
| Absolute path to the source .pptx file. | |
| out_dir : Path | |
| Directory where LibreOffice will write the output PDF. | |
| Returns | |
| ------- | |
| Path | |
| Path to the generated PDF file. | |
| Raises | |
| ------ | |
| RuntimeError | |
| If soffice is not available, the process times out, or conversion fails. | |
| """ | |
| if not soffice_available(): | |
| raise RuntimeError("soffice is not available on $PATH") | |
| run_id = uuid.uuid4().hex | |
| lo_user_dir = Path(f"/tmp/lo_{run_id}") | |
| lo_user_dir.mkdir(parents=True, exist_ok=True) | |
| cmd = [ | |
| "soffice", | |
| "--headless", | |
| f"-env:UserInstallation=file://{lo_user_dir}", | |
| "--convert-to", | |
| "pdf", | |
| "--outdir", | |
| str(out_dir), | |
| str(pptx_path), | |
| ] | |
| logger.debug("Running soffice: %s", " ".join(cmd)) | |
| try: | |
| proc = subprocess.Popen( | |
| cmd, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE, | |
| # Start in a new process group so we can kill the whole group on timeout | |
| preexec_fn=os.setsid, | |
| ) | |
| try: | |
| stdout, stderr = proc.communicate(timeout=SOFFICE_TIMEOUT_SECONDS) | |
| except subprocess.TimeoutExpired: | |
| # Kill the entire process group | |
| try: | |
| os.killpg(os.getpgid(proc.pid), signal.SIGKILL) | |
| except ProcessLookupError: | |
| pass | |
| proc.wait() | |
| raise RuntimeError( | |
| f"soffice timed out after {SOFFICE_TIMEOUT_SECONDS}s converting " | |
| f"{pptx_path.name}" | |
| ) | |
| if proc.returncode != 0: | |
| stderr_text = stderr.decode("utf-8", errors="replace").strip() | |
| raise RuntimeError( | |
| f"soffice exited with code {proc.returncode} for {pptx_path.name}: " | |
| f"{stderr_text}" | |
| ) | |
| # LibreOffice names the output <stem>.pdf in out_dir | |
| pdf_path = out_dir / (pptx_path.stem + ".pdf") | |
| if not pdf_path.exists(): | |
| raise RuntimeError( | |
| f"soffice succeeded but expected PDF not found: {pdf_path}" | |
| ) | |
| logger.debug("soffice produced: %s", pdf_path) | |
| return pdf_path | |
| finally: | |
| # Always clean up the isolated UserInstallation dir | |
| try: | |
| shutil.rmtree(lo_user_dir, ignore_errors=True) | |
| except Exception: | |
| pass | |