Personal tools
You are here: Home Linux Merge PCI IDs

Merge PCI IDs

— filed under: , ,
by Harald Hoyer last modified Oct 03, 2007 14:54

Seems like http://pciids.sourceforge.net/ and http://www.pcidatabase.com/ duplicate the same effort. Here is a small python script to merge both sources.

readpci.py — Python Source, 2Kb

File contents

#/usr/bin/env python
import re
import string
import  sys

man_re = re.compile (r'^(?P<vendorid>[%s]{4})\s+(?P<vendorname>.*\S)\s*$' % (string.hexdigits))
dman_re = re.compile (r'^\t(?P<deviceid>[%s]{4})\s+(?P<devicename>.*\S)\s*$' % (string.hexdigits))
sdman_re = re.compile (r'^\t\t(?P<svid>[%s]{4})\s+(?P<sdid>[%s]{4})\s+(?P<devicename>.*\S)\s*$' % (string.hexdigits, string.hexdigits))
vnames = {}
dnames = {}
sdnames = {}

def load_ids (lines):
    for line in lines.split("\n"):
        m = man_re.match (line)
        if m:
            vid = m.group ('vendorid').lower()
            vname = m.group ('vendorname').strip()
            vnames[vid] = vname
        else:
            m = dman_re.match (line)
            if m:
                did = m.group ('deviceid').lower()
                dname = m.group ('devicename').strip()
                if not dnames.get(vid):
                    dnames[vid] = {}
                dnames[vid][did] = dname
            else:
                m = sdman_re.match (line)
                if m:
                    sdid = "%s %s" % (m.group ('svid').lower(), 
                                      m.group ('sdid').lower())
                    dname = m.group('devicename').strip()
                    if not sdnames.get(vid):
                        sdnames[vid]={}
                    if not sdnames[vid].get(did):
                        sdnames[vid][did] = {}
                    sdnames[vid][did][sdid] = dname


import urllib
data = urllib.urlopen('http://www.pcidatabase.com/reports.php?type=tab-delimeted').read()
load_ids(data)
data = urllib.urlopen('http://pciids.sourceforge.net/pci.ids').read()
load_ids(data)

vkeys = vnames.keys()
vkeys.sort()
for vkey in vkeys:
    print "%s  %s" % (vkey, vnames[vkey])
    dn = dnames.get(vkey)
    if dn:
        dkeys = dn.keys()
        dkeys.sort()
        for dkey in dkeys:
            print "\t%s  %s" % (dkey, dnames[vkey][dkey])
            dn = sdnames.get(vkey)
            if dn:
                sdn = dn.get(dkey)
                if sdn:
                    sdkeys = sdn.keys()
                    sdkeys.sort()
                    for sdkey in sdkeys:
                        print "\t\t%s  %s" % (sdkey, sdnames[vkey][dkey][sdkey])

Document Actions
  • Print this
  • Hits: 002720