Siyuan Hu commited on
Commit
7453a49
Β·
1 Parent(s): ae14754

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
@@ -2275,6 +2275,114 @@ def debug_compile_output_zip():
2275
  _write_logs(LOG_PATH, logs)
2276
  return f"<div>Compiled but preview failed: {e}</div>"
2277
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2278
  def debug_compile_uploaded_zip(zip_file):
2279
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
2280
  logs = [f"🐞 Debug(upload) at {_now_str()}"]
 
2275
  _write_logs(LOG_PATH, logs)
2276
  return f"<div>Compiled but preview failed: {e}</div>"
2277
 
2278
+ def _find_last_pipeline_zip():
2279
+ try:
2280
+ candidates = []
2281
+ for d in RUNS_DIR.iterdir():
2282
+ try:
2283
+ if d.is_dir():
2284
+ z = d / "output.zip"
2285
+ if z.exists():
2286
+ candidates.append((z.stat().st_mtime, z))
2287
+ except Exception:
2288
+ pass
2289
+ if not candidates:
2290
+ return None
2291
+ candidates.sort(key=lambda x: x[0], reverse=True)
2292
+ return candidates[0][1]
2293
+ except Exception:
2294
+ return None
2295
+
2296
+ def debug_compile_last_pipeline_zip():
2297
+ """Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
2298
+ logs = [f"🐞 Debug(last-pipeline-zip) at {_now_str()}"]
2299
+ last_zip = _find_last_pipeline_zip()
2300
+ if not last_zip:
2301
+ return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
2302
+
2303
+ # Prepare workspace
2304
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
2305
+ work_zip_dir = WORK_DIR / "zip_last"
2306
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
2307
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
2308
+ logs.append(f"Using: {last_zip}")
2309
+
2310
+ # Extract zip
2311
+ try:
2312
+ import zipfile as _zf
2313
+ with _zf.ZipFile(last_zip, 'r') as zf:
2314
+ zf.extractall(work_zip_dir)
2315
+ except Exception as e:
2316
+ logs.append(f"❌ unzip failed: {e}")
2317
+ _write_logs(LOG_PATH, logs)
2318
+ return "<div style='color:#b00'>Unzip failed.</div>"
2319
+
2320
+ # Locate tex
2321
+ tex_path = None
2322
+ for name in ("poster_output.tex", "poster.tex"):
2323
+ cand = list(work_zip_dir.rglob(name))
2324
+ if cand:
2325
+ tex_path = cand[0]
2326
+ break
2327
+ if tex_path is None:
2328
+ cand = list(work_zip_dir.rglob("*.tex"))
2329
+ if cand:
2330
+ tex_path = cand[0]
2331
+ if tex_path is None:
2332
+ logs.append("❌ No .tex file found in last pipeline zip")
2333
+ _write_logs(LOG_PATH, logs)
2334
+ return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
2335
+
2336
+ # Ensure local fonts and theme precedence (same as other debug path)
2337
+ try:
2338
+ src_fonts = ROOT / "template" / "fonts"
2339
+ dst_fonts = work_zip_dir / "fonts"
2340
+ if src_fonts.exists():
2341
+ for root_dir, dirs, files in os.walk(src_fonts):
2342
+ rel = Path(root_dir).relative_to(src_fonts)
2343
+ out_dir = dst_fonts / rel
2344
+ out_dir.mkdir(parents=True, exist_ok=True)
2345
+ for fn in files:
2346
+ if fn.lower().endswith((".ttf", ".otf")):
2347
+ shutil.copy2(Path(root_dir)/fn, out_dir/fn)
2348
+ logs.append("πŸ“ Copied local fonts β†’ zip_last/fonts/")
2349
+ # Copy repository theme .sty next to tex and at root
2350
+ try:
2351
+ tmpl_dir = ROOT / "template"
2352
+ for sty in tmpl_dir.glob("*.sty"):
2353
+ shutil.copy2(sty, work_zip_dir / sty.name)
2354
+ shutil.copy2(sty, tex_path.parent / sty.name)
2355
+ logs.append("πŸ“„ Copied template/*.sty β†’ zip_last/ and tex dir")
2356
+ except Exception as e:
2357
+ logs.append(f"⚠️ Copy sty failed: {e}")
2358
+ except Exception as e:
2359
+ logs.append(f"⚠️ Local font setup failed: {e}")
2360
+
2361
+ # Compile to PDF
2362
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
2363
+ if not pdf_path or not pdf_path.exists():
2364
+ logs.append("❌ Failed to compile last pipeline zip PDF.")
2365
+ _write_logs(LOG_PATH, logs)
2366
+ return (
2367
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
2368
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
2369
+ + "\n".join(logs)
2370
+ + "</pre>"
2371
+ )
2372
+ try:
2373
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
2374
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
2375
+ html = (
2376
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
2377
+ + _pdf_to_iframe_html(pdf_path, height="700px")
2378
+ )
2379
+ _write_logs(LOG_PATH, logs)
2380
+ return html
2381
+ except Exception as e:
2382
+ logs.append(f"⚠️ preview failed: {e}")
2383
+ _write_logs(LOG_PATH, logs)
2384
+ return f"<div>Compiled but preview failed: {e}</div>"
2385
+
2386
  def debug_compile_uploaded_zip(zip_file):
2387
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
2388
  logs = [f"🐞 Debug(upload) at {_now_str()}"]