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

fix/platform_detect #33

Merged
merged 5 commits into from
Mar 3, 2022
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
19 changes: 4 additions & 15 deletions ovos_utils/configuration.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import sys
from importlib.util import find_spec
from os.path import isfile, join, isdir, dirname
from os.path import isfile, join, dirname

from json_database import JsonStorage

from ovos_utils.json_helper import load_commented_json, merge_dict
from ovos_utils.log import LOG
from ovos_utils.system import search_mycroft_core_location
from ovos_utils.system import search_mycroft_core_location, is_running_from_module
from ovos_utils.xdg_utils import (
xdg_config_home,
xdg_config_dirs,
Expand Down Expand Up @@ -42,22 +39,14 @@ def get_xdg_cache_save_path(folder=None):
return join(xdg_cache_home(), folder)


def _is_running_from_module(module_name):
#is_installed = find_spec(module_name) is not None
#if not is_installed:
# return False
in_path = any([p.endswith(f"/{module_name}") for p in sys.path])
return in_path


def get_ovos_config():
# populate default values
config = {"xdg": True,
"base_folder": "mycroft",
"config_filename": "mycroft.conf"}
try:
config["default_config_path"] = find_default_config()
except FileNotFoundError: # not a mycroft device
except FileNotFoundError: # not a mycroft device
config["default_config_path"] = join(dirname(__file__), "res", "fallback_mycroft.conf")

# load ovos.conf
Expand All @@ -74,7 +63,7 @@ def get_ovos_config():
# TODO this works if using dedicated .venvs what about system installs?
cores = config.get("module_overrides") or {}
for k in cores:
if _is_running_from_module(k):
if is_running_from_module(k):
config = merge_dict(config, cores[k])
break
else:
Expand Down
7 changes: 5 additions & 2 deletions ovos_utils/fingerprinting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import socket
from enum import Enum
from os.path import join, isfile
from ovos_utils.system import is_installed, has_screen, \
from ovos_utils.system import is_installed, is_running_from_module, has_screen, \
get_desktop_environment, search_mycroft_core_location, is_process_running
from ovos_utils.configuration import is_using_xdg

Expand Down Expand Up @@ -143,6 +143,9 @@ def is_mycroft_core():
except ImportError:
return False

def is_vanilla_mycroft_core():
return is_mycroft_core() and not is_ovos()


def is_holmes():
return "HolmesV" in (get_mycroft_version() or "") or is_mycroft_lib()
Expand All @@ -153,7 +156,7 @@ def is_mycroft_lib():


def is_ovos():
return "OpenVoiceOS" in (get_mycroft_version() or "")
return is_running_from_module("ovos-core")


def classify_platform_print(fingerprint=None):
Expand Down
33 changes: 27 additions & 6 deletions ovos_utils/system.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import inspect
import os
import subprocess
import re
import shutil
import subprocess
import sys
import sysconfig
from enum import Enum
from os.path import expanduser, exists, join

from os.path import expanduser, exists, join, isfile
from ovos_utils.log import LOG


Expand All @@ -23,6 +24,30 @@ class MycroftRootLocations(str, Enum):
_USER_DEFINED_ROOT = None


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


# system utils
def ntp_sync():
# Force the system clock to synchronize with internet time servers
Expand Down Expand Up @@ -186,7 +211,3 @@ def has_screen():
except ImportError:
pass
return have_display