Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2493 from pypeclub/bugfix/OP-2333_PYTHONPATH-may-…
Browse files Browse the repository at this point in the history
…break-OpenPype

General: PYTHONPATH may break OpenPype dependencies
  • Loading branch information
iLLiCiTiT authored Jan 10, 2022
2 parents 7a8fef7 + 2a0f7b4 commit 1b3cfad
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 24 deletions.
5 changes: 2 additions & 3 deletions openpype/hooks/pre_non_python_host_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from openpype.lib import (
PreLaunchHook,
get_pype_execute_args
get_openpype_execute_args
)

from openpype import PACKAGE_DIR as OPENPYPE_DIR
Expand Down Expand Up @@ -35,7 +35,7 @@ def execute(self):
"non_python_host_launch.py"
)

new_launch_args = get_pype_execute_args(
new_launch_args = get_openpype_execute_args(
"run", script_path, executable_path
)
# Add workfile path if exists
Expand All @@ -48,4 +48,3 @@ def execute(self):

if remainders:
self.launch_context.launch_args.extend(remainders)

4 changes: 2 additions & 2 deletions openpype/hosts/tvpaint/hooks/pre_launch_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from openpype.hosts import tvpaint
from openpype.lib import (
PreLaunchHook,
get_pype_execute_args
get_openpype_execute_args
)

import avalon
Expand All @@ -30,7 +30,7 @@ def execute(self):
while self.launch_context.launch_args:
remainders.append(self.launch_context.launch_args.pop(0))

new_launch_args = get_pype_execute_args(
new_launch_args = get_openpype_execute_args(
"run", self.launch_script_path(), executable_path
)

Expand Down
6 changes: 6 additions & 0 deletions openpype/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@

from .terminal import Terminal
from .execute import (
get_openpype_execute_args,
get_pype_execute_args,
get_linux_launcher_args,
execute,
run_subprocess,
run_openpype_process,
clean_envs_for_openpype_process,
path_to_subprocess_arg,
CREATE_NO_WINDOW
)
Expand Down Expand Up @@ -173,10 +176,13 @@
terminal = Terminal

__all__ = [
"get_openpype_execute_args",
"get_pype_execute_args",
"get_linux_launcher_args",
"execute",
"run_subprocess",
"run_openpype_process",
"clean_envs_for_openpype_process",
"path_to_subprocess_arg",
"CREATE_NO_WINDOW",

Expand Down
55 changes: 55 additions & 0 deletions openpype/lib/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -147,6 +190,18 @@ def path_to_subprocess_arg(path):


def get_pype_execute_args(*args):
"""Backwards compatible function for 'get_openpype_execute_args'."""
import traceback

log = Logger.get_logger("get_pype_execute_args")
stack = "\n".join(traceback.format_stack())
log.warning((
"Using deprecated function 'get_pype_execute_args'. Called from:\n{}"
).format(stack))
return get_openpype_execute_args(*args)


def get_openpype_execute_args(*args):
"""Arguments to run pype command.
Arguments for subprocess when need to spawn new pype process. Which may be
Expand Down
4 changes: 2 additions & 2 deletions openpype/lib/pype_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import openpype.version
from openpype.settings.lib import get_local_settings
from .execute import get_pype_execute_args
from .execute import get_openpype_execute_args
from .local_settings import get_local_site_id
from .python_module_tools import import_filepath

Expand Down Expand Up @@ -71,7 +71,7 @@ def is_running_staging():

def get_pype_info():
"""Information about currently used Pype process."""
executable_args = get_pype_execute_args()
executable_args = get_openpype_execute_args()
if is_running_from_build():
version_type = "build"
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import ftrack_api
import pymongo
from openpype.lib import (
get_pype_execute_args,
get_openpype_execute_args,
OpenPypeMongoConnection,
get_openpype_version,
get_build_version,
Expand Down Expand Up @@ -136,7 +136,7 @@ def legacy_server(ftrack_url):

if subproc is None:
if subproc_failed_count < max_fail_count:
args = get_pype_execute_args("run", subproc_path)
args = get_openpype_execute_args("run", subproc_path)
subproc = subprocess.Popen(
args,
stdout=subprocess.PIPE
Expand Down Expand Up @@ -248,7 +248,7 @@ def on_exit(processor_thread, storer_thread, statuser_thread):
["Username", getpass.getuser()],
["Host Name", host_name],
["Host IP", socket.gethostbyname(host_name)],
["OpenPype executable", get_pype_execute_args()[-1]],
["OpenPype executable", get_openpype_execute_args()[-1]],
["OpenPype version", get_openpype_version() or "N/A"],
["OpenPype build version", get_build_version() or "N/A"]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import traceback
import subprocess
from openpype.api import Logger
from openpype.lib import get_pype_execute_args
from openpype.lib import get_openpype_execute_args


class SocketThread(threading.Thread):
Expand Down Expand Up @@ -59,7 +59,7 @@ def run(self):
env = os.environ.copy()
env["OPENPYPE_PROCESS_MONGO_ID"] = str(Logger.mongo_process_id)
# OpenPype executable (with path to start script if not build)
args = get_pype_execute_args(
args = get_openpype_execute_args(
# Add `run` command
"run",
self.filepath,
Expand Down
4 changes: 2 additions & 2 deletions openpype/modules/standalonepublish_action.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import platform
import subprocess
from openpype.lib import get_pype_execute_args
from openpype.lib import get_openpype_execute_args
from openpype.modules import OpenPypeModule
from openpype_interfaces import ITrayAction

Expand Down Expand Up @@ -35,7 +35,7 @@ def connect_with_modules(self, enabled_modules):
self.publish_paths.extend(publish_paths)

def run_standalone_publisher(self):
args = get_pype_execute_args("standalonepublisher")
args = get_openpype_execute_args("standalonepublisher")
kwargs = {}
if platform.system().lower() == "darwin":
new_args = ["open", "-na", args.pop(0), "--args"]
Expand Down
9 changes: 4 additions & 5 deletions openpype/plugins/publish/extract_burnin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import openpype
import openpype.api
from openpype.lib import (
get_pype_execute_args,
run_openpype_process,

get_transcode_temp_directory,
convert_for_ffmpeg,
Expand Down Expand Up @@ -168,9 +168,8 @@ def main_process(self, instance):
anatomy = instance.context.data["anatomy"]
scriptpath = self.burnin_script_path()

# Executable args that will execute the script
# [pype executable, *pype script, "run"]
executable_args = get_pype_execute_args("run", scriptpath)
# Args that will execute the script
executable_args = ["run", scriptpath]
burnins_per_repres = self._get_burnins_per_representations(
instance, burnin_defs
)
Expand Down Expand Up @@ -313,7 +312,7 @@ def main_process(self, instance):
if platform.system().lower() == "windows":
process_kwargs["creationflags"] = CREATE_NO_WINDOW

openpype.api.run_subprocess(args, **process_kwargs)
run_openpype_process(*args, **process_kwargs)
# Remove the temporary json
os.remove(temporary_json_filepath)

Expand Down
4 changes: 2 additions & 2 deletions openpype/tools/standalonepublish/widgets/widget_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from avalon import io
from openpype.api import execute, Logger
from openpype.lib import (
get_pype_execute_args,
get_openpype_execute_args,
apply_project_environments_value
)

Expand Down Expand Up @@ -193,7 +193,7 @@ def cli_publish(data, publish_paths, gui=True):
project_name = os.environ["AVALON_PROJECT"]
env_copy = apply_project_environments_value(project_name, envcopy)

args = get_pype_execute_args("run", PUBLISH_SCRIPT_PATH)
args = get_openpype_execute_args("run", PUBLISH_SCRIPT_PATH)
result = execute(args, env=envcopy)

result = {}
Expand Down
6 changes: 3 additions & 3 deletions openpype/tools/tray/pype_tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
resources,
get_system_settings
)
from openpype.lib import get_pype_execute_args
from openpype.lib import get_openpype_execute_args
from openpype.modules import TrayModulesManager
from openpype import style
from openpype.settings import (
Expand Down Expand Up @@ -208,10 +208,10 @@ def restart(self):
First creates new process with same argument and close current tray.
"""
args = get_pype_execute_args()
args = get_openpype_execute_args()
# Create a copy of sys.argv
additional_args = list(sys.argv)
# Check last argument from `get_pype_execute_args`
# Check last argument from `get_openpype_execute_args`
# - when running from code it is the same as first from sys.argv
if args[-1] == additional_args[0]:
additional_args.pop(0)
Expand Down

0 comments on commit 1b3cfad

Please sign in to comment.