From e30a09717e25e5383392ba0b22a15070cf79508f Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Wed, 31 Jul 2024 10:11:36 +0200 Subject: [PATCH] command.Command._run_local_command(): add environment variable RUNNING_INSIDE_AUTOMATIX=1 This allows tools and commands to evaluate whether they are running inside automatix and adjust their behaviour accordingly. --- automatix/command.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/automatix/command.py b/automatix/command.py index 941242d..b24ba58 100644 --- a/automatix/command.py +++ b/automatix/command.py @@ -263,15 +263,28 @@ def _build_command(self, path: str) -> str: return f'. {path}/' + f'; . {path}/'.join(self.env.imports) + '; ' + self.get_resolved_value() def _run_local_command(self, cmd: str) -> int: - self.env.LOG.debug(f'Executing: {cmd}') + process_environment = os.environ.copy() + process_environment['RUNNING_INSIDE_AUTOMATIX'] = '1' + self.env.LOG.debug(f'Executing: {repr(cmd)} with environment {repr(process_environment)}') if self.assignment_var: - proc = subprocess.run(cmd, shell=True, executable=SHELL_EXECUTABLE, stdout=subprocess.PIPE) + proc = subprocess.run( + cmd, + env=process_environment, + executable=SHELL_EXECUTABLE, + shell=True, + stdout=subprocess.PIPE, + ) output = proc.stdout.decode(self.env.config["encoding"]) self.env.vars[self.assignment_var] = assigned_value = output.rstrip('\r\n') hint = ' (trailing newline removed)' if (output.endswith('\n') or output.endswith('\r')) else '' self.env.LOG.info(f'Variable {self.assignment_var} = "{assigned_value}"{hint}') else: - proc = subprocess.run(cmd, shell=True, executable=SHELL_EXECUTABLE) + proc = subprocess.run( + cmd, + env=process_environment, + executable=SHELL_EXECUTABLE, + shell=True, + ) return proc.returncode def _remote_action(self) -> int: