# coding=utf-8 import importlib import json import os.path import sys SCAN_MODULES = ('_winapi', 'ctypes', 'errno', 'os', 'select', 'signal', 'socket', 'mmap', 'msilib', 'msvcrt', 'multiprocessing', 'winsound', 'winreg') DUMP_FILE_NAME = 'winsdk_dump.json' VERSION_KEY = '0' def get_dict(): def get_constant_list(module_name): module = importlib.import_module(module_name) return [s for s in dir(module) if s.isupper()] dic = dict() # use '0' to represent python version string dic[VERSION_KEY] = sys.version for module in SCAN_MODULES: dic[module] = get_constant_list(module) return dic def dump(): dic = get_dict() with open(DUMP_FILE_NAME, 'w', encoding='utf-8') as f: json.dump(dic, f, indent=2) msg = 'Constants of "%s" has been dump to %s' % \ (dic[VERSION_KEY], DUMP_FILE_NAME) print(msg) def load(): with open(DUMP_FILE_NAME, encoding='utf-8') as f: dic = json.load(f) return dic def compare(): if not os.path.isfile(DUMP_FILE_NAME): print('File %s does not exist.' % DUMP_FILE_NAME) return current_dict = get_dict() file_dict = load() assert current_dict.keys() == file_dict.keys(),\ "Modules list are different, maybe SCAN_MODULES has been changed." msg = "Comparing from A to B:\nA: %s\nB: %s\n" % \ (file_dict[VERSION_KEY], current_dict[VERSION_KEY]) print(msg) count = 0 for k, v in file_dict.items(): if k == VERSION_KEY: continue old_set = set(v) new_set = set(current_dict[k]) if old_set == new_set: continue added = new_set.difference(old_set) removed = old_set.difference(new_set) if added: print('%s added %d constants: %s' % (k, len(added), added)) if removed: print('%s removed %d constants: %s' % (k, len(removed), removed)) count += 1 print() print('Finished, %d modules changed constants.' % count) def main(): if len(sys.argv) == 2 and sys.argv[1].lower() == 'dump': dump() return if len(sys.argv) == 2 and sys.argv[1].lower() == 'compare': compare() return else: msg = (f'''\ Usage: 1. dump current constants to {DUMP_FILE_NAME}: winsdk_watchdog.py dump 2. compare current constants with {DUMP_FILE_NAME}: winsdk_watchdog.py compare ''') print(msg) if __name__ == '__main__': main()