home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / hwdb-xml < prev    next >
Encoding:
Text File  |  2007-04-25  |  5.6 KB  |  183 lines

  1. #!/usr/bin/python
  2.  
  3. """
  4. ubuntu hwdb xml creator.
  5.  
  6. (c) 2004 Oliver Grawert (ogra) <hostmaster@grawert.net>
  7.  
  8. This software is distributed under the terms and conditions of the GNU General
  9. Public License. See http://www.gnu.org/copyleft/gpl.html for details.
  10. """
  11.  
  12. import os
  13. import sys
  14. import string
  15.  
  16. class hwdata2xml:
  17.     def __init__(self):
  18.  
  19.          if sys.argv.__contains__('-h') or len(sys.argv) == 1:
  20.             print "\n\tusage: hwdb-xml [options]\n"
  21.             print "hwdb-xml collects all hardware data needed for "
  22.             print "the hwdb submission, converts it to xml and prints"
  23.             print "to stdout\n"
  24.             print "options:" 
  25.             print " -a    collect all data"
  26.             print " -d    collect device data"
  27.             print " -x    collect xorg.conf and Xorg.0.log"
  28.             print " -b    collect bootlog (dmesg)"
  29.             print " -h    show this help\n"
  30.             return
  31.  
  32.         if len(sys.argv) > 1:    
  33.             for line in self.xml_header():
  34.                 print line
  35.             if sys.argv.__contains__('-d') or sys.argv.__contains__('-a'):
  36.                 for line in self.lshal_xml():
  37.                 print line
  38.             if sys.argv.__contains__('-x') or sys.argv.__contains__('-a'):
  39.                     for line in self.xorgconf():
  40.                     print line
  41.                        for line in self.xorglog():
  42.                     print line
  43.             if sys.argv.__contains__('-b') or sys.argv.__contains__('-a'):
  44.                 for line in self.bootlog():
  45.                     print line
  46.         print "</hwinfo>"
  47.  
  48.     
  49.     def xml_header(self):
  50.         doc = []
  51.  
  52.         doc.append("<?xml version=\"1.0\" standalone=\"no\"?> <!--*- mode: xml -*-->")
  53.         doc.append("<!DOCTYPE hwinfo SYSTEM \"http://www.ubuntu.com/hwdb\">\n")
  54.         doc.append("<hwinfo>")
  55.  
  56.         return doc
  57.  
  58.     def lshal_xml(self):
  59.         doc = []
  60.         devs = []
  61.         dev = None
  62.         devnum = 0
  63.  
  64.         doc.append("<devicelist>")
  65.  
  66.         proc = open("/proc/cpuinfo")
  67.         procinfo = proc.readlines()
  68.         doc.append(' <device id="processor">')
  69.         for line in procinfo:
  70.         data = line.rstrip().split(":")
  71.         if len(data) > 1:
  72.             doc.append('  <property class="linux" subclass="cpuinfo" name="'+data[0].rstrip()+'" type="string">'+data[1].lstrip()+'</property>')
  73.         doc.append(' </device>')
  74.  
  75.         proc = open("/proc/meminfo")
  76.         procinfo = proc.readlines()
  77.         doc.append(' <device id="memory">')
  78.         for line in procinfo:
  79.         data = line.rstrip().split(":")
  80.         if len(data) > 1:
  81.             doc.append('  <property class="linux" subclass="meminfo" name="'+data[0].rstrip()+'" type="string">'+data[1].lstrip()+'</property>')
  82.         doc.append(' </device>')
  83.  
  84.         for line in os.popen('/usr/bin/lshal'):
  85.             joinline = []
  86.             fields = string.splitfields(line, '=')
  87.         
  88.             if len(fields) == 2:
  89.                 if fields[0].strip() == "udi":
  90.                 devname = string.splitfields(line, '/')
  91.             if devnum != 0:
  92.                 doc.append(" </device>\n")
  93.             doc.append(" <device id=\""+devname[len(devname)-1].rstrip("'\n").lower()+"\">")
  94.                 devnum = devnum+1
  95.             else:
  96.                 tab = string.maketrans(fields[1],fields[1])
  97.             val = fields[1].translate(tab, '()\'')
  98.             newval = string.splitfields(val)
  99.  
  100.             propvals = string.splitfields(fields[0].lstrip(), '.')
  101.  
  102.             classname = propvals[0].rstrip()
  103.             subclass = propvals[1].rstrip()
  104.             type = newval[len(newval)-1]
  105.             newval[len(newval)-1] = ""
  106.  
  107.             if type == "int" or type == "uint64":
  108.                 newval[len(newval)-2] = ""
  109.             name = ""
  110.             if len(propvals) == 2:
  111.                 name = subclass.rstrip()
  112.                 subclass = ""
  113.             else:
  114.                 name = propvals[2].strip()
  115.             if subclass == "procfs":
  116.                 subclass = name
  117.                 name = propvals[3].strip()
  118.  
  119.             if not string.join(newval).strip().startswith('<'):
  120.                 joinline.append("  <property class=\""+classname+"\"")
  121.                 if subclass:
  122.                     joinline.append("subclass=\""+subclass+"\"")
  123.                 joinline.append("name=\""+name+"\" type=\""+type+"\">"+string.join(newval).strip()+"</property>")
  124.                 doc.append(string.join(joinline))
  125.         doc.append(" </device>\n")
  126.         doc.append("</devicelist>\n")
  127.         return doc
  128.             
  129.     def bootlog(self):
  130.         try: file = open("/var/log/dmesg", "r")
  131.             except: return []
  132.         bootinfo = []
  133.         bootinfo.append("<bootinfo>")
  134.         for line in file:
  135.             newline = string.replace(line, '<', ' ')
  136.             newline = string.replace(newline, '\010', ' ')
  137.             bootinfo.append(" <bootdata>"+newline.strip()+"</bootdata>")
  138.         bootinfo.append("</bootinfo>\n")
  139.         return bootinfo
  140.         
  141.     def xorglog(self):
  142.         try: file = open("/var/log/Xorg.0.log", "r")
  143.             except: return []
  144.         xinfo = []
  145.         xinfo.append("<xloginfo>")
  146.         for line in file:
  147.             newline = string.replace(line, '&', ' ')
  148.         if len(newline) > 1:
  149.                 xinfo.append(" <xlogdata>"+newline.strip()+"</xlogdata>")
  150.         xinfo.append("</xloginfo>\n")
  151.         return xinfo
  152.  
  153.     def xorgconf(self):
  154.         file = open("/etc/X11/xorg.conf", "r")
  155.         xconf = []
  156.         xconf.append("<xconfinfo>")
  157.         for line in file:
  158.             line = line.lstrip()
  159.             if not line.startswith('#') and len(line) > 1:
  160.             if line.startswith('Section'):
  161.                 splitted = string.splitfields(line, ' ')
  162.                     xconf.append(" <section name="+splitted[1].strip()+">")
  163.             elif line.startswith("EndSection"):
  164.                 xconf.append(" </section>\n") 
  165.             elif line.startswith('SubSection'):
  166.                 splitted = string.splitfields(line, ' ')
  167.                     xconf.append("  <subsection name="+splitted[1].strip()+">")
  168.             elif line.startswith("EndSubSection"):
  169.                 xconf.append("  </subsection>\n") 
  170.             else:
  171.                 newline = string.replace(line, '<', ' ')
  172.                     newline = string.replace(newline, '\t', ' ')
  173.                 splitted = string.splitfields(newline, ' ')
  174.                 name = splitted[0]
  175.                 splitted[0] = ""
  176.                     xconf.append("    <property name=\""+name.lower()+"\">"+string.join(splitted).strip()+"</property>")
  177.         xconf.append("</xconfinfo>\n")
  178.         return xconf
  179.  
  180.  
  181. if __name__ == "__main__":
  182.     self = hwdata2xml()
  183.