Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions acceptance/bin/update_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
Replace all strings OLD with NEW in FILENAME.

If OLD is not found in FILENAME, the script reports error.

The file is read and written as bytes so that only the matched bytes change.
Text mode on Windows rewrites every line ending as CRLF, which changes the
file's content hash and upload payload.
"""

import sys
Expand All @@ -20,9 +24,19 @@
# call update_file.py on that file.
assert filename != "output.txt"

data = open(filename).read()
newdata = data.replace(old, new)
with open(filename, "rb") as fobj:
data = fobj.read()

old_bytes = old.encode()
new_bytes = new.encode()

# Match the file's line endings so multi-line OLD/NEW work on CRLF checkouts.
if b"\r\n" in data:
old_bytes = old_bytes.replace(b"\r\n", b"\n").replace(b"\n", b"\r\n")
new_bytes = new_bytes.replace(b"\r\n", b"\n").replace(b"\n", b"\r\n")

newdata = data.replace(old_bytes, new_bytes)
if newdata == data:
sys.exit(f"{old=} not found in {filename=}\n{data}")
with open(filename, "w") as fobj:
sys.exit(f"{old=} not found in {filename=}\n{data.decode(errors='replace')}")
with open(filename, "wb") as fobj:
fobj.write(newdata)
Loading