Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow service installation #186

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/iSponsorBlockTV/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from . import config_setup, main, setup_wizard
from .constants import config_file_blacklist_keys
from .service.service_helpers import service


class Device:
Expand Down Expand Up @@ -213,6 +214,8 @@ def start(ctx):
if os.getenv("PYAPP"):
cli.add_command(pyapp_group)

cli.add_command(service)


def app_start():
cli(obj={})
57 changes: 0 additions & 57 deletions src/iSponsorBlockTV/macos_install.py

This file was deleted.

Empty file.
76 changes: 76 additions & 0 deletions src/iSponsorBlockTV/service/service_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import os
import sys

import rich_click as click

from .service_managers import select_service_manager


@click.group()
@click.pass_context
def service(ctx):
"""Manage the program as a service (executable only)"""
ctx.ensure_object(dict)
if os.getenv("PYAPP") is None:
print(
"Service commands are only available in the executable version of the"
" program"
)
sys.exit(1)
ctx.obj["service_manager"] = select_service_manager()(os.getenv("PYAPP"))


@service.command()
@click.pass_context
def start(ctx):
"""Start the service"""
ctx.obj["service_manager"].start()


@service.command()
@click.pass_context
def stop(ctx):
"""Stop the service"""
ctx.obj["service_manager"].stop()


@service.command()
@click.pass_context
def restart(ctx):
"""Restart the service"""
ctx.obj["service_manager"].restart()


@service.command()
@click.pass_context
def status(ctx):
"""Get the status of the service"""
ctx.obj["service_manager"].status()


@service.command()
@click.pass_context
def install(ctx):
"""Install the service"""
ctx.obj["service_manager"].install()


@service.command()
@click.pass_context
def uninstall(ctx):
"""Uninstall the service"""
ctx.obj["service_manager"].uninstall()


@service.command()
@click.pass_context
def enable(ctx):
"""Enable the service"""
ctx.obj["service_manager"].enable()


@service.command()
@click.pass_context
def disable(ctx):
"""Disable the service"""
ctx.obj["service_manager"].disable()
107 changes: 107 additions & 0 deletions src/iSponsorBlockTV/service/service_managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import os
import plistlib
import subprocess
from platform import system

from appdirs import user_log_dir


def select_service_manager() -> "ServiceManager":
platform = system()
if platform == "Darwin":
return Launchd
elif platform == "Linux":
return Systemd
else:
raise NotImplementedError("Unsupported platform")


class ServiceManager:
def __init__(self, executable_path, *args, **kwargs):
self.executable_path = executable_path

def start(self):
pass

def stop(self):
pass

def restart(self):
pass

def status(self):
pass

def install(self):
pass

def uninstall(self):
pass

def enable(self):
pass

def disable(self):
pass


class Launchd(ServiceManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.service_name = "com.dmunozv04.iSponsorBlockTV"
self.service_path = (
os.path.expanduser("~/Library/LaunchAgents/") + self.service_name + ".plist"
)

def start(self):
subprocess.run(["launchctl", "start", self.service_name])

def stop(self):
subprocess.run(["launchctl", "stop", self.service_name])

def restart(self):
subprocess.run(["launchctl", "restart", self.service_name])

def status(self):
subprocess.run(["launchctl", "list", self.service_name])

def install(self):
if os.path.exists(self.service_path):
print("Service already installed")
return
logs_dir = user_log_dir("iSponsorBlockTV", "dmunozv04")
# ensure the logs directory exists
os.makedirs(logs_dir, exist_ok=True)
plist = {
"Label": "com.dmunozv04.iSponsorBlockTV",
"RunAtLoad": True,
"StartInterval": 20,
"EnvironmentVariables": {"PYTHONUNBUFFERED": "YES"},
"StandardErrorPath": logs_dir + "/iSponsorBlockTV.err",
"StandardOutPath": logs_dir + "/iSponsorBlockTV.out",
"Program": self.executable_path,
}
with open(self.service_path, "wb") as fp:
plistlib.dump(plist, fp)
print("Service installed")
self.enable()

def uninstall(self):
self.disable()
# Remove the file
try:
os.remove(self.service_path)
print("Service uninstalled")
except FileNotFoundError:
print("Service not found")

def enable(self):
subprocess.run(["launchctl", "load", self.service_path])

def disable(self):
subprocess.run(["launchctl", "stop", self.service_name])
subprocess.run(["launchctl", "unload", self.service_path])


class Systemd(ServiceManager):
pass