Tools: make-pci-ids fixups.

Add ID count sanity checks and make sure we don't update pci-ids.c
unless the checks pass. Fix a bunch of Pylint warnings. Strip leading
whitespace from our output strings.
This commit is contained in:
Gerald Combs 2021-11-01 15:41:11 -07:00 committed by Wireshark GitLab Utility
parent 51c714bfb5
commit 85b99c18d9
2 changed files with 36200 additions and 37455 deletions

File diff suppressed because it is too large Load Diff

View File

@ -13,14 +13,17 @@
# SPDX-License-Identifier: GPL-2.0-or-later
#
import re
import string
import sys
import urllib.request, urllib.error, urllib.parse
OUTPUT_FILE = "epan/pci-ids.c"
code_prefix = """ *
MIN_VENDOR_COUNT = 2250 # 2261 on 2021-11-01
MIN_DEVICE_COUNT = 33000 # 33724 on 2021-11-01
CODE_PREFIX = """\
*
* Generated by tools/make-pci-ids.py
* By Caleb Chiu <caleb.chiu@macnica.com>
* Copyright 2021
@ -52,7 +55,7 @@ typedef struct
"""
code_postfix = """
CODE_POSTFIX = """
static pci_vid_index_t *get_vid_index(guint16 vid)
{
guint32 start_index = 0;
@ -115,113 +118,132 @@ const char *pci_id_str(guint16 vid, guint16 did, guint16 svid, guint16 ssid)
id_list=[]
count_list=[]
def exit_msg(msg=None, status=1):
if msg is not None:
sys.stderr.write(msg + '\n')
sys.exit(status)
def main():
req_headers = { 'User-Agent': 'Wireshark make-pci-ids' }
req = urllib.request.Request('https://github.com/pciutils/pciids/raw/master/pci.ids', headers=req_headers)
response = urllib.request.urlopen(req)
lines = response.read().decode('UTF-8', 'replace').splitlines()
foutput = open(OUTPUT_FILE, "w", encoding="utf-8")
print("/* pci-ids.c\n *", file = foutput)
print(" * pci-ids.c is based on the pci.ids of The PCI ID Repository:", file = foutput)
out_lines = '''\
/* pci-ids.c
*
* pci-ids.c is based on the pci.ids of The PCI ID Repository at
* https://pci-ids.ucw.cz/, fetched indirectly via
* https://github.com/pciutils/pciids
'''
vid = -1
did = -1
svid = -1
ssvid = -1
entries = 0;
line_num = 0;
entries = 0
line_num = 0
for line in lines:
line = line.strip('\n')
line_num += 1
if line_num <= 15:
line = line.replace('#', ' ', 1)
line = line.lstrip()
line = line.replace("GNU General Public License","GPL")
if line:
line = ' * ' + line
else:
line = ' *' + line
print(line, file = foutput)
line = line.replace('#', ' ', 1)
line = line.lstrip()
line = line.replace("GNU General Public License", "GPL")
if line:
line = ' * ' + line
else:
line = ' *' + line
out_lines += line + '\n'
if line_num == 15:
foutput.write(code_prefix)
out_lines += CODE_PREFIX
line = line.replace("\\","\\\\")
line = line.replace("\\","\\\\")
line = line.replace("\"","\\\"")
line = line.replace("?","?-")
line = line.replace("?","?-")
tabs = len(line) - len(line.lstrip('\t'))
if tabs == 0:
#print line
words = line.split(" ", 1)
if len(words) < 2:
#print line
words = line.split(" ", 1)
if len(words) < 2:
continue
if len(words[0]) != 4:
if len(words[0]) != 4:
continue
if all(c in string.hexdigits for c in words[0]):
if all(c in string.hexdigits for c in words[0]):
hex_int = int(words[0], 16)
if vid != -1:
print("};/* pci_vid_%04X[] */"% (vid), file = foutput)
print("\n", file = foutput)
out_lines += "}; /* pci_vid_%04X[] */\n\n" % (vid)
count_list.append(entries)
vid = hex_int;
entries = 1;
vid = hex_int
entries = 1
did = -1
svid = -1
ssid = -1
print("pci_id_t pci_vid_%04X[] = {"% (vid), file = foutput)
print("{0x%04X, 0xFFFF, 0xFFFF, 0xFFFF, \"%s(0x%04X)\"},"% (vid, words[1], vid), file = foutput)
id_list.append(vid);
continue;
out_lines += "pci_id_t pci_vid_%04X[] = {\n" % (vid)
out_lines += "{0x%04X, 0xFFFF, 0xFFFF, 0xFFFF, \"%s(0x%04X)\"},\n" % (vid, words[1].strip(), vid)
id_list.append(vid)
continue
if tabs == 1:
line = line.strip('\t')
words = line.split(" ", 1)
if len(words) < 2:
line = line.strip('\t')
words = line.split(" ", 1)
if len(words) < 2:
continue
if len(words[0]) != 4:
if len(words[0]) != 4:
continue
if all(c in string.hexdigits for c in words[0]):
if all(c in string.hexdigits for c in words[0]):
hex_int = int(words[0], 16)
did = hex_int
svid = -1
ssid = -1
print("{0x%04X, 0x%04X, 0xFFFF, 0xFFFF, \"%s(0x%04X)\"},"% (vid, did, words[1], did), file = foutput)
entries += 1;
continue;
out_lines += "{0x%04X, 0x%04X, 0xFFFF, 0xFFFF, \"%s(0x%04X)\"},\n" % (vid, did, words[1].strip(), did)
entries += 1
continue
if tabs == 2:
line = line.strip('\t')
words = line.split(" ", 2)
if len(words[0]) != 4:
line = line.strip('\t')
words = line.split(" ", 2)
if len(words[0]) != 4:
continue
if all(c in string.hexdigits for c in words[0]):
if all(c in string.hexdigits for c in words[0]):
hex_int = int(words[0], 16)
svid = hex_int
if all(c in string.hexdigits for c in words[1]):
if all(c in string.hexdigits for c in words[1]):
hex_int = int(words[1], 16)
ssid = hex_int
print("{0x%04X, 0x%04X, 0x%04X, 0x%04X, \"%s(0x%04X-0x%04X)\"},"% (vid, did, svid, ssid, words[2], svid, ssid), file = foutput)
entries += 1;
svid = -1
ssid = -1
continue;
print("};/* pci_vid_%04X[] */"% (vid), file = foutput)
out_lines += "{0x%04X, 0x%04X, 0x%04X, 0x%04X, \"%s(0x%04X-0x%04X)\"},\n" % (vid, did, svid, ssid, words[2].strip(), svid, ssid)
entries += 1
svid = -1
ssid = -1
continue
out_lines += "}; /* pci_vid_%04X[] */\n" % (vid)
count_list.append(entries)
print("\npci_vid_index_t pci_vid_index[] = {", file = foutput)
out_lines += "\npci_vid_index_t pci_vid_index[] = {\n"
length = len(id_list)
for i in range(length):
print("{0x%04X, %d, pci_vid_%04X },"% (id_list[i], count_list[i],id_list[i]), file = foutput)
vendor_count = len(id_list)
device_count = 0
for i in range(vendor_count):
out_lines += "{0x%04X, %d, pci_vid_%04X },\n" % (id_list[i], count_list[i], id_list[i])
device_count += count_list[i]
print("};/* We have %d VIDs */"% (length), file = foutput)
out_lines += "}; /* We have %d VIDs */\n" % (vendor_count)
foutput.write(code_postfix)
foutput.close()
out_lines += CODE_POSTFIX
if vendor_count < MIN_VENDOR_COUNT:
exit_msg(f'Too view vendors. Wanted {MIN_VENDOR_COUNT}, got {vendor_count}.')
if device_count < MIN_DEVICE_COUNT:
exit_msg(f'Too view devices. Wanted {MIN_DEVICE_COUNT}, got {device_count}.')
with open(OUTPUT_FILE, "w", encoding="utf-8") as pci_ids_f:
pci_ids_f.write(out_lines)
if __name__ == '__main__':
main()