Provide a runnable local Beta and fix multi-page SVG previews
This commit is contained in:
1 parent
fb557c09c6
commit
9841e88900
7 files changed
+324
-9
No files matched your search
@@ -0,0 +1,192 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Prepare and run a local Route-D editor and CLI with one TeX configuration."""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shlex
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
|
||||
RESOURCE_ENV = (
|
||||
"TEXINPUTS", "TFMFONTS", "VFFONTS", "T1FONTS", "OPENTYPEFONTS", "TTFONTS",
|
||||
"PKFONTS", "ENCFONTS", "TEXFONTMAPS", "TEXFORMATS", "TEXMFCNF", "MISCFONTS",
|
||||
"SFDFONTS", "CMAPFONTS", "texmf_casefold_search",
|
||||
"FONTCONFIG_FILE", "FONTCONFIG_PATH", "TZ", "SOURCE_DATE_EPOCH", "FORCE_SOURCE_DATE",
|
||||
)
|
||||
FORMAT_ENTRIES = {"tex8bit": "pdflatex.ini", "xetex": "xelatex.ini"}
|
||||
MANIFEST = "bundle.json"
|
||||
FORMAT = "latex.tfx"
|
||||
RESOURCES = "format-resources.json"
|
||||
|
||||
|
||||
def digest(path):
|
||||
with path.open("rb") as stream:
|
||||
return hashlib.file_digest(stream, "sha256").hexdigest()
|
||||
|
||||
|
||||
def installed_path(argument):
|
||||
output = subprocess.check_output(["kpsewhich", argument], text=True).strip()
|
||||
if not output:
|
||||
raise ValueError(f"TeX Live did not provide a path for {argument}")
|
||||
return Path(output).resolve(strict=True)
|
||||
|
||||
|
||||
def binary_name(name):
|
||||
return name + (".exe" if os.name == "nt" else "")
|
||||
|
||||
|
||||
def native_command(binary, profile, roots):
|
||||
command = [binary, "native", "--job", "--etex", "--input-profile", profile,
|
||||
"--input-policy", "tex", "--step-limit", "600000000"]
|
||||
for root in roots:
|
||||
command.extend(["--texmf-root", root])
|
||||
return command
|
||||
|
||||
|
||||
def roots_from_manifest(path):
|
||||
roots = json.loads(path.read_text(encoding="utf-8"))["texmf_roots"]
|
||||
for root in map(Path, roots):
|
||||
if not root.is_absolute() or not root.is_dir():
|
||||
raise ValueError(f"The TeX resource directory is unavailable: {root}")
|
||||
return roots
|
||||
|
||||
|
||||
def prepare(args):
|
||||
repo = Path(__file__).resolve().parent.parent
|
||||
target = repo / os.environ.get("CARGO_TARGET_DIR", "target") / "release"
|
||||
texrra = (args.texrra or target / binary_name("texrra")).resolve(strict=True)
|
||||
editor = (args.editor or target / "examples" / binary_name("native_editor")).resolve(strict=True)
|
||||
saved_format = args.format_image.resolve(strict=True) if args.format_image else None
|
||||
saved_resources = args.format_resources.resolve(strict=True) if args.format_resources else None
|
||||
if saved_format:
|
||||
roots_from_manifest(saved_resources)
|
||||
else:
|
||||
ini = (args.ini or installed_path(FORMAT_ENTRIES[args.input_profile])).resolve(strict=True)
|
||||
roots = args.texmf_root or [
|
||||
installed_path("-var-value=TEXMFDIST"),
|
||||
installed_path("-var-value=TEXMFSYSVAR"),
|
||||
]
|
||||
roots = [root.resolve(strict=True) for root in roots]
|
||||
|
||||
bundle = args.bundle.resolve()
|
||||
bundle.mkdir(parents=True)
|
||||
format_path = bundle / FORMAT
|
||||
resources = bundle / RESOURCES
|
||||
if saved_format:
|
||||
shutil.copyfile(saved_format, format_path)
|
||||
shutil.copyfile(saved_resources, resources)
|
||||
else:
|
||||
env = {**os.environ, "TEXRRA_STATE_DIR": str(bundle / "state")}
|
||||
command = native_command(texrra, args.input_profile, roots)
|
||||
with (bundle / "format.log").open("wb") as log:
|
||||
subprocess.run(
|
||||
[*command, "--initex", "--dump-format", "--resource-manifest", resources,
|
||||
ini, format_path],
|
||||
env=env, cwd=bundle, stdout=log, stderr=subprocess.STDOUT, check=True,
|
||||
)
|
||||
roots = roots_from_manifest(resources)
|
||||
|
||||
binary_dir = bundle / "bin"
|
||||
binary_dir.mkdir()
|
||||
texrra_path = binary_dir / binary_name("texrra")
|
||||
editor_path = binary_dir / binary_name("native_editor")
|
||||
shutil.copy2(texrra, texrra_path)
|
||||
shutil.copy2(editor, editor_path)
|
||||
shutil.copy2(Path(__file__), bundle / "native-beta.py")
|
||||
example = bundle / "a4-project"
|
||||
shutil.copytree(repo / "examples/native-format-stage/a4-project", example)
|
||||
files = [texrra_path, editor_path, format_path, resources, example / "main.tex",
|
||||
example / "chapter.tex"]
|
||||
receipt = {
|
||||
"input_profile": args.input_profile,
|
||||
"texmf_roots": roots,
|
||||
"environment": {name: os.environ[name] for name in RESOURCE_ENV if name in os.environ},
|
||||
"sha256": {path.relative_to(bundle).as_posix(): digest(path) for path in files},
|
||||
}
|
||||
(bundle / MANIFEST).write_text(json.dumps(receipt, indent=2) + "\n", encoding="utf-8")
|
||||
print(f"Beta bundle ready at {bundle}")
|
||||
print(f"Start the sample with python3 {shlex.quote(str(bundle / 'native-beta.py'))} open")
|
||||
|
||||
|
||||
def launch(args):
|
||||
bundle = Path(__file__).resolve().parent
|
||||
manifest = json.loads((bundle / MANIFEST).read_text(encoding="utf-8"))
|
||||
roots = roots_from_manifest(bundle / RESOURCES)
|
||||
if roots != manifest["texmf_roots"]:
|
||||
raise ValueError("The format resource roots differ from the prepared bundle")
|
||||
env = os.environ.copy()
|
||||
for name in (*RESOURCE_ENV, "TEXRRA_TEXMF"):
|
||||
env.pop(name, None)
|
||||
env.update(manifest["environment"])
|
||||
env["TEXRRA_STATE_DIR"] = str(bundle / "state")
|
||||
profile = manifest["input_profile"]
|
||||
if args.action == "open":
|
||||
source = args.entry.resolve(strict=True) if args.entry else bundle / "a4-project/main.tex"
|
||||
assets = args.file if args.entry else ["chapter.tex", *args.file]
|
||||
runs = bundle / "runs"
|
||||
if args.run_dir:
|
||||
run = args.run_dir.resolve()
|
||||
else:
|
||||
runs.mkdir(exist_ok=True)
|
||||
run = Path(tempfile.mkdtemp(prefix="editor-", dir=runs)) / "project"
|
||||
command = [
|
||||
bundle / "bin" / binary_name("native_editor"), "--input-profile", profile,
|
||||
source, bundle / FORMAT, bundle / RESOURCES, run, str(args.port), *assets,
|
||||
]
|
||||
else:
|
||||
command = native_command(bundle / "bin" / binary_name("texrra"), profile, roots)
|
||||
command.extend(["--load-format", bundle / FORMAT, args.entry.resolve(strict=True),
|
||||
args.output.resolve()])
|
||||
os.execve(command[0], command, env)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
commands = parser.add_subparsers(dest="action", required=True)
|
||||
|
||||
setup = commands.add_parser("prepare", help="create a new local Beta directory")
|
||||
setup.add_argument("bundle", type=Path)
|
||||
setup.add_argument("--texrra", type=Path)
|
||||
setup.add_argument("--editor", type=Path)
|
||||
setup.add_argument("--input-profile", choices=FORMAT_ENTRIES, default="tex8bit")
|
||||
setup.add_argument("--texmf-root", action="append", type=Path)
|
||||
setup.add_argument("--ini", type=Path)
|
||||
setup.add_argument("--format-image", type=Path)
|
||||
setup.add_argument("--format-resources", type=Path)
|
||||
|
||||
editor = commands.add_parser("open", help="open a project or the bundled A4 sample")
|
||||
editor.add_argument("entry", type=Path, nargs="?")
|
||||
editor.add_argument("--file", action="append", default=[], help="path relative to the entry")
|
||||
editor.add_argument("--run-dir", type=Path)
|
||||
editor.add_argument("--port", type=int, default=0)
|
||||
|
||||
compiler = commands.add_parser("compile", help="produce a PDF with the bundle's native CLI")
|
||||
compiler.add_argument("entry", type=Path)
|
||||
compiler.add_argument("output", type=Path)
|
||||
|
||||
args = parser.parse_args()
|
||||
if args.action == "prepare":
|
||||
if bool(args.format_image) != bool(args.format_resources):
|
||||
setup.error("--format-image and --format-resources must be supplied together")
|
||||
if args.format_image and (args.ini or args.texmf_root):
|
||||
setup.error("--ini and --texmf-root apply when creating a format")
|
||||
try:
|
||||
if args.action == "prepare":
|
||||
prepare(args)
|
||||
else:
|
||||
launch(args)
|
||||
except subprocess.CalledProcessError as error:
|
||||
log = args.bundle.resolve() / "format.log"
|
||||
context = f". See {log}" if log.is_file() else ""
|
||||
parser.exit(1, f"Beta preparation failed (exit {error.returncode}){context}\n")
|
||||
except (OSError, ValueError, KeyError) as error:
|
||||
parser.exit(1, f"{error}\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user