implemented functions to run openpype subprocess with cleanin environments

This commit is contained in:
iLLiCiTiT 2022-01-06 16:28:03 +01:00
parent 4ab54f4d73
commit 6de956dae2
2 changed files with 47 additions and 0 deletions

View file

@ -29,6 +29,8 @@ from .execute import (
get_linux_launcher_args,
execute,
run_subprocess,
run_openpype_process,
clean_envs_for_openpype_process,
path_to_subprocess_arg,
CREATE_NO_WINDOW
)
@ -179,6 +181,8 @@ __all__ = [
"get_linux_launcher_args",
"execute",
"run_subprocess",
"run_openpype_process",
"clean_envs_for_openpype_process",
"path_to_subprocess_arg",
"CREATE_NO_WINDOW",

View file

@ -138,6 +138,49 @@ def run_subprocess(*args, **kwargs):
return full_output
def clean_envs_for_openpype_process(env=None):
"""Modify environemnts that may affect OpenPype process.
Main reason to implement this function is to pop PYTHONPATH which may be
affected by in-host environments.
"""
if env is None:
env = os.environ
return {
key: value
for key, value in env.items()
if key not in ("PYTHONPATH",)
}
def run_openpype_process(*args, **kwargs):
"""Execute OpenPype process with passed arguments and wait.
Wrapper for 'run_process' which prepends OpenPype executable arguments
before passed arguments and define environments if are not passed.
Values from 'os.environ' are used for environments if are not passed.
They are cleaned using 'clean_envs_for_openpype_process' function.
Example:
```
run_openpype_process(["run", "<path to .py script>"])
```
Args:
*args (tuple): OpenPype cli arguments.
**kwargs (dict): Keyword arguments for for subprocess.Popen.
"""
args = get_openpype_execute_args(*args)
env = kwargs.pop("env", None)
# Keep env untouched if are passed and not empty
if not env:
# Skip envs that can affect OpenPype process
# - fill more if you find more
env = clean_envs_for_openpype_process(os.environ)
return run_subprocess(args, env=env, **kwargs)
def path_to_subprocess_arg(path):
"""Prepare path for subprocess arguments.