#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """Copy the approved offline reader edition into the app, preserving its relative links.""" import os import json from pathlib import Path import shutil import sys from tempfile import TemporaryDirectory TOOLS = Path(__file__).resolve().parents[3] / "tools" sys.path.insert(0, str(TOOLS)) sys.path.insert(0, str(TOOLS / "manuals")) from build_content import content_profile from lookup_resources import build_resources def read_regular(root, relative): path = Path(relative) if path.is_absolute() or ".." in path.parts: raise ValueError("Resource input must be repository-relative") source = root / path if not source.is_file() or any(p.is_symlink() for p in (source, *source.parents) if p != root and root in p.parents): raise ValueError("Resource input must be a regular, unlinked file: " + relative) return source.read_bytes() def install_files(destination, files): if destination.is_symlink(): raise ValueError("Resource destination must not be linked") destination.parent.mkdir(parents=True, exist_ok=True) with TemporaryDirectory(dir=destination.parent) as temporary: staged = Path(temporary) / destination.name staged.mkdir() for name, data in files.items(): relative = Path(name) if relative.is_absolute() or ".." in relative.parts: raise ValueError("Invalid resource output") target = staged / relative target.parent.mkdir(parents=True, exist_ok=True) target.write_bytes(data) # The app is a build product, never a user's reader cache or documents. if destination.exists(): shutil.rmtree(destination) staged.rename(destination) def stage(project, resources, profile): project = project.resolve() root = project.parent.parent if resources.parts[-2:] != ("Contents", "Resources") or resources.parent.parent.suffix != ".app": raise ValueError("Expected an app Contents/Resources destination") if any(p.is_symlink() for p in (resources, resources.parent, resources.parent.parent)): raise ValueError("App resource destination must not be linked") marker = resources / "BuildContent.json" for target in (resources / "ManualLibrary", resources / "RuntimeTemplates", marker): if target.is_symlink(): raise ValueError("Resource destination must not be linked") if marker.exists() and (not marker.is_file() or marker.stat().st_nlink != 1): raise ValueError("Build content marker must be a regular, unlinked file") if profile == "normal": prefix = "$(SRCROOT)/../../docs/manuals/" files = {} manifest = project / "Tools/ManualLibrary.xcfilelist" for entry in manifest.read_text().splitlines(): if not entry.startswith(prefix): raise ValueError("Unexpected manual input: " + entry) name = entry[len(prefix):] files[name] = read_regular(root, "docs/manuals/" + name) elif profile == "source": prefix = "docs/manuals/" generated = build_resources(lambda path: read_regular(root, path)) if any(not path.startswith(prefix) for path in generated): raise ValueError("Unexpected lookup resource") files = {path[len(prefix):]: data for path, data in generated.items()} else: raise ValueError("Unsupported build content profile") runtime = json.loads(read_regular(root, "platforms/macos/Resources/RuntimeTemplates.json")) if runtime.get("schemaVersion") != 1 or set(runtime.get("profiles", {})) != {"normal", "source"}: raise ValueError("Unsupported runtime-template manifest") templates = {name: read_regular(root, path) for name, path in runtime["profiles"][profile].items()} if set(templates) != {"PROJECT_SETUP_PREAMBLE.txt"} or not templates["PROJECT_SETUP_PREAMBLE.txt"].strip(): raise ValueError("Missing runtime project setup prompt") install_files(resources / "ManualLibrary", files) install_files(resources / "RuntimeTemplates", templates) with TemporaryDirectory(dir=resources) as temporary: staged_marker = Path(temporary) / marker.name staged_marker.write_text(json.dumps({"schemaVersion": 1, "profile": profile, "manuals": "full" if profile == "normal" else "lookup"}, sort_keys=True) + "\n") staged_marker.replace(marker) def main(): project = Path(os.environ["SRCROOT"]) resources = Path(os.environ["TARGET_BUILD_DIR"]) / os.environ["UNLOCALIZED_RESOURCES_FOLDER_PATH"] stage(project, resources, content_profile()) if __name__ == "__main__": main()