diff --git a/acceptance/bin/update_file.py b/acceptance/bin/update_file.py index ef643239533..8a69d5f5fef 100755 --- a/acceptance/bin/update_file.py +++ b/acceptance/bin/update_file.py @@ -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 @@ -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)