more explicit daemonization

xkb
sezanzeb 3 years ago committed by sezanzeb
parent c78ccc26b5
commit 330113e061

@ -152,19 +152,18 @@ def internals(options):
key-mapper-control should be started with sudo or pkexec for this.
"""
debug = ' -d' if options.debug else ''
if options.command == HELPER:
cmd = ['key-mapper-helper']
cmd = f'key-mapper-helper{debug}'
elif options.command == START_DAEMON:
cmd = ['key-mapper-service', '--hide-info']
cmd = f'key-mapper-service --hide-info{debug}'
else:
return
if options.debug:
cmd.append('-d')
# Popen makes os.system of the main process continue with stuff while
# cmd runs in the background.
subprocess.Popen(cmd)
# daemonize
cmd = f'{cmd} &'
os.system(cmd)
if __name__ == '__main__':

@ -198,9 +198,8 @@ class Daemon:
# Blocks until pkexec is done asking for the password.
# Runs via key-mapper-control so that auth_admin_keep works
# for all pkexec calls of the gui
cmd = 'pkexec key-mapper-control --command start-daemon'
if is_debug():
cmd += ' -d'
debug = ' -d' if is_debug() else ''
cmd = f'pkexec key-mapper-control --command start-daemon {debug}'
# using pkexec will also cause the service to continue running in
# the background after the gui has been closed, which will keep

@ -111,6 +111,8 @@ class Window:
def __init__(self):
self.dbus = None
self.start_processes()
self.selected_device = None
self.selected_preset = None
@ -130,8 +132,6 @@ class Window:
builder.connect_signals(self)
self.builder = builder
self.start_processes()
self.confirm_delete = builder.get_object('confirm-delete')
self.about = builder.get_object('about-dialog')
self.about.connect('delete-event', on_close_about)
@ -169,21 +169,20 @@ class Window:
self.ctrl = False
self.unreleased_warn = 0
if not is_helper_running():
self.show_status(CTX_ERROR, 'The helper did not start')
def start_processes(self):
"""Start helper and daemon via pkexec to run in the background."""
# this function is overwritten in tests
self.dbus = Daemon.connect()
cmd = 'pkexec key-mapper-control --command helper'
if is_debug:
cmd += ' -d'
debug = ' -d' if is_debug() else ''
cmd = f'pkexec key-mapper-control --command helper {debug}'
logger.debug('Running `%s`', cmd)
os.system(cmd)
if not is_helper_running():
self.show_status(CTX_ERROR, 'The helper did not start')
def show_confirm_delete(self):
"""Blocks until the user decided about an action."""
text = f'Are you sure to delete preset "{self.selected_preset}"?'

@ -249,17 +249,17 @@ class TestControl(unittest.TestCase):
self.assertEqual(config.get('foo'), 'bar')
def test_internals(self):
with mock.patch('subprocess.Popen') as popen_patch:
with mock.patch('os.system') as os_system_patch:
internals(options('helper', None, None, None, False, False, False))
popen_patch.assert_called_once()
self.assertIn('key-mapper-helper', popen_patch.call_args.args[0])
self.assertNotIn('-d', popen_patch.call_args.args[0])
os_system_patch.assert_called_once()
self.assertIn('key-mapper-helper', os_system_patch.call_args.args[0])
self.assertNotIn('-d', os_system_patch.call_args.args[0])
with mock.patch('subprocess.Popen') as popen_patch:
with mock.patch('os.system') as os_system_patch:
internals(options('start-daemon', None, None, None, False, False, True))
popen_patch.assert_called_once()
self.assertIn('key-mapper-service', popen_patch.call_args.args[0])
self.assertIn('-d', popen_patch.call_args.args[0])
os_system_patch.assert_called_once()
self.assertIn('key-mapper-service', os_system_patch.call_args.args[0])
self.assertIn('-d', os_system_patch.call_args.args[0])
if __name__ == "__main__":

Loading…
Cancel
Save