From eb431c28a9e7df0cc8c2a701b9d0f053739dcbb4 Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Tue, 2 Jan 2018 17:09:02 +0100 Subject: [refactor] factor out common functions used from both entrypoints for linux and mac apps. --- src/leap/bitmask/gui/app.py | 37 +++++++----------------------- src/leap/bitmask/gui/app2.py | 44 +++++------------------------------- src/leap/bitmask/gui/housekeeping.py | 44 ++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 67 deletions(-) create mode 100644 src/leap/bitmask/gui/housekeeping.py (limited to 'src/leap') diff --git a/src/leap/bitmask/gui/app.py b/src/leap/bitmask/gui/app.py index 03d5b5c4..a616dc3b 100644 --- a/src/leap/bitmask/gui/app.py +++ b/src/leap/bitmask/gui/app.py @@ -35,6 +35,9 @@ from leap.bitmask.core.launcher import run_bitmaskd, pid from leap.bitmask.gui import app_rc from leap.common.config import get_path_prefix +from .housekeeping import cleanup, terminate, reset_authtoken +from .housekeeping import get_authenticated_url + from .systray import WithTrayIcon if platform.system() == 'Windows': @@ -87,24 +90,8 @@ class BrowserWindow(QWebView, WithTrayIcon): def __init__(self, *args, **kw): url = kw.pop('url', None) - first = False if not url: - url = "http://localhost:7070" - path = os.path.join(get_path_prefix(), 'leap', 'authtoken') - waiting = 20 - while not os.path.isfile(path): - if waiting == 0: - # If we arrive here, something really messed up happened, - # because touching the token file is one of the first - # things the backend does, and this BrowserWindow - # should be called *right after* launching the backend. - raise NoAuthToken( - 'No authentication token found!') - time.sleep(0.1) - waiting -= 1 - token = open(path).read().strip() - url += '#' + token - first = True + url = get_authenticated_url() self.url = url self.closing = False @@ -147,16 +134,13 @@ class BrowserWindow(QWebView, WithTrayIcon): self.load(url) def shutdown(self, *args): + global bitmaskd if self.closing: return self.closing = True - global bitmaskd bitmaskd.join() - if os.path.isfile(pid): - with open(pid) as f: - pidno = int(f.read()) - print('[bitmask] terminating bitmaskd...') - os.kill(pidno, signal.SIGTERM) + terminate(pid) + cleanup() print('[bitmask] shutting down gui...') try: self.stop() @@ -269,12 +253,7 @@ def start_app(): from leap.bitmask.cli import bitmask_cli return bitmask_cli.main() - prev_auth = os.path.join(get_path_prefix(), 'leap', 'authtoken') - try: - os.remove(prev_auth) - except OSError: - pass - + reset_authtoken() launch_gui() diff --git a/src/leap/bitmask/gui/app2.py b/src/leap/bitmask/gui/app2.py index 8690f88f..649d5deb 100644 --- a/src/leap/bitmask/gui/app2.py +++ b/src/leap/bitmask/gui/app2.py @@ -43,6 +43,8 @@ from leap.bitmask.core.launcher import run_bitmaskd, pid from leap.common.config import get_path_prefix from leap.bitmask.gui.systray import WithTrayIcon +from leap.bitmask.gui.housekeeping import cleanup, terminate, reset_authtoken +from leap.bitmask.gui.housekeeping import get_authenticated_url DEBUG = os.environ.get("DEBUG", False) @@ -94,24 +96,8 @@ class BrowserWindow(object): def __init__(self, *args, **kw): url = kw.pop('url', None) - first = False if not url: - url = "http://localhost:7070" - path = os.path.join(get_path_prefix(), 'leap', 'authtoken') - waiting = 20 - while not os.path.isfile(path): - if waiting == 0: - # If we arrive here, something really messed up happened, - # because touching the token file is one of the first - # things the backend does, and this BrowserWindow - # should be called *right after* launching the backend. - raise NoAuthToken( - 'No authentication token found!') - time.sleep(0.1) - waiting -= 1 - token = open(path).read().strip() - url += '#' + token - first = True + url = get_authenticated_url() self.url = url self.closing = False @@ -128,22 +114,10 @@ class BrowserWindow(object): return self.closing = True bitmaskd.join() - if os.path.isfile(pid): - with open(pid) as f: - pidno = int(f.read()) - print('[bitmask] terminating bitmaskd') - os.kill(pidno, signal.SIGTERM) - self.cleanup() + terminate(pid) + cleanup() print('[bitmask] shutting down gui') - def cleanup(self): - print('[bitmask] cleaning up files') - base = os.path.join(get_path_prefix(), 'leap') - token = os.path.join(base, 'authtoken') - pid = os.path.join(base, 'bitmaskd.pid') - for _f in [token, pid]: - if os.path.isfile(_f): - os.unlink(_f) def launch_gui(): @@ -184,7 +158,6 @@ def launch_gui(): def start_app(): from leap.bitmask.util import STANDALONE - mypid = os.getpid() # Kill a previously-running process @@ -203,12 +176,7 @@ def start_app(): from leap.bitmask.cli import bitmask_cli return bitmask_cli.main() - prev_auth = os.path.join(get_path_prefix(), 'leap', 'authtoken') - try: - os.remove(prev_auth) - except OSError: - pass - + reset_authtoken() launch_gui() diff --git a/src/leap/bitmask/gui/housekeeping.py b/src/leap/bitmask/gui/housekeeping.py new file mode 100644 index 00000000..3202f5e6 --- /dev/null +++ b/src/leap/bitmask/gui/housekeeping.py @@ -0,0 +1,44 @@ +import os + +from leap.common.config import get_path_prefix + +def get_authenticated_url(): + url = "http://localhost:7070" + path = os.path.join(get_path_prefix(), 'leap', 'authtoken') + waiting = 20 + while not os.path.isfile(path): + if waiting == 0: + # If we arrive here, something really messed up happened, + # because touching the token file is one of the first + # things the backend does, and this BrowserWindow + # should be called *right after* launching the backend. + raise NoAuthToken( + 'No authentication token found!') + time.sleep(0.1) + waiting -= 1 + token = open(path).read().strip() + url += '#' + token + return url + +def terminate(pid): + if os.path.isfile(pid): + with open(pid) as f: + pidno = int(f.read()) + print('[bitmask] terminating bitmaskd...') + os.kill(pidno, signal.SIGTERM) + +def reset_authtoken(): + prev_auth = os.path.join(get_path_prefix(), 'leap', 'authtoken') + try: + os.remove(prev_auth) + except OSError: + pass + +def cleanup(): + print('[bitmask] cleaning up files') + base = os.path.join(get_path_prefix(), 'leap') + token = os.path.join(base, 'authtoken') + pid = os.path.join(base, 'bitmaskd.pid') + for _f in [token, pid]: + if os.path.isfile(_f): + os.unlink(_f) -- cgit v1.2.3