From ae336c2cf8c2b2cc993f15acda9a919843c6f52c Mon Sep 17 00:00:00 2001 From: Jeff Ahrenholz Date: Fri, 2 Oct 2020 09:51:01 -0700 Subject: [PATCH 1/2] add support for /tmp/pycore.nnnnn/environment file, DRY up env merges --- daemon/core/emulator/session.py | 40 ++++++++++++++++----------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/daemon/core/emulator/session.py b/daemon/core/emulator/session.py index 64276dcc..ea0a04d6 100644 --- a/daemon/core/emulator/session.py +++ b/daemon/core/emulator/session.py @@ -978,6 +978,15 @@ class Session: corexmldeployment.CoreXmlDeployment(self, xml_writer.scenario) xml_writer.write(xml_file_name) + @staticmethod + def merge_environment(f, env, title): + try: + if os.path.isfile(f): + utils.load_config(f, env) + except IOError: + logging.warning(f"{title} file does not exist: {f}") + return env + def get_environment(self, state: bool = True) -> Dict[str, str]: """ Get an environment suitable for a subprocess.Popen call. @@ -997,28 +1006,17 @@ class Session: env["SESSION_USER"] = str(self.user) if state: env["SESSION_STATE"] = str(self.state) - # attempt to read and add environment config file - environment_config_file = os.path.join(constants.CORE_CONF_DIR, "environment") - try: - if os.path.isfile(environment_config_file): - utils.load_config(environment_config_file, env) - except IOError: - logging.warning( - "environment configuration file does not exist: %s", - environment_config_file, - ) - # attempt to read and add user environment file + # try reading and merging optional environments from: + # /etc/core/environment + # /home/user/.core/environment + # /tmp/session.nnnnn/environment + env_file = os.path.join(constants.CORE_CONF_DIR, "environment") + env = self.merge_environment(env_file, env, "environment configuration") if self.user: - environment_user_file = os.path.join( - "/home", self.user, ".core", "environment" - ) - try: - utils.load_config(environment_user_file, env) - except IOError: - logging.debug( - "user core environment settings file not present: %s", - environment_user_file, - ) + env_user_file = os.path.join("/home", self.user, ".core", "environment") + env = self.merge_environment(env_user_file, env, "user environemnt") + session_env_file = os.path.join(self.session_dir, "environment") + env = self.merge_environment(session_env_file, env, "session environemnt") return env def set_thumbnail(self, thumb_file: str) -> None: From ea44f1b6e73356b40be049cd5a80af410b27c3c6 Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Sun, 11 Oct 2020 08:22:33 -0700 Subject: [PATCH 2/2] daemon: updated environment reading to leverage pathlib, also added .coregui user environment as a possibility --- daemon/core/emulator/session.py | 37 +++++++++++++++++---------------- daemon/core/utils.py | 13 ++++++------ 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/daemon/core/emulator/session.py b/daemon/core/emulator/session.py index ea0a04d6..1a46e8f2 100644 --- a/daemon/core/emulator/session.py +++ b/daemon/core/emulator/session.py @@ -12,6 +12,7 @@ import sys import tempfile import threading import time +from pathlib import Path from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, TypeVar from core import constants, utils @@ -978,15 +979,6 @@ class Session: corexmldeployment.CoreXmlDeployment(self, xml_writer.scenario) xml_writer.write(xml_file_name) - @staticmethod - def merge_environment(f, env, title): - try: - if os.path.isfile(f): - utils.load_config(f, env) - except IOError: - logging.warning(f"{title} file does not exist: {f}") - return env - def get_environment(self, state: bool = True) -> Dict[str, str]: """ Get an environment suitable for a subprocess.Popen call. @@ -1007,16 +999,25 @@ class Session: if state: env["SESSION_STATE"] = str(self.state) # try reading and merging optional environments from: - # /etc/core/environment - # /home/user/.core/environment - # /tmp/session.nnnnn/environment - env_file = os.path.join(constants.CORE_CONF_DIR, "environment") - env = self.merge_environment(env_file, env, "environment configuration") + # /etc/core/environment + # /home/user/.core/environment + # /tmp/pycore./environment + core_env_path = Path(constants.CORE_CONF_DIR) / "environment" + session_env_path = Path(self.session_dir) / "environment" if self.user: - env_user_file = os.path.join("/home", self.user, ".core", "environment") - env = self.merge_environment(env_user_file, env, "user environemnt") - session_env_file = os.path.join(self.session_dir, "environment") - env = self.merge_environment(session_env_file, env, "session environemnt") + user_home_path = Path(f"~{self.user}").expanduser() + user_env1 = user_home_path / ".core" / "environment" + user_env2 = user_home_path / ".coregui" / "environment" + paths = [core_env_path, user_env1, user_env2, session_env_path] + else: + paths = [core_env_path, session_env_path] + for path in paths: + if path.is_file(): + try: + logging.info("loading environment config: %s", path) + utils.load_config(path, env) + except IOError: + logging.exception("error reading environment file: %s", path) return env def set_thumbnail(self, thumb_file: str) -> None: diff --git a/daemon/core/utils.py b/daemon/core/utils.py index 40001fe1..4a9d6ca6 100644 --- a/daemon/core/utils.py +++ b/daemon/core/utils.py @@ -15,6 +15,7 @@ import random import shlex import shutil import sys +from pathlib import Path from subprocess import PIPE, STDOUT, Popen from typing import ( TYPE_CHECKING, @@ -315,27 +316,25 @@ def sysctl_devname(devname: str) -> Optional[str]: return devname.replace(".", "/") -def load_config(filename: str, d: Dict[str, str]) -> None: +def load_config(file_path: Path, d: Dict[str, str]) -> None: """ Read key=value pairs from a file, into a dict. Skip comments; strip newline characters and spacing. - :param filename: file to read into a dictionary - :param d: dictionary to read file into + :param file_path: file path to read data from + :param d: dictionary to config into :return: nothing """ - with open(filename, "r") as f: + with file_path.open("r") as f: lines = f.readlines() - for line in lines: if line[:1] == "#": continue - try: key, value = line.split("=", 1) d[key] = value.strip() except ValueError: - logging.exception("error reading file to dict: %s", filename) + logging.exception("error reading file to dict: %s", file_path) def load_classes(path: str, clazz: Generic[T]) -> T: