From 6a5d43a4790d9156c814a88457f92aa00e7cfb65 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Sat, 15 Feb 2020 11:06:49 +0100 Subject: [PATCH] `get_path_to_ffmpeg` replaced with `get_paths_from_environ` and `get_ffmpeg_tool_path` --- pype/lib.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/pype/lib.py b/pype/lib.py index 9cde3bd3af..87b9facdb3 100644 --- a/pype/lib.py +++ b/pype/lib.py @@ -13,14 +13,61 @@ import avalon log = logging.getLogger(__name__) -def get_path_to_ffmpeg(): - paths = os.environ.get("FFMPEG_PATH") or "" +def get_paths_from_environ(env_key, return_first=False): + """Return existing paths from specific envirnment variable. + + :param env_key: Environment key where should look for paths. + :type env_key: str + :param return_first: Return first path on `True`, list of all on `False`. + :type return_first: boolean + + Difference when none of paths exists: + - when `return_first` is set to `False` then function returns empty list. + - when `return_first` is set to `True` then function returns `None`. + """ + + existing_paths = [] + paths = os.environ.get(env_key) or "" path_items = paths.split(os.pathsep) - for item in path_items: - item = os.path.normpath(item) - if os.path.exists(item): - return item - return "" + for path in path_items: + # Skip empty string + if not path: + continue + # Normalize path + path = os.path.normpath(path) + # Check if path exists + if os.path.exists(path): + # Return path if `return_first` is set to True + if return_first: + return path + # Store path + existing_paths.append(path) + + # Return None if none of paths exists + if return_first: + return None + # Return all existing paths from environment variable + return existing_paths + + +def get_ffmpeg_tool_path(tool="ffmpeg"): + """Find path to ffmpeg tool in FFMPEG_PATH paths. + + Function looks for tool in paths set in FFMPEG_PATH environment. If tool + exists then returns it's full path. + + Returns tool name itself when tool path was not found. (FFmpeg path may be + set in PATH environment variable) + """ + + dir_paths = get_paths_from_environ("FFMPEG_PATH") + for dir_path in dir_paths: + for file_name in os.listdir(dir_path): + base, ext = os.path.splitext(file_name) + if base.lower() == tool.lower(): + return os.path.join(dir_path, tool) + return tool + # Special naming case for subprocess since its a built-in method. def _subprocess(*args, **kwargs):