diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml index d94b0e7e..a24f52ef 100644 --- a/.github/workflows/unit_tests.yml +++ b/.github/workflows/unit_tests.yml @@ -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 diff --git a/ovos_utils/system.py b/ovos_utils/system.py index a9b4bc93..88b9a3a2 100644 --- a/ovos_utils/system.py +++ b/ovos_utils/system.py @@ -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: @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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): """ @@ -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 @@ -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: @@ -260,7 +302,6 @@ def has_screen(): pass return have_display - def module_property(func): """ Decorator to turn module functions into properties. diff --git a/ovos_utils/version.py b/ovos_utils/version.py index c01b579b..cac9840e 100644 --- a/ovos_utils/version.py +++ b/ovos_utils/version.py @@ -3,5 +3,5 @@ VERSION_MAJOR = 0 VERSION_MINOR = 1 VERSION_BUILD = 0 -VERSION_ALPHA = 13 +VERSION_ALPHA = 14 # END_VERSION_BLOCK diff --git a/test/unittests/test_system.py b/test/unittests/test_system.py index e513ac80..9cd1543d 100644 --- a/test/unittests/test_system.py +++ b/test/unittests/test_system.py @@ -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