Replace occurrences of a string in a file. The updated file contents are written out in place.
(p: pathlib.Path, search, replace)
| 291 | |
| 292 | |
| 293 | def static_replace_in_file(p: pathlib.Path, search, replace): |
| 294 | """Replace occurrences of a string in a file. |
| 295 | |
| 296 | The updated file contents are written out in place. |
| 297 | """ |
| 298 | |
| 299 | with p.open("rb") as fh: |
| 300 | data = fh.read() |
| 301 | |
| 302 | # Build should be as deterministic as possible. Assert that wanted changes |
| 303 | # actually occur. |
| 304 | if search not in data: |
| 305 | raise NoSearchStringError("search string (%s) not in %s" % (search, p)) |
| 306 | |
| 307 | log("replacing `%s` with `%s` in %s" % (search, replace, p)) |
| 308 | data = data.replace(search, replace) |
| 309 | |
| 310 | with p.open("wb") as fh: |
| 311 | fh.write(data) |
| 312 | |
| 313 | |
| 314 | def apply_source_patch(cpython_source_path: pathlib.Path, patch_path: pathlib.Path): |
no test coverage detected