~brian-murray/update-manager/no-zero-byte-files

« back to all changes in this revision

Viewing changes to UpdateManager/DistUpgradeFetcher.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2007-02-07 18:02:35 UTC
  • Revision ID: james.westby@ubuntu.com-20070207180235-i0mgzoi54v54ezq7
Tags: 0.56
* added --proposed switch to fetch a release-upgrader from a different
  location (similar to the -proposed pocket in the archive)
* split into update-manager and update-manager-core and support
  cli release upgrades with the later
* added fdsend module to support file descriptor passing over sockets
  this will allow a better seperation between frontend and backend
  in the future

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import pygtk
23
23
pygtk.require('2.0')
24
24
import gtk
25
 
import os
26
 
import apt_pkg
27
 
import tarfile
28
 
import urllib2
29
 
import tempfile
30
 
import shutil
31
 
import GnuPGInterface
32
 
from gettext import gettext as _
33
25
 
34
 
import GtkProgress
35
26
from ReleaseNotesViewer import ReleaseNotesViewer
36
27
from Common.utils import *
37
 
 
38
 
class DistUpgradeFetcher(object):
39
 
 
40
 
    def __init__(self, parent, new_dist):
 
28
from Core.DistUpgradeFetcherCore import DistUpgradeFetcherCore
 
29
from gettext import gettext as _
 
30
import urllib2
 
31
 
 
32
class DistUpgradeFetcherGtk(DistUpgradeFetcherCore):
 
33
 
 
34
    def __init__(self, new_dist, progress, parent):
 
35
        DistUpgradeFetcherCore.__init__(self,new_dist,progress)
41
36
        self.parent = parent
42
37
        self.window_main = parent.window_main
43
 
        self.new_dist = new_dist
 
38
 
 
39
    def error(self, summary, message):
 
40
        return error(self.window_main, summary, message)
44
41
 
45
42
    def showReleaseNotes(self):
46
43
      # FIXME: care about i18n! (append -$lang or something)
92
89
              return False
93
90
      return True
94
91
 
95
 
    def authenticate(self):
96
 
        if self.new_dist.upgradeToolSig:
97
 
            f = self.tmpdir+"/"+os.path.basename(self.new_dist.upgradeTool)
98
 
            sig = self.tmpdir+"/"+os.path.basename(self.new_dist.upgradeToolSig)
99
 
            print "authenticate '%s' against '%s' " % (f,sig)
100
 
            if not self.gpgauthenticate(f, sig):
101
 
                return False
102
 
 
103
 
        # we may return False here by default if we want to make a sig
104
 
        # mandatory
105
 
        return True
106
 
 
107
 
    def gpgauthenticate(self, file, signature,
108
 
                        keyring='/etc/apt/trusted.gpg'):
109
 
        """ authenticated a file against a given signature, if no keyring
110
 
            is given use the apt default keyring
111
 
        """
112
 
        gpg = GnuPGInterface.GnuPG()
113
 
        gpg.options.extra_args = ['--no-options',
114
 
                                  '--homedir',self.tmpdir,
115
 
                                  '--no-default-keyring',
116
 
                                  '--keyring', keyring]
117
 
        proc = gpg.run(['--verify', signature, file],
118
 
                       create_fhs=['status','logger','stderr'])
119
 
        gpgres = proc.handles['status'].read()
120
 
        try:
121
 
            proc.wait()
122
 
        except IOError,e:
123
 
            # gnupg returned a problem (non-zero exit)
124
 
            print "exception from gpg: %s" % e
125
 
            print "Debug information: "
126
 
            print proc.handles['status'].read()
127
 
            print proc.handles['stderr'].read()
128
 
            print proc.handles['logger'].read()
129
 
            return False
130
 
        if "VALIDSIG" in gpgres:
131
 
            return True
132
 
        print "invalid result from gpg:"
133
 
        print gpgres
134
 
        return False
135
 
 
136
 
    def extractDistUpgrader(self):
137
 
          # extract the tarbal
138
 
          print "extracting '%s'" % (self.tmpdir+"/"+os.path.basename(self.uri))
139
 
          tar = tarfile.open(self.tmpdir+"/"+os.path.basename(self.uri),"r")
140
 
          for tarinfo in tar:
141
 
              tar.extract(tarinfo)
142
 
          tar.close()
143
 
          return True
144
 
 
145
 
    def verifyDistUprader(self):
146
 
        # FIXME: check a internal dependency file to make sure
147
 
        #        that the script will run correctly
148
 
          
149
 
        # see if we have a script file that we can run
150
 
        self.script = script = "%s/%s" % (self.tmpdir, self.new_dist.name)
151
 
        if not os.path.exists(script):
152
 
            # no script file found in extracted tarbal
153
 
            primary = "<span weight=\"bold\" size=\"larger\">%s</span>" % \
154
 
                      _("Could not run the upgrade tool")
155
 
            secondary = _("This is most likely a bug in the upgrade tool. "
156
 
                          "Please report it as a bug")
157
 
            dialog = gtk.MessageDialog(self.window_main,gtk.DIALOG_MODAL,
158
 
                                       gtk.MESSAGE_ERROR,gtk.BUTTONS_CLOSE,"")
159
 
            dialog.set_title("")
160
 
            dialog.set_markup(primary);
161
 
            dialog.format_secondary_text(secondary);
162
 
            dialog.run()
163
 
            dialog.destroy()
164
 
            return False
165
 
        return True
166
 
 
167
 
    def fetchDistUpgrader(self):
168
 
        # now download the tarball with the upgrade script
169
 
        self.tmpdir = tmpdir = tempfile.mkdtemp()
170
 
        os.chdir(tmpdir)
171
 
 
172
 
        # turn debugging on here (if required)
173
 
        #apt_pkg.Config.Set("Debug::Acquire::http","1")
174
 
 
175
 
        progress = GtkProgress.GtkFetchProgress(self.parent,
176
 
                                                _("Downloading the upgrade "
177
 
                                                  "tool"),
178
 
                                                _("The upgrade tool will "
179
 
                                                  "guide you through the "
180
 
                                                  "upgrade process."))
181
 
        fetcher = apt_pkg.GetAcquire(progress)
182
 
 
183
 
        if self.new_dist.upgradeToolSig != None:
184
 
            uri = self.new_dist.upgradeToolSig
185
 
            af = apt_pkg.GetPkgAcqFile(fetcher,uri, descr=_("Upgrade tool signature"))
186
 
        if self.new_dist.upgradeTool != None:
187
 
            self.uri = self.new_dist.upgradeTool
188
 
            af = apt_pkg.GetPkgAcqFile(fetcher,self.uri, descr=_("Upgrade tool"))
189
 
            if fetcher.Run() != fetcher.ResultContinue:
190
 
                return False
191
 
            return True
192
 
        return False
193
 
 
194
 
    def runDistUpgrader(self):
195
 
        #print "runing: %s" % script
196
 
        if os.getuid() != 0:
197
 
            os.execv("/usr/bin/gksu",["gksu",self.script])
198
 
        else:
199
 
            os.execv(self.script,[self.script])
200
 
 
201
 
    def cleanup(self):
202
 
      # cleanup
203
 
      os.chdir("..")
204
 
      # del tmpdir
205
 
      shutil.rmtree(self.tmpdir)
206
 
 
207
 
    def run(self):
208
 
        # see if we have release notes
209
 
        if not self.showReleaseNotes():
210
 
            return
211
 
        if not self.fetchDistUpgrader():
212
 
            error(self.window_main,
213
 
                  _("Failed to fetch"),
214
 
                  _("Fetching the upgrade failed. There may be a network "
215
 
                    "problem. "))
216
 
            return
217
 
        if not self.extractDistUpgrader():
218
 
            error(self.window_main,
219
 
                  _("Failed to extract"),
220
 
                  _("Extracting the upgrade failed. There may be a problem "
221
 
                  "with the network or with the server. "))
222
 
                  
223
 
            return
224
 
        if not self.verifyDistUprader():
225
 
            error(self.window_main,
226
 
                  _("Verfication failed"),
227
 
                  _("Verifying the upgrade failed.  There may be a problem "
228
 
                    "with the network or with the server. "))
229
 
            self.cleanup()
230
 
            return
231
 
        if not self.authenticate():
232
 
            error(self.window_main,
233
 
                  _("Authentication failed"),
234
 
                  _("Authenticating the upgrade failed. There may be a problem "
235
 
                    "with the network or with the server. "))
236
 
            self.cleanup()
237
 
            return
238
 
        self.runDistUpgrader()
239
 
 
240
 
 
241
92
if __name__ == "__main__":
242
93
    error(None, "summary","message")
243
 
    d = DistUpgradeFetcher(None,None)
 
94
    d = DistUpgradeFetcherGtk(None,None)
244
95
    print d.authenticate('/tmp/Release','/tmp/Release.gpg')
245
96