1
# Copyright (C) 2008, 2009 Canonical Ltd.
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License version 3,
5
# as published by the Free Software Foundation.
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
# GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License
13
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
from usbcreator.misc import (
20
USBCreatorProcessException,
24
from threading import Thread, Event
26
from hashlib import md5
27
from usbcreator.misc import MAX_DBUS_TIMEOUT
30
class install(Thread):
31
def __init__(self, source, target, device=None,
32
allow_system_internal=False):
37
self.allow_system_internal = allow_system_internal
38
self._stopevent = Event()
39
logging.debug('install thread source: %s' % source)
40
logging.debug('install thread target: %s' % target)
48
if callable(self.success):
51
def failure(self, message=None):
54
def _failure(self, message=None):
55
logging.critical(message)
56
if callable(self.failure):
60
def progress(self, complete):
61
'''Emitted with an integer percentage of progress completed, time
62
remaining, and speed.'''
65
def progress_message(self, message):
66
'''Emitted with a translated string like "Installing the
71
def retry(self, message):
72
'''Will be called when we need to know if the user wants to try a
73
failed operation again. Must return a boolean value.'''
76
def join(self, timeout=None):
78
Thread.join(self, timeout)
81
if self._stopevent.isSet():
82
logging.debug('Asked by the controlling thread to shut down.')
85
# Exception catching wrapper.
89
if os.path.isfile(self.source):
90
ext = os.path.splitext(self.source)[1].lower()
91
if ext not in ['.iso', '.img']:
92
self._failure(_('The extension "%s" is not supported.') %
94
self.diskimage_install()
96
self.diskimage_install()
98
except Exception as e:
99
# TODO evand 2009-07-25: Bring up our own apport-like utility.
100
logging.exception('Exception raised:')
101
self._failure(_('An uncaught exception was raised:\n%s') % str(e))
103
# Helpers for core routines.
104
def diskimage_install(self):
105
self.progress_message(_('Writing disk image...'))
106
failure_msg = _('Could not write the disk image (%(source)s) to the device'
107
' (%(device)s).') % {'source': self.source,
108
'device': self.device}
112
bus = dbus.SystemBus()
113
obj = bus.get_object('com.ubuntu.USBCreator',
114
'/com/ubuntu/USBCreator')
115
obj.Image(self.source, self.device, self.allow_system_internal,
116
dbus_interface='com.ubuntu.USBCreator',
117
timeout=MAX_DBUS_TIMEOUT)
118
except dbus.DBusException:
119
self._failure(failure_msg)