~ubuntu-core-dev/update-manager/main

« back to all changes in this revision

Viewing changes to UpdateManager/DistUpgradeFetcherKDE.py

  • Committer: Michael Terry
  • Date: 2012-06-28 16:15:24 UTC
  • Revision ID: michael.terry@canonical.com-20120628161524-hqs1adtbgl6qahio
drop DistUpgradeFetcher and related bits from this package and move them into ubuntu-release-upgrader

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# DistUpgradeFetcherKDE.py
2
 
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 4; coding: utf-8 -*-
3
 
#
4
 
#  Copyright (c) 2008 Canonical Ltd
5
 
#
6
 
#  Author: Jonathan Riddell <jriddell@ubuntu.com>
7
 
#
8
 
#  This program is free software; you can redistribute it and/or
9
 
#  modify it under the terms of the GNU General Public License as
10
 
#  published by the Free Software Foundation; either version 2 of the
11
 
#  License, or (at your option) any later version.
12
 
#
13
 
#  This program is distributed in the hope that it will be useful,
14
 
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
#  GNU General Public License for more details.
17
 
#
18
 
#  You should have received a copy of the GNU General Public License
19
 
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
 
21
 
from __future__ import absolute_import
22
 
 
23
 
from PyKDE4.kdecore import ki18n, KAboutData, KCmdLineOptions, KCmdLineArgs
24
 
from PyKDE4.kdeui import KIcon, KMessageBox, KApplication, KStandardGuiItem
25
 
from PyQt4.QtCore import QDir, QTimer
26
 
from PyQt4.QtGui import QDialog, QDialogButtonBox
27
 
from PyQt4 import uic
28
 
 
29
 
import apt_pkg
30
 
import sys
31
 
 
32
 
from .Core.utils import inhibit_sleep, allow_sleep
33
 
from .Core.DistUpgradeFetcherCore import DistUpgradeFetcherCore
34
 
from gettext import gettext as _
35
 
try:
36
 
    from urllib.request import urlopen
37
 
    from urllib.error import HTTPError
38
 
except ImportError:
39
 
    from urllib2 import urlopen, HTTPError
40
 
import os
41
 
 
42
 
from .Core.MetaRelease import MetaReleaseCore
43
 
import time
44
 
import apt
45
 
 
46
 
 
47
 
class DistUpgradeFetcherKDE(DistUpgradeFetcherCore):
48
 
    """A small application run by Adept to download, verify
49
 
    and run the dist-upgrade tool"""
50
 
 
51
 
    def __init__(self, useDevelopmentRelease=False, useProposed=False):
52
 
        self.useDevelopmentRelease = useDevelopmentRelease
53
 
        self.useProposed = useProposed
54
 
        metaRelease = MetaReleaseCore(useDevelopmentRelease, useProposed)
55
 
        while metaRelease.downloading:
56
 
            time.sleep(0.2)
57
 
        if metaRelease.new_dist is None and __name__ == "__main__":
58
 
            sys.exit()
59
 
        elif metaRelease.new_dist is None:
60
 
            return
61
 
 
62
 
        self.progressDialogue = QDialog()
63
 
        if os.path.exists("fetch-progress.ui"):
64
 
            self.APPDIR = QDir.currentPath()
65
 
        else:
66
 
            self.APPDIR = "/usr/share/update-manager"
67
 
 
68
 
        uic.loadUi(self.APPDIR + "/fetch-progress.ui", self.progressDialogue)
69
 
        self.progressDialogue.setWindowIcon(KIcon("system-software-update"))
70
 
        self.progressDialogue.setWindowTitle(_("Upgrade"))
71
 
        self.progress = KDEAcquireProgressAdapter(
72
 
            self.progressDialogue.installationProgress,
73
 
            self.progressDialogue.installingLabel,
74
 
            None)
75
 
        DistUpgradeFetcherCore.__init__(self, metaRelease.new_dist,
76
 
                                        self.progress)
77
 
 
78
 
    def error(self, summary, message):
79
 
        KMessageBox.sorry(None, message, summary)
80
 
 
81
 
    def runDistUpgrader(self):
82
 
        inhibit_sleep()
83
 
        # now run it with sudo
84
 
        if os.getuid() != 0:
85
 
            os.execv("/usr/bin/kdesudo",
86
 
                     ["kdesudo",
87
 
                      self.script + " --frontend=DistUpgradeViewKDE"])
88
 
        else:
89
 
            os.execv(self.script,
90
 
                     [self.script] + ["--frontend=DistUpgradeViewKDE"] +
91
 
                     self.run_options)
92
 
        # we shouldn't come to this point, but if we do, undo our
93
 
        # inhibit sleep
94
 
        allow_sleep()
95
 
 
96
 
    def showReleaseNotes(self):
97
 
        # FIXME: care about i18n! (append -$lang or something)
98
 
        self.dialogue = QDialog()
99
 
        uic.loadUi(self.APPDIR + "/dialog_release_notes.ui", self.dialogue)
100
 
        upgradeButton = self.dialogue.buttonBox.button(QDialogButtonBox.Ok)
101
 
        upgradeButton.setText(_("Upgrade"))
102
 
        upgradeButton.setIcon(KIcon("dialog-ok"))
103
 
        cancelButton = self.dialogue.buttonBox.button(QDialogButtonBox.Cancel)
104
 
        cancelButton.setIcon(KIcon("dialog-cancel"))
105
 
        self.dialogue.setWindowTitle(_("Release Notes"))
106
 
        self.dialogue.show()
107
 
        if self.new_dist.releaseNotesURI is not None:
108
 
            uri = self._expandUri(self.new_dist.releaseNotesURI)
109
 
            # download/display the release notes
110
 
            # FIXME: add some progress reporting here
111
 
            result = None
112
 
            try:
113
 
                release_notes = urlopen(uri)
114
 
                notes = release_notes.read().decode("UTF-8", "replace")
115
 
                self.dialogue.scrolled_notes.setText(notes)
116
 
                result = self.dialogue.exec_()
117
 
            except HTTPError:
118
 
                primary = "<span weight=\"bold\" size=\"larger\">%s</span>" % \
119
 
                          _("Could not find the release notes")
120
 
                secondary = _("The server may be overloaded. ")
121
 
                KMessageBox.sorry(None, primary + "<br />" + secondary, "")
122
 
            except IOError:
123
 
                primary = "<span weight=\"bold\" size=\"larger\">%s</span>" % \
124
 
                          _("Could not download the release notes")
125
 
                secondary = _("Please check your internet connection.")
126
 
                KMessageBox.sorry(None, primary + "<br />" + secondary, "")
127
 
            # user clicked cancel
128
 
            if result == QDialog.Accepted:
129
 
                self.progressDialogue.show()
130
 
                return True
131
 
        if __name__ == "__main__":
132
 
            KApplication.kApplication().exit(1)
133
 
        if self.useDevelopmentRelease or self.useProposed:
134
 
            #FIXME why does KApplication.kApplication().exit() crash but
135
 
            # this doesn't?
136
 
            sys.exit()
137
 
        return False
138
 
 
139
 
 
140
 
class KDEAcquireProgressAdapter(apt.progress.base.AcquireProgress):
141
 
    def __init__(self, progress, label, parent):
142
 
        self.progress = progress
143
 
        self.label = label
144
 
        self.parent = parent
145
 
 
146
 
    def start(self):
147
 
        self.label.setText(_("Downloading additional package files..."))
148
 
        self.progress.setValue(0)
149
 
 
150
 
    def stop(self):
151
 
        pass
152
 
 
153
 
    def pulse(self, owner):
154
 
        apt.progress.base.AcquireProgress.pulse(self, owner)
155
 
        self.progress.setValue((self.current_bytes + self.current_items) /
156
 
                               float(self.total_bytes + self.total_items))
157
 
        current_item = self.current_items + 1
158
 
        if current_item > self.total_items:
159
 
            current_item = self.total_items
160
 
        label_text = _("Downloading additional package files...")
161
 
        if self.current_cps > 0:
162
 
            label_text += _("File %s of %s at %sB/s") % (
163
 
                self.current_items, self.total_items,
164
 
                apt_pkg.size_to_str(self.current_cps))
165
 
        else:
166
 
            label_text += _("File %s of %s") % (
167
 
                self.current_items, self.total_items)
168
 
        self.label.setText(label_text)
169
 
        KApplication.kApplication().processEvents()
170
 
        return True
171
 
 
172
 
    def mediaChange(self, medium, drive):
173
 
        msg = _("Please insert '%s' into the drive '%s'") % (medium, drive)
174
 
        #change = QMessageBox.question(None, _("Media Change"), msg,
175
 
        #                              QMessageBox.Ok, QMessageBox.Cancel)
176
 
        change = KMessageBox.questionYesNo(None, _("Media Change"),
177
 
                                           _("Media Change") + "<br>" + msg,
178
 
                                           KStandardGuiItem.ok(),
179
 
                                           KStandardGuiItem.cancel())
180
 
        if change == KMessageBox.Yes:
181
 
            return True
182
 
        return False
183
 
 
184
 
if __name__ == "__main__":
185
 
 
186
 
    appName = "dist-upgrade-fetcher"
187
 
    catalog = ""
188
 
    programName = ki18n("Dist Upgrade Fetcher")
189
 
    version = "0.3.4"
190
 
    description = ki18n("Dist Upgrade Fetcher")
191
 
    license = KAboutData.License_GPL
192
 
    copyright = ki18n("(c) 2008 Canonical Ltd")
193
 
    text = ki18n("none")
194
 
    homePage = "https://launchpad.net/update-manager"
195
 
    bugEmail = ""
196
 
 
197
 
    aboutData = KAboutData(appName, catalog, programName, version, description,
198
 
                           license, copyright, text, homePage, bugEmail)
199
 
 
200
 
    aboutData.addAuthor(ki18n("Jonathan Riddell"), ki18n("Author"))
201
 
 
202
 
    options = KCmdLineOptions()
203
 
 
204
 
    KCmdLineArgs.init(sys.argv, aboutData)
205
 
    KCmdLineArgs.addCmdLineOptions(options)
206
 
 
207
 
    app = KApplication()
208
 
    fetcher = DistUpgradeFetcherKDE()
209
 
    QTimer.singleShot(10, fetcher.run)
210
 
 
211
 
    app.exec_()