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

Option for systemd-timesyncd #200

Merged
merged 2 commits into from
Feb 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
strategy:
max-parallel: 2
matrix:
python-version: [ 3.7, 3.8, 3.9, "3.10" ]
python-version: [ 3.8, 3.9, "3.10" ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
63 changes: 52 additions & 11 deletions ovos_utils/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import subprocess
import sys

from ovos_utils.log import LOG

from ovos_utils.log import LOG, deprecated

def is_running_from_module(module_name):
# Stack:
Expand All @@ -31,15 +30,17 @@ def is_running_from_module(module_name):
return True
return False


# system utils
@deprecated("DEPRECATED: use ovos-PHAL-plugin-system", "0.2.0")
def ntp_sync():
# Force the system clock to synchronize with internet time servers
"""
Force the system clock to synchronize with internet time servers
"""
subprocess.call('service ntp stop', shell=True)
subprocess.call('ntpd -gq', shell=True)
subprocess.call('service ntp start', shell=True)


@deprecated("DEPRECATED: use ovos-PHAL-plugin-system", "0.2.0")
def system_shutdown(sudo=True):
"""
Turn the system completely off (with no option to inhibit it)
Expand All @@ -51,7 +52,7 @@ def system_shutdown(sudo=True):
LOG.debug(cmd)
subprocess.call(cmd, shell=True)


@deprecated("DEPRECATED: use ovos-PHAL-plugin-system", "0.2.0")
def system_reboot(sudo=True):
"""
Shut down and restart the system
Expand All @@ -63,7 +64,7 @@ def system_reboot(sudo=True):
LOG.debug(cmd)
subprocess.call(cmd, shell=True)


@deprecated("DEPRECATED: use ovos-PHAL-plugin-system", "0.2.0")
def ssh_enable(sudo=True, user=False):
"""
Permanently allow SSH access
Expand All @@ -72,7 +73,7 @@ def ssh_enable(sudo=True, user=False):
"""
enable_service("ssh.service", sudo=sudo, user=user)


@deprecated("DEPRECATED: use ovos-PHAL-plugin-system", "0.2.0")
def ssh_disable(sudo=True, user=False):
"""
Permanently block SSH access from the outside
Expand All @@ -81,7 +82,7 @@ def ssh_disable(sudo=True, user=False):
"""
disable_service("ssh.service", sudo=sudo, user=user)


@deprecated("DEPRECATED: use ovos-PHAL-plugin-system", "0.2.0")
def restart_mycroft_service(sudo=True, user=False):
"""
Restarts the `mycroft.service` systemd service
Expand All @@ -90,6 +91,28 @@ def restart_mycroft_service(sudo=True, user=False):
"""
restart_service("mycroft.service", sudo=sudo, user=user)

def is_running_from_module(module_name):
# Stack:
# [0] - _log()
# [1] - debug(), info(), warning(), or error()
# [2] - caller
stack = inspect.stack()

# Record:
# [0] - frame object
# [1] - filename
# [2] - line number
# [3] - function
# ...
for record in stack[2:]:
mod = inspect.getmodule(record[0])
name = mod.__name__ if mod else ''
# module name in file path of caller
# or import name matches module name
if f"/{module_name}/" in record[1] or \
name.startswith(module_name.replace("-", "_").replace(" ", "_")):
return True
return False

def restart_service(service_name, sudo=True, user=False):
"""
Expand Down Expand Up @@ -160,6 +183,25 @@ def check_service_active(service_name, sudo=False, user=False) -> bool:
state = subprocess.run(status_command, shell=True).returncode
return state == 0

def check_service_installed(service_name, sudo=False, user=False) -> bool:
"""
Checks if a systemd service is installed using systemctl
@param service_name: name of service to check
@param user: pass --user flag when calling systemctl
@param sudo: use sudo when calling systemctl
@return: True if the service is installed, else False
"""
if not service_name.endswith('.service'):
service_name = f"{service_name}.service"
installed_base_command = f"systemctl list-unit-files -t service"
if user:
status_command = f"{installed_base_command} --user"
elif sudo:
status_command = f"sudo {installed_base_command}"
# Add a grep to the command
status_command = f"{status_command} | grep -i {service_name}"
state = subprocess.call(status_command, shell=True)
return state == 0

def get_desktop_environment():
# From http://stackoverflow.com/questions/2035657/what-is-my-current-desktop-environment
Expand Down Expand Up @@ -246,7 +288,7 @@ def has_screen():
pass

# fallback check using matplotlib if available
# seems to be foolproof and OS agnostic
# seems to be foolproof and OS agnostic
# but do not want to drag the dependency
if not have_display:
try:
Expand All @@ -260,7 +302,6 @@ def has_screen():
pass
return have_display


def module_property(func):
"""
Decorator to turn module functions into properties.
Expand Down
2 changes: 1 addition & 1 deletion ovos_utils/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
VERSION_MAJOR = 0
VERSION_MINOR = 1
VERSION_BUILD = 0
VERSION_ALPHA = 13
VERSION_ALPHA = 14
# END_VERSION_BLOCK
4 changes: 0 additions & 4 deletions test/unittests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ def test_is_running_from_module(self):
self.assertFalse(is_running_from_module("mycroft"))
self.assertTrue(is_running_from_module("unittest"))

def test_ntp_sync(self):
# TODO
pass

@patch("subprocess.Popen")
def test_system_shutdown(self, popen):
from ovos_utils.system import system_shutdown
Expand Down
Loading