Add environment variable to pw_cli.process module
This change sets an environment variable in the subprocess run by
pw_cli.process.run() to indicate that it is running as a subprocess.
Change-Id: I1def3259b62578a156a4b374d41af99692147699
diff --git a/pw_cli/py/pw_cli/process.py b/pw_cli/py/pw_cli/process.py
index 767d6c0..d747f3b 100644
--- a/pw_cli/py/pw_cli/process.py
+++ b/pw_cli/py/pw_cli/process.py
@@ -16,6 +16,7 @@
import asyncio
import logging
+import os
import shlex
from pw_cli.color import Color
@@ -24,6 +25,11 @@
_LOG = logging.getLogger(__name__)
+# Environment variable passed down to subprocesses to indicate that they are
+# running as a subprocess. Can be imported by other code.
+PW_SUBPROCESS_ENV = 'PW_SUBPROCESS'
+
+
async def run_async(*args: str, silent: bool = False) -> int:
"""Runs a command, capturing and logging its output.
@@ -31,13 +37,17 @@
"""
command = args[0]
- _LOG.info('Running `%s`', shlex.join(command))
+ _LOG.debug('Running `%s`', shlex.join(command))
+
+ env = os.environ.copy()
+ env[PW_SUBPROCESS_ENV] = '1'
stdout = asyncio.subprocess.DEVNULL if silent else asyncio.subprocess.PIPE
process = await asyncio.create_subprocess_exec(
*command,
stdout=stdout,
- stderr=asyncio.subprocess.STDOUT)
+ stderr=asyncio.subprocess.STDOUT,
+ env=env)
if process.stdout is not None:
while line := await process.stdout.readline():