Make PEP8 happier

This commit is contained in:
Frédéric Pierret (fepitre) 2019-10-20 13:24:56 +02:00
parent 1d3a214648
commit a5102d752f
No known key found for this signature in database
GPG Key ID: 484010B5CDC576E2
3 changed files with 77 additions and 68 deletions

View File

@ -167,13 +167,13 @@ class QubesBase(qubesadmin.base.PropertyHolder):
def list_vmclass(self): def list_vmclass(self):
"""Call Qubesd in order to obtain the vm classes list""" """Call Qubesd in order to obtain the vm classes list"""
vmclass = self.qubesd_call('dom0', 'admin.vmclass.List')\ vmclass = self.qubesd_call('dom0', 'admin.vmclass.List') \
.decode().splitlines() .decode().splitlines()
return sorted(vmclass) return sorted(vmclass)
def list_deviceclass(self): def list_deviceclass(self):
"""Call Qubesd in order to obtain the device classes list""" """Call Qubesd in order to obtain the device classes list"""
deviceclasses = self.qubesd_call('dom0', 'admin.deviceclass.List')\ deviceclasses = self.qubesd_call('dom0', 'admin.deviceclass.List') \
.decode().splitlines() .decode().splitlines()
return sorted(deviceclasses) return sorted(deviceclasses)

View File

@ -1 +0,0 @@
/home/user/qubes-builder/qubes-src/core-admin-client/qubesadmin

View File

@ -18,7 +18,7 @@
# You should have received a copy of the GNU Lesser General Public License along # You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>. # with this program; if not, see <http://www.gnu.org/licenses/>.
''' GUI daemon launcher tool''' """ GUI daemon launcher tool"""
import os import os
import signal import signal
@ -35,10 +35,12 @@ import qubesadmin
import qubesadmin.exc import qubesadmin.exc
import qubesadmin.tools import qubesadmin.tools
import qubesadmin.vm import qubesadmin.vm
have_events = False have_events = False
try: try:
# pylint: disable=wrong-import-position # pylint: disable=wrong-import-position
import qubesadmin.events import qubesadmin.events
have_events = True have_events = True
except ImportError: except ImportError:
pass pass
@ -47,7 +49,7 @@ GUI_DAEMON_PATH = '/usr/bin/qubes-guid'
QUBES_ICON_DIR = '/usr/share/icons/hicolor/128x128/devices' QUBES_ICON_DIR = '/usr/share/icons/hicolor/128x128/devices'
# "LVDS connected 1024x768+0+0 (normal left inverted right) 304mm x 228mm" # "LVDS connected 1024x768+0+0 (normal left inverted right) 304mm x 228mm"
REGEX_OUTPUT = re.compile(r''' REGEX_OUTPUT = re.compile(r"""
(?x) # ignore whitespace (?x) # ignore whitespace
^ # start of string ^ # start of string
(?P<output>[A-Za-z0-9\-]*)[ ] # LVDS VGA etc (?P<output>[A-Za-z0-9\-]*)[ ] # LVDS VGA etc
@ -67,11 +69,11 @@ REGEX_OUTPUT = re.compile(r'''
)? )?
.* # ignore rest of line .* # ignore rest of line
)? # everything after (dis)connect is optional )? # everything after (dis)connect is optional
''') """)
def get_monitor_layout(): def get_monitor_layout():
'''Get list of monitors and their size/position''' """Get list of monitors and their size/position"""
outputs = [] outputs = []
for line in subprocess.Popen( for line in subprocess.Popen(
@ -112,18 +114,19 @@ def get_monitor_layout():
class GUILauncher(object): class GUILauncher(object):
'''Launch GUI daemon for VMs''' """Launch GUI daemon for VMs"""
def __init__(self, app: qubesadmin.app.QubesBase): def __init__(self, app: qubesadmin.app.QubesBase):
''' Initialize GUILauncher. """ Initialize GUILauncher.
:param app: :py:class:`qubesadmin.Qubes` instance :param app: :py:class:`qubesadmin.Qubes` instance
''' """
self.app = app self.app = app
self.started_processes = {} self.started_processes = {}
@staticmethod @staticmethod
def kde_guid_args(vm): def kde_guid_args(vm):
'''Return KDE-specific arguments for gui-daemon, if applicable''' """Return KDE-specific arguments for gui-daemon, if applicable"""
guid_cmd = [] guid_cmd = []
# Avoid using environment variables for checking the current session, # Avoid using environment variables for checking the current session,
@ -151,11 +154,12 @@ class GUILauncher(object):
guid_cmd += ['-p', guid_cmd += ['-p',
'_KDE_NET_WM_COLOR_SCHEME=s:{}'.format( '_KDE_NET_WM_COLOR_SCHEME=s:{}'.format(
os.path.join(data_dir, os.path.join(data_dir,
'qubes-kde', vm.label.name + '.colors'))] 'qubes-kde',
vm.label.name + '.colors'))]
return guid_cmd return guid_cmd
def common_guid_args(self, vm): def common_guid_args(self, vm):
'''Common qubes-guid arguments for PV(H), HVM and Stubdomain''' """Common qubes-guid arguments for PV(H), HVM and Stubdomain"""
guid_cmd = [GUI_DAEMON_PATH, guid_cmd = [GUI_DAEMON_PATH,
'-N', vm.name, '-N', vm.name,
@ -177,19 +181,19 @@ class GUILauncher(object):
@staticmethod @staticmethod
def guid_pidfile(xid): def guid_pidfile(xid):
'''Helper function to construct a pidfile path''' """Helper function to construct a pidfile path"""
return '/var/run/qubes/guid-running.{}'.format(xid) return '/var/run/qubes/guid-running.{}'.format(xid)
@asyncio.coroutine @asyncio.coroutine
def start_gui_for_vm(self, vm, monitor_layout=None): def start_gui_for_vm(self, vm, monitor_layout=None):
'''Start GUI daemon (qubes-guid) connected directly to a VM """Start GUI daemon (qubes-guid) connected directly to a VM
This function is a coroutine. This function is a coroutine.
:param vm: VM for which start GUI daemon :param vm: VM for which start GUI daemon
:param monitor_layout: monitor layout to send; if None, fetch it from :param monitor_layout: monitor layout to send; if None, fetch it from
local X server. local X server.
''' """
guid_cmd = self.common_guid_args(vm) guid_cmd = self.common_guid_args(vm)
guid_cmd.extend(['-d', str(vm.xid)]) guid_cmd.extend(['-d', str(vm.xid)])
@ -212,10 +216,10 @@ class GUILauncher(object):
@asyncio.coroutine @asyncio.coroutine
def start_gui_for_stubdomain(self, vm, force=False): def start_gui_for_stubdomain(self, vm, force=False):
'''Start GUI daemon (qubes-guid) connected to a stubdomain """Start GUI daemon (qubes-guid) connected to a stubdomain
This function is a coroutine. This function is a coroutine.
''' """
want_stubdom = force want_stubdom = force
if not want_stubdom and \ if not want_stubdom and \
vm.features.check_with_template('gui-emulated', False): vm.features.check_with_template('gui-emulated', False):
@ -239,17 +243,17 @@ class GUILauncher(object):
@asyncio.coroutine @asyncio.coroutine
def start_gui(self, vm, force_stubdom=False, monitor_layout=None): def start_gui(self, vm, force_stubdom=False, monitor_layout=None):
'''Start GUI daemon regardless of start event. """Start GUI daemon regardless of start event.
This function is a coroutine. This function is a coroutine.
:param vm: VM for which GUI daemon should be started :param vm: VM for which GUI daemon should be started
:param force_stubdom: Force GUI daemon for stubdomain, even if the :param force_stubdom: Force GUI daemon for stubdomain, even if the
one for target AppVM is running. one for target AppVM is running.
''' :param monitor_layout: monitor layout configuration
"""
if vm.virt_mode == 'hvm': if vm.virt_mode == 'hvm':
yield from self.start_gui_for_stubdomain(vm, yield from self.start_gui_for_stubdomain(vm, force=force_stubdom)
force=force_stubdom)
if not vm.features.check_with_template('gui', True): if not vm.features.check_with_template('gui', True):
return return
@ -259,7 +263,7 @@ class GUILauncher(object):
@asyncio.coroutine @asyncio.coroutine
def send_monitor_layout(self, vm, layout=None, startup=False): def send_monitor_layout(self, vm, layout=None, startup=False):
'''Send monitor layout to a given VM """Send monitor layout to a given VM
This function is a coroutine. This function is a coroutine.
@ -268,7 +272,7 @@ class GUILauncher(object):
local X server. local X server.
:param startup: :param startup:
:return: None :return: None
''' """
# pylint: disable=no-self-use # pylint: disable=no-self-use
if vm.features.check_with_template('no-monitor-layout', False) \ if vm.features.check_with_template('no-monitor-layout', False) \
or not vm.is_running(): or not vm.is_running():
@ -293,8 +297,10 @@ class GUILauncher(object):
pass pass
try: try:
yield from asyncio.get_event_loop().run_in_executor(None, yield from asyncio.get_event_loop(). \
functools.partial(vm.run_service_for_stdio, run_in_executor(None,
functools.partial(
vm.run_service_for_stdio,
'qubes.SetMonitorLayout', 'qubes.SetMonitorLayout',
input=''.join(layout).encode(), input=''.join(layout).encode(),
autostart=False)) autostart=False))
@ -302,7 +308,7 @@ class GUILauncher(object):
vm.log.warning('Failed to send monitor layout: %s', e.stderr) vm.log.warning('Failed to send monitor layout: %s', e.stderr)
def send_monitor_layout_all(self): def send_monitor_layout_all(self):
'''Send monitor layout to all (running) VMs''' """Send monitor layout to all (running) VMs"""
monitor_layout = get_monitor_layout() monitor_layout = get_monitor_layout()
for vm in self.app.domains: for vm in self.app.domains:
if vm.klass == 'AdminVM': if vm.klass == 'AdminVM':
@ -314,7 +320,7 @@ class GUILauncher(object):
monitor_layout)) monitor_layout))
def on_domain_spawn(self, vm, _event, **kwargs): def on_domain_spawn(self, vm, _event, **kwargs):
'''Handler of 'domain-spawn' event, starts GUI daemon for stubdomain''' """Handler of 'domain-spawn' event, starts GUI daemon for stubdomain"""
try: try:
if not vm.features.check_with_template('gui', True): if not vm.features.check_with_template('gui', True):
return return
@ -325,7 +331,7 @@ class GUILauncher(object):
vm.log.warning('Failed to start GUI for %s: %s', vm.name, str(e)) vm.log.warning('Failed to start GUI for %s: %s', vm.name, str(e))
def on_domain_start(self, vm, _event, **kwargs): def on_domain_start(self, vm, _event, **kwargs):
'''Handler of 'domain-start' event, starts GUI daemon for actual VM''' """Handler of 'domain-start' event, starts GUI daemon for actual VM"""
try: try:
if not vm.features.check_with_template('gui', True): if not vm.features.check_with_template('gui', True):
return return
@ -335,8 +341,8 @@ class GUILauncher(object):
vm.log.warning('Failed to start GUI for %s: %s', vm.name, str(e)) vm.log.warning('Failed to start GUI for %s: %s', vm.name, str(e))
def on_connection_established(self, _subject, _event, **_kwargs): def on_connection_established(self, _subject, _event, **_kwargs):
'''Handler of 'connection-established' event, used to launch GUI """Handler of 'connection-established' event, used to launch GUI
daemon for domains started before this tool. ''' daemon for domains started before this tool. """
monitor_layout = get_monitor_layout() monitor_layout = get_monitor_layout()
self.app.domains.clear_cache() self.app.domains.clear_cache()
@ -347,8 +353,8 @@ class GUILauncher(object):
continue continue
power_state = vm.get_power_state() power_state = vm.get_power_state()
if power_state == 'Running': if power_state == 'Running':
asyncio.ensure_future(self.start_gui(vm, asyncio.ensure_future(
monitor_layout=monitor_layout)) self.start_gui(vm, monitor_layout=monitor_layout))
elif power_state == 'Transient': elif power_state == 'Transient':
# it is still starting, we'll get 'domain-start' event when # it is still starting, we'll get 'domain-start' event when
# fully started # fully started
@ -356,21 +362,23 @@ class GUILauncher(object):
asyncio.ensure_future(self.start_gui_for_stubdomain(vm)) asyncio.ensure_future(self.start_gui_for_stubdomain(vm))
def register_events(self, events): def register_events(self, events):
'''Register domain startup events in app.events dispatcher''' """Register domain startup events in app.events dispatcher"""
events.add_handler('domain-spawn', self.on_domain_spawn) events.add_handler('domain-spawn', self.on_domain_spawn)
events.add_handler('domain-start', self.on_domain_start) events.add_handler('domain-start', self.on_domain_start)
events.add_handler('connection-established', events.add_handler('connection-established',
self.on_connection_established) self.on_connection_established)
def x_reader(conn, callback): def x_reader(conn, callback):
'''Try reading something from X connection to check if it's still alive. """Try reading something from X connection to check if it's still alive.
In case it isn't, call *callback*. In case it isn't, call *callback*.
''' """
try: try:
conn.poll_for_event() conn.poll_for_event()
except xcffib.ConnectionException: except xcffib.ConnectionException:
callback() callback()
if 'XDG_RUNTIME_DIR' in os.environ: if 'XDG_RUNTIME_DIR' in os.environ:
pidfile_path = os.path.join(os.environ['XDG_RUNTIME_DIR'], pidfile_path = os.path.join(os.environ['XDG_RUNTIME_DIR'],
'qvm-start-gui.pid') 'qvm-start-gui.pid')
@ -381,18 +389,20 @@ else:
parser = qubesadmin.tools.QubesArgumentParser( parser = qubesadmin.tools.QubesArgumentParser(
description='start GUI for qube(s)', vmname_nargs='*') description='start GUI for qube(s)', vmname_nargs='*')
parser.add_argument('--watch', action='store_true', parser.add_argument('--watch', action='store_true',
help='Keep watching for further domains startups, must be used with --all') help='Keep watching for further domains'
' startups, must be used with --all')
parser.add_argument('--force-stubdomain', action='store_true', parser.add_argument('--force-stubdomain', action='store_true',
help='Start GUI to stubdomain-emulated VGA, even if gui-agent is running ' help='Start GUI to stubdomain-emulated VGA,'
'in the VM') ' even if gui-agent is running in the VM')
parser.add_argument('--pidfile', action='store', default=pidfile_path, parser.add_argument('--pidfile', action='store', default=pidfile_path,
help='Pidfile path to create in --watch mode') help='Pidfile path to create in --watch mode')
parser.add_argument('--notify-monitor-layout', action='store_true', parser.add_argument('--notify-monitor-layout', action='store_true',
help='Notify running instance in --watch mode about changed monitor layout') help='Notify running instance in --watch mode'
' about changed monitor layout')
def main(args=None): def main(args=None):
''' Main function of qvm-start-gui tool''' """ Main function of qvm-start-gui tool"""
args = parser.parse_args(args) args = parser.parse_args(args)
if args.watch and not args.all_domains: if args.watch and not args.all_domains:
parser.error('--watch option must be used with --all') parser.error('--watch option must be used with --all')