Siyuan Hu commited on
Commit
448fbdc
Β·
1 Parent(s): f839dfd

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
@@ -2528,6 +2528,114 @@ def debug_compile_output_zip():
2528
  _write_logs(LOG_PATH, logs)
2529
  return f"<div>Compiled but preview failed: {e}</div>"
2530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2531
  def debug_compile_uploaded_zip(zip_file):
2532
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
2533
  logs = [f"🐞 Debug(upload) at {_now_str()}"]
 
2528
  _write_logs(LOG_PATH, logs)
2529
  return f"<div>Compiled but preview failed: {e}</div>"
2530
 
2531
+ def _find_last_pipeline_zip():
2532
+ try:
2533
+ candidates = []
2534
+ for d in RUNS_DIR.iterdir():
2535
+ try:
2536
+ if d.is_dir():
2537
+ z = d / "output.zip"
2538
+ if z.exists():
2539
+ candidates.append((z.stat().st_mtime, z))
2540
+ except Exception:
2541
+ pass
2542
+ if not candidates:
2543
+ return None
2544
+ candidates.sort(key=lambda x: x[0], reverse=True)
2545
+ return candidates[0][1]
2546
+ except Exception:
2547
+ return None
2548
+
2549
+ def debug_compile_last_pipeline_zip():
2550
+ """Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
2551
+ logs = [f"🐞 Debug(last-pipeline-zip) at {_now_str()}"]
2552
+ last_zip = _find_last_pipeline_zip()
2553
+ if not last_zip:
2554
+ return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
2555
+
2556
+ # Prepare workspace
2557
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
2558
+ work_zip_dir = WORK_DIR / "zip_last"
2559
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
2560
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
2561
+ logs.append(f"Using: {last_zip}")
2562
+
2563
+ # Extract zip
2564
+ try:
2565
+ import zipfile as _zf
2566
+ with _zf.ZipFile(last_zip, 'r') as zf:
2567
+ zf.extractall(work_zip_dir)
2568
+ except Exception as e:
2569
+ logs.append(f"❌ unzip failed: {e}")
2570
+ _write_logs(LOG_PATH, logs)
2571
+ return "<div style='color:#b00'>Unzip failed.</div>"
2572
+
2573
+ # Locate tex
2574
+ tex_path = None
2575
+ for name in ("poster_output.tex", "poster.tex"):
2576
+ cand = list(work_zip_dir.rglob(name))
2577
+ if cand:
2578
+ tex_path = cand[0]
2579
+ break
2580
+ if tex_path is None:
2581
+ cand = list(work_zip_dir.rglob("*.tex"))
2582
+ if cand:
2583
+ tex_path = cand[0]
2584
+ if tex_path is None:
2585
+ logs.append("❌ No .tex file found in last pipeline zip")
2586
+ _write_logs(LOG_PATH, logs)
2587
+ return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
2588
+
2589
+ # Ensure local fonts and theme precedence (same as other debug path)
2590
+ try:
2591
+ src_fonts = ROOT / "template" / "fonts"
2592
+ dst_fonts = work_zip_dir / "fonts"
2593
+ if src_fonts.exists():
2594
+ for root_dir, dirs, files in os.walk(src_fonts):
2595
+ rel = Path(root_dir).relative_to(src_fonts)
2596
+ out_dir = dst_fonts / rel
2597
+ out_dir.mkdir(parents=True, exist_ok=True)
2598
+ for fn in files:
2599
+ if fn.lower().endswith((".ttf", ".otf")):
2600
+ shutil.copy2(Path(root_dir)/fn, out_dir/fn)
2601
+ logs.append("πŸ“ Copied local fonts β†’ zip_last/fonts/")
2602
+ # Copy repository theme .sty next to tex and at root
2603
+ try:
2604
+ tmpl_dir = ROOT / "template"
2605
+ for sty in tmpl_dir.glob("*.sty"):
2606
+ shutil.copy2(sty, work_zip_dir / sty.name)
2607
+ shutil.copy2(sty, tex_path.parent / sty.name)
2608
+ logs.append("πŸ“„ Copied template/*.sty β†’ zip_last/ and tex dir")
2609
+ except Exception as e:
2610
+ logs.append(f"⚠️ Copy sty failed: {e}")
2611
+ except Exception as e:
2612
+ logs.append(f"⚠️ Local font setup failed: {e}")
2613
+
2614
+ # Compile to PDF
2615
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
2616
+ if not pdf_path or not pdf_path.exists():
2617
+ logs.append("❌ Failed to compile last pipeline zip PDF.")
2618
+ _write_logs(LOG_PATH, logs)
2619
+ return (
2620
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
2621
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
2622
+ + "\n".join(logs)
2623
+ + "</pre>"
2624
+ )
2625
+ try:
2626
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
2627
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
2628
+ html = (
2629
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
2630
+ + _pdf_to_iframe_html(pdf_path, height="700px")
2631
+ )
2632
+ _write_logs(LOG_PATH, logs)
2633
+ return html
2634
+ except Exception as e:
2635
+ logs.append(f"⚠️ preview failed: {e}")
2636
+ _write_logs(LOG_PATH, logs)
2637
+ return f"<div>Compiled but preview failed: {e}</div>"
2638
+
2639
  def debug_compile_uploaded_zip(zip_file):
2640
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
2641
  logs = [f"🐞 Debug(upload) at {_now_str()}"]