INNER CODE UNIT · Python
patch_file
mumu-lhl/Ciyue · tools/patch_wpe_sandbox_paths.py:19
def patch_file(path: Path) -> bool:
path = path.resolve()
original = path.read_bytes()
content = bytearray(original)
modified = False
for old, new in REPLACEMENTS:
assert len(old) == len(new), f"Replacement length mismatch: {old!r} vs {new!r}"
count = content.count(old)
if count > 0:
content = content.replace(old, new)
modified = True
print(f"Replaced {count} occurrence(s) of {old.decode('latin1', errors='replace')} in {path.name}")
if modified:
assert len(content) == len(original), f"File size changed for {path.name}"
assert b"/usr/sbin/bwrap\0" not in content
assert b"/usr/sbin/xdg-dbus-proxy\0" not in content