diff --git a/install b/install index e44361d..fe1f261 100755 --- a/install +++ b/install @@ -6,12 +6,7 @@ import json import os import sys import tarfile -try: -# For Python 3.0 and later - from urllib.request import urlopen -except ImportError: -# Fall back to Python 2's urllib2 - from urllib2 import urlopen +from urllib.request import urlopen from threading import Thread import subprocess @@ -59,7 +54,7 @@ class Install(Thread): with open('config/release.json', 'w') as fd: json.dump(release, fd, indent=2) if sys.platform == 'darwin': - self.install_launchd() + os.system('./ctl install_launcher') elif sys.platform.startswith('linux'): apt_packages = '' yum_packages = '' @@ -88,7 +83,7 @@ class Install(Thread): print('You need to install pdftocairo (part of poppler-utils)') if 'miredo' in apt_packages: print('You need to install miredo (or get IPv6 in another way)') - self.install_application() + os.system('./ctl install_launcher') self.status['done'] = True def download(self, url, filename): @@ -112,54 +107,6 @@ class Install(Thread): data = json.loads(u.read().decode('utf-8')) return data - def install_application(self): - app = os.path.expanduser('~/.local/share/applications/openmedialibrary.desktop') - with open(app, 'w') as fd: - fd.write('''[Desktop Entry] -Type=Application -Name=Open Media Library -Comment=Open Media Library -Exec=%s/ctl open -Icon=%s/openmedialibrary/static/png/oml.png -Terminal=false -Categories=Network;FileTransfer;P2P; -''' % (self.target, self.target)) - - start = os.path.expanduser('~/.config/autostart/openmedialibrary.desktop') - makefolder(start) - with open(start, 'w') as fd: - fd.write('''[Desktop Entry] -Type=Application -Exec=%s/ctl start -Icon=%s/openmedialibrary/static/png/oml.png -Hidden=false -NoDisplay=false -X-GNOME-Autostart-enabled=true -Name=Start Open Media Library -Comment= -''' % (self.target, self.target)) - - def install_launchd(self): - plist = os.path.expanduser('~/Library/LaunchAgents/com.openmedialibrary.loginscript.plist') - with open(plist, 'w') as f: - f.write(''' - - - - Label - com.openmedialibrary.loginscript - ProgramArguments - - %s/ctl - start - - RunAtLoad - - -''' % self.target) - os.system('launchctl load "%s"' % plist) - os.system('launchctl start com.openmedialibrary.loginscript') - def url(self, url): return self.base_url + url diff --git a/oml/commands.py b/oml/commands.py index 2b08f73..2d6df73 100644 --- a/oml/commands.py +++ b/oml/commands.py @@ -9,19 +9,10 @@ import sys import shutil import settings +from utils import run, get root_dir = dirname(settings.base_dir) -def run(*cmd): - p = subprocess.Popen(cmd, close_fds=True) - p.wait() - return p.returncode - -def get(*cmd): - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) - stdout, error = p.communicate() - return stdout.decode() - def r(*cmd): print(' '.join(cmd)) return subprocess.call(cmd) @@ -64,6 +55,20 @@ def command_stop(*args): """ pass +def command_install_launcher(*args): + """ + Install launcher + """ + import integration + integration.install_launcher() + +def command_uninstall_launcher(*args): + """ + Uninstall launcher + """ + import integration + integration.uninstall_launcher() + def command_install_update(*args): """ Install available updates diff --git a/oml/integration.py b/oml/integration.py new file mode 100644 index 0000000..88ecaf5 --- /dev/null +++ b/oml/integration.py @@ -0,0 +1,82 @@ +import os +import sys +import settings + +from utils import run, makefolder + +root_dir = os.path.dirname(settings.base_dir) + +def install_launcher(): + if sys.platform == 'darwin': + install_launchd() + elif sys.platform.startswith('linux'): + install_xdg() + else: + print('no launcher integration supported for %s' % sys.platform) + +def uninstall_launcher(): + if sys.platform == 'darwin': + name = 'com.openmedialibrary.loginscript' + plist = os.path.expanduser('~/Library/LaunchAgents/%s.plist'%name) + if os.path.exists(plist): + run('launchctl', 'stop', name) + run('launchctl', 'unload', plist) + os.unlink(plist) + elif sys.platform.startswith('linux'): + for f in map(os.path.expanduser, [ + '~/.local/share/applications/openmedialibrary.desktop', + '~/.config/autostart/openmedialibrary.desktop' + ]): + if os.path.exists(f): + os.unlink(f) + +def install_launchd(): + name = 'com.openmedialibrary.loginscript' + plist = os.path.expanduser('~/Library/LaunchAgents/%s.plist'%name) + if os.path.exists(plist): + run('launchctl', 'stop', name) + run('launchctl', 'unload', plist) + with open(plist, 'w') as f: + f.write(''' + + + + Label + %s + ProgramArguments + + %s/ctl + start + + RunAtLoad + + +''' % (name, root_dir)) + run('launchctl', 'load', plist) + run('launchctl', 'start', name) + +def install_xdg(): + app = os.path.expanduser('~/.local/share/applications/openmedialibrary.desktop') + with open(app, 'w') as fd: + fd.write('''[Desktop Entry] +Type=Application +Name=Open Media Library +Comment=Open Media Library +Exec=%(base)s/ctl open +Icon=%(base)s/openmedialibrary/static/png/oml.png +Terminal=false +Categories=Network;FileTransfer;P2P; +''' % {'base': root_dir}) + + start = os.path.expanduser('~/.config/autostart/openmedialibrary.desktop') + makefolder(start) + with open(start, 'w') as fd: + fd.write('''[Desktop Entry] +Type=Application +Name=Start Open Media Library +Exec=%(base)s/ctl start +Icon=%(base)s/openmedialibrary/static/png/oml.png +Hidden=false +NoDisplay=false +X-GNOME-Autostart-enabled=true +''' % {'base': root_dir}) diff --git a/oml/utils.py b/oml/utils.py index 91178fe..ae5aec3 100644 --- a/oml/utils.py +++ b/oml/utils.py @@ -230,3 +230,18 @@ def datetime2ts(dt): def ts2datetime(ts): return datetime.utcfromtimestamp(float(ts)) + +def run(*cmd): + p = subprocess.Popen(cmd, close_fds=True) + p.wait() + return p.returncode + +def get(*cmd): + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) + stdout, error = p.communicate() + return stdout.decode() + +def makefolder(path): + dirname = os.path.dirname(path) + if not os.path.exists(dirname): + os.makedirs(dirname) diff --git a/static/html/load.html b/static/html/load.html index 64ddb9d..0b7ca46 100644 --- a/static/html/load.html +++ b/static/html/load.html @@ -3,7 +3,7 @@ Open Media Library - +