brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.3 KiB · f5b9dcf Raw
48 lines · python
1#!/usr/bin/env python2"""A command line utility to merge two JSON files.3 4This is a python program that merges two JSON files into a single one. The5intended use for this is to combine generated 'compile_commands.json' files6created by CMake when performing an LLVM runtime build.7"""8 9import argparse10import json11import sys12 13 14def main():15    parser = argparse.ArgumentParser(description=__doc__)16    parser.add_argument(17        "-o",18        type=str,19        help="The output file to write JSON data to",20        default=None,21        nargs="?",22    )23    parser.add_argument(24        "json_files", type=str, nargs="+", help="Input JSON files to merge"25    )26    args = parser.parse_args()27 28    merged_data = []29 30    for json_file in args.json_files:31        try:32            with open(json_file, "r") as f:33                data = json.load(f)34                merged_data.extend(data)35        except (IOError, json.JSONDecodeError) as e:36            continue37 38    # Deduplicate by converting each entry to a tuple of sorted key-value pairs39    unique_data = list({json.dumps(entry, sort_keys=True) for entry in merged_data})40    unique_data = [json.loads(entry) for entry in unique_data]41 42    with open(args.o, "w") as f:43        json.dump(unique_data, f, indent=2)44 45 46if __name__ == "__main__":47    main()48