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(