MCPcopy
hub / github.com/dmlc/dgl / wrap_cmd_with_local_envvars

Function wrap_cmd_with_local_envvars

tools/launch.py:445–468  ·  view source on GitHub ↗

Wraps a CLI command with desired env vars with the following properties: (1) env vars persist for the entire `cmd`, even if it consists of multiple "chained" commands like: cmd = "ls && pwd && python run/something.py" (2) env vars don't pollute the environment after `cmd` completes.

(cmd: str, env_vars: str)

Source from the content-addressed store, hash-verified

443
444
445def wrap_cmd_with_local_envvars(cmd: str, env_vars: str) -> str:
446 """Wraps a CLI command with desired env vars with the following properties:
447 (1) env vars persist for the entire `cmd`, even if it consists of multiple "chained" commands like:
448 cmd = "ls && pwd && python run/something.py"
449 (2) env vars don't pollute the environment after `cmd` completes.
450
451 Example:
452 >>> cmd = "ls && pwd"
453 >>> env_vars = "VAR1=value1 VAR2=value2"
454 >>> wrap_cmd_with_local_envvars(cmd, env_vars)
455 "(export VAR1=value1 VAR2=value2; ls && pwd)"
456
457 Args:
458 cmd:
459 env_vars: A string containing env vars, eg "VAR1=val1 VAR2=val2"
460
461 Returns:
462 cmd_with_env_vars:
463
464 """
465 # use `export` to persist env vars for entire cmd block. required if udf_command is a chain of commands
466 # also: wrap in parens to not pollute env:
467 # https://stackoverflow.com/a/45993803
468 return f"(export {env_vars}; {cmd})"
469
470
471def wrap_cmd_with_extra_envvars(cmd: str, env_vars: list) -> str:

Callers 3

submit_jobsFunction · 0.70
test_simpleMethod · 0.50

Calls

no outgoing calls

Tested by 1

test_simpleMethod · 0.40