diff --git a/tools/README.md b/tools/README.md new file mode 100644 index 0000000000000000000000000000000000000000..797c46d867df1af7a532b9229820f4392486831f --- /dev/null +++ b/tools/README.md @@ -0,0 +1,28 @@ +# 工具说明 + +本文件夹内存放开发过程中使用的程序代码。每一类工具的代码会被放在同一个文件夹中,文件夹的名称是用到工具的模块 + + + +## 硬件扫描 & 硬件兼容性数据集制作工具 + +用于 utmtc-scanhardware + +- `scan_local_hardware.py` : 使用 lspci 扫描设备已连接的 pci 设备,并将这些设备的信息在执行目录下输出为一个 `json` 文件。 + + - 用法示例:`$ python3 ./scan_local_hardware.py` + + 输出文件命名规则为 `hardware_list_<当前设备架构>.json` 示例: + + ```json + [{"vendorID": "8086", "deviceID": "9b43", "svID": "1849", "ssID": "9b43", "type": "Host bridge", "chipVendor": "Intel Corporation"}........] + ``` + +- `merge_hardware_lists.py` : 用于合并多个由上面的工具生成的 json 列表,去除所有重复项,输出一个名为 `ut_compat_hardware.json` 的文件 + + - 用法示例: `$ python3 ./ut_compat_hardware.json` + + 运行完成后会在执行目录下输出一个合并了所有其他 json 文件内容的 `ut_compat_hardware.json` 文件。 + + - 注意,本工具只进行同目录下多个 json 文件的去重合并,不会检查内容准确性 + diff --git a/tools/scanhardware/merge_hardware_lists.py b/tools/scanhardware/merge_hardware_lists.py new file mode 100644 index 0000000000000000000000000000000000000000..112629c76099cb715884e6dc0e0c4bfc11909ee8 --- /dev/null +++ b/tools/scanhardware/merge_hardware_lists.py @@ -0,0 +1,37 @@ +import json +import os + +def main(): + file_list = [] + + raw_device_str_list = [] + device_list = [] + + for root, dirs, files in os.walk("./"): + file_list = files + + for item in file_list: + if item.endswith('.json'): + with open('./'+item, 'r') as f: + item_json_array = json.loads(f.read()) + for json_item in item_json_array: + raw_device_str_list.append(json.dumps(json_item)) + + for dev in set(raw_device_str_list): + device_list.append(dev) + + print(device_list) + + jsonlist = [] + + for dev in device_list: + jsonlist.append(json.loads(dev)) + + print(jsonlist) + + with open('./ut_compat_hardwares.json', 'w') as f: + f.write(json.dumps(jsonlist)) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/tools/scanhardware/scan_local_hardware.py b/tools/scanhardware/scan_local_hardware.py new file mode 100644 index 0000000000000000000000000000000000000000..4773b8b2881a5d110fa67ea0b71e48bc851cb3df --- /dev/null +++ b/tools/scanhardware/scan_local_hardware.py @@ -0,0 +1,57 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +import pylspci +import json +import platform + + +def get_pci_list()->list: + return pylspci.parsers.SimpleParser().run() + + +def int_id_to_hex(int_id: int)->str: + hex_id = hex(int_id)[2:] + if len(hex_id)==3 : + hex_id = hex_id + '0' + return hex_id + + +def get_hex_id(name_with_id: pylspci.fields.NameWithID)->str: + int_id = name_with_id.as_dict().get('id') + if int_id==None : # 若为空,直接返回 + return '' + hex_id = hex(int_id)[2:] + if len(hex_id)==3 : # 否则判断补全一下 + hex_id = hex_id + '0' + return hex_id + + +def get_pci_info() -> list: + pci_dev_list = pylspci.parsers.SimpleParser().run() + + parsed_list = [] + for pci_dev_item in pci_dev_list: + item_dict = {} + item_dict['vendorID'] = get_hex_id(pci_dev_item.vendor) + item_dict['deviceID'] = get_hex_id(pci_dev_item.device) + item_dict['svID'] = get_hex_id(pci_dev_item.subsystem_vendor) + item_dict['ssID'] = get_hex_id(pci_dev_item.subsystem_device) + item_dict['type'] = pci_dev_item.cls.name + item_dict['chipVendor'] = pci_dev_item.vendor.name + parsed_list.append(item_dict) + + return parsed_list + + +def main(): + jsonstr = json.dumps(get_pci_info()) + jsonfile = open("hardware_list_"+platform.machine()+".json", 'w') + with jsonfile: + jsonfile.write(jsonstr) + + print("complete. save hardware info file in current direcory") + + +if __name__ == "__main__": + main() \ No newline at end of file