Siyuan Hu commited on
Commit
935382c
Β·
1 Parent(s): f269530

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
@@ -1928,6 +1928,114 @@ def debug_compile_output_zip():
1928
  _write_logs(LOG_PATH, logs)
1929
  return f"<div>Compiled but preview failed: {e}</div>"
1930
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1931
  def debug_compile_uploaded_zip(zip_file):
1932
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
1933
  logs = [f"🐞 Debug(upload) at {_now_str()}"]
 
1928
  _write_logs(LOG_PATH, logs)
1929
  return f"<div>Compiled but preview failed: {e}</div>"
1930
 
1931
+ def _find_last_pipeline_zip():
1932
+ try:
1933
+ candidates = []
1934
+ for d in RUNS_DIR.iterdir():
1935
+ try:
1936
+ if d.is_dir():
1937
+ z = d / "output.zip"
1938
+ if z.exists():
1939
+ candidates.append((z.stat().st_mtime, z))
1940
+ except Exception:
1941
+ pass
1942
+ if not candidates:
1943
+ return None
1944
+ candidates.sort(key=lambda x: x[0], reverse=True)
1945
+ return candidates[0][1]
1946
+ except Exception:
1947
+ return None
1948
+
1949
+ def debug_compile_last_pipeline_zip():
1950
+ """Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
1951
+ logs = [f"🐞 Debug(last-pipeline-zip) at {_now_str()}"]
1952
+ last_zip = _find_last_pipeline_zip()
1953
+ if not last_zip:
1954
+ return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
1955
+
1956
+ # Prepare workspace
1957
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
1958
+ work_zip_dir = WORK_DIR / "zip_last"
1959
+ work_zip_dir.mkdir(parents=True, exist_ok=True)
1960
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
1961
+ logs.append(f"Using: {last_zip}")
1962
+
1963
+ # Extract zip
1964
+ try:
1965
+ import zipfile as _zf
1966
+ with _zf.ZipFile(last_zip, 'r') as zf:
1967
+ zf.extractall(work_zip_dir)
1968
+ except Exception as e:
1969
+ logs.append(f"❌ unzip failed: {e}")
1970
+ _write_logs(LOG_PATH, logs)
1971
+ return "<div style='color:#b00'>Unzip failed.</div>"
1972
+
1973
+ # Locate tex
1974
+ tex_path = None
1975
+ for name in ("poster_output.tex", "poster.tex"):
1976
+ cand = list(work_zip_dir.rglob(name))
1977
+ if cand:
1978
+ tex_path = cand[0]
1979
+ break
1980
+ if tex_path is None:
1981
+ cand = list(work_zip_dir.rglob("*.tex"))
1982
+ if cand:
1983
+ tex_path = cand[0]
1984
+ if tex_path is None:
1985
+ logs.append("❌ No .tex file found in last pipeline zip")
1986
+ _write_logs(LOG_PATH, logs)
1987
+ return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
1988
+
1989
+ # Ensure local fonts and theme precedence (same as other debug path)
1990
+ try:
1991
+ src_fonts = ROOT / "template" / "fonts"
1992
+ dst_fonts = work_zip_dir / "fonts"
1993
+ if src_fonts.exists():
1994
+ for root_dir, dirs, files in os.walk(src_fonts):
1995
+ rel = Path(root_dir).relative_to(src_fonts)
1996
+ out_dir = dst_fonts / rel
1997
+ out_dir.mkdir(parents=True, exist_ok=True)
1998
+ for fn in files:
1999
+ if fn.lower().endswith((".ttf", ".otf")):
2000
+ shutil.copy2(Path(root_dir)/fn, out_dir/fn)
2001
+ logs.append("πŸ“ Copied local fonts β†’ zip_last/fonts/")
2002
+ # Copy repository theme .sty next to tex and at root
2003
+ try:
2004
+ tmpl_dir = ROOT / "template"
2005
+ for sty in tmpl_dir.glob("*.sty"):
2006
+ shutil.copy2(sty, work_zip_dir / sty.name)
2007
+ shutil.copy2(sty, tex_path.parent / sty.name)
2008
+ logs.append("πŸ“„ Copied template/*.sty β†’ zip_last/ and tex dir")
2009
+ except Exception as e:
2010
+ logs.append(f"⚠️ Copy sty failed: {e}")
2011
+ except Exception as e:
2012
+ logs.append(f"⚠️ Local font setup failed: {e}")
2013
+
2014
+ # Compile to PDF
2015
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
2016
+ if not pdf_path or not pdf_path.exists():
2017
+ logs.append("❌ Failed to compile last pipeline zip PDF.")
2018
+ _write_logs(LOG_PATH, logs)
2019
+ return (
2020
+ "<div style='color:#b00'><b>Compile failed.</b></div>"
2021
+ + "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
2022
+ + "\n".join(logs)
2023
+ + "</pre>"
2024
+ )
2025
+ try:
2026
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
2027
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
2028
+ html = (
2029
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
2030
+ + _pdf_to_iframe_html(pdf_path, height="700px")
2031
+ )
2032
+ _write_logs(LOG_PATH, logs)
2033
+ return html
2034
+ except Exception as e:
2035
+ logs.append(f"⚠️ preview failed: {e}")
2036
+ _write_logs(LOG_PATH, logs)
2037
+ return f"<div>Compiled but preview failed: {e}</div>"
2038
+
2039
  def debug_compile_uploaded_zip(zip_file):
2040
  """Compile an uploaded poster zip (user-provided) and preview PDF."""
2041
  logs = [f"🐞 Debug(upload) at {_now_str()}"]