Siyuan Hu commited on
Commit
63e7414
Β·
1 Parent(s): 02328c0

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