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 / DistUpgradeFetcherCore.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  9.6 KB  |  268 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from string import Template
  5. import os
  6. import apt_pkg
  7. import apt
  8. import tarfile
  9. import socket
  10. import urlparse
  11. import urllib2
  12. import tempfile
  13. import shutil
  14. import sys
  15. import GnuPGInterface
  16. from gettext import gettext as _
  17. from aptsources.sourceslist import SourcesList
  18. from utils import *
  19.  
  20. class DistUpgradeFetcherCore(object):
  21.     ''' base class (without GUI) for the upgrade fetcher '''
  22.     DEFAULT_MIRROR = 'http://archive.ubuntu.com/ubuntu'
  23.     DEFAULT_COMPONENT = 'main'
  24.     DEBUG = 'DEBUG_UPDATE_MANAGER' in os.environ
  25.     
  26.     def __init__(self, new_dist, progress):
  27.         self.new_dist = new_dist
  28.         self.current_dist_name = get_dist()
  29.         self._progress = progress
  30.         self.run_options = []
  31.  
  32.     
  33.     def _debug(self, msg):
  34.         ''' helper to show debug information '''
  35.         if self.DEBUG:
  36.             sys.stderr.write(msg + '\n')
  37.         
  38.  
  39.     
  40.     def showReleaseNotes(self):
  41.         return True
  42.  
  43.     
  44.     def error(self, summary, message):
  45.         ''' dummy implementation for error display, should be overwriten
  46.             by subclasses that want to more fancy method
  47.         '''
  48.         print summary
  49.         print message
  50.         return False
  51.  
  52.     
  53.     def authenticate(self):
  54.         if self.new_dist.upgradeToolSig:
  55.             f = self.tmpdir + '/' + os.path.basename(self.new_dist.upgradeTool)
  56.             sig = self.tmpdir + '/' + os.path.basename(self.new_dist.upgradeToolSig)
  57.             print "authenticate '%s' against '%s' " % (os.path.basename(f), os.path.basename(sig))
  58.             if not self.gpgauthenticate(f, sig):
  59.                 return False
  60.         
  61.         return True
  62.  
  63.     
  64.     def gpgauthenticate(self, file, signature, keyring = '/etc/apt/trusted.gpg'):
  65.         ''' authenticated a file against a given signature, if no keyring
  66.             is given use the apt default keyring
  67.         '''
  68.         gpg = GnuPGInterface.GnuPG()
  69.         gpg.options.extra_args = [
  70.             '--no-options',
  71.             '--homedir',
  72.             self.tmpdir,
  73.             '--no-default-keyring',
  74.             '--ignore-time-conflict',
  75.             '--keyring',
  76.             keyring]
  77.         proc = gpg.run([
  78.             '--verify',
  79.             signature,
  80.             file], create_fhs = [
  81.             'status',
  82.             'logger',
  83.             'stderr'])
  84.         gpgres = proc.handles['status'].read()
  85.         
  86.         try:
  87.             proc.wait()
  88.         except IOError:
  89.             e = None
  90.             print 'exception from gpg: %s' % e
  91.             print 'Debug information: '
  92.             print proc.handles['status'].read()
  93.             print proc.handles['stderr'].read()
  94.             print proc.handles['logger'].read()
  95.             return False
  96.  
  97.         if 'VALIDSIG' in gpgres:
  98.             return True
  99.         print 'invalid result from gpg:'
  100.         print gpgres
  101.         return False
  102.  
  103.     
  104.     def extractDistUpgrader(self):
  105.         fname = os.path.join(self.tmpdir, os.path.basename(self.uri))
  106.         print "extracting '%s'" % os.path.basename(fname)
  107.         if not os.path.exists(fname):
  108.             return False
  109.         
  110.         try:
  111.             tar = tarfile.open(self.tmpdir + '/' + os.path.basename(self.uri), 'r')
  112.             for tarinfo in tar:
  113.                 tar.extract(tarinfo)
  114.             
  115.             tar.close()
  116.         except tarfile.ReadError:
  117.             os.path.exists(fname)
  118.             e = os.path.exists(fname)
  119.             logging.error('failed to open tarfile (%s)' % e)
  120.             return False
  121.  
  122.         return True
  123.  
  124.     
  125.     def verifyDistUprader(self):
  126.         self.script = script = '%s/%s' % (self.tmpdir, self.new_dist.name)
  127.         if not os.path.exists(script):
  128.             return self.error(_('Could not run the upgrade tool'), _('Could not run the upgrade tool') + '.  ' + _('This is most likely a bug in the upgrade tool. Please report it as a bug'))
  129.         return True
  130.  
  131.     
  132.     def mirror_from_sources_list(self, uri, default_uri):
  133.         '''
  134.       try to figure what the mirror is from current sources.list
  135.  
  136.       do this by looing for matching DEFAULT_COMPONENT, current dist
  137.       in sources.list and then doing a http HEAD/ftp size request
  138.       to see if the uri is available on this server
  139.       '''
  140.         self._debug('mirror_from_sources_list: %s' % self.current_dist_name)
  141.         sources = SourcesList(withMatcher = False)
  142.         seen = set()
  143.         for e in sources.list:
  144.             if e.disabled and e.invalid or not (e.type == 'deb'):
  145.                 continue
  146.             
  147.             if e.uri in seen:
  148.                 continue
  149.             
  150.             if e.uri.startswith(default_uri) and e.dist == self.current_dist_name and self.DEFAULT_COMPONENT in e.comps:
  151.                 return uri
  152.             if e.dist == self.current_dist_name and 'main' in e.comps:
  153.                 mirror_uri = e.uri + uri[len(default_uri):]
  154.                 if url_downloadable(mirror_uri, self._debug):
  155.                     return mirror_uri
  156.                 seen.add(e.uri)
  157.                 continue
  158.             url_downloadable(mirror_uri, self._debug)
  159.         
  160.         self._debug('no mirror found')
  161.         return ''
  162.  
  163.     
  164.     def _expandUri(self, uri):
  165.         '''
  166.         expand the uri so that it uses a mirror if the url starts
  167.         with a well know string (like archive.ubuntu.com)
  168.         '''
  169.         if uri.startswith(self.DEFAULT_MIRROR):
  170.             self._debug('trying to find suitable mirror')
  171.             new_uri = self.mirror_from_sources_list(uri, self.DEFAULT_MIRROR)
  172.             if new_uri:
  173.                 return new_uri
  174.         
  175.         uri_template = Template(uri)
  176.         m = country_mirror()
  177.         new_uri = uri_template.safe_substitute(countrymirror = m)
  178.         
  179.         try:
  180.             if not url_downloadable(new_uri, self._debug):
  181.                 raise Exception('failed to download %s' % new_uri)
  182.             url_downloadable(new_uri, self._debug)
  183.         except Exception:
  184.             e = None
  185.             self._debug("url '%s' could not be downloaded" % e)
  186.             new_uri = uri_template.safe_substitute(countrymirror = '')
  187.  
  188.         return new_uri
  189.  
  190.     
  191.     def fetchDistUpgrader(self):
  192.         ''' download the tarball with the upgrade script '''
  193.         self.tmpdir = tmpdir = tempfile.mkdtemp()
  194.         os.chdir(tmpdir)
  195.         fetcher = apt_pkg.GetAcquire(self._progress)
  196.         if self.new_dist.upgradeToolSig != None:
  197.             uri = self._expandUri(self.new_dist.upgradeToolSig)
  198.             af = apt_pkg.GetPkgAcqFile(fetcher, uri, descr = _('Upgrade tool signature'))
  199.         
  200.         if self.new_dist.upgradeTool != None:
  201.             self.uri = self._expandUri(self.new_dist.upgradeTool)
  202.             af = apt_pkg.GetPkgAcqFile(fetcher, self.uri, descr = _('Upgrade tool'))
  203.             if fetcher.Run() != fetcher.ResultContinue:
  204.                 return False
  205.             for f in [
  206.                 os.path.basename(self.new_dist.upgradeToolSig),
  207.                 os.path.basename(self.new_dist.upgradeTool)]:
  208.                 if not os.path.exists(f) and os.path.getsize(f) > 0:
  209.                     return False
  210.             
  211.             return True
  212.         return False
  213.  
  214.     
  215.     def runDistUpgrader(self):
  216.         args = [
  217.             self.script] + self.run_options
  218.         if os.getuid() != 0:
  219.             os.execv('/usr/bin/sudo', [
  220.                 'sudo'] + args)
  221.         else:
  222.             os.execv(self.script, args)
  223.  
  224.     
  225.     def cleanup(self):
  226.         os.chdir('..')
  227.         shutil.rmtree(self.tmpdir)
  228.  
  229.     
  230.     def run(self):
  231.         if not self.showReleaseNotes():
  232.             return None
  233.         if not self.fetchDistUpgrader():
  234.             self.error(_('Failed to fetch'), _('Fetching the upgrade failed. There may be a network problem. '))
  235.             return None
  236.         if not self.extractDistUpgrader():
  237.             self.error(_('Failed to extract'), _('Extracting the upgrade failed. There may be a problem with the network or with the server. '))
  238.             return None
  239.         if not self.verifyDistUprader():
  240.             self.error(_('Verfication failed'), _('Verifying the upgrade failed.  There may be a problem with the network or with the server. '))
  241.             self.cleanup()
  242.             return None
  243.         if not self.authenticate():
  244.             self.error(_('Authentication failed'), _('Authenticating the upgrade failed. There may be a problem with the network or with the server. '))
  245.             self.cleanup()
  246.             return None
  247.         
  248.         try:
  249.             self.runDistUpgrader()
  250.         except OSError:
  251.             self.authenticate()
  252.             e = self.authenticate()
  253.             self.verifyDistUprader()
  254.             if e.errno == 13:
  255.                 self.error(_('Can not run the upgrade'), _('This usually is caused by a system were /tmp is mounted noexec. Please remount without noexec and run the upgrade again.'))
  256.                 return False
  257.             self.error(_('Can not run the upgrade'), _("The error message is '%s'." % e.strerror))
  258.         except:
  259.             e.errno == 13
  260.  
  261.         return True
  262.  
  263.  
  264. if __name__ == '__main__':
  265.     d = DistUpgradeFetcherCore(None, None)
  266.     print "got mirror: '%s'" % d.mirror_from_sources_list('http://archive.ubuntu.com/ubuntu/dists/intrepid-proposed/main/dist-upgrader-all/0.93.34/intrepid.tar.gz', 'http://archive.ubuntu.com/ubuntu')
  267.  
  268.