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

add support for /tmp/pycore.nnnnn/environment file, DRY up env merges #514

Merged
merged 2 commits into from
Nov 7, 2020
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
41 changes: 20 additions & 21 deletions daemon/core/emulator/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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
Expand Down Expand Up @@ -997,28 +998,26 @@ def get_environment(self, state: bool = True) -> Dict[str, str]:
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/pycore.<session id>/environment
core_env_path = Path(constants.CORE_CONF_DIR) / "environment"
session_env_path = Path(self.session_dir) / "environment"
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,
)
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:
Expand Down
13 changes: 6 additions & 7 deletions daemon/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import shlex
import shutil
import sys
from pathlib import Path
from subprocess import PIPE, STDOUT, Popen
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -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:
Expand Down