Skip to content

Commit

Permalink
WIP: Fix systemd units for Flatpak
Browse files Browse the repository at this point in the history
  • Loading branch information
chase9 committed Dec 13, 2023
1 parent b0599aa commit f92e036
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
28 changes: 21 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
{
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.provider": "none"
}
"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"
]
}
22 changes: 22 additions & 0 deletions sh.oskar.yin-yang.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand All @@ -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"
}
]
}
]
}
14 changes: 11 additions & 3 deletions tests/test_daemon_handler.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions yin_yang/daemon_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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():
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion yin_yang/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""


Expand Down

0 comments on commit f92e036

Please sign in to comment.