ffi_installed.py (8809B)
1 """Install an owned candidate and verify native inputs without building them.""" 2 3 from __future__ import annotations 4 5 import hashlib 6 import json 7 import os 8 import shutil 9 import sys 10 import tempfile 11 from pathlib import Path 12 from typing import Any 13 14 import ffi_artifacts as artifacts 15 import ffi_provenance as provenance 16 import ffi_source as source 17 import ffi_notices as notices 18 import package_contract as contract 19 20 MANIFEST = "TeraFFI/provenance.json" 21 LOCK = "TeraFFI/source.lock" 22 23 24 def installed_paths() -> dict[str, str]: 25 result = {} 26 for relative in sorted(artifacts.expected_paths()): 27 if relative == notices.ARTIFACT_PATH: 28 result[relative] = notices.INSTALLED_PATH 29 elif relative.startswith(artifacts.FRAMEWORK + "/"): 30 result[relative] = "Tera/Frameworks/" + relative 31 elif relative == "generated/TeraKitBindings.swift": 32 result[relative] = "Tera/Generated/TeraKitBindings.swift" 33 elif relative.startswith(("api/", "source/")) or relative == "abi_symbols.json": 34 result[relative] = "TeraFFI/" + relative 35 return result 36 37 38 def installed_manifest(candidate: dict[str, Any]) -> dict[str, Any]: 39 paths = installed_paths() 40 files = [ 41 {**item, "path": paths[item["path"]]} 42 for item in candidate["files"] 43 if item["path"] in paths 44 ] 45 return { 46 "schema": "tera.installed-native-artifacts.v2", 47 "candidate": candidate, 48 "files": sorted(files, key=lambda item: item["path"]), 49 "disposition": "local_installed_not_release_qualified", 50 } 51 52 53 def encoded_lock( 54 manifest: dict[str, Any], foundation: dict[str, Any], epoch: int 55 ) -> bytes: 56 producer = manifest["candidate"]["source"] 57 values = { 58 "schema": "tera.installed-source.v2", 59 "repository": producer["repository"], 60 "source_tree": producer["tree"], 61 "manifest_sha256": hashlib.sha256(provenance.encoded(manifest)).hexdigest(), 62 "source_date_epoch": epoch, 63 } 64 text = "".join(f"{key} = {json.dumps(value)}\n" for key, value in values.items()) 65 text += "\n[foundation]\n" 66 text += "".join( 67 f"{key} = {json.dumps(foundation[key])}\n" 68 for key in ("repository", "revision", "version") 69 ) 70 return text.encode() 71 72 73 def verify_files(root: Path, manifest: dict[str, Any]) -> None: 74 expected = installed_manifest(manifest["candidate"]) 75 if manifest != expected: 76 raise source.ProvenanceError("installed artifact manifest differs") 77 notices.verify_material( 78 root / notices.INSTALLED_PATH, manifest["candidate"]["redistribution"] 79 ) 80 notices.verify_inventory( 81 manifest["files"], 82 manifest["candidate"]["redistribution"], 83 notices.INSTALLED_PATH, 84 ) 85 paths = set(installed_paths().values()) 86 if {item["path"] for item in manifest["files"]} != paths: 87 raise source.ProvenanceError("installed artifact inventory differs") 88 for item in manifest["files"]: 89 if artifacts.file_record(root, item["path"]) != item: 90 raise source.ProvenanceError("installed artifact bytes are stale") 91 verify_directory_inventory(root, paths) 92 93 94 def verify_directory_inventory(root: Path, paths: set[str]) -> None: 95 for relative in ( 96 "Tera/Generated", 97 "Tera/Frameworks/TeraFFI.xcframework", 98 "TeraFFI/api", 99 "TeraFFI/source", 100 ): 101 observed = set() 102 for path in (root / relative).rglob("*"): 103 if path.is_symlink(): 104 raise source.ProvenanceError( 105 "installed artifact inventory contains a symlink" 106 ) 107 if path.is_file(): 108 observed.add(path.relative_to(root).as_posix()) 109 if observed != {path for path in paths if path.startswith(relative + "/")}: 110 raise source.ProvenanceError("installed artifact inventory differs") 111 112 113 def check(root: Path) -> dict[str, Any]: 114 manifest = contract._read_json(root / MANIFEST) 115 verify_files(root, manifest) 116 config = source.producer_contract(root) 117 snapshot = source.source_snapshot(root, config["source_inputs"]) 118 foundation = contract._read_toml(root / config["foundation_lock"]) 119 candidate = manifest["candidate"] 120 notices.verify_expected( 121 candidate["redistribution"], foundation, config["redistribution"] 122 ) 123 if candidate["source"] != { 124 "repository": config["repository"], 125 "tree": snapshot["tree"], 126 }: 127 raise source.ProvenanceError("installed producer source is stale") 128 for target in artifacts.TARGETS: 129 record = contract._read_json(root / "TeraFFI/source" / f"{target}.json") 130 if ( 131 record["source"] != snapshot 132 or record["foundation"] != foundation 133 or record["build"]["target"] != target 134 or record["redistribution"] != candidate["redistribution"] 135 ): 136 raise source.ProvenanceError("installed producer tuple differs") 137 if contract._read_regular(root / LOCK) != encoded_lock( 138 manifest, foundation, config["build"]["source_date_epoch"] 139 ): 140 raise source.ProvenanceError("installed producer lock is stale") 141 return manifest 142 143 144 def install( 145 root: Path, candidate_root: Path, records: dict[str, dict[str, Any]] 146 ) -> None: 147 if not os.environ.get("EXT_BUILD_RUN_ACTIVE"): 148 raise source.ProvenanceError("native installation requires extbuild") 149 local_destination(root, "TeraFFI") 150 candidate = artifacts.check(candidate_root, records) 151 manifest = installed_manifest(candidate) 152 config = source.producer_contract(root) 153 foundation = contract._read_toml(root / config["foundation_lock"]) 154 # Final-directory swaps stay on the repository filesystem. A failed install 155 # restores every prior directory/file, and publishes its manifest last. 156 with tempfile.TemporaryDirectory( 157 prefix=".install-", dir=root / "TeraFFI" 158 ) as temporary: 159 staging = Path(temporary) / "next" 160 for relative, destination in installed_paths().items(): 161 path = staging / destination 162 path.parent.mkdir(parents=True, exist_ok=True) 163 shutil.copyfile(candidate_root / relative, path) 164 (staging / MANIFEST).write_bytes(provenance.encoded(manifest)) 165 (staging / LOCK).write_bytes( 166 encoded_lock(manifest, foundation, config["build"]["source_date_epoch"]) 167 ) 168 verify_files(staging, manifest) 169 replace_installation(root, staging, Path(temporary) / "previous") 170 print( 171 "owned native candidate installed and source-verified; not release qualification" 172 ) 173 174 175 def local_destination(root: Path, relative: str) -> Path: 176 current = root 177 for part in Path(relative).parts: 178 current = current / part 179 if current.is_symlink(): 180 raise source.ProvenanceError("native installation path contains a symlink") 181 return current 182 183 184 def replace_installation(root: Path, staging: Path, previous: Path) -> None: 185 paths = ( 186 "Tera/Generated", 187 "Tera/Frameworks/TeraFFI.xcframework", 188 "TeraFFI/api", 189 "TeraFFI/source", 190 "TeraFFI/abi_symbols.json", 191 notices.INSTALLED_PATH, 192 LOCK, 193 MANIFEST, 194 ) 195 destinations = {relative: local_destination(root, relative) for relative in paths} 196 changed = [] 197 try: 198 for relative in paths: 199 destination = destinations[relative] 200 backup = previous / relative 201 destination.parent.mkdir(parents=True, exist_ok=True) 202 existed = destination.exists() 203 if existed: 204 backup.parent.mkdir(parents=True, exist_ok=True) 205 destination.rename(backup) 206 changed.append((relative, existed)) 207 (staging / relative).rename(destination) 208 check(root) 209 except Exception: 210 for relative, existed in reversed(changed): 211 destination = root / relative 212 if destination.is_dir(): 213 shutil.rmtree(destination) 214 else: 215 destination.unlink(missing_ok=True) 216 if existed: 217 (previous / relative).rename(destination) 218 raise 219 220 221 def main() -> int: 222 root = Path(__file__).resolve().parent.parent 223 try: 224 check(root) 225 except ( 226 source.ProvenanceError, 227 contract.PackageContractError, 228 OSError, 229 ValueError, 230 KeyError, 231 TypeError, 232 ) as error: 233 print( 234 f"installed native artifacts: {error}; run make ffi-bootstrap", 235 file=sys.stderr, 236 ) 237 return 1 238 print( 239 "installed native artifacts match the owned source tree and exact foundation lock" 240 ) 241 return 0 242 243 244 if __name__ == "__main__": 245 raise SystemExit(main())