#!/usr/bin/env python3 """ compare_names.py Compares the `deviceName` values from File 1 with the `name` values from File 2. If a `deviceName` does not appear in File 2, it is written to *missing.txt*. """ import json import argparse import sys from pathlib import Path def load_json(path: Path): """Return the parsed JSON data from *path*.""" try: with path.open("r", encoding="utf-8") as f: return json.load(f) except FileNotFoundError: print(f"❌ File not found: {path}", file=sys.stderr) sys.exit(1) except json.JSONDecodeError as exc: print(f"❌ Invalid JSON in {path}:\n{exc}", file=sys.stderr) sys.exit(1) def extract_device_names_from_file1(data): """Return a list of deviceName values from the File 1 structure.""" names = [] for mac, obj in data.items(): # Some devices may not have a name; skip them quietly. name = obj.get("deviceName") if isinstance(name, str): names.append(name) return names def extract_names_from_file2(data): """Return a set of name values from the File 2 list.""" names = set() for entry in data: name = entry.get("name") if isinstance(name, str): names.add(name) return names def main(file1_path: Path, file2_path: Path, output_path: Path): # Load data data1 = load_json(file1_path) data2 = load_json(file2_path) # Pull out the two collections we care about names_in_file1 = extract_device_names_from_file1(data1) names_in_file2 = extract_names_from_file2(data2) # Find names that are missing from File 2 missing = [name for name in names_in_file1 if name not in names_in_file2] # Output if missing: print(f"⚠️ Found {len(missing)} device(s) whose name is missing in File 2.") # Write to a text file – one name per line with output_path.open("w", encoding="utf-8") as out: out.write("\n".join(missing)) print(f"✔️ Missing names written to {output_path}") else: print("✅ All deviceNames from File 1 are present in File 2.") if __name__ == "__main__": parser = argparse.ArgumentParser( description="Find deviceName values in File 1 that are not present in File 2." ) parser.add_argument("file1", type=Path, help="Path to File 1 (JSON dict).") parser.add_argument("file2", type=Path, help="Path to File 2 (JSON list).") parser.add_argument( "-o", "--output", type=Path, default=Path("missing.txt"), help="Where to write the list of missing names (default: missing.txt)", ) args = parser.parse_args() main(args.file1, args.file2, args.output)