Siyuan Hu commited on
Commit
ef69de6
Β·
1 Parent(s): 05d9cf1

feat(debug): add 'Test last pipeline zip' to compile most recent runs/*/output.zip with local fonts + template precedence; widen TEXINPUTS

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py CHANGED
@@ -1461,6 +1461,114 @@ def debug_compile_output_zip():
1461
  _write_logs(LOG_PATH, logs)
1462
  return f"<div>Compiled but preview failed: {e}</div>"
1463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1464
  def debug_compile_uploaded_zip(zip_file):
1465
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
1466
  logs = [f"🐞 Debug(upload) at {_now_str()}"]
 
1461
  _write_logs(LOG_PATH, logs)
1462
  return f"<div>Compiled but preview failed: {e}</div>"
1463
 
1464
+ def _find_last_pipeline_zip():
1465
+ try:
1466
+ candidates = []
1467
+ for d in RUNS_DIR.iterdir():
1468
+ try:
1469
+ if d.is_dir():
1470
+ z = d / "output.zip"
1471
+ if z.exists():
1472
+ candidates.append((z.stat().st_mtime, z))
1473
+ except Exception:
1474
+ pass
1475
+ if not candidates:
1476
+ return None
1477
+ candidates.sort(key=lambda x: x[0], reverse=True)
1478
+ return candidates[0][1]
1479
+ except Exception:
1480
+ return None
1481
+
1482
+ def debug_compile_last_pipeline_zip():
1483
+ """Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
1484
+ logs = [f"🐞 Debug(last-pipeline-zip) at {_now_str()}"]
1485
+ last_zip = _find_last_pipeline_zip()
1486
+ if not last_zip:
1487
+ return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
1488
+
1489
+ # Prepare workspace
1490
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
1491
+ work_zip_dir = WORK_DIR / "zip_last"
1492
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
1493
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
1494
+ logs.append(f"Using: {last_zip}")
1495
+
1496
+ # Extract zip
1497
+ try:
1498
+ import zipfile as _zf
1499
+ with _zf.ZipFile(last_zip, 'r') as zf:
1500
+ zf.extractall(work_zip_dir)
1501
+ except Exception as e:
1502
+ logs.append(f"❌ unzip failed: {e}")
1503
+ _write_logs(LOG_PATH, logs)
1504
+ return "<div style='color:#b00'>Unzip failed.</div>"
1505
+
1506
+ # Locate tex
1507
+ tex_path = None
1508
+ for name in ("poster_output.tex", "poster.tex"):
1509
+ cand = list(work_zip_dir.rglob(name))
1510
+ if cand:
1511
+ tex_path = cand[0]
1512
+ break
1513
+ if tex_path is None:
1514
+ cand = list(work_zip_dir.rglob("*.tex"))
1515
+ if cand:
1516
+ tex_path = cand[0]
1517
+ if tex_path is None:
1518
+ logs.append("❌ No .tex file found in last pipeline zip")
1519
+ _write_logs(LOG_PATH, logs)
1520
+ return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
1521
+
1522
+ # Ensure local fonts and theme precedence (same as other debug path)
1523
+ try:
1524
+ src_fonts = ROOT / "template" / "fonts"
1525
+ dst_fonts = work_zip_dir / "fonts"
1526
+ if src_fonts.exists():
1527
+ for root_dir, dirs, files in os.walk(src_fonts):
1528
+ rel = Path(root_dir).relative_to(src_fonts)
1529
+ out_dir = dst_fonts / rel
1530
+ out_dir.mkdir(parents=True, exist_ok=True)
1531
+ for fn in files:
1532
+ if fn.lower().endswith((".ttf", ".otf")):
1533
+ shutil.copy2(Path(root_dir)/fn, out_dir/fn)
1534
+ logs.append("πŸ“ Copied local fonts β†’ zip_last/fonts/")
1535
+ # Copy repository theme .sty next to tex and at root
1536
+ try:
1537
+ tmpl_dir = ROOT / "template"
1538
+ for sty in tmpl_dir.glob("*.sty"):
1539
+ shutil.copy2(sty, work_zip_dir / sty.name)
1540
+ shutil.copy2(sty, tex_path.parent / sty.name)
1541
+ logs.append("πŸ“„ Copied template/*.sty β†’ zip_last/ and tex dir")
1542
+ except Exception as e:
1543
+ logs.append(f"⚠️ Copy sty failed: {e}")
1544
+ except Exception as e:
1545
+ logs.append(f"⚠️ Local font setup failed: {e}")
1546
+
1547
+ # Compile to PDF
1548
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
1549
+ if not pdf_path or not pdf_path.exists():
1550
+ logs.append("❌ Failed to compile last pipeline zip PDF.")
1551
+ _write_logs(LOG_PATH, logs)
1552
+ return (
1553
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
1554
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
1555
+ + "\n".join(logs)
1556
+ + "</pre>"
1557
+ )
1558
+ try:
1559
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
1560
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
1561
+ html = (
1562
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
1563
+ + _pdf_to_iframe_html(pdf_path, height="700px")
1564
+ )
1565
+ _write_logs(LOG_PATH, logs)
1566
+ return html
1567
+ except Exception as e:
1568
+ logs.append(f"⚠️ preview failed: {e}")
1569
+ _write_logs(LOG_PATH, logs)
1570
+ return f"<div>Compiled but preview failed: {e}</div>"
1571
+
1572
  def debug_compile_uploaded_zip(zip_file):
1573
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
1574
  logs = [f"🐞 Debug(upload) at {_now_str()}"]