From bdf120016ac8e0dab0e4631c69fea5095ed24990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Andr=C3=A9=20Vadla=20Ravn=C3=A5s?= Date: Mon, 2 Dec 2024 19:41:21 +0100 Subject: [PATCH] api: Avoid redundant writes to output files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Modify the output logic to skip writing to files if the contents remain unchanged, preventing unnecessary file updates. This means quicker incremental builds. Co-authored-by: Håvard Sørbø --- src/api/generate.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/api/generate.py b/src/api/generate.py index 4152e37be..9978b7b4b 100644 --- a/src/api/generate.py +++ b/src/api/generate.py @@ -1,6 +1,7 @@ from __future__ import annotations import argparse from dataclasses import dataclass +from io import StringIO from pathlib import Path import re from typing import List, Set @@ -86,7 +87,7 @@ def main(): emit_vapi(api, output_dir) def emit_header(api, output_dir): - with (output_dir / 'frida-core.h').open("w", encoding='utf-8') as output_header_file: + with OutputFile(output_dir / 'frida-core.h') as output_header_file: output_header_file.write("#ifndef __FRIDA_CORE_H__\n#define __FRIDA_CORE_H__\n\n") output_header_file.write("#include \n#include \n#include \n#include \n") @@ -214,13 +215,14 @@ def merge_and_transform_elements(tag_name: str, spec_set: Set[str]): result = ET.tostring(merged_root, encoding="unicode", xml_declaration=True) - (output_dir / f"Frida-{api.version}.gir").write_text(result, encoding='utf-8') + with OutputFile(output_dir / f"Frida-{api.version}.gir") as output_gir: + output_gir.write(result) def filter_elements(elements: List[ET.Element], spec_set: Set[str]): return [elem for elem in elements if elem.get("name") in spec_set] def emit_vapi(api, output_dir): - with (output_dir / f"frida-core-{api.version}.vapi").open("w", encoding='utf-8') as output_vapi_file: + with OutputFile(output_dir / f"frida-core-{api.version}.vapi") as output_vapi_file: output_vapi_file.write("[CCode (cheader_filename = \"frida-core.h\", cprefix = \"Frida\", lower_case_cprefix = \"frida_\")]") output_vapi_file.write("\nnamespace Frida {") output_vapi_file.write("\n\tpublic static void init ();") @@ -258,7 +260,7 @@ def emit_vapi(api, output_dir): output_vapi_file.write("\n}\n") - with (output_dir / f"frida-core-{api.version}.deps").open("w", encoding='utf-8') as output_deps_file: + with OutputFile(output_dir / f"frida-core-{api.version}.deps") as output_deps_file: output_deps_file.write("glib-2.0\n") output_deps_file.write("gobject-2.0\n") output_deps_file.write("gio-2.0\n") @@ -581,6 +583,23 @@ def fuzzysort(items, keys): result.extend(remaining) return result +class OutputFile: + def __init__(self, output_path): + self._output_path = output_path + self._io = StringIO() + + def __enter__(self): + return self._io + + def __exit__(self, *exc): + result = self._io.getvalue() + if self._output_path.exists(): + existing_contents = self._output_path.read_text(encoding='utf-8') + if existing_contents == result: + return False + self._output_path.write_text(result, encoding='utf-8') + return False + if __name__ == '__main__': main()