- greatly simplify (and fix in the process) parsing of lspci output
authorMark Huang <mlhuang@cs.princeton.edu>
Thu, 29 Jun 2006 16:02:20 +0000 (16:02 +0000)
committerMark Huang <mlhuang@cs.princeton.edu>
Thu, 29 Jun 2006 16:02:20 +0000 (16:02 +0000)
source/systeminfo.py

index 36d0ce6..5840ab0 100755 (executable)
@@ -65,6 +65,7 @@ import string
 import os
 import popen2
 import merge_hw_tables
+import re
 
 hwdatapath = "usr/share/hwdata"
 class systeminfo:
@@ -380,21 +381,31 @@ class systeminfo:
         # search for the corresponding driver from the dictionary
         # generated by merge_hw_tables
         for line in lspci_prog.fromchild:
-            if string.strip(line) == "":
-                continue
-    
-            parts= string.split(line)
-
+            # A sample line:
+            #
+            # 00:1f.1 "Class 0101" "8086" "2411" -r02 -p80 "8086" "2411"
+            #
+            # Remove '"', 'Class ', and anything beginning with '-'
+            # (usually revisions and prog-if flags) so that we can
+            # split on whitespace:
+            #
+            # 00:1f.1 0101 8086 2411 8086 2411
+            #
+            line = line.strip()
+            line = line.replace('"', '')
+            line = line.replace('Class ', '')
+            line = re.sub('-[^ ]*', '', line)
+
+            parts = line.split()
             try:
-                classid= self.remove_quotes(parts[2])
-                classid= long(classid,16)
-            except IndexError:
-                print( "Skipping invalid classid:", string.strip(line) )
+                if len(parts) < 4:
+                    raise
+                classid = long(parts[1], 16)
+                vendorid = long(parts[2], 16)
+                deviceid = long(parts[3], 16)
+            except:
+                print "Invalid line:", line
                 continue
-            except ValueError, e:
-                print( "Skipping invalid classid:", string.strip(line) )
-                continue
-
 
             if classid not in (self.PCI_CLASS_NETWORK_ETHERNET,
                                self.PCI_CLASS_STORAGE_SCSI,
@@ -403,75 +414,13 @@ class systeminfo:
                                self.PCI_CLASS_STORAGE_IDE):
                 continue
 
-            vendorid    = self.PCI_ANY
-            deviceid    = self.PCI_ANY
-            subvendorid = self.PCI_ANY
-            subdeviceid = self.PCI_ANY
-
-            # parse in vendorid
+            # Device may have a subvendorid and subdeviceid
             try:
-                vendorid= self.remove_quotes(parts[3])
-                vendorid= long(vendorid,16)
-            except IndexError:
-                print( "Skipping invalid vendorid:", string.strip(line) )
-                continue
-            except ValueError, e:
-                print( "Skipping invalid vendorid:", string.strip(line) )
-                continue
-
-            # parse in deviceid
-            try:
-                deviceid= self.remove_quotes(parts[4])
-                deviceid= long(deviceid,16)
-            except IndexError:
-                print( "Skipping invalid deviceid:", string.strip(line) )
-                continue
-            except ValueError, e:
-                print( "Skipping invalid deviceid:", string.strip(line) )
-                continue
-
-            # Now get the subvendor & subdevice portion by searching
-            # parts[5:] of the lspci output. Note that we have to skip
-            # the portions of the lspci output string that indicate
-            # revision info.
-
-            # parse in subvendorid
-            subvendorindex = -1
-            for i in range(5,len(parts)):
-                p = self.remove_quotes(parts[i])
-                if len(p)>0 and p[0] != '-':
-                    subvendorindex = i
-                    break
-
-            if subvendorindex != -1:
-                try:
-                    subvendorid= self.remove_quotes(parts[subvendorindex])
-                    subvendorid= long(subvendorid,16)
-                except IndexError:
-                    print( "Skipping invalid line:", string.strip(line) )
-                    continue
-                except ValueError, e:
-                    print( "Skipping invalid line:", string.strip(line) )
-                    continue
-
-            # parse in subdeviceid
-            subdeviceindex = -1
-            for i in range(subvendorindex+1,len(parts)):
-                p = self.remove_quotes(parts[i])
-                if p[0] != '-':
-                    subdeviceindex = i
-                    break
-            if subdeviceindex != -1:
-                error_msg = "Skipping invalid subdeviceid:"
-                try:
-                    subdeviceid= self.remove_quotes(parts[subdeviceindex])
-                    subdeviceid= long(subdeviceid,16)
-                except IndexError:
-                    print( error_msg, string.strip(line) )
-                    continue
-                except ValueError, e:
-                    print( error_msg, string.strip(line) )
-                    continue
+                subvendorid = long(parts[4], 16)
+                subdeviceid = long(parts[5], 16)
+            except:
+                subvendorid = self.PCI_ANY
+                subdeviceid = self.PCI_ANY
 
             # search for driver that most closely matches the full_id
             # to drivers that can handle any subvendor/subdevice
@@ -490,6 +439,8 @@ class systeminfo:
                                      self.PCI_CLASS_STORAGE_OTHER,
                                      self.PCI_CLASS_STORAGE_IDE):
                         scsi_mods.append(module[0])
+                    else:
+                        print "not network or scsi: 0x%x" % classid
                     break
 
         system_mods[self.MODULE_CLASS_SCSI]= scsi_mods