From f92e03664a17fe71215cd7eacfe5c15c11e1751e Mon Sep 17 00:00:00 2001 From: Chase Lau Date: Mon, 4 Dec 2023 14:33:08 -0600 Subject: [PATCH] WIP: Fix systemd units for Flatpak --- .vscode/settings.json | 28 +++++++++++++++++++++------- sh.oskar.yin-yang.json | 22 ++++++++++++++++++++++ tests/test_daemon_handler.py | 14 +++++++++++--- yin_yang/daemon_handler.py | 9 +++++++++ yin_yang/helpers.py | 2 +- 5 files changed, 64 insertions(+), 11 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 67261f41..99f7a410 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,22 @@ { - "python.linting.pylintEnabled": true, - "python.linting.enabled": true, - "[python]": { - "editor.defaultFormatter": "ms-python.black-formatter" - }, - "python.formatting.provider": "none" -} \ No newline at end of file + "python.analysis.autoImportCompletions": true, + "python.analysis.exclude": [ + ".git", + "**/__pycache__", + "**/node_modules", + "build", + ".flatpak-builder" + ], + "python.analysis.ignore": [ + ".flatpak-builder", + "build", + "**/__pycache__", + ".git" + ], + "pylint.ignorePatterns": [ + "**/__pycache__", + ".git", + "build", + ".flatpak-builder" + ] +} diff --git a/sh.oskar.yin-yang.json b/sh.oskar.yin-yang.json index 3c446a3d..1eba1d04 100644 --- a/sh.oskar.yin-yang.json +++ b/sh.oskar.yin-yang.json @@ -31,6 +31,7 @@ "install -D yin_yang/__main__.py /app/__main__.py", "cp -r yin_yang /app/yin_yang", "cp -r resources /app/resources", + "sed -i 's|/ExecStart=/usr/bin/yin-yang --systemd|ExecStart=$HOME/.local/share/flatpak/exports/bin/sh.oskar.yin-yang --systemd|' /app/resources/yin_yang.service", "install -D runner.sh /app/bin/runner.sh", "sed -i 's|/bin/env python3|/app/bin/python3|' /app/__main__.py" ], @@ -40,6 +41,27 @@ "path": "." } ] + }, + { + "name": "yin-yang-metadata", + "buildsystem": "simple", + "build-commands": [ + "install -Dm644 Yin-Yang.desktop /app/share/applications/sh.oskar.yin-yang.desktop", + "sed -i 's|Path=/opt/yin-yang|Path=$HOME/.local/share/flatpak/app/sh.oskar.yin-yang/current/active/files|' /app/share/applications/sh.oskar.yin-yang.desktop", + "sed -i 's|Icon=yin_yang|Icon=sh.oskar.yin-yang|' /app/share/applications/sh.oskar.yin-yang.desktop", + "sed -i 's|Exec=yin-yang|Exec=runner.sh|' /app/share/applications/sh.oskar.yin-yang.desktop", + "install -Dm664 logo.svg /app/share/icons/hicolor/256x256/apps/sh.oskar.yin-yang.svg" + ], + "sources": [ + { + "type": "file", + "path": "./resources/Yin-Yang.desktop" + }, + { + "type": "file", + "path": "./resources/logo.svg" + } + ] } ] } \ No newline at end of file diff --git a/tests/test_daemon_handler.py b/tests/test_daemon_handler.py index 0d845b43..c8a32096 100644 --- a/tests/test_daemon_handler.py +++ b/tests/test_daemon_handler.py @@ -1,11 +1,12 @@ -import pathlib +from pathlib import Path +import re import shutil import subprocess import unittest from datetime import time from os.path import isfile -from yin_yang import daemon_handler +from yin_yang import daemon_handler, helpers from yin_yang.config import config from yin_yang.meta import Modes, ConfigEvent @@ -26,9 +27,16 @@ def tearDown(self) -> None: def setUpClass(cls) -> None: super().setUpClass() if not isfile(daemon_handler.TIMER_PATH): - pathlib.Path(daemon_handler.SYSTEMD_PATH).mkdir(parents=True, exist_ok=True) + Path(daemon_handler.SYSTEMD_PATH).mkdir(parents=True, exist_ok=True) shutil.copyfile('./resources/yin_yang.timer', daemon_handler.TIMER_PATH) shutil.copyfile('./resources/yin_yang.service', daemon_handler.SERVICE_PATH) + # If we're in a flatpak, the service file needs to be updated + if (helpers.is_flatpak()): + with open(daemon_handler.SERVICE_PATH, 'r') as service: + lines = service.readlines() + with open(daemon_handler.SERVICE_PATH, 'w') as service: + for line in lines: + service.write(re.sub('ExecStart=\/usr\/bin\/yin-yang --systemd', 'ExecStart='+str(Path.home())+'\/.local\/share\/flatpak\/exports\/bin\/sh.oskar.yin-yang --systemd', line)) shutil.copyfile(daemon_handler.TIMER_PATH, daemon_handler.TIMER_PATH.with_suffix('.timer_backup')) @classmethod diff --git a/yin_yang/daemon_handler.py b/yin_yang/daemon_handler.py index 7ee39ac2..096b2f8d 100644 --- a/yin_yang/daemon_handler.py +++ b/yin_yang/daemon_handler.py @@ -3,6 +3,7 @@ import subprocess from enum import Enum, auto from pathlib import Path +import re from yin_yang import helpers from .config import ConfigWatcher, config @@ -14,6 +15,7 @@ SERVICE_PATH = SYSTEMD_PATH / 'yin_yang.service' + def create_files(): logger.debug('Creating systemd files') if not SYSTEMD_PATH.is_dir(): @@ -22,6 +24,13 @@ def create_files(): shutil.copy('./resources/yin_yang.timer', TIMER_PATH) if not SERVICE_PATH.is_file(): shutil.copy('./resources/yin_yang.service', SERVICE_PATH) + # TODO: Will this cause an issue switching back from flatpak? + if (helpers.is_flatpak()): + with open(SERVICE_PATH, 'r') as service: + lines = service.readlines() + with open(SERVICE_PATH, 'w') as service: + for line in lines: + service.write(re.sub('ExecStart=\/usr\/bin\/yin-yang --systemd', 'ExecStart='+str(Path.home())+'/.local/share/flatpak/exports/bin/sh.oskar.yin-yang --systemd', line)) def run_command(command, **kwargs): diff --git a/yin_yang/helpers.py b/yin_yang/helpers.py index 29b83e78..35a3b798 100644 --- a/yin_yang/helpers.py +++ b/yin_yang/helpers.py @@ -4,7 +4,7 @@ """Check output of a command. This is a helper method which will change how we check output depending on if -The application is running in a flatpak or not. +The application is running in a Flatpak or not. """