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

This commit is contained in:
Jeff Ahrenholz 2020-10-02 09:51:01 -07:00
parent 3d3e2271be
commit ae336c2cf8

View file

@ -978,6 +978,15 @@ class Session:
corexmldeployment.CoreXmlDeployment(self, xml_writer.scenario) corexmldeployment.CoreXmlDeployment(self, xml_writer.scenario)
xml_writer.write(xml_file_name) 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]: def get_environment(self, state: bool = True) -> Dict[str, str]:
""" """
Get an environment suitable for a subprocess.Popen call. Get an environment suitable for a subprocess.Popen call.
@ -997,28 +1006,17 @@ 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/session.nnnnn/environment
utils.load_config(environment_config_file, env) env_file = os.path.join(constants.CORE_CONF_DIR, "environment")
except IOError: env = self.merge_environment(env_file, env, "environment configuration")
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( env_user_file = os.path.join("/home", self.user, ".core", "environment")
"/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")
try: env = self.merge_environment(session_env_file, env, "session environemnt")
utils.load_config(environment_user_file, env)
except IOError:
logging.debug(
"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: