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 tempfile
import threading import threading
import time import time
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, TypeVar from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, TypeVar
from core import constants, utils from core import constants, utils
@ -997,28 +998,26 @@ class Session:
env["SESSION_USER"] = str(self.user) env["SESSION_USER"] = str(self.user)
if state: if state:
env["SESSION_STATE"] = str(self.state) env["SESSION_STATE"] = str(self.state)
# attempt to read and add environment config file # try reading and merging optional environments from:
environment_config_file = os.path.join(constants.CORE_CONF_DIR, "environment") # /etc/core/environment
try: # /home/user/.core/environment
if os.path.isfile(environment_config_file): # /tmp/pycore.<session id>/environment
utils.load_config(environment_config_file, env) core_env_path = Path(constants.CORE_CONF_DIR) / "environment"
except IOError: session_env_path = Path(self.session_dir) / "environment"
logging.warning(
"environment configuration file does not exist: %s",
environment_config_file,
)
# attempt to read and add user environment file
if self.user: if self.user:
environment_user_file = os.path.join( user_home_path = Path(f"~{self.user}").expanduser()
"/home", self.user, ".core", "environment" 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: try:
utils.load_config(environment_user_file, env) logging.info("loading environment config: %s", path)
utils.load_config(path, env)
except IOError: except IOError:
logging.debug( logging.exception("error reading environment file: %s", path)
"user core environment settings file not present: %s",
environment_user_file,
)
return env return env
def set_thumbnail(self, thumb_file: str) -> None: def set_thumbnail(self, thumb_file: str) -> None:

View file

@ -15,6 +15,7 @@ import random
import shlex import shlex
import shutil import shutil
import sys import sys
from pathlib import Path
from subprocess import PIPE, STDOUT, Popen from subprocess import PIPE, STDOUT, Popen
from typing import ( from typing import (
TYPE_CHECKING, TYPE_CHECKING,
@ -315,27 +316,25 @@ def sysctl_devname(devname: str) -> Optional[str]:
return devname.replace(".", "/") 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 Read key=value pairs from a file, into a dict. Skip comments; strip newline
characters and spacing. characters and spacing.
:param filename: file to read into a dictionary :param file_path: file path to read data from
:param d: dictionary to read file into :param d: dictionary to config into
:return: nothing :return: nothing
""" """
with open(filename, "r") as f: with file_path.open("r") as f:
lines = f.readlines() lines = f.readlines()
for line in lines: for line in lines:
if line[:1] == "#": if line[:1] == "#":
continue continue
try: try:
key, value = line.split("=", 1) key, value = line.split("=", 1)
d[key] = value.strip() d[key] = value.strip()
except ValueError: 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: def load_classes(path: str, clazz: Generic[T]) -> T: