/usr/lib/python3/dist-packages/systemimage/api.py is in system-image-common 2.2-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | # Copyright (C) 2013-2014 Canonical Ltd.
# Author: Barry Warsaw <barry@ubuntu.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""DBus API mediator."""
__all__ = [
'Mediator',
'Update',
]
import logging
from systemimage.helpers import last_update_date
from systemimage.state import State
log = logging.getLogger('systemimage')
class Update:
"""A representation of the available update."""
def __init__(self, winners=None, error=''):
self._winners = [] if winners is None else winners
self.error = error
@property
def is_available(self):
return len(self._winners) > 0
@property
def size(self):
total_size = 0
for image in self._winners:
total_size += sum(filerec.size for filerec in image.files)
return total_size
@property
def descriptions(self):
return [image.descriptions for image in self._winners]
@property
def version(self):
try:
return str(self._winners[-1].version)
except IndexError:
# No winners.
return ''
@property
def last_update_date(self):
return last_update_date()
class Mediator:
"""This is the DBus API mediator.
It essentially implements the entire DBus API, but at a level below the
mechanics of DBus. Methods of this class are hooked directly into the
DBus layer to satisfy that interface.
"""
def __init__(self, callback=None):
self._state = State()
self._update = None
self._callback = callback
def __repr__(self):
return '<Mediator at 0x{:x} | State at 0x{:x}>'.format(
id(self), id(self._state))
def cancel(self):
self._state.downloader.cancel()
def pause(self):
self._state.downloader.pause()
def resume(self):
self._state.downloader.resume()
def check_for_update(self):
"""Is there an update available for this machine?
:return: Flag indicating whether an update is available or not.
:rtype: bool
"""
if self._update is None:
try:
self._state.run_until('download_files')
except Exception as error:
# Rather than letting this percolate up, eventually reaching
# the GLib main loop and thus triggering apport, Let's log the
# error and set the relevant information in the class.
log.exception('check_for_update failed')
self._update = Update(error=str(error))
else:
self._update = Update(self._state.winner)
return self._update
def download(self):
"""Download the available update."""
# We only want callback progress during the actual download.
callback = self._state.downloader.callback
try:
self._state.downloader.callback = self._callback
self._state.run_until('reboot')
finally:
self._state.downloader.callback = callback
def reboot(self):
"""Issue the reboot."""
# Transition through all remaining states.
list(self._state)
|