Merge pull request #514 from coreemu/feature/add-session-environment-support

add support for /tmp/pycore.nnnnn/environment file, DRY up env merges
This commit is contained in:
bharnden 2020-11-07 09:38:15 -08:00 committed by GitHub
commit dde74f0927
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 28 deletions

View file

@ -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
@ -997,28 +998,26 @@ 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/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"
)
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:
utils.load_config(environment_user_file, env)
logging.info("loading environment config: %s", path)
utils.load_config(path, env)
except IOError:
logging.debug(
"user core environment settings file not present: %s",
environment_user_file,
)
logging.exception("error reading environment file: %s", path)
return env
def set_thumbnail(self, thumb_file: str) -> None:

View file

@ -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: