Siyuan Hu commited on
Commit
2155b6e
Β·
1 Parent(s): de944c5

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
@@ -1780,6 +1780,114 @@ def debug_compile_output_zip():
1780
  _write_logs(LOG_PATH, logs)
1781
  return f"<div>Compiled but preview failed: {e}</div>"
1782
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1783
  def debug_compile_uploaded_zip(zip_file):
1784
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
1785
  logs = [f"🐞 Debug(upload) at {_now_str()}"]
 
1780
  _write_logs(LOG_PATH, logs)
1781
  return f"<div>Compiled but preview failed: {e}</div>"
1782
 
1783
+ def _find_last_pipeline_zip():
1784
+ try:
1785
+ candidates = []
1786
+ for d in RUNS_DIR.iterdir():
1787
+ try:
1788
+ if d.is_dir():
1789
+ z = d / "output.zip"
1790
+ if z.exists():
1791
+ candidates.append((z.stat().st_mtime, z))
1792
+ except Exception:
1793
+ pass
1794
+ if not candidates:
1795
+ return None
1796
+ candidates.sort(key=lambda x: x[0], reverse=True)
1797
+ return candidates[0][1]
1798
+ except Exception:
1799
+ return None
1800
+
1801
+ def debug_compile_last_pipeline_zip():
1802
+ """Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
1803
+ logs = [f"🐞 Debug(last-pipeline-zip) at {_now_str()}"]
1804
+ last_zip = _find_last_pipeline_zip()
1805
+ if not last_zip:
1806
+ return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
1807
+
1808
+ # Prepare workspace
1809
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
1810
+ work_zip_dir = WORK_DIR / "zip_last"
1811
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
1812
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
1813
+ logs.append(f"Using: {last_zip}")
1814
+
1815
+ # Extract zip
1816
+ try:
1817
+ import zipfile as _zf
1818
+ with _zf.ZipFile(last_zip, 'r') as zf:
1819
+ zf.extractall(work_zip_dir)
1820
+ except Exception as e:
1821
+ logs.append(f"❌ unzip failed: {e}")
1822
+ _write_logs(LOG_PATH, logs)
1823
+ return "<div style='color:#b00'>Unzip failed.</div>"
1824
+
1825
+ # Locate tex
1826
+ tex_path = None
1827
+ for name in ("poster_output.tex", "poster.tex"):
1828
+ cand = list(work_zip_dir.rglob(name))
1829
+ if cand:
1830
+ tex_path = cand[0]
1831
+ break
1832
+ if tex_path is None:
1833
+ cand = list(work_zip_dir.rglob("*.tex"))
1834
+ if cand:
1835
+ tex_path = cand[0]
1836
+ if tex_path is None:
1837
+ logs.append("❌ No .tex file found in last pipeline zip")
1838
+ _write_logs(LOG_PATH, logs)
1839
+ return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
1840
+
1841
+ # Ensure local fonts and theme precedence (same as other debug path)
1842
+ try:
1843
+ src_fonts = ROOT / "template" / "fonts"
1844
+ dst_fonts = work_zip_dir / "fonts"
1845
+ if src_fonts.exists():
1846
+ for root_dir, dirs, files in os.walk(src_fonts):
1847
+ rel = Path(root_dir).relative_to(src_fonts)
1848
+ out_dir = dst_fonts / rel
1849
+ out_dir.mkdir(parents=True, exist_ok=True)
1850
+ for fn in files:
1851
+ if fn.lower().endswith((".ttf", ".otf")):
1852
+ shutil.copy2(Path(root_dir)/fn, out_dir/fn)
1853
+ logs.append("πŸ“ Copied local fonts β†’ zip_last/fonts/")
1854
+ # Copy repository theme .sty next to tex and at root
1855
+ try:
1856
+ tmpl_dir = ROOT / "template"
1857
+ for sty in tmpl_dir.glob("*.sty"):
1858
+ shutil.copy2(sty, work_zip_dir / sty.name)
1859
+ shutil.copy2(sty, tex_path.parent / sty.name)
1860
+ logs.append("πŸ“„ Copied template/*.sty β†’ zip_last/ and tex dir")
1861
+ except Exception as e:
1862
+ logs.append(f"⚠️ Copy sty failed: {e}")
1863
+ except Exception as e:
1864
+ logs.append(f"⚠️ Local font setup failed: {e}")
1865
+
1866
+ # Compile to PDF
1867
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
1868
+ if not pdf_path or not pdf_path.exists():
1869
+ logs.append("❌ Failed to compile last pipeline zip PDF.")
1870
+ _write_logs(LOG_PATH, logs)
1871
+ return (
1872
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
1873
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
1874
+ + "\n".join(logs)
1875
+ + "</pre>"
1876
+ )
1877
+ try:
1878
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
1879
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
1880
+ html = (
1881
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
1882
+ + _pdf_to_iframe_html(pdf_path, height="700px")
1883
+ )
1884
+ _write_logs(LOG_PATH, logs)
1885
+ return html
1886
+ except Exception as e:
1887
+ logs.append(f"⚠️ preview failed: {e}")
1888
+ _write_logs(LOG_PATH, logs)
1889
+ return f"<div>Compiled but preview failed: {e}</div>"
1890
+
1891
  def debug_compile_uploaded_zip(zip_file):
1892
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
1893
  logs = [f"🐞 Debug(upload) at {_now_str()}"]