#!/usr/bin/env python3

import json
import platform


def make_sys_report(anonymous=False, skipUsb=False, skipPackages=False):
    def get_usb():
        try:
            import usb.core
        except ImportError:
            yield "NoLib"
            return
        speeds = ["Unknown", "Low", "Full", "High", "Super", "SuperPlus"]
        format_hex = lambda val: f"{val:#0{6}x}"
        try:
            for dev in usb.core.find(find_all=True):
                yield {
                    "port": dev.port_number,
                    "vendor_id": format_hex(dev.idVendor),
                    "product_id": format_hex(dev.idProduct),
                    "speed": speeds[dev.speed] if dev.speed < len(speeds) else dev.speed
                }
        except usb.core.NoBackendError:
            yield "No USB backend found"

    result = {
        "architecture": ' '.join(platform.architecture()).strip(),
        "machine": platform.machine(),
        "platform": platform.platform(),
        "processor": platform.processor(),
        "python_build": ' '.join(platform.python_build()).strip(),
        "python_compiler": platform.python_compiler(),
        "python_implementation": platform.python_implementation(),
        "python_version": platform.python_version(),
        "release": platform.release(),
        "system": platform.system(),
        "version": platform.version(),
        "win32_ver": ' '.join(platform.win32_ver()).strip(),
    }

    if not skipPackages:
        from pip._internal.operations.freeze import freeze
        result["packages"] = list(freeze(local_only=True))
    if not skipUsb:
        result["usb"] = list(get_usb())
    if not anonymous:
        result["uname"] = ' '.join(platform.uname()).strip(),
    return result


if __name__ == "__main__":
    data = make_sys_report()
    with open("log_system_information.json", "w") as f:
        json.dump(data, f, indent=4)

    print(json.dumps(data, indent=4))
    print("System info gathered successfully - saved as \"log_system_information.json\"")

[((928, 946), 'platform.machine', 'platform.machine', ([], {}), '()\n', (944, 946), False, 'import platform\n'), ((968, 987), 'platform.platform', 'platform.platform', ([], {}), '()\n', (985, 987), False, 'import platform\n'), ((1010, 1030), 'platform.processor', 'platform.processor', ([], {}), '()\n', (1028, 1030), False, 'import platform\n'), ((1126, 1152), 'platform.python_compiler', 'platform.python_compiler', ([], {}), '()\n', (1150, 1152), False, 'import platform\n'), ((1187, 1219), 'platform.python_implementation', 'platform.python_implementation', ([], {}), '()\n', (1217, 1219), False, 'import platform\n'), ((1247, 1272), 'platform.python_version', 'platform.python_version', ([], {}), '()\n', (1270, 1272), False, 'import platform\n'), ((1293, 1311), 'platform.release', 'platform.release', ([], {}), '()\n', (1309, 1311), False, 'import platform\n'), ((1331, 1348), 'platform.system', 'platform.system', ([], {}), '()\n', (1346, 1348), False, 'import platform\n'), ((1369, 1387), 'platform.version', 'platform.version', ([], {}), '()\n', (1385, 1387), False, 'import platform\n'), ((1884, 1912), 'json.dump', 'json.dump', (['data', 'f'], {'indent': '(4)'}), '(data, f, indent=4)\n', (1893, 1912), False, 'import json\n'), ((1924, 1950), 'json.dumps', 'json.dumps', (['data'], {'indent': '(4)'}), '(data, indent=4)\n', (1934, 1950), False, 'import json\n'), ((1575, 1598), 'pip._internal.operations.freeze.freeze', 'freeze', ([], {'local_only': '(True)'}), '(local_only=True)\n', (1581, 1598), False, 'from pip._internal.operations.freeze import freeze\n'), ((875, 898), 'platform.architecture', 'platform.architecture', ([], {}), '()\n', (896, 898), False, 'import platform\n'), ((1065, 1088), 'platform.python_build', 'platform.python_build', ([], {}), '()\n', (1086, 1088), False, 'import platform\n'), ((1419, 1439), 'platform.win32_ver', 'platform.win32_ver', ([], {}), '()\n', (1437, 1439), False, 'import platform\n'), ((1717, 1733), 'platform.uname', 'platform.uname', ([], {}), '()\n', (1731, 1733), False, 'import platform\n')]