Siyuan Hu commited on
Commit
a459a0d
Β·
1 Parent(s): 2bc9abf

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
@@ -3572,6 +3572,114 @@ def debug_compile_output_zip():
3572
  _write_logs(LOG_PATH, logs)
3573
  return f"<div>Compiled but preview failed: {e}</div>"
3574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3575
  def debug_compile_uploaded_zip(zip_file):
3576
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
3577
  logs = [f"🐞 Debug(upload) at {_now_str()}"]
 
3572
  _write_logs(LOG_PATH, logs)
3573
  return f"<div>Compiled but preview failed: {e}</div>"
3574
 
3575
+ def _find_last_pipeline_zip():
3576
+ try:
3577
+ candidates = []
3578
+ for d in RUNS_DIR.iterdir():
3579
+ try:
3580
+ if d.is_dir():
3581
+ z = d / "output.zip"
3582
+ if z.exists():
3583
+ candidates.append((z.stat().st_mtime, z))
3584
+ except Exception:
3585
+ pass
3586
+ if not candidates:
3587
+ return None
3588
+ candidates.sort(key=lambda x: x[0], reverse=True)
3589
+ return candidates[0][1]
3590
+ except Exception:
3591
+ return None
3592
+
3593
+ def debug_compile_last_pipeline_zip():
3594
+ """Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
3595
+ logs = [f"🐞 Debug(last-pipeline-zip) at {_now_str()}"]
3596
+ last_zip = _find_last_pipeline_zip()
3597
+ if not last_zip:
3598
+ return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
3599
+
3600
+ # Prepare workspace
3601
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
3602
+ work_zip_dir = WORK_DIR / "zip_last"
3603
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
3604
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
3605
+ logs.append(f"Using: {last_zip}")
3606
+
3607
+ # Extract zip
3608
+ try:
3609
+ import zipfile as _zf
3610
+ with _zf.ZipFile(last_zip, 'r') as zf:
3611
+ zf.extractall(work_zip_dir)
3612
+ except Exception as e:
3613
+ logs.append(f"❌ unzip failed: {e}")
3614
+ _write_logs(LOG_PATH, logs)
3615
+ return "<div style='color:#b00'>Unzip failed.</div>"
3616
+
3617
+ # Locate tex
3618
+ tex_path = None
3619
+ for name in ("poster_output.tex", "poster.tex"):
3620
+ cand = list(work_zip_dir.rglob(name))
3621
+ if cand:
3622
+ tex_path = cand[0]
3623
+ break
3624
+ if tex_path is None:
3625
+ cand = list(work_zip_dir.rglob("*.tex"))
3626
+ if cand:
3627
+ tex_path = cand[0]
3628
+ if tex_path is None:
3629
+ logs.append("❌ No .tex file found in last pipeline zip")
3630
+ _write_logs(LOG_PATH, logs)
3631
+ return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
3632
+
3633
+ # Ensure local fonts and theme precedence (same as other debug path)
3634
+ try:
3635
+ src_fonts = ROOT / "template" / "fonts"
3636
+ dst_fonts = work_zip_dir / "fonts"
3637
+ if src_fonts.exists():
3638
+ for root_dir, dirs, files in os.walk(src_fonts):
3639
+ rel = Path(root_dir).relative_to(src_fonts)
3640
+ out_dir = dst_fonts / rel
3641
+ out_dir.mkdir(parents=True, exist_ok=True)
3642
+ for fn in files:
3643
+ if fn.lower().endswith((".ttf", ".otf")):
3644
+ shutil.copy2(Path(root_dir)/fn, out_dir/fn)
3645
+ logs.append("πŸ“ Copied local fonts β†’ zip_last/fonts/")
3646
+ # Copy repository theme .sty next to tex and at root
3647
+ try:
3648
+ tmpl_dir = ROOT / "template"
3649
+ for sty in tmpl_dir.glob("*.sty"):
3650
+ shutil.copy2(sty, work_zip_dir / sty.name)
3651
+ shutil.copy2(sty, tex_path.parent / sty.name)
3652
+ logs.append("πŸ“„ Copied template/*.sty β†’ zip_last/ and tex dir")
3653
+ except Exception as e:
3654
+ logs.append(f"⚠️ Copy sty failed: {e}")
3655
+ except Exception as e:
3656
+ logs.append(f"⚠️ Local font setup failed: {e}")
3657
+
3658
+ # Compile to PDF
3659
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
3660
+ if not pdf_path or not pdf_path.exists():
3661
+ logs.append("❌ Failed to compile last pipeline zip PDF.")
3662
+ _write_logs(LOG_PATH, logs)
3663
+ return (
3664
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
3665
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
3666
+ + "\n".join(logs)
3667
+ + "</pre>"
3668
+ )
3669
+ try:
3670
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
3671
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
3672
+ html = (
3673
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
3674
+ + _pdf_to_iframe_html(pdf_path, height="700px")
3675
+ )
3676
+ _write_logs(LOG_PATH, logs)
3677
+ return html
3678
+ except Exception as e:
3679
+ logs.append(f"⚠️ preview failed: {e}")
3680
+ _write_logs(LOG_PATH, logs)
3681
+ return f"<div>Compiled but preview failed: {e}</div>"
3682
+
3683
  def debug_compile_uploaded_zip(zip_file):
3684
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
3685
  logs = [f"🐞 Debug(upload) at {_now_str()}"]