Skip to content

Commit 8df90bc

Browse files
committed
Extract commoin logic from run_command() and run_python_command()
1 parent 3fc21cf commit 8df90bc

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

source/isaaclab/isaaclab/cli/utils.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,37 @@ def print_debug(message, stream=sys.stdout):
7272
print(f"{label} {message}", file=stream)
7373

7474

75+
def _print_debug_env(prefix, env):
76+
"""
77+
Print the environment for debugging purpose.
78+
Only prints the vars that are added, changed or removed vs the os.environ.
79+
"""
80+
81+
if env is None:
82+
print_debug(f"{prefix}: ENV: <os.environ>")
83+
return
84+
85+
current_env = os.environ
86+
env_added = {key: value for key, value in env.items() if key not in current_env}
87+
env_changed = {
88+
key: {"from": current_env[key], "to": value}
89+
for key, value in env.items()
90+
if key in current_env and current_env[key] != value
91+
}
92+
env_removed = [key for key in current_env if key not in env]
93+
94+
if not env_added and not env_changed and not env_removed:
95+
print_debug(f"{prefix}: ENV: <os.environ>")
96+
return
97+
98+
if env_added:
99+
print_debug(f"{prefix}: ENV added: {env_added}")
100+
if env_changed:
101+
print_debug(f"{prefix}: ENV changed: {env_changed}")
102+
if env_removed:
103+
print_debug(f"{prefix}: ENV removed: {env_removed}")
104+
105+
75106
def run_command(cmd, cwd=None, env=None, shell=False, check=True, stdout=None, stderr=None):
76107
"""Run a command in a subprocess."""
77108

@@ -83,28 +114,7 @@ def run_command(cmd, cwd=None, env=None, shell=False, check=True, stdout=None, s
83114
# Print some debug info.
84115
print_debug(f'run_command(): CWD: "{cwd}"')
85116
print_debug(f'run_command(): CMD: "{command_str}"')
86-
87-
if env is None:
88-
print_debug("run_command(): ENV: <os.environ>")
89-
else:
90-
current_env = os.environ
91-
env_added = {key: value for key, value in env.items() if key not in current_env}
92-
env_changed = {
93-
key: {"from": current_env[key], "to": value}
94-
for key, value in env.items()
95-
if key in current_env and current_env[key] != value
96-
}
97-
env_removed = [key for key in current_env if key not in env]
98-
99-
if not env_added and not env_changed and not env_removed:
100-
print_debug("run_command(): ENV: <os.environ>")
101-
else:
102-
if env_added:
103-
print_debug(f"run_command(): ENV added: {env_added}")
104-
if env_changed:
105-
print_debug(f"run_command(): ENV changed: {env_changed}")
106-
if env_removed:
107-
print_debug(f"run_command(): ENV removed: {env_removed}")
117+
_print_debug_env("run_command()", env)
108118

109119
try:
110120
return subprocess.run(cmd, cwd=cwd, env=env, shell=shell, check=check, stdout=stdout, stderr=stderr)
@@ -352,17 +362,15 @@ def run_python_command(
352362
cmd.append(str(script_or_module))
353363
cmd.extend(args)
354364

365+
env_for_debug = env
355366
if env is None:
356367
env = os.environ.copy()
357368

358369
command_str = " ".join(str(part) for part in cmd)
359370

360-
print_debug(f'run_python_command(): DIR: "{os.getcwd()}"')
371+
print_debug(f'run_python_command(): CWD: "{os.getcwd()}"')
361372
print_debug(f'run_python_command(): CMD: "{command_str}"')
362-
if env is None:
363-
print_debug("run_python_command(): ENV: <inherited>")
364-
else:
365-
print_debug(f"run_python_command(): ENV: {env}")
373+
_print_debug_env("run_python_command()", env_for_debug)
366374

367375
return subprocess.run(
368376
cmd,

0 commit comments

Comments
 (0)