Siyuan Hu commited on
Commit
02c0a6e
Β·
1 Parent(s): 43b6fc7

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
@@ -2876,6 +2876,114 @@ def debug_compile_output_zip():
2876
  _write_logs(LOG_PATH, logs)
2877
  return f"<div>Compiled but preview failed: {e}</div>"
2878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2879
  def debug_compile_uploaded_zip(zip_file):
2880
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
2881
  logs = [f"🐞 Debug(upload) at {_now_str()}"]
 
2876
  _write_logs(LOG_PATH, logs)
2877
  return f"<div>Compiled but preview failed: {e}</div>"
2878
 
2879
+ def _find_last_pipeline_zip():
2880
+ try:
2881
+ candidates = []
2882
+ for d in RUNS_DIR.iterdir():
2883
+ try:
2884
+ if d.is_dir():
2885
+ z = d / "output.zip"
2886
+ if z.exists():
2887
+ candidates.append((z.stat().st_mtime, z))
2888
+ except Exception:
2889
+ pass
2890
+ if not candidates:
2891
+ return None
2892
+ candidates.sort(key=lambda x: x[0], reverse=True)
2893
+ return candidates[0][1]
2894
+ except Exception:
2895
+ return None
2896
+
2897
+ def debug_compile_last_pipeline_zip():
2898
+ """Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
2899
+ logs = [f"🐞 Debug(last-pipeline-zip) at {_now_str()}"]
2900
+ last_zip = _find_last_pipeline_zip()
2901
+ if not last_zip:
2902
+ return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
2903
+
2904
+ # Prepare workspace
2905
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
2906
+ work_zip_dir = WORK_DIR / "zip_last"
2907
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
2908
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
2909
+ logs.append(f"Using: {last_zip}")
2910
+
2911
+ # Extract zip
2912
+ try:
2913
+ import zipfile as _zf
2914
+ with _zf.ZipFile(last_zip, 'r') as zf:
2915
+ zf.extractall(work_zip_dir)
2916
+ except Exception as e:
2917
+ logs.append(f"❌ unzip failed: {e}")
2918
+ _write_logs(LOG_PATH, logs)
2919
+ return "<div style='color:#b00'>Unzip failed.</div>"
2920
+
2921
+ # Locate tex
2922
+ tex_path = None
2923
+ for name in ("poster_output.tex", "poster.tex"):
2924
+ cand = list(work_zip_dir.rglob(name))
2925
+ if cand:
2926
+ tex_path = cand[0]
2927
+ break
2928
+ if tex_path is None:
2929
+ cand = list(work_zip_dir.rglob("*.tex"))
2930
+ if cand:
2931
+ tex_path = cand[0]
2932
+ if tex_path is None:
2933
+ logs.append("❌ No .tex file found in last pipeline zip")
2934
+ _write_logs(LOG_PATH, logs)
2935
+ return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
2936
+
2937
+ # Ensure local fonts and theme precedence (same as other debug path)
2938
+ try:
2939
+ src_fonts = ROOT / "template" / "fonts"
2940
+ dst_fonts = work_zip_dir / "fonts"
2941
+ if src_fonts.exists():
2942
+ for root_dir, dirs, files in os.walk(src_fonts):
2943
+ rel = Path(root_dir).relative_to(src_fonts)
2944
+ out_dir = dst_fonts / rel
2945
+ out_dir.mkdir(parents=True, exist_ok=True)
2946
+ for fn in files:
2947
+ if fn.lower().endswith((".ttf", ".otf")):
2948
+ shutil.copy2(Path(root_dir)/fn, out_dir/fn)
2949
+ logs.append("πŸ“ Copied local fonts β†’ zip_last/fonts/")
2950
+ # Copy repository theme .sty next to tex and at root
2951
+ try:
2952
+ tmpl_dir = ROOT / "template"
2953
+ for sty in tmpl_dir.glob("*.sty"):
2954
+ shutil.copy2(sty, work_zip_dir / sty.name)
2955
+ shutil.copy2(sty, tex_path.parent / sty.name)
2956
+ logs.append("πŸ“„ Copied template/*.sty β†’ zip_last/ and tex dir")
2957
+ except Exception as e:
2958
+ logs.append(f"⚠️ Copy sty failed: {e}")
2959
+ except Exception as e:
2960
+ logs.append(f"⚠️ Local font setup failed: {e}")
2961
+
2962
+ # Compile to PDF
2963
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
2964
+ if not pdf_path or not pdf_path.exists():
2965
+ logs.append("❌ Failed to compile last pipeline zip PDF.")
2966
+ _write_logs(LOG_PATH, logs)
2967
+ return (
2968
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
2969
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
2970
+ + "\n".join(logs)
2971
+ + "</pre>"
2972
+ )
2973
+ try:
2974
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
2975
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
2976
+ html = (
2977
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
2978
+ + _pdf_to_iframe_html(pdf_path, height="700px")
2979
+ )
2980
+ _write_logs(LOG_PATH, logs)
2981
+ return html
2982
+ except Exception as e:
2983
+ logs.append(f"⚠️ preview failed: {e}")
2984
+ _write_logs(LOG_PATH, logs)
2985
+ return f"<div>Compiled but preview failed: {e}</div>"
2986
+
2987
  def debug_compile_uploaded_zip(zip_file):
2988
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
2989
  logs = [f"🐞 Debug(upload) at {_now_str()}"]