Siyuan Hu commited on
Commit
bcfd028
Β·
1 Parent(s): e73e293

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
@@ -979,6 +979,114 @@ def debug_compile_last_pipeline_zip():
979
  _write_logs(LOG_PATH, logs)
980
  return f"<div>Compiled but preview failed: {e}</div>", None
981
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
982
  def debug_compile_uploaded_zip(zip_file):
983
  """Compile an uploaded poster zip (user-provided); return preview HTML + PDF path."""
984
  logs = [f"🐞 Debug(upload) at {_now_str()}"]
 
979
  _write_logs(LOG_PATH, logs)
980
  return f"<div>Compiled but preview failed: {e}</div>", None
981
 
982
+ def _find_last_pipeline_zip():
983
+ try:
984
+ candidates = []
985
+ for d in RUNS_DIR.iterdir():
986
+ try:
987
+ if d.is_dir():
988
+ z = d / "output.zip"
989
+ if z.exists():
990
+ candidates.append((z.stat().st_mtime, z))
991
+ except Exception:
992
+ pass
993
+ if not candidates:
994
+ return None
995
+ candidates.sort(key=lambda x: x[0], reverse=True)
996
+ return candidates[0][1]
997
+ except Exception:
998
+ return None
999
+
1000
+ def debug_compile_last_pipeline_zip():
1001
+ """Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
1002
+ logs = [f"🐞 Debug(last-pipeline-zip) at {_now_str()}"]
1003
+ last_zip = _find_last_pipeline_zip()
1004
+ if not last_zip:
1005
+ return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
1006
+
1007
+ # Prepare workspace
1008
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
1009
+ work_zip_dir = WORK_DIR / "zip_last"
1010
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
1011
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
1012
+ logs.append(f"Using: {last_zip}")
1013
+
1014
+ # Extract zip
1015
+ try:
1016
+ import zipfile as _zf
1017
+ with _zf.ZipFile(last_zip, 'r') as zf:
1018
+ zf.extractall(work_zip_dir)
1019
+ except Exception as e:
1020
+ logs.append(f"❌ unzip failed: {e}")
1021
+ _write_logs(LOG_PATH, logs)
1022
+ return "<div style='color:#b00'>Unzip failed.</div>"
1023
+
1024
+ # Locate tex
1025
+ tex_path = None
1026
+ for name in ("poster_output.tex", "poster.tex"):
1027
+ cand = list(work_zip_dir.rglob(name))
1028
+ if cand:
1029
+ tex_path = cand[0]
1030
+ break
1031
+ if tex_path is None:
1032
+ cand = list(work_zip_dir.rglob("*.tex"))
1033
+ if cand:
1034
+ tex_path = cand[0]
1035
+ if tex_path is None:
1036
+ logs.append("❌ No .tex file found in last pipeline zip")
1037
+ _write_logs(LOG_PATH, logs)
1038
+ return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
1039
+
1040
+ # Ensure local fonts and theme precedence (same as other debug path)
1041
+ try:
1042
+ src_fonts = ROOT / "template" / "fonts"
1043
+ dst_fonts = work_zip_dir / "fonts"
1044
+ if src_fonts.exists():
1045
+ for root_dir, dirs, files in os.walk(src_fonts):
1046
+ rel = Path(root_dir).relative_to(src_fonts)
1047
+ out_dir = dst_fonts / rel
1048
+ out_dir.mkdir(parents=True, exist_ok=True)
1049
+ for fn in files:
1050
+ if fn.lower().endswith((".ttf", ".otf")):
1051
+ shutil.copy2(Path(root_dir)/fn, out_dir/fn)
1052
+ logs.append("πŸ“ Copied local fonts β†’ zip_last/fonts/")
1053
+ # Copy repository theme .sty next to tex and at root
1054
+ try:
1055
+ tmpl_dir = ROOT / "template"
1056
+ for sty in tmpl_dir.glob("*.sty"):
1057
+ shutil.copy2(sty, work_zip_dir / sty.name)
1058
+ shutil.copy2(sty, tex_path.parent / sty.name)
1059
+ logs.append("πŸ“„ Copied template/*.sty β†’ zip_last/ and tex dir")
1060
+ except Exception as e:
1061
+ logs.append(f"⚠️ Copy sty failed: {e}")
1062
+ except Exception as e:
1063
+ logs.append(f"⚠️ Local font setup failed: {e}")
1064
+
1065
+ # Compile to PDF
1066
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
1067
+ if not pdf_path or not pdf_path.exists():
1068
+ logs.append("❌ Failed to compile last pipeline zip PDF.")
1069
+ _write_logs(LOG_PATH, logs)
1070
+ return (
1071
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
1072
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
1073
+ + "\n".join(logs)
1074
+ + "</pre>"
1075
+ )
1076
+ try:
1077
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
1078
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
1079
+ html = (
1080
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
1081
+ + _pdf_to_iframe_html(pdf_path, height="700px")
1082
+ )
1083
+ _write_logs(LOG_PATH, logs)
1084
+ return html
1085
+ except Exception as e:
1086
+ logs.append(f"⚠️ preview failed: {e}")
1087
+ _write_logs(LOG_PATH, logs)
1088
+ return f"<div>Compiled but preview failed: {e}</div>"
1089
+
1090
  def debug_compile_uploaded_zip(zip_file):
1091
  """Compile an uploaded poster zip (user-provided); return preview HTML + PDF path."""
1092
  logs = [f"🐞 Debug(upload) at {_now_str()}"]