Spaces:
Sleeping
Sleeping
Siyuan Hu
commited on
Commit
Β·
ce0eab9
1
Parent(s):
8416c99
feat(debug): add 'Test last pipeline zip' to compile most recent runs/*/output.zip with local fonts + template precedence; widen TEXINPUTS
Browse files
app.py
CHANGED
|
@@ -2083,6 +2083,114 @@ def debug_compile_output_zip():
|
|
| 2083 |
_write_logs(LOG_PATH, logs)
|
| 2084 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 2085 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2086 |
def debug_compile_uploaded_zip(zip_file):
|
| 2087 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 2088 |
logs = [f"π Debug(upload) at {_now_str()}"]
|
|
|
|
| 2083 |
_write_logs(LOG_PATH, logs)
|
| 2084 |
return f"<div>Compiled but preview failed: {e}</div>"
|
| 2085 |
|
| 2086 |
+
def _find_last_pipeline_zip():
|
| 2087 |
+
try:
|
| 2088 |
+
candidates = []
|
| 2089 |
+
for d in RUNS_DIR.iterdir():
|
| 2090 |
+
try:
|
| 2091 |
+
if d.is_dir():
|
| 2092 |
+
z = d / "output.zip"
|
| 2093 |
+
if z.exists():
|
| 2094 |
+
candidates.append((z.stat().st_mtime, z))
|
| 2095 |
+
except Exception:
|
| 2096 |
+
pass
|
| 2097 |
+
if not candidates:
|
| 2098 |
+
return None
|
| 2099 |
+
candidates.sort(key=lambda x: x[0], reverse=True)
|
| 2100 |
+
return candidates[0][1]
|
| 2101 |
+
except Exception:
|
| 2102 |
+
return None
|
| 2103 |
+
|
| 2104 |
+
def debug_compile_last_pipeline_zip():
|
| 2105 |
+
"""Find the most recent runs/*/output.zip from pipeline, compile, and preview the PDF."""
|
| 2106 |
+
logs = [f"π Debug(last-pipeline-zip) at {_now_str()}"]
|
| 2107 |
+
last_zip = _find_last_pipeline_zip()
|
| 2108 |
+
if not last_zip:
|
| 2109 |
+
return "<div style='color:#b00'>No recent pipeline output.zip found under runs/.</div>"
|
| 2110 |
+
|
| 2111 |
+
# Prepare workspace
|
| 2112 |
+
run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
|
| 2113 |
+
work_zip_dir = WORK_DIR / "zip_last"
|
| 2114 |
+
work_zip_dir.mkdir(parents=True, exist_ok=True)
|
| 2115 |
+
logs.append(f"Workspace: runs/{WORK_DIR.name}")
|
| 2116 |
+
logs.append(f"Using: {last_zip}")
|
| 2117 |
+
|
| 2118 |
+
# Extract zip
|
| 2119 |
+
try:
|
| 2120 |
+
import zipfile as _zf
|
| 2121 |
+
with _zf.ZipFile(last_zip, 'r') as zf:
|
| 2122 |
+
zf.extractall(work_zip_dir)
|
| 2123 |
+
except Exception as e:
|
| 2124 |
+
logs.append(f"β unzip failed: {e}")
|
| 2125 |
+
_write_logs(LOG_PATH, logs)
|
| 2126 |
+
return "<div style='color:#b00'>Unzip failed.</div>"
|
| 2127 |
+
|
| 2128 |
+
# Locate tex
|
| 2129 |
+
tex_path = None
|
| 2130 |
+
for name in ("poster_output.tex", "poster.tex"):
|
| 2131 |
+
cand = list(work_zip_dir.rglob(name))
|
| 2132 |
+
if cand:
|
| 2133 |
+
tex_path = cand[0]
|
| 2134 |
+
break
|
| 2135 |
+
if tex_path is None:
|
| 2136 |
+
cand = list(work_zip_dir.rglob("*.tex"))
|
| 2137 |
+
if cand:
|
| 2138 |
+
tex_path = cand[0]
|
| 2139 |
+
if tex_path is None:
|
| 2140 |
+
logs.append("β No .tex file found in last pipeline zip")
|
| 2141 |
+
_write_logs(LOG_PATH, logs)
|
| 2142 |
+
return "<div style='color:#b00'>No .tex found in last pipeline zip</div>"
|
| 2143 |
+
|
| 2144 |
+
# Ensure local fonts and theme precedence (same as other debug path)
|
| 2145 |
+
try:
|
| 2146 |
+
src_fonts = ROOT / "template" / "fonts"
|
| 2147 |
+
dst_fonts = work_zip_dir / "fonts"
|
| 2148 |
+
if src_fonts.exists():
|
| 2149 |
+
for root_dir, dirs, files in os.walk(src_fonts):
|
| 2150 |
+
rel = Path(root_dir).relative_to(src_fonts)
|
| 2151 |
+
out_dir = dst_fonts / rel
|
| 2152 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 2153 |
+
for fn in files:
|
| 2154 |
+
if fn.lower().endswith((".ttf", ".otf")):
|
| 2155 |
+
shutil.copy2(Path(root_dir)/fn, out_dir/fn)
|
| 2156 |
+
logs.append("π Copied local fonts β zip_last/fonts/")
|
| 2157 |
+
# Copy repository theme .sty next to tex and at root
|
| 2158 |
+
try:
|
| 2159 |
+
tmpl_dir = ROOT / "template"
|
| 2160 |
+
for sty in tmpl_dir.glob("*.sty"):
|
| 2161 |
+
shutil.copy2(sty, work_zip_dir / sty.name)
|
| 2162 |
+
shutil.copy2(sty, tex_path.parent / sty.name)
|
| 2163 |
+
logs.append("π Copied template/*.sty β zip_last/ and tex dir")
|
| 2164 |
+
except Exception as e:
|
| 2165 |
+
logs.append(f"β οΈ Copy sty failed: {e}")
|
| 2166 |
+
except Exception as e:
|
| 2167 |
+
logs.append(f"β οΈ Local font setup failed: {e}")
|
| 2168 |
+
|
| 2169 |
+
# Compile to PDF
|
| 2170 |
+
pdf_path = _compile_tex_to_pdf(tex_path, logs)
|
| 2171 |
+
if not pdf_path or not pdf_path.exists():
|
| 2172 |
+
logs.append("β Failed to compile last pipeline zip PDF.")
|
| 2173 |
+
_write_logs(LOG_PATH, logs)
|
| 2174 |
+
return (
|
| 2175 |
+
"<div style='color:#b00'><b>Compile failed.</b></div>"
|
| 2176 |
+
+ "<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>"
|
| 2177 |
+
+ "\n".join(logs)
|
| 2178 |
+
+ "</pre>"
|
| 2179 |
+
)
|
| 2180 |
+
try:
|
| 2181 |
+
b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
|
| 2182 |
+
open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
|
| 2183 |
+
html = (
|
| 2184 |
+
f"<div style='margin-bottom:8px'>{open_tab}</div>"
|
| 2185 |
+
+ _pdf_to_iframe_html(pdf_path, height="700px")
|
| 2186 |
+
)
|
| 2187 |
+
_write_logs(LOG_PATH, logs)
|
| 2188 |
+
return html
|
| 2189 |
+
except Exception as e:
|
| 2190 |
+
logs.append(f"β οΈ preview failed: {e}")
|
| 2191 |
+
_write_logs(LOG_PATH, logs)
|
| 2192 |
+
return f"<div>Compiled but preview failed: {e}</div>"
|
| 2193 |
+
|
| 2194 |
def debug_compile_uploaded_zip(zip_file):
|
| 2195 |
"""Compile an uploaded poster zip (user-provided) and preview PDF."""
|
| 2196 |
logs = [f"π Debug(upload) at {_now_str()}"]
|