Skip to content

Commit

Permalink
massage file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mpw96 committed Jun 30, 2024
1 parent e541a88 commit fc1267f
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions pio-scripts/patch_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ def replaceInFile(in_file, out_file, text, subs, flags=0):
Taken from https://www.studytonight.com/python-howtos/search-and-replace-a-text-in-a-file-in-python
"""
if os.path.exists(in_file):
with open(in_file, "rb") as infile:
with open(out_file, "wb") as outfile:
#read the file contents
file_contents = infile.read()
text_pattern = re.compile(re.escape(text), flags)
file_contents = text_pattern.sub(subs, file_contents.decode('utf-8'))
outfile.seek(0)
outfile.truncate()
outfile.write(file_contents.encode())
#read the file contents
with open(in_file, "r", encoding="utf-8") as infile:
file_contents = infile.read()

# do replacement
text_pattern = re.compile(re.escape(text), flags)
file_contents = text_pattern.sub(subs, file_contents)

# write the result
with open(out_file, "w", encoding="utf-8") as outfile:
outfile.write(file_contents)

def main():
if (env.GetProjectOption('custom_patches', '') == ''):
Expand Down

0 comments on commit fc1267f

Please sign in to comment.