From 91d0df1985e427d4f82cf13306db08c2b59dce81 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 2 Nov 2023 22:26:26 +0000 Subject: [PATCH] Don't write or reload if there's no change --- create-nginx-map.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/create-nginx-map.py b/create-nginx-map.py index 75bb265..34af038 100644 --- a/create-nginx-map.py +++ b/create-nginx-map.py @@ -1,5 +1,6 @@ from typing import NamedTuple import re +from io import StringIO import sys import tomllib from pathlib import Path @@ -103,12 +104,21 @@ def main(): for route in routes: grouped_routes[route.hostname, route.destination].add(route.name) - with args.output.open("w", buffering=1024 * 1024) as f: - for (hostname, destination), names in sorted(grouped_routes.items()): - print(f"{hostname}\t{destination}; # {', '.join(names)}", file=f) - print("Found", len(routes), "routes, grouped into", len(grouped_routes), "groups") + output = StringIO() + for (hostname, destination), names in sorted(grouped_routes.items()): + output.write(f"{hostname}\t{destination}; # {', '.join(names)}\n") + + if args.output.is_file(): + current_output = args.output.read_text() + + if current_output == output.getvalue(): + print("Output identical - aborting") + return + + args.output.write_text(output.getvalue()) + if args.reload: print("Reloading nginx") subprocess.check_call(