Files
zoitechat/data/misc/fetch_offline_docs.py

90 lines
2.5 KiB
Python
Raw Normal View History

2026-06-04 14:44:56 -06:00
#!/usr/bin/env python3
2026-06-09 12:09:14 -06:00
2026-06-04 14:44:56 -06:00
import html
import io
import pathlib
import shutil
import sys
import tarfile
2026-06-09 12:09:14 -06:00
import tempfile
2026-06-04 14:44:56 -06:00
import urllib.error
import urllib.request
2026-06-09 12:09:14 -06:00
2026-06-04 14:44:56 -06:00
url = sys.argv[1]
out_dir = pathlib.Path(sys.argv[2])
source_dir = pathlib.Path(sys.argv[3]) if len(sys.argv) > 3 else pathlib.Path.cwd()
2026-06-09 12:09:14 -06:00
docs_dir = out_dir / "offline-docs"
2026-06-04 14:44:56 -06:00
if docs_dir.exists():
shutil.rmtree(docs_dir)
docs_dir.mkdir(parents=True, exist_ok=True)
2026-06-09 12:09:14 -06:00
def write_source_docs() -> None:
parts = [
'<!doctype html><html><head><meta charset="utf-8">'
"<title>ZoiteChat Documentation</title></head><body>"
"<h1>ZoiteChat Documentation</h1>"
]
for name in ("readme.md", "troubleshooting.md", "changelog.rst"):
2026-06-04 14:44:56 -06:00
path = source_dir / name
if path.exists():
2026-06-09 12:09:14 -06:00
parts.append(
f"<h2>{html.escape(name)}</h2>"
f"<pre>{html.escape(path.read_text(encoding='utf-8', errors='replace'))}</pre>"
)
parts.append("</body></html>")
(docs_dir / "index.html").write_text("\n".join(parts), encoding="utf-8")
def safe_extract(tar: tarfile.TarFile, target: pathlib.Path) -> None:
target = target.resolve()
for member in tar.getmembers():
member_path = (target / member.name).resolve()
if not str(member_path).startswith(str(target) + "/"):
raise tarfile.TarError(f"unsafe archive path: {member.name}")
tar.extractall(target)
def copy_index_tree(extracted_dir: pathlib.Path) -> bool:
indexes = sorted(extracted_dir.rglob("index.html"))
if not indexes:
return False
root = indexes[0].parent
for item in root.iterdir():
dest = docs_dir / item.name
if item.is_dir():
shutil.copytree(item, dest, dirs_exist_ok=True)
else:
shutil.copy2(item, dest)
return (docs_dir / "index.html").exists()
2026-06-04 14:44:56 -06:00
if url:
try:
2026-06-09 12:09:14 -06:00
with urllib.request.urlopen(url, timeout=30) as response:
data = response.read()
with tempfile.TemporaryDirectory() as tmp:
extract_dir = pathlib.Path(tmp)
with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as tar:
safe_extract(tar, extract_dir)
if not copy_index_tree(extract_dir):
write_source_docs()
2026-06-04 14:44:56 -06:00
except (OSError, tarfile.TarError, urllib.error.URLError) as error:
2026-06-09 12:09:14 -06:00
print(f"offline docs download failed: {error}", file=sys.stderr)
2026-06-04 14:44:56 -06:00
write_source_docs()
else:
write_source_docs()
2026-06-09 12:09:14 -06:00
(out_dir / "offline-docs.stamp").write_text("ok", encoding="utf-8")