dc05456
#!/usr/bin/env python3
dc05456
#
dc05456
# This script removes the given package entries in the bundled_packages JSON files. This is used by the PluginInstall
dc05456
# CMake module.
dc05456
#
dc05456
dc05456
import argparse
dc05456
import collections
dc05456
import json
dc05456
import os
dc05456
import sys
dc05456
dc05456
dc05456
def find_json_files(work_dir: str) -> list:
dc05456
    """Finds all JSON files in the given directory recursively and returns a list of those files in absolute paths.
dc05456
dc05456
    :param work_dir: The directory to look for JSON files recursively.
dc05456
    :return: A list of JSON files in absolute paths that are found in the given directory.
dc05456
    """
dc05456
dc05456
    json_file_list = []
dc05456
    for root, dir_names, file_names in os.walk(work_dir):
dc05456
        for file_name in file_names:
dc05456
            abs_path = os.path.abspath(os.path.join(root, file_name))
dc05456
            json_file_list.append(abs_path)
dc05456
    return json_file_list
dc05456
dc05456
dc05456
def remove_entries_from_json_file(file_path: str, entries: list) -> None:
dc05456
    """Removes the given entries from the given JSON file. The file will modified in-place.
dc05456
dc05456
    :param file_path: The JSON file to modify.
dc05456
    :param entries: A list of strings as entries to remove.
dc05456
    :return: None
dc05456
    """
dc05456
dc05456
    try:
dc05456
        with open(file_path, "r", encoding = "utf-8") as f:
dc05456
            package_dict = json.load(f, object_hook = collections.OrderedDict)
dc05456
    except Exception as e:
dc05456
        msg = "Failed to load '{file_path}' as a JSON file. This file will be ignored Exception: {e}"\
dc05456
            .format(file_path = file_path, e = e)
dc05456
        sys.stderr.write(msg + os.linesep)
dc05456
        return
dc05456
dc05456
    for entry in entries:
dc05456
        if entry in package_dict:
dc05456
            del package_dict[entry]
dc05456
            print("[INFO] Remove entry [{entry}] from [{file_path}]".format(file_path = file_path, entry = entry))
dc05456
dc05456
    try:
dc05456
        with open(file_path, "w", encoding = "utf-8", newline = "\n") as f:
dc05456
            json.dump(package_dict, f, indent = 4)
dc05456
    except Exception as e:
dc05456
        msg = "Failed to write '{file_path}' as a JSON file. Exception: {e}".format(file_path = file_path, e = e)
dc05456
        raise IOError(msg)
dc05456
dc05456
dc05456
def main() -> None:
dc05456
    parser = argparse.ArgumentParser("mod_bundled_packages_json")
dc05456
    parser.add_argument("-d", "--dir", dest = "work_dir",
dc05456
                        help = "The directory to look for bundled packages JSON files, recursively.")
dc05456
    parser.add_argument("entries", metavar = "ENTRIES", type = str, nargs = "+")
dc05456
dc05456
    args = parser.parse_args()
dc05456
dc05456
    json_file_list = find_json_files(args.work_dir)
dc05456
    for json_file_path in json_file_list:
dc05456
        remove_entries_from_json_file(json_file_path, args.entries)
dc05456
dc05456
dc05456
if __name__ == "__main__":
dc05456
    main()