better linux launcher
- catch ctrl-c and shut down properly - only run one instance - support custom ports
This commit is contained in:
parent
cd3bbc3cb6
commit
78a8061c8c
1 changed files with 70 additions and 6 deletions
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/env python3
|
||||
import os
|
||||
from os.path import dirname, abspath
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import webbrowser
|
||||
|
@ -28,6 +29,7 @@ class OMLIcon:
|
|||
|
||||
def __init__(self, autostart=False):
|
||||
self.autostart = autostart
|
||||
self.create_pid()
|
||||
if appindicator:
|
||||
self.indicator = appindicator.Indicator.new("OML", icon,
|
||||
appindicator.IndicatorCategory.APPLICATION_STATUS)
|
||||
|
@ -40,7 +42,7 @@ class OMLIcon:
|
|||
self.icon.set_title(title)
|
||||
self.icon.connect("activate", self._click)
|
||||
self.icon.connect("popup-menu", self._click)
|
||||
subprocess.Popen([ctl, 'start'])
|
||||
subprocess.Popen([ctl, 'start'], close_fds=True, preexec_fn=preexec)
|
||||
if not self.autostart:
|
||||
GLib.timeout_add_seconds(1, self._open, None)
|
||||
GLib.timeout_add_seconds(60, self._check, None)
|
||||
|
@ -52,6 +54,38 @@ class OMLIcon:
|
|||
else:
|
||||
self._quit(None)
|
||||
|
||||
def create_pid(self):
|
||||
pid = self.get_pid()
|
||||
if pid:
|
||||
with open(pid, 'w') as fd:
|
||||
fd.write('%s' % os.getpid())
|
||||
|
||||
@classmethod
|
||||
def remove_pid(cls):
|
||||
pid = cls.get_pid()
|
||||
if pid and os.path.exists(pid):
|
||||
os.unlink(pid)
|
||||
|
||||
@classmethod
|
||||
def get_pid(cls):
|
||||
run = '/run/user/%s' % os.getuid()
|
||||
if os.path.exists(run):
|
||||
oml = os.path.join(run, 'openmedialibrary')
|
||||
if not os.path.exists(oml):
|
||||
os.makedirs(oml)
|
||||
pid = os.path.join(oml, 'pid')
|
||||
else:
|
||||
pid = None
|
||||
return pid
|
||||
|
||||
@classmethod
|
||||
def is_running(cls):
|
||||
pid = cls.get_pid()
|
||||
if pid and os.path.exists(pid):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def get_menu(self):
|
||||
menu = Gtk.Menu()
|
||||
t = Gtk.MenuItem(label=title)
|
||||
|
@ -79,15 +113,45 @@ class OMLIcon:
|
|||
|
||||
def _quit(self, q):
|
||||
Gtk.main_quit()
|
||||
p = subprocess.Popen([ctl, 'stop'])
|
||||
self.stop()
|
||||
|
||||
def stop(self, *args, **kwargs):
|
||||
p = subprocess.Popen([ctl, 'stop'], close_fds=True)
|
||||
self.menu = None
|
||||
self.remove_pid()
|
||||
|
||||
def _open(self, *args):
|
||||
url = 'file://' + base + '/openmedialibrary/static/html/load.html'
|
||||
webbrowser.open_new_tab(url)
|
||||
self.load()
|
||||
self.menu = None
|
||||
|
||||
@classmethod
|
||||
def load(cls):
|
||||
config = os.path.join(base, 'data', 'server.json')
|
||||
default_port = port = 9842
|
||||
if os.path.exists(config):
|
||||
try:
|
||||
with open(config) as fd:
|
||||
config = json.load(fd)
|
||||
port = config.get('port', port)
|
||||
except:
|
||||
pass
|
||||
url = 'file://' + base + '/openmedialibrary/static/html/load.html'
|
||||
if port != default_port:
|
||||
url += '#%s' % port
|
||||
webbrowser.open_new_tab(url)
|
||||
|
||||
def preexec(): # Don't forward signals.
|
||||
os.setpgrp()
|
||||
|
||||
if __name__ == '__main__':
|
||||
import signal
|
||||
autostart = len(sys.argv) > 1 and sys.argv[1] == '--autostart'
|
||||
OMLIcon(autostart)
|
||||
Gtk.main()
|
||||
if OMLIcon.is_running():
|
||||
OMLIcon.load()
|
||||
else:
|
||||
oml = OMLIcon(autostart)
|
||||
main_loop = GLib.MainLoop()
|
||||
try:
|
||||
main_loop.run()
|
||||
except KeyboardInterrupt:
|
||||
oml.stop()
|
||||
|
|
Loading…
Reference in a new issue