Skip to content

Commit dde74f0

Browse files
authored
Merge pull request #514 from coreemu/feature/add-session-environment-support
add support for /tmp/pycore.nnnnn/environment file, DRY up env merges
2 parents 0c847cf + ea44f1b commit dde74f0

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

daemon/core/emulator/session.py

+20-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import tempfile
1313
import threading
1414
import time
15+
from pathlib import Path
1516
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, TypeVar
1617

1718
from core import constants, utils
@@ -997,28 +998,26 @@ def get_environment(self, state: bool = True) -> Dict[str, str]:
997998
env["SESSION_USER"] = str(self.user)
998999
if state:
9991000
env["SESSION_STATE"] = str(self.state)
1000-
# attempt to read and add environment config file
1001-
environment_config_file = os.path.join(constants.CORE_CONF_DIR, "environment")
1002-
try:
1003-
if os.path.isfile(environment_config_file):
1004-
utils.load_config(environment_config_file, env)
1005-
except IOError:
1006-
logging.warning(
1007-
"environment configuration file does not exist: %s",
1008-
environment_config_file,
1009-
)
1010-
# attempt to read and add user environment file
1001+
# try reading and merging optional environments from:
1002+
# /etc/core/environment
1003+
# /home/user/.core/environment
1004+
# /tmp/pycore.<session id>/environment
1005+
core_env_path = Path(constants.CORE_CONF_DIR) / "environment"
1006+
session_env_path = Path(self.session_dir) / "environment"
10111007
if self.user:
1012-
environment_user_file = os.path.join(
1013-
"/home", self.user, ".core", "environment"
1014-
)
1015-
try:
1016-
utils.load_config(environment_user_file, env)
1017-
except IOError:
1018-
logging.debug(
1019-
"user core environment settings file not present: %s",
1020-
environment_user_file,
1021-
)
1008+
user_home_path = Path(f"~{self.user}").expanduser()
1009+
user_env1 = user_home_path / ".core" / "environment"
1010+
user_env2 = user_home_path / ".coregui" / "environment"
1011+
paths = [core_env_path, user_env1, user_env2, session_env_path]
1012+
else:
1013+
paths = [core_env_path, session_env_path]
1014+
for path in paths:
1015+
if path.is_file():
1016+
try:
1017+
logging.info("loading environment config: %s", path)
1018+
utils.load_config(path, env)
1019+
except IOError:
1020+
logging.exception("error reading environment file: %s", path)
10221021
return env
10231022

10241023
def set_thumbnail(self, thumb_file: str) -> None:

daemon/core/utils.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import shlex
1616
import shutil
1717
import sys
18+
from pathlib import Path
1819
from subprocess import PIPE, STDOUT, Popen
1920
from typing import (
2021
TYPE_CHECKING,
@@ -315,27 +316,25 @@ def sysctl_devname(devname: str) -> Optional[str]:
315316
return devname.replace(".", "/")
316317

317318

318-
def load_config(filename: str, d: Dict[str, str]) -> None:
319+
def load_config(file_path: Path, d: Dict[str, str]) -> None:
319320
"""
320321
Read key=value pairs from a file, into a dict. Skip comments; strip newline
321322
characters and spacing.
322323
323-
:param filename: file to read into a dictionary
324-
:param d: dictionary to read file into
324+
:param file_path: file path to read data from
325+
:param d: dictionary to config into
325326
:return: nothing
326327
"""
327-
with open(filename, "r") as f:
328+
with file_path.open("r") as f:
328329
lines = f.readlines()
329-
330330
for line in lines:
331331
if line[:1] == "#":
332332
continue
333-
334333
try:
335334
key, value = line.split("=", 1)
336335
d[key] = value.strip()
337336
except ValueError:
338-
logging.exception("error reading file to dict: %s", filename)
337+
logging.exception("error reading file to dict: %s", file_path)
339338

340339

341340
def load_classes(path: str, clazz: Generic[T]) -> T:

0 commit comments

Comments
 (0)