2015-07-21 15:55:02 +00:00
|
|
|
# Copyright 2015 Canonical, Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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 Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-01-13 01:42:31 +00:00
|
|
|
from concurrent import futures
|
2017-09-08 03:59:30 +00:00
|
|
|
import fcntl
|
2015-07-21 15:55:02 +00:00
|
|
|
import logging
|
2017-09-08 03:59:30 +00:00
|
|
|
import sys
|
2017-01-17 01:26:07 +00:00
|
|
|
|
2015-07-21 15:55:02 +00:00
|
|
|
import urwid
|
2017-11-16 00:29:22 +00:00
|
|
|
import yaml
|
2017-01-13 01:42:31 +00:00
|
|
|
|
2016-06-30 18:17:01 +00:00
|
|
|
from subiquitycore.signals import Signal
|
|
|
|
from subiquitycore.prober import Prober, ProberException
|
2015-07-21 15:55:02 +00:00
|
|
|
|
2016-06-30 18:17:01 +00:00
|
|
|
log = logging.getLogger('subiquitycore.core')
|
2015-07-21 15:55:02 +00:00
|
|
|
|
|
|
|
|
2016-07-25 00:38:19 +00:00
|
|
|
class ApplicationError(Exception):
|
2015-07-21 15:55:02 +00:00
|
|
|
""" Basecontroller exception """
|
|
|
|
pass
|
|
|
|
|
2017-09-08 03:59:30 +00:00
|
|
|
# The next little bit is cribbed from
|
|
|
|
# https://github.com/EvanPurkhiser/linux-vt-setcolors/blob/master/setcolors.c:
|
|
|
|
|
|
|
|
# From uapi/linux/kd.h:
|
|
|
|
KDGKBTYPE = 0x4B33 # get keyboard type
|
|
|
|
GIO_CMAP = 0x4B70 # gets colour palette on VGA+
|
|
|
|
PIO_CMAP = 0x4B71 # sets colour palette on VGA+
|
2017-09-12 13:28:08 +00:00
|
|
|
UO_R, UO_G, UO_B = 0xe9, 0x54, 0x20
|
2017-09-08 03:59:30 +00:00
|
|
|
|
|
|
|
|
2017-11-21 22:26:47 +00:00
|
|
|
class ISO_8613_3_Screen(urwid.raw_display.Screen):
|
|
|
|
|
|
|
|
def __init__(self, pal):
|
|
|
|
from subiquity.palette import URWID_16_NAMES
|
|
|
|
d = {}
|
|
|
|
for i, n in enumerate(URWID_16_NAMES):
|
|
|
|
d[n] = pal[i]
|
|
|
|
self._fg_to_rgb = d.copy()
|
|
|
|
self._fg_to_rgb['default'] = pal[0]
|
|
|
|
self._bg_to_rgb = d.copy()
|
|
|
|
self._bg_to_rgb['default'] = pal[7]
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def _attrspec_to_escape(self, a):
|
|
|
|
f_r, f_g, f_b = self._fg_to_rgb[a.foreground]
|
|
|
|
b_r, b_g, b_b = self._bg_to_rgb[a.background]
|
|
|
|
return "\x1b[38;2;{};{};{};48;2;{};{};{}m".format(f_r, f_g, f_b, b_r, b_g, b_b)
|
2017-09-12 13:28:08 +00:00
|
|
|
|
2017-09-08 03:59:30 +00:00
|
|
|
|
|
|
|
def is_linux_tty():
|
|
|
|
try:
|
|
|
|
r = fcntl.ioctl(sys.stdout.fileno(), KDGKBTYPE, ' ')
|
|
|
|
except IOError as e:
|
|
|
|
log.debug("KDGKBTYPE failed %r", e)
|
|
|
|
return False
|
|
|
|
log.debug("KDGKBTYPE returned %r", r)
|
|
|
|
return r == b'\x02'
|
|
|
|
|
2015-07-21 15:55:02 +00:00
|
|
|
|
2017-11-21 22:26:47 +00:00
|
|
|
def setup_screen(pal):
|
|
|
|
if is_linux_tty():
|
|
|
|
curpal = bytearray(16*3)
|
|
|
|
fcntl.ioctl(sys.stdout.fileno(), GIO_CMAP, curpal)
|
|
|
|
for i in range(8):
|
|
|
|
for j in range(3):
|
|
|
|
curpal[i*3+j] = pal[i][j]
|
|
|
|
fcntl.ioctl(sys.stdout.fileno(), PIO_CMAP, curpal)
|
|
|
|
return urwid.raw_display.Screen()
|
|
|
|
else:
|
|
|
|
return ISO_8613_3_Screen(pal)
|
|
|
|
|
|
|
|
|
2016-07-25 00:38:19 +00:00
|
|
|
class Application:
|
|
|
|
|
2016-07-25 00:51:39 +00:00
|
|
|
# A concrete subclass must set project and controllers attributes, e.g.:
|
|
|
|
#
|
|
|
|
# project = "subiquity"
|
2016-09-27 02:33:54 +00:00
|
|
|
# controllers = [
|
|
|
|
# "Welcome",
|
|
|
|
# "Installpath",
|
|
|
|
# "Network",
|
|
|
|
# "Filesystem",
|
|
|
|
# "Identity",
|
|
|
|
# "InstallProgress",
|
|
|
|
# "Login",
|
|
|
|
# ]
|
2016-09-29 01:06:09 +00:00
|
|
|
# The 'next-screen' and 'prev-screen' signals move through the list of
|
|
|
|
# controllers in order, calling the default method on the controller
|
|
|
|
# instance.
|
2016-06-30 18:50:21 +00:00
|
|
|
|
2015-07-21 15:55:02 +00:00
|
|
|
def __init__(self, ui, opts):
|
2015-10-23 15:03:04 +00:00
|
|
|
try:
|
|
|
|
prober = Prober(opts)
|
|
|
|
except ProberException as e:
|
|
|
|
err = "Prober init failed: {}".format(e)
|
|
|
|
log.exception(err)
|
2016-07-25 00:38:19 +00:00
|
|
|
raise ApplicationError(err)
|
2015-10-23 15:03:04 +00:00
|
|
|
|
2017-03-16 09:52:05 +00:00
|
|
|
opts.project = self.project
|
|
|
|
|
2017-11-16 00:29:22 +00:00
|
|
|
answers = {}
|
|
|
|
if opts.answers is not None:
|
|
|
|
answers = yaml.safe_load(open(opts.answers).read())
|
|
|
|
log.debug("Loaded answers %s", answers)
|
|
|
|
|
2015-08-31 15:55:46 +00:00
|
|
|
self.common = {
|
|
|
|
"ui": ui,
|
|
|
|
"opts": opts,
|
|
|
|
"signal": Signal(),
|
2015-10-23 15:03:04 +00:00
|
|
|
"prober": prober,
|
2017-01-13 01:42:31 +00:00
|
|
|
"loop": None,
|
|
|
|
"pool": futures.ThreadPoolExecutor(1),
|
2017-11-16 00:29:22 +00:00
|
|
|
"answers": answers,
|
2015-08-31 15:55:46 +00:00
|
|
|
}
|
2016-11-22 23:08:36 +00:00
|
|
|
if opts.screens:
|
|
|
|
self.controllers = [c for c in self.controllers if c in opts.screens]
|
2017-09-26 13:01:22 +00:00
|
|
|
ui.progress_completion = len(self.controllers)
|
2016-09-27 02:33:54 +00:00
|
|
|
self.common['controllers'] = dict.fromkeys(self.controllers)
|
|
|
|
self.controller_index = -1
|
2015-07-21 20:34:46 +00:00
|
|
|
|
2015-08-18 16:29:56 +00:00
|
|
|
def _connect_base_signals(self):
|
2015-07-21 20:34:46 +00:00
|
|
|
""" Connect signals used in the core controller
|
|
|
|
"""
|
|
|
|
signals = []
|
|
|
|
|
2015-07-23 02:31:21 +00:00
|
|
|
signals.append(('quit', self.exit))
|
2016-08-30 22:55:42 +00:00
|
|
|
if self.common['opts'].dry_run:
|
|
|
|
signals.append(('control-x-quit', self.exit))
|
2015-08-24 03:55:45 +00:00
|
|
|
signals.append(('refresh', self.redraw_screen))
|
2016-09-27 02:33:54 +00:00
|
|
|
signals.append(('next-screen', self.next_screen))
|
|
|
|
signals.append(('prev-screen', self.prev_screen))
|
2015-08-31 15:55:46 +00:00
|
|
|
self.common['signal'].connect_signals(signals)
|
2015-08-18 16:29:56 +00:00
|
|
|
|
|
|
|
# Registers signals from each controller
|
2016-09-27 02:33:54 +00:00
|
|
|
for controller, controller_class in self.common['controllers'].items():
|
2015-08-18 16:29:56 +00:00
|
|
|
controller_class.register_signals()
|
2015-08-31 15:55:46 +00:00
|
|
|
log.debug(self.common['signal'])
|
2015-07-21 20:34:46 +00:00
|
|
|
|
2016-09-27 02:33:54 +00:00
|
|
|
def next_screen(self, *args):
|
|
|
|
self.controller_index += 1
|
|
|
|
if self.controller_index >= len(self.controllers):
|
|
|
|
self.exit()
|
2017-09-26 13:01:22 +00:00
|
|
|
self.common['ui'].progress_current += 1
|
2016-09-27 02:33:54 +00:00
|
|
|
controller_name = self.controllers[self.controller_index]
|
2017-03-06 01:59:05 +00:00
|
|
|
log.debug("moving to screen %s", controller_name)
|
2016-09-27 02:33:54 +00:00
|
|
|
next_controller = self.common['controllers'][controller_name]
|
|
|
|
next_controller.default()
|
|
|
|
|
|
|
|
def prev_screen(self, *args):
|
|
|
|
if self.controller_index == 0:
|
|
|
|
return
|
|
|
|
self.controller_index -= 1
|
|
|
|
if self.controller_index >= len(self.controllers):
|
|
|
|
self.exit()
|
2017-09-26 13:01:22 +00:00
|
|
|
self.common['ui'].progress_current -= 1
|
2016-09-27 02:33:54 +00:00
|
|
|
controller_name = self.controllers[self.controller_index]
|
|
|
|
next_controller = self.common['controllers'][controller_name]
|
|
|
|
next_controller.default()
|
|
|
|
|
2015-07-21 15:55:02 +00:00
|
|
|
# EventLoop -------------------------------------------------------------------
|
|
|
|
def redraw_screen(self):
|
2017-01-13 01:48:25 +00:00
|
|
|
if self.common['loop'] is not None:
|
2015-07-21 15:55:02 +00:00
|
|
|
try:
|
2015-08-31 15:55:46 +00:00
|
|
|
self.common['loop'].draw_screen()
|
2015-07-21 15:55:02 +00:00
|
|
|
except AssertionError as e:
|
|
|
|
log.critical("Redraw screen error: {}".format(e))
|
|
|
|
|
|
|
|
def exit(self):
|
|
|
|
raise urwid.ExitMainLoop()
|
|
|
|
|
|
|
|
def header_hotkeys(self, key):
|
2015-10-21 18:03:15 +00:00
|
|
|
return False
|
2015-07-21 15:55:02 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
if not hasattr(self, 'loop'):
|
2017-09-07 09:37:48 +00:00
|
|
|
palette = self.STYLES
|
2017-11-21 22:26:47 +00:00
|
|
|
if self.common['opts'].run_on_serial:
|
|
|
|
palette = self.STYLES_MONO
|
|
|
|
screen = urwid.raw_display.Screen()
|
|
|
|
else:
|
|
|
|
screen = setup_screen(self.PALETTE)
|
2015-07-21 15:55:02 +00:00
|
|
|
additional_opts = {
|
2017-11-21 22:26:47 +00:00
|
|
|
'screen': screen,
|
2015-07-21 15:55:02 +00:00
|
|
|
'unhandled_input': self.header_hotkeys,
|
2017-01-09 03:11:32 +00:00
|
|
|
'handle_mouse': False,
|
|
|
|
'pop_ups': True,
|
2015-07-21 15:55:02 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 15:55:46 +00:00
|
|
|
self.common['loop'] = urwid.MainLoop(
|
2017-01-17 00:48:21 +00:00
|
|
|
self.common['ui'], palette, **additional_opts)
|
2015-08-31 15:55:46 +00:00
|
|
|
log.debug("Running event loop: {}".format(
|
|
|
|
self.common['loop'].event_loop))
|
2015-07-21 15:55:02 +00:00
|
|
|
|
2017-11-14 21:30:09 +00:00
|
|
|
self.common['base_model'] = self.model_class(self.common)
|
|
|
|
|
2015-07-21 15:55:02 +00:00
|
|
|
try:
|
2017-01-13 01:48:25 +00:00
|
|
|
self.common['loop'].set_alarm_in(0.05, self.next_screen)
|
2017-01-17 00:48:21 +00:00
|
|
|
controllers_mod = __import__('%s.controllers' % self.project, None, None, [''])
|
2017-09-26 13:01:22 +00:00
|
|
|
for k in self.controllers:
|
2015-08-31 15:55:46 +00:00
|
|
|
log.debug("Importing controller: {}".format(k))
|
2017-01-17 00:48:21 +00:00
|
|
|
klass = getattr(controllers_mod, k+"Controller")
|
2016-09-27 02:33:54 +00:00
|
|
|
self.common['controllers'][k] = klass(self.common)
|
|
|
|
log.debug("*** %s", self.common['controllers'])
|
2015-08-31 15:55:46 +00:00
|
|
|
self._connect_base_signals()
|
|
|
|
self.common['loop'].run()
|
2015-07-21 15:55:02 +00:00
|
|
|
except:
|
|
|
|
log.exception("Exception in controller.run():")
|
|
|
|
raise
|