home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / DistUpgrade / xorg_fix_proprietary.py < prev   
Encoding:
Python Source  |  2009-04-27  |  4.4 KB  |  123 lines

  1. #!/usr/bin/python
  2. #
  3. # this script will exaimne /etc/xorg/xorg.conf and 
  4. # transition from broken proprietary drivers to the free ones
  5. #
  6.  
  7. import apt
  8. import sys
  9. import os
  10. import os.path
  11. import logging
  12. import time
  13. import shutil
  14. import subprocess
  15. import apt_pkg
  16.  
  17. XORG_CONF="/etc/X11/xorg.conf"
  18.  
  19. def remove_input_devices(xorg_source=XORG_CONF, xorg_destination=XORG_CONF):
  20.     logging.debug("remove_input_devices")
  21.     content=[]
  22.     in_input_devices = False
  23.     for raw in open(xorg_source):
  24.         line = raw.strip()
  25.         if (line.lower().startswith("section") and 
  26.             line.lower().split("#")[0].strip().endswith('"inputdevice"')):
  27.             logging.debug("found 'InputDevice' section")
  28.             content.append("# commented out by update-manager, HAL is now used\n")
  29.             content.append("#"+raw)
  30.             in_input_devices=True
  31.         elif line.lower().startswith("endsection") and in_input_devices:
  32.             content.append("#"+raw)
  33.             in_input_devices=False
  34.         elif line.lower().startswith("inputdevice"):
  35.             logging.debug("commenting out '%s' " % line)
  36.             content.append("# commented out by update-manager, HAL is now used\n")
  37.             content.append("#"+raw)
  38.         elif in_input_devices:
  39.             logging.debug("commenting out '%s' " % line)
  40.             content.append("#"+raw)
  41.         else:
  42.             content.append(raw)
  43.     open(xorg_destination+".new","w").write("".join(content))
  44.     os.rename(xorg_destination+".new", xorg_destination)
  45.     return True
  46.  
  47. def replace_driver_from_xorg(old_driver, new_driver, xorg=XORG_CONF):
  48.     """
  49.     this removes the fglrx driver from the xorg.conf and subsitutes
  50.     it with the ati one
  51.     """
  52.     if not os.path.exists(xorg):
  53.         logging.warning("file %s not found" % xorg)
  54.         return
  55.     content=[]
  56.     for line in open(xorg):
  57.         # remove comments
  58.         s=line.split("#")[0].strip()
  59.         # check for fglrx driver entry
  60.         if (s.lower().startswith("driver") and
  61.             s.endswith('"%s"' % old_driver)):
  62.             logging.debug("line '%s' found" % line)
  63.             line='\tDriver\t"%s"\n' % new_driver
  64.             logging.debug("replacing with '%s'" % line)
  65.         content.append(line)
  66.     # write out the new version
  67.     if open(xorg).readlines() != content:
  68.         logging.info("saveing new %s (%s -> %s)" % (xorg, old_driver, new_driver))
  69.         open(xorg+".xorg_fix","w").write("".join(content))
  70.         os.rename(xorg+".xorg_fix", xorg)
  71.  
  72. def is_multiseat(xorg_source=XORG_CONF):
  73.     " check if we have a multiseat xorg config "
  74.     def is_serverlayout_line(line):
  75.         return (not line.strip().startswith("#") and
  76.                 line.strip().lower().endswith('"serverlayout"'))
  77.     msl = len(filter(is_serverlayout_line, open(xorg_source)))
  78.     logging.debug("is_multiseat: lines %i", msl)
  79.     return msl > 1
  80.  
  81. if __name__ == "__main__":
  82.     if not os.getuid() == 0:
  83.         print "Need to run as root"
  84.         sys.exit(1)
  85.  
  86.     # we pretend to be update-manger so that apport picks up when we crash
  87.     sys.argv[0] = "/usr/bin/update-manager"
  88.  
  89.     # setup logging
  90.     logging.basicConfig(level=logging.DEBUG,
  91.                         filename="/var/log/dist-upgrade/xorg_fix_intrepid.log",
  92.                         filemode='w')
  93.     
  94.     logging.info("%s running" % sys.argv[0])
  95.  
  96.     if not os.path.exists(XORG_CONF):
  97.         logging.info("No xorg.conf, exiting")
  98.         sys.exit(0)
  99.         
  100.     #make a backup of the xorg.conf
  101.     backup = XORG_CONF + ".dist-upgrade-" + time.strftime("%Y%m%d%H%M")
  102.     logging.debug("creating backup '%s'" % backup)
  103.     shutil.copy(XORG_CONF, backup)
  104.  
  105.     if (not os.path.exists("/usr/lib/xorg/modules/drivers/fglrx_drv.so") and
  106.         "fglrx" in open(XORG_CONF).read()):
  107.         logging.info("Removing fglrx from %s" % XORG_CONF)
  108.         replace_driver_from_xorg("fglrx","ati")
  109.  
  110.     if (not os.path.exists("/usr/lib/xorg/modules/drivers/nvidia_drv.so") and
  111.         "nvidia" in open(XORG_CONF).read()):
  112.         logging.info("Removing nvidia from %s" % XORG_CONF)
  113.         replace_driver_from_xorg("nvidia","nv")
  114.  
  115.     # now run the removeInputDevices() if we have a new xserver
  116.     ver=subprocess.Popen(["dpkg-query","-W","-f=${Version}","xserver-xorg-core"], stdout=subprocess.PIPE).communicate()[0]
  117.     logging.info("xserver-xorg-core version is '%s'" % ver)
  118.     if ver and apt_pkg.VersionCompare(ver, "2:1.5.0") > 0:
  119.         if not is_multiseat():
  120.             remove_input_devices()
  121.         else:
  122.             logging.info("multiseat setup, ignoring")
  123.