From fc1267fe5532813c8d20265f8848d5a9324dec87 Mon Sep 17 00:00:00 2001 From: Marc-Philip Date: Sun, 30 Jun 2024 18:48:55 +0200 Subject: [PATCH 1/3] massage file handling --- pio-scripts/patch_apply.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pio-scripts/patch_apply.py b/pio-scripts/patch_apply.py index 5734c4aa0..0091f3c22 100644 --- a/pio-scripts/patch_apply.py +++ b/pio-scripts/patch_apply.py @@ -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', '') == ''): From 1d92d9ed08ab9c275d5d72caa69fd9684d17b34a Mon Sep 17 00:00:00 2001 From: Marc-Philip Date: Sun, 30 Jun 2024 20:46:12 +0200 Subject: [PATCH 2/3] fix comment --- pio-scripts/patch_apply.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pio-scripts/patch_apply.py b/pio-scripts/patch_apply.py index 0091f3c22..a15ae3433 100644 --- a/pio-scripts/patch_apply.py +++ b/pio-scripts/patch_apply.py @@ -23,10 +23,8 @@ def is_tool(name): return which(name) is not None def replaceInFile(in_file, out_file, text, subs, flags=0): - """ - Function for replacing content for the given file - Taken from https://www.studytonight.com/python-howtos/search-and-replace-a-text-in-a-file-in-python - """ + """Function for replacing content for the given file.""" + if os.path.exists(in_file): #read the file contents with open(in_file, "r", encoding="utf-8") as infile: From 5ee411fcc6c64e6b39cacf4356645de6b883d699 Mon Sep 17 00:00:00 2001 From: Marc-Philip Date: Mon, 1 Jul 2024 06:53:10 +0200 Subject: [PATCH 3/3] Update patch_apply.py --- pio-scripts/patch_apply.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pio-scripts/patch_apply.py b/pio-scripts/patch_apply.py index a15ae3433..5c77f6839 100644 --- a/pio-scripts/patch_apply.py +++ b/pio-scripts/patch_apply.py @@ -26,7 +26,7 @@ def replaceInFile(in_file, out_file, text, subs, flags=0): """Function for replacing content for the given file.""" if os.path.exists(in_file): - #read the file contents + # read the file contents with open(in_file, "r", encoding="utf-8") as infile: file_contents = infile.read()