home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / DistUpgrade / DistUpgradeQuirks.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  25.6 KB  |  735 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import glob
  5. import logging
  6. import os
  7. import os.path as os
  8. import re
  9. import shutil
  10. import string
  11. import sys
  12. import subprocess
  13. from subprocess import PIPE, Popen, call
  14. from hashlib import md5
  15. from DistUpgradeGettext import gettext as _
  16. from DistUpgradeGettext import ngettext
  17. import gettext
  18. from computerjanitor.plugin import PluginManager
  19.  
  20. class DistUpgradeQuirks(object):
  21.     '''
  22.     This class collects the various quirks handlers that can
  23.     be hooked into to fix/work around issues that the individual
  24.     releases have
  25.     '''
  26.     
  27.     def __init__(self, controller, config):
  28.         self.controller = controller
  29.         self._view = controller._view
  30.         self.config = config
  31.         self.uname = Popen([
  32.             'uname',
  33.             '-r'], stdout = PIPE).communicate()[0].strip()
  34.         self.plugin_manager = PluginManager(self.controller, [
  35.             './plugins'])
  36.  
  37.     
  38.     def run(self, quirksName):
  39.         '''
  40.         Run the specific quirks handler, the follow handlers are supported:
  41.         - PostInitialUpdate: run *before* the sources.list is rewritten but
  42.                              after a initial apt-get update
  43.         - PostDistUpgradeCache: run *after* the dist-upgrade was calculated
  44.                                 in the cache
  45.         - StartUpgrade: before the first package gets installed (but the
  46.                         download is finished)
  47.         - PostUpgrade: run *after* the upgrade is finished successfully and 
  48.                        packages got installed
  49.         - PostCleanup: run *after* the cleanup (orphaned etc) is finished
  50.         '''
  51.         for condition in [
  52.             quirksName,
  53.             '%s%s' % (self.config.get('Sources', 'To'), quirksName),
  54.             'from_%s%s' % (self.config.get('Sources', 'From'), quirksName)]:
  55.             for plugin in self.plugin_manager.get_plugins(condition):
  56.                 logging.debug('running quirks plugin %s' % plugin)
  57.                 plugin.do_cleanup_cruft()
  58.             
  59.         
  60.         funcname = '%s' % quirksName
  61.         func = getattr(self, funcname, None)
  62.         if func is not None:
  63.             logging.debug('quirks: running %s' % funcname)
  64.             func()
  65.         
  66.         funcname = '%s%s' % (self.config.get('Sources', 'To'), quirksName)
  67.         func = getattr(self, funcname, None)
  68.         if func is not None:
  69.             logging.debug('quirks: running %s' % funcname)
  70.             func()
  71.         
  72.         funcname = 'from_%s%s' % (self.config.get('Sources', 'From'), quirksName)
  73.         func = getattr(self, funcname, None)
  74.         if func is not None:
  75.             logging.debug('quirks: running %s' % funcname)
  76.             func()
  77.         
  78.  
  79.     
  80.     def PostCleanup(self):
  81.         ''' run after cleanup '''
  82.         logging.debug('running Quirks.PostCleanup')
  83.  
  84.     
  85.     def from_dapperPostUpgrade(self):
  86.         ''' this works around quirks for dapper->hardy upgrades '''
  87.         logging.debug('running Controller.from_dapperQuirks handler')
  88.         self._rewriteFstab()
  89.         self._checkAdminGroup()
  90.  
  91.     
  92.     def intrepidPostUpgrade(self):
  93.         ''' this applies rules for the hardy->intrepid upgrade '''
  94.         logging.debug('running Controller.intrepidQuirks handler')
  95.         self._addRelatimeToFstab()
  96.  
  97.     
  98.     def gutsyPostUpgrade(self):
  99.         ''' this function works around quirks in the feisty->gutsy upgrade '''
  100.         logging.debug('running Controller.gutsyQuirks handler')
  101.  
  102.     
  103.     def feistyPostUpgrade(self):
  104.         ''' this function works around quirks in the edgy->feisty upgrade '''
  105.         logging.debug('running Controller.feistyQuirks handler')
  106.         self._rewriteFstab()
  107.         self._checkAdminGroup()
  108.  
  109.     
  110.     def jauntyPostInitialUpdate(self):
  111.         ''' quirks that are run before the upgrade to jaunty '''
  112.         logging.debug('running %s' % sys._getframe().f_code.co_name)
  113.         if self._checkVideoDriver('fglrx') and not self._supportInModaliases('fglrx'):
  114.             res = self._view.askYesNoQuestion(_('Upgrading may reduce desktop effects, and performance in games and other graphically intensive programs.'), _("This computer is currently using the AMD 'fglrx' graphics driver. No version of this driver is available that works with your hardware in Ubuntu 9.04.\n\nDo you want to continue?"))
  115.             if res == False:
  116.                 self.controller.abort()
  117.             
  118.             logging.debug('remove xorg-driver-fglrx,xorg-driver-fglrx-envy,fglrx-kernel-source')
  119.             l = self.controller.config.getlist('Distro', 'PostUpgradePurge')
  120.             l.append('xorg-driver-fglrx')
  121.             l.append('xorg-driver-fglrx-envy')
  122.             l.append('fglrx-kernel-source')
  123.             l.append('fglrx-amdcccle')
  124.             l.append('xorg-driver-fglrx-dev')
  125.             l.append('libamdxvba1')
  126.             self.controller.config.set('Distro', 'PostUpgradePurge', ','.join(l))
  127.         
  128.  
  129.     
  130.     def from_dapperPostDistUpgradeCache(self):
  131.         self.hardyPostDistUpgradeCache()
  132.         self.gutsyPostDistUpgradeCache()
  133.         self.feistyPostDistUpgradeCache()
  134.         self.edgyPostDistUpgradeCache()
  135.  
  136.     
  137.     def jauntyPostDistUpgradeCache(self):
  138.         ''' 
  139.         this function works around quirks in the 
  140.         intrepid->jaunty upgrade calculation
  141.         '''
  142.         logging.debug('running %s' % sys._getframe().f_code.co_name)
  143.         for pkg in [
  144.             'pidgin-libnotify']:
  145.             if self.controller.cache.has_key(pkg) and self.controller.cache[pkg].isInstalled and not (self.controller.cache[pkg].markedUpgrade):
  146.                 logging.debug("forcing '%s' upgrade" % pkg)
  147.                 self.controller.cache[pkg].markUpgrade()
  148.                 continue
  149.         
  150.         for pkg in [
  151.             'gwenview',
  152.             'digikam']:
  153.             if self.controller.cache.has_key(pkg) and self.controller.cache[pkg].isInstalled and not (self.controller.cache[pkg].markedUpgrade):
  154.                 logging.debug("forcing libkipi '%s' upgrade" % pkg)
  155.                 if self.controller.cache.has_key('libkipi0'):
  156.                     logging.debug('removing  libkipi0)')
  157.                     self.controller.cache['libkipi0'].markDelete()
  158.                 
  159.                 self.controller.cache[pkg].markUpgrade()
  160.                 continue
  161.         
  162.  
  163.     
  164.     def intrepidPostDistUpgradeCache(self):
  165.         ''' 
  166.         this function works around quirks in the 
  167.         hardy->intrepid upgrade 
  168.         '''
  169.         logging.debug('running %s' % sys._getframe().f_code.co_name)
  170.         deps_found = False
  171.         if self.config.getlist(frompkg, 'KeyDependencies'):
  172.             deps_found = True
  173.             for pkg in self.config.getlist(frompkg, 'KeyDependencies'):
  174.                 if self.controller.cache.has_key(pkg):
  175.                     pass
  176.                 deps_found &= self.controller.cache[pkg].isInstalled
  177.             
  178.         
  179.         if deps_found:
  180.             logging.debug('transitioning %s to %s (via key depends)' % (frompkg, topkg))
  181.             self.controller.cache[topkg].markInstall()
  182.         
  183.         cache = self.controller.cache
  184.         for pkgname in [
  185.             'nvidia-glx-71',
  186.             'nvidia-glx-96']:
  187.             if cache.has_key(pkgname) and cache[pkgname].markedInstall and self._checkVideoDriver('nvidia'):
  188.                 logging.debug('found %s video driver' % pkgname)
  189.                 res = self._view.askYesNoQuestion(_('Upgrading may reduce desktop effects, and performance in games and other graphically intensive programs.'), _("This computer is currently using the NVIDIA 'nvidia' graphics driver. No version of this driver is available that works with your video card in Ubuntu 8.10.\n\nDo you want to continue?"))
  190.                 if res == False:
  191.                     self.controller.abort()
  192.                 
  193.                 self.controller.cache[pkgname].markKeep()
  194.                 continue
  195.         
  196.         for pkgname in [
  197.             'nvidia-glx-173',
  198.             'nvidia-glx-177']:
  199.             if cache.has_key(pkgname) and cache[pkgname].markedInstall and self._checkVideoDriver('nvidia'):
  200.                 logging.debug('found %s video driver' % pkgname)
  201.                 if not self._cpuHasSSESupport():
  202.                     logging.warning('nvidia driver that needs SSE but cpu has no SSE support')
  203.                     res = self._view.askYesNoQuestion(_('Upgrading may reduce desktop effects, and performance in games and other graphically intensive programs.'), _("This computer is currently using the NVIDIA 'nvidia' graphics driver. No version of this driver is available that works with your video card in Ubuntu 8.10.\n\nDo you want to continue?"))
  204.                     if res == False:
  205.                         self.controller.abort()
  206.                     
  207.                     self.controller.cache[pkgname].markKeep()
  208.                 
  209.             self._cpuHasSSESupport()
  210.         
  211.         fromp = 'kdelibs4-dev'
  212.         to = 'kdelibs5-dev'
  213.         if self.controller.cache.has_key(fromp) and self.controller.cache[fromp].isInstalled and self.controller.cache.has_key(to):
  214.             self.controller.cache.markInstall(to, 'kdelibs4-dev -> kdelibs5-dev transition')
  215.         
  216.  
  217.     
  218.     def hardyPostDistUpgradeCache(self):
  219.         ''' 
  220.         this function works around quirks in the 
  221.         {dapper,gutsy}->hardy upgrade 
  222.         '''
  223.         logging.debug('running %s' % sys._getframe().f_code.co_name)
  224.         if self.controller.cache.has_key('nautilus') and self.controller.cache['nautilus'].isInstalled and not (self.controller.cache['nautilus'].markedUpgrade):
  225.             for broken in 'link-monitor-applet':
  226.                 if self.controller.cache.has_key(broken) and self.controller.cache[broken].isInstalled:
  227.                     self.controller.cache[broken].markDelete()
  228.                     continue
  229.             
  230.             self.controller.cache['nautilus'].markInstall()
  231.         
  232.         self._checkAndRemoveEvms()
  233.         if self.config.get('Options', 'withNetwork') == 'True':
  234.             for pkg in self.controller.cache:
  235.                 if pkg.name.startswith('language-support-') and pkg.isInstalled and not (pkg.markedUpgrade):
  236.                     self.controller.cache.markInstall(pkg.name, 'extra language-support- kick')
  237.                     continue
  238.             
  239.         
  240.  
  241.     
  242.     def gutsyPostDistUpgradeCache(self):
  243.         ''' this function works around quirks in the feisty->gutsy upgrade '''
  244.         logging.debug('running %s' % sys._getframe().f_code.co_name)
  245.         
  246.         try:
  247.             (version, build, flavour) = self.uname.split('-')
  248.             if flavour == 'lowlatency' and flavour == '686' or flavour == 'k7':
  249.                 kernel = 'linux-image-generic'
  250.                 if not self.controller.cache[kernel].isInstalled or self.controller.cache[kernel].markedInstall:
  251.                     logging.debug("Selecting new kernel '%s'" % kernel)
  252.                     self.controller.cache[kernel].markInstall()
  253.                 
  254.         except Exception:
  255.             e = None
  256.             logging.warning('problem while transitioning lowlatency kernel (%s)' % e)
  257.  
  258.         
  259.         try:
  260.             for line in map(string.strip, open('/proc/mounts')):
  261.                 if line == '' or line.startswith('#'):
  262.                     continue
  263.                 
  264.                 
  265.                 try:
  266.                     (device, mount_point, fstype, options, a, b) = line.split()
  267.                 except Exception:
  268.                     e = None
  269.                     logging.error("can't parse line '%s'" % line)
  270.                     continue
  271.  
  272.                 if 'nfs' in fstype:
  273.                     logging.debug("found nfs mount in line '%s', marking nfs-common for install " % line)
  274.                     self.controller.cache['nfs-common'].markInstall()
  275.                     break
  276.                     continue
  277.         except Exception:
  278.             e = None
  279.             logging.warning('problem while transitioning util-linux -> nfs-common (%s)' % e)
  280.  
  281.  
  282.     
  283.     def feistyPostDistUpgradeCache(self):
  284.         ''' this function works around quirks in the edgy->feisty upgrade '''
  285.         logging.debug('running %s' % sys._getframe().f_code.co_name)
  286.         for fr, to in [
  287.             ('ndiswrapper-utils-1.8', 'ndiswrapper-utils-1.9')]:
  288.             if self.controller.cache.has_key(fr) and self.controller.cache.has_key(to):
  289.                 if self.controller.cache[fr].isInstalled and not (self.controller.cache[to].markedInstall):
  290.                     
  291.                     try:
  292.                         self.controller.cache.markInstall(to, '%s->%s quirk upgrade rule' % (fr, to))
  293.                     except SystemError:
  294.                         e = None
  295.                         logging.warning('Failed to apply %s->%s install (%s)' % (fr, to, e))
  296.                     except:
  297.                         None<EXCEPTION MATCH>SystemError
  298.                     
  299.  
  300.                 None<EXCEPTION MATCH>SystemError
  301.                 continue
  302.         
  303.  
  304.     
  305.     def edgyPostDistUpgradeCache(self):
  306.         ''' this function works around quirks in the dapper->edgy upgrade '''
  307.         logging.debug('running %s' % sys._getframe().f_code.co_name)
  308.         for pkg in self.controller.cache:
  309.             if pkg.name.startswith('python2.4-') and pkg.isInstalled and not (pkg.markedUpgrade):
  310.                 basepkg = 'python-' + pkg.name[len('python2.4-'):]
  311.                 if self.controller.cache.has_key(basepkg) and self.controller.cache[basepkg].candidateDownloadable and not (self.controller.cache[basepkg].markedInstall):
  312.                     
  313.                     try:
  314.                         self.controller.cache.markInstall(basepkg, 'python2.4->python upgrade rule')
  315.                     except SystemError:
  316.                         e = None
  317.                         logging.debug('Failed to apply python2.4->python install: %s (%s)' % (basepkg, e))
  318.                     except:
  319.                         None<EXCEPTION MATCH>SystemError
  320.                     
  321.  
  322.                 None<EXCEPTION MATCH>SystemError
  323.             
  324.             if pkg.name.startswith('xserver-xorg-input-') and pkg.isInstalled and not (pkg.markedUpgrade):
  325.                 
  326.                 try:
  327.                     self.controller.cache.markInstall(pkg.name, 'xserver-xorg-input fixup rule')
  328.                 except SystemError:
  329.                     e = None
  330.                     logging.debug('Failed to apply fixup: %s (%s)' % (pkg.name, e))
  331.                 except:
  332.                     None<EXCEPTION MATCH>SystemError
  333.                 
  334.  
  335.             None<EXCEPTION MATCH>SystemError
  336.         
  337.         for pkgname in [
  338.             'hpijs',
  339.             'bzr',
  340.             'tomboy']:
  341.             if self.controller.cache.has_key(pkgname) and self.controller.cache[pkgname].isInstalled and self.controller.cache[pkgname].isUpgradable and not (self.controller.cache[pkgname].markedUpgrade):
  342.                 
  343.                 try:
  344.                     self.controller.cache.markInstall(pkgname, '%s quirk upgrade rule' % pkgname)
  345.                 except SystemError:
  346.                     e = None
  347.                     logging.debug('Failed to apply %s install (%s)' % (pkgname, e))
  348.                 except:
  349.                     None<EXCEPTION MATCH>SystemError
  350.                 
  351.  
  352.             None<EXCEPTION MATCH>SystemError
  353.         
  354.         if self.controller.cache.has_key('libgl1-mesa-dri'):
  355.             pkg = self.controller.cache['libgl1-mesa-dri']
  356.             if pkg.candidateVersion == pkg.installedVersion and '+cvs2006' in pkg.candidateVersion:
  357.                 for ver in pkg._pkg.VersionList:
  358.                     if '~2006' in ver.VerStr:
  359.                         for VerFileIter, index in ver.FileList:
  360.                             indexfile = self.controller.cache._list.FindIndex(VerFileIter)
  361.                             if indexfile and indexfile.IsTrusted:
  362.                                 logging.info('Forcing downgrade of libgl1-mesa-dri for xgl.compz.info installs')
  363.                                 self.controller.cache._depcache.SetCandidateVer(pkg._pkg, ver)
  364.                                 break
  365.                                 continue
  366.                         
  367.                 
  368.             
  369.         
  370.         for fr, to in [
  371.             ('xserver-xorg-driver-all', 'xserver-xorg-video-all')]:
  372.             if self.controller.cache.has_key(fr) and self.controller.cache.has_key(to):
  373.                 if self.controller.cache[fr].isInstalled and not (self.controller.cache[to].markedInstall):
  374.                     
  375.                     try:
  376.                         self.controller.cache.markInstall(to, '%s->%s quirk upgrade rule' % (fr, to))
  377.                     except SystemError:
  378.                         e = None
  379.                         logging.debug('Failed to apply %s->%s install (%s)' % (fr, to, e))
  380.                     except:
  381.                         None<EXCEPTION MATCH>SystemError
  382.                     
  383.  
  384.                 None<EXCEPTION MATCH>SystemError
  385.                 continue
  386.         
  387.  
  388.     
  389.     def dapperPostDistUpgradeCache(self):
  390.         ''' this function works around quirks in the breezy->dapper upgrade '''
  391.         logging.debug('running %s' % sys._getframe().f_code.co_name)
  392.         if self.controller.cache.has_key('nvidia-glx') and self.controller.cache['nvidia-glx'].isInstalled and self.controller.cache.has_key('nvidia-settings') and self.controller.cache['nvidia-settings'].isInstalled:
  393.             logging.debug('nvidia-settings and nvidia-glx is installed')
  394.             self.controller.cache.markRemove('nvidia-settings')
  395.             self.controller.cache.markInstall('nvidia-glx')
  396.         
  397.  
  398.     
  399.     def from_hardyPostDistUpgradeCache(self):
  400.         ''' this function works around quirks in upgrades from hardy '''
  401.         logging.debug('running %s' % sys._getframe().f_code.co_name)
  402.         if self._usesEvmsInMounts():
  403.             logging.error('evms in use in /etc/fstab')
  404.             self._view.error(_('evms in use'), _("Your system uses the 'evms' volume manager in /proc/mounts. The 'evms' software is no longer supported, please switch it off and run the upgrade again when this is done."))
  405.             self.controller.abort()
  406.         
  407.  
  408.     
  409.     def StartUpgrade(self):
  410.         self._applyPatches()
  411.         self._removeOldApportCrashes()
  412.         self._removeBadMaintainerScripts()
  413.  
  414.     
  415.     def jauntyStartUpgrade(self):
  416.         self._createPycentralPkgRemove()
  417.         ap = '/var/lib/dpkg/info/hal.postinst'
  418.         if os.path.exists(ap):
  419.             md5sum = '146145275900af343d990a4dea968d7c'
  420.             if md5(open(ap).read()).hexdigest() == md5sum:
  421.                 logging.debug("removing bad script '%s'" % ap)
  422.                 os.unlink(ap)
  423.             
  424.         
  425.  
  426.     
  427.     def _removeBadMaintainerScripts(self):
  428.         ''' remove bad/broken maintainer scripts (last resort) '''
  429.         bad_scripts = [
  430.             '/var/lib/dpkg/info/apache2-common.prerm',
  431.             '/var/lib/dpkg/info/pango-libthai.postrm']
  432.         for ap in bad_scripts:
  433.             if os.path.exists(ap):
  434.                 logging.debug("removing bad script '%s'" % ap)
  435.                 os.unlink(ap)
  436.                 continue
  437.         
  438.  
  439.     
  440.     def _createPycentralPkgRemove(self):
  441.         '''
  442.         intrepid->jaunty, create /var/lib/pycentral/pkgremove flag file
  443.         to help python-central so that it removes all preinst links
  444.         on upgrade
  445.         '''
  446.         logging.debug('adding pkgremove file')
  447.         if not os.path.exists('/var/lib/pycentral/'):
  448.             os.makedirs('/var/lib/pycentral')
  449.         
  450.         open('/var/lib/pycentral/pkgremove', 'w')
  451.  
  452.     
  453.     def _removeOldApportCrashes(self):
  454.         ''' remove old apport crash files '''
  455.         
  456.         try:
  457.             for f in glob.glob('/var/crash/*.crash'):
  458.                 logging.debug("removing old crash file '%s'" % f)
  459.                 os.unlink(f)
  460.         except Exception:
  461.             e = None
  462.             logging.warning('error during unlink of old crash files (%s)' % e)
  463.  
  464.  
  465.     
  466.     def _cpuHasSSESupport(self, cpuinfo = '/proc/cpuinfo'):
  467.         ''' helper that checks if the given cpu has sse support '''
  468.         if not os.path.exists(cpuinfo):
  469.             return False
  470.         for line in open(cpuinfo):
  471.             if line.startswith('flags') and ' sse' not in line:
  472.                 return False
  473.         
  474.         return True
  475.  
  476.     
  477.     def _usesEvmsInMounts(self):
  478.         ''' check if evms is used in /proc/mounts '''
  479.         logging.debug('running _usesEvmsInMounts')
  480.         for line in open('/proc/mounts'):
  481.             line = line.strip()
  482.             if line == '' or line.startswith('#'):
  483.                 continue
  484.             
  485.             
  486.             try:
  487.                 (device, mount_point, fstype, options, a, b) = line.split()
  488.             except Exception:
  489.                 e = None
  490.                 logging.error("can't parse line '%s'" % line)
  491.                 continue
  492.  
  493.             if 'evms' in device:
  494.                 logging.debug("found evms device in line '%s', skipping " % line)
  495.                 return True
  496.         
  497.         return False
  498.  
  499.     
  500.     def _checkAndRemoveEvms(self):
  501.         ''' check if evms is in use and if not, remove it '''
  502.         logging.debug('running _checkAndRemoveEvms')
  503.         if self._usesEvmsInMounts():
  504.             return False
  505.         for pkg in [
  506.             'evms',
  507.             'libevms-2.5',
  508.             'libevms-dev',
  509.             'evms-ncurses',
  510.             'evms-ha',
  511.             'evms-bootdebug',
  512.             'evms-gui',
  513.             'evms-cli',
  514.             'linux-patch-evms']:
  515.             if self.controller.cache.has_key(pkg) and self.controller.cache[pkg].isInstalled:
  516.                 self.controller.cache[pkg].markDelete()
  517.                 continue
  518.             self._usesEvmsInMounts()
  519.         
  520.         return True
  521.  
  522.     
  523.     def _addRelatimeToFstab(self):
  524.         ''' add the relatime option to ext2/ext3 filesystems on upgrade '''
  525.         logging.debug('_addRelatime')
  526.         replaced = False
  527.         lines = []
  528.         for line in open('/etc/fstab'):
  529.             line = line.strip()
  530.             if line == '' or line.startswith('#'):
  531.                 lines.append(line)
  532.                 continue
  533.             
  534.             
  535.             try:
  536.                 (device, mount_point, fstype, options, a, b) = line.split()
  537.             except Exception:
  538.                 e = None
  539.                 logging.error("can't parse line '%s'" % line)
  540.                 lines.append(line)
  541.                 continue
  542.  
  543.             if ('ext2' in fstype or 'ext3' in fstype) and 'noatime' not in options and 'relatime' not in options:
  544.                 logging.debug("adding 'relatime' to line '%s' " % line)
  545.                 line = line.replace(options, '%s,relatime' % options)
  546.                 logging.debug("replaced line is '%s' " % line)
  547.                 replaced = True
  548.             
  549.             lines.append(line)
  550.         
  551.         if replaced:
  552.             logging.debug('writing new /etc/fstab')
  553.             f = open('/etc/fstab.intrepid', 'w')
  554.             f.write('\n'.join(lines))
  555.             f.write('\n')
  556.             f.close()
  557.             os.rename('/etc/fstab.intrepid', '/etc/fstab')
  558.         
  559.         return True
  560.  
  561.     
  562.     def _rewriteFstab(self):
  563.         ''' convert /dev/{hd?,scd0} to /dev/cdrom for the feisty upgrade '''
  564.         logging.debug('_rewriteFstab()')
  565.         replaced = 0
  566.         lines = []
  567.         for line in open('/etc/fstab'):
  568.             line = line.strip()
  569.             if line == '' or line.startswith('#'):
  570.                 lines.append(line)
  571.                 continue
  572.             
  573.             
  574.             try:
  575.                 (device, mount_point, fstype, options, a, b) = line.split()
  576.             except Exception:
  577.                 e = None
  578.                 logging.error("can't parse line '%s'" % line)
  579.                 lines.append(line)
  580.                 continue
  581.  
  582.             if 'iso9660' in fstype and device != '/dev/cdrom' and os.path.exists('/dev/cdrom') and os.path.realpath('/dev/cdrom') == device:
  583.                 logging.debug("replacing '%s' " % line)
  584.                 line = line.replace(device, '/dev/cdrom')
  585.                 logging.debug("replaced line is '%s' " % line)
  586.                 replaced += 1
  587.             
  588.             lines.append(line)
  589.         
  590.         if replaced > 0:
  591.             logging.debug('writing new /etc/fstab')
  592.             shutil.copy('/etc/fstab', '/etc/fstab.edgy')
  593.             f = open('/etc/fstab', 'w')
  594.             f.write('\n'.join(lines))
  595.             f.write('\n')
  596.             f.close()
  597.         
  598.         return True
  599.  
  600.     
  601.     def _checkAdminGroup(self):
  602.         ''' check if the current sudo user is in the admin group '''
  603.         logging.debug('_checkAdminGroup')
  604.         import grp
  605.         
  606.         try:
  607.             admin_group = grp.getgrnam('admin').gr_mem
  608.         except KeyError:
  609.             e = None
  610.             logging.warning('System has no admin group (%s)' % e)
  611.             subprocess.call([
  612.                 'addgroup',
  613.                 '--system',
  614.                 'admin'])
  615.  
  616.         
  617.         try:
  618.             admin_group = grp.getgrnam('admin').gr_mem
  619.         except KeyError:
  620.             e = None
  621.             logging.warning('adding the admin group failed (%s)' % e)
  622.             return None
  623.  
  624.         if os.environ.has_key('SUDO_USER') and os.environ['SUDO_USER'] not in admin_group:
  625.             admin_user = os.environ['SUDO_USER']
  626.             logging.info('SUDO_USER=%s is not in admin group' % admin_user)
  627.             cmd = [
  628.                 'usermod',
  629.                 '-a',
  630.                 '-G',
  631.                 'admin',
  632.                 admin_user]
  633.             res = subprocess.call(cmd)
  634.             logging.debug('cmd: %s returned %i' % (cmd, res))
  635.         
  636.  
  637.     
  638.     def _checkVideoDriver(self, name):
  639.         ''' check if the given driver is in use in xorg.conf '''
  640.         XORG = '/etc/X11/xorg.conf'
  641.         if not os.path.exists(XORG):
  642.             return False
  643.         for line in open(XORG):
  644.             s = line.split('#')[0].strip()
  645.             if s.lower().startswith('driver') and s.endswith('"%s"' % name):
  646.                 return True
  647.         
  648.         return False
  649.  
  650.     
  651.     def _applyPatches(self, patchdir = './patches'):
  652.         '''
  653.         helper that applies the patches in patchdir. the format is
  654.         _path_to_file.md5sum
  655.         
  656.         and it will apply the diff to that file if the md5sum
  657.         matches
  658.         '''
  659.         if not os.path.exists(patchdir):
  660.             logging.debug('no patchdir')
  661.             return None
  662.         if not 'PATH' in os.environ and _[1]:
  663.             logging.debug("no binary 'patch' found in PATH")
  664.             return None
  665.         for f in os.listdir(patchdir):
  666.             logging.debug("check if patch '%s' needs to be applied" % f)
  667.             (encoded_path, md5sum) = string.split(f, '.', 1)
  668.             path = encoded_path.replace('_', '/')
  669.             if os.path.exists(path) and md5(open(path).read()).hexdigest() == md5sum:
  670.                 logging.info("applying '%s'" % f)
  671.                 res = call([
  672.                     'patch',
  673.                     '--dry-run',
  674.                     '-s',
  675.                     '-p0',
  676.                     '-i',
  677.                     patchdir + '/' + f])
  678.                 if res == 0:
  679.                     res = call([
  680.                         'patch',
  681.                         '-p0',
  682.                         '-s',
  683.                         '-i',
  684.                         patchdir + '/' + f])
  685.                     logging.info("applied '%s' with %i status" % (f, res))
  686.                 else:
  687.                     logging.warning("dry run failed, ignoring patch '%s'" % f)
  688.             res == 0
  689.         
  690.  
  691.     
  692.     def _supportInModaliases(self, xorgdrivername, modaliasesdir = './modaliases', lspci = None):
  693.         ''' 
  694.         Check if xorgdriver will work on this hardware
  695.  
  696.         This helper will check with the modaliasesdir if the given 
  697.         xorgdriver will work on this hardware (or the hardware given
  698.         via the lspci argument)
  699.         '''
  700.         if not lspci:
  701.             lspci = set()
  702.             p = subprocess.Popen([
  703.                 'lspci',
  704.                 '-n'], stdout = subprocess.PIPE)
  705.             for line in p.communicate()[0].split('\n'):
  706.                 if line:
  707.                     lspci.add(line.split()[2])
  708.                     continue
  709.             
  710.         
  711.         for filename in os.listdir(modaliasesdir):
  712.             for line in open(os.path.join(modaliasesdir, filename)):
  713.                 line = line.strip()
  714.                 if line == '' or line.startswith('#'):
  715.                     continue
  716.                 
  717.                 (key, pciid, xorgdriver, pkgname) = line.split()
  718.                 if xorgdriver != xorgdrivername:
  719.                     continue
  720.                 
  721.                 m = re.match('pci:v0000(.+)d0000(.+)sv.*', pciid)
  722.                 if m:
  723.                     matchid = '%s:%s' % (m.group(1), m.group(2))
  724.                     if matchid.lower() in lspci:
  725.                         logging.debug("found system pciid '%s' in modaliases" % matchid)
  726.                         return True
  727.                     continue
  728.                 matchid.lower() in lspci
  729.             
  730.         
  731.         logging.debug('checking for %s support in modaliases but none found' % xorgdrivername)
  732.         return False
  733.  
  734.  
  735.