Siyuan Hu commited on
Commit
3364b1b
Β·
1 Parent(s): a3ffcf5

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
@@ -3855,6 +3855,114 @@ def debug_compile_output_zip():
3855
  _write_logs(LOG_PATH, logs)
3856
  return f"<div>Compiled but preview failed: {e}</div>"
3857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3858
  def debug_compile_uploaded_zip(zip_file):
3859
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
3860
  logs = [f"🐞 Debug(upload) at {_now_str()}"]
 
3855
  _write_logs(LOG_PATH, logs)
3856
  return f"<div>Compiled but preview failed: {e}</div>"
3857
 
3858
+ def _find_last_pipeline_zip():
3859
+ try:
3860
+ candidates = []
3861
+ for d in RUNS_DIR.iterdir():
3862
+ try:
3863
+ if d.is_dir():
3864
+ z = d / "output.zip"
3865
+ if z.exists():
3866
+ candidates.append((z.stat().st_mtime, z))
3867
+ except Exception:
3868
+ pass
3869
+ if not candidates:
3870
+ return None
3871
+ candidates.sort(key=lambda x: x[0], reverse=True)
3872
+ return candidates[0][1]
3873
+ except Exception:
3874
+ return None
3875
+
3876
+ def debug_compile_last_pipeline_zip():
3877
+ """Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
3878
+ logs = [f"🐞 Debug(last-pipeline-zip) at {_now_str()}"]
3879
+ last_zip = _find_last_pipeline_zip()
3880
+ if not last_zip:
3881
+ return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
3882
+
3883
+ # Prepare workspace
3884
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
3885
+ work_zip_dir = WORK_DIR / "zip_last"
3886
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
3887
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
3888
+ logs.append(f"Using: {last_zip}")
3889
+
3890
+ # Extract zip
3891
+ try:
3892
+ import zipfile as _zf
3893
+ with _zf.ZipFile(last_zip, 'r') as zf:
3894
+ zf.extractall(work_zip_dir)
3895
+ except Exception as e:
3896
+ logs.append(f"❌ unzip failed: {e}")
3897
+ _write_logs(LOG_PATH, logs)
3898
+ return "<div style='color:#b00'>Unzip failed.</div>"
3899
+
3900
+ # Locate tex
3901
+ tex_path = None
3902
+ for name in ("poster_output.tex", "poster.tex"):
3903
+ cand = list(work_zip_dir.rglob(name))
3904
+ if cand:
3905
+ tex_path = cand[0]
3906
+ break
3907
+ if tex_path is None:
3908
+ cand = list(work_zip_dir.rglob("*.tex"))
3909
+ if cand:
3910
+ tex_path = cand[0]
3911
+ if tex_path is None:
3912
+ logs.append("❌ No .tex file found in last pipeline zip")
3913
+ _write_logs(LOG_PATH, logs)
3914
+ return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
3915
+
3916
+ # Ensure local fonts and theme precedence (same as other debug path)
3917
+ try:
3918
+ src_fonts = ROOT / "template" / "fonts"
3919
+ dst_fonts = work_zip_dir / "fonts"
3920
+ if src_fonts.exists():
3921
+ for root_dir, dirs, files in os.walk(src_fonts):
3922
+ rel = Path(root_dir).relative_to(src_fonts)
3923
+ out_dir = dst_fonts / rel
3924
+ out_dir.mkdir(parents=True, exist_ok=True)
3925
+ for fn in files:
3926
+ if fn.lower().endswith((".ttf", ".otf")):
3927
+ shutil.copy2(Path(root_dir)/fn, out_dir/fn)
3928
+ logs.append("πŸ“ Copied local fonts β†’ zip_last/fonts/")
3929
+ # Copy repository theme .sty next to tex and at root
3930
+ try:
3931
+ tmpl_dir = ROOT / "template"
3932
+ for sty in tmpl_dir.glob("*.sty"):
3933
+ shutil.copy2(sty, work_zip_dir / sty.name)
3934
+ shutil.copy2(sty, tex_path.parent / sty.name)
3935
+ logs.append("πŸ“„ Copied template/*.sty β†’ zip_last/ and tex dir")
3936
+ except Exception as e:
3937
+ logs.append(f"⚠️ Copy sty failed: {e}")
3938
+ except Exception as e:
3939
+ logs.append(f"⚠️ Local font setup failed: {e}")
3940
+
3941
+ # Compile to PDF
3942
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
3943
+ if not pdf_path or not pdf_path.exists():
3944
+ logs.append("❌ Failed to compile last pipeline zip PDF.")
3945
+ _write_logs(LOG_PATH, logs)
3946
+ return (
3947
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
3948
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
3949
+ + "\n".join(logs)
3950
+ + "</pre>"
3951
+ )
3952
+ try:
3953
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
3954
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
3955
+ html = (
3956
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
3957
+ + _pdf_to_iframe_html(pdf_path, height="700px")
3958
+ )
3959
+ _write_logs(LOG_PATH, logs)
3960
+ return html
3961
+ except Exception as e:
3962
+ logs.append(f"⚠️ preview failed: {e}")
3963
+ _write_logs(LOG_PATH, logs)
3964
+ return f"<div>Compiled but preview failed: {e}</div>"
3965
+
3966
  def debug_compile_uploaded_zip(zip_file):
3967
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
3968
  logs = [f"🐞 Debug(upload) at {_now_str()}"]