From f875cc1fcc439d500f50897711dac8d8a9ff91d9 Mon Sep 17 00:00:00 2001 From: henryruhs Date: Sat, 7 Sep 2024 23:21:11 +0200 Subject: [PATCH] Resolve subprocess calls to cover edge cases like broken PATH --- facefusion/core.py | 3 +++ facefusion/download.py | 3 ++- facefusion/ffmpeg.py | 5 +++-- facefusion/installer.py | 14 +++++++------- facefusion/wording.py | 1 + install.py | 3 ++- 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/facefusion/core.py b/facefusion/core.py index 0680843f..3bf2033f 100755 --- a/facefusion/core.py +++ b/facefusion/core.py @@ -82,6 +82,9 @@ def pre_check() -> bool: if sys.version_info < (3, 9): logger.error(wording.get('python_not_supported').format(version = '3.9'), __name__) return False + if not shutil.which('curl'): + logger.error(wording.get('curl_not_installed'), __name__) + return False if not shutil.which('ffmpeg'): logger.error(wording.get('ffmpeg_not_installed'), __name__) return False diff --git a/facefusion/download.py b/facefusion/download.py index d241cf1b..43c92a0e 100644 --- a/facefusion/download.py +++ b/facefusion/download.py @@ -1,4 +1,5 @@ import os +import shutil import ssl import subprocess import urllib.request @@ -27,7 +28,7 @@ def conditional_download(download_directory_path : str, urls : List[str]) -> Non if initial_size < download_size: with tqdm(total = download_size, initial = initial_size, desc = wording.get('downloading'), unit = 'B', unit_scale = True, unit_divisor = 1024, ascii = ' =', disable = state_manager.get_item('log_level') in [ 'warn', 'error' ]) as progress: - subprocess.Popen([ 'curl', '--create-dirs', '--silent', '--insecure', '--location', '--continue-at', '-', '--output', download_file_path, url ]) + subprocess.Popen([ shutil.which('curl'), '--create-dirs', '--silent', '--insecure', '--location', '--continue-at', '-', '--output', download_file_path, url ]) current_size = initial_size progress.set_postfix(file = download_file_name) diff --git a/facefusion/ffmpeg.py b/facefusion/ffmpeg.py index 90ccff2a..68107c51 100644 --- a/facefusion/ffmpeg.py +++ b/facefusion/ffmpeg.py @@ -1,4 +1,5 @@ import os +import shutil import subprocess import tempfile from typing import List, Optional @@ -13,7 +14,7 @@ from facefusion.vision import restrict_video_fps def run_ffmpeg(args : List[str]) -> subprocess.Popen[bytes]: - commands = [ 'ffmpeg', '-hide_banner', '-loglevel', 'error' ] + commands = [ shutil.which('ffmpeg'), '-hide_banner', '-loglevel', 'error' ] commands.extend(args) process = subprocess.Popen(commands, stderr = subprocess.PIPE, stdout = subprocess.PIPE) @@ -32,7 +33,7 @@ def run_ffmpeg(args : List[str]) -> subprocess.Popen[bytes]: def open_ffmpeg(args : List[str]) -> subprocess.Popen[bytes]: - commands = [ 'ffmpeg', '-hide_banner', '-loglevel', 'quiet' ] + commands = [ shutil.which('ffmpeg'), '-hide_banner', '-loglevel', 'quiet' ] commands.extend(args) return subprocess.Popen(commands, stdin = subprocess.PIPE, stdout = subprocess.PIPE) diff --git a/facefusion/installer.py b/facefusion/installer.py index 656be6e5..32a26cb4 100644 --- a/facefusion/installer.py +++ b/facefusion/installer.py @@ -56,7 +56,7 @@ def run(program : ArgumentParser) -> None: onnxruntime = answers['onnxruntime'] onnxruntime_name, onnxruntime_version = ONNXRUNTIMES[onnxruntime] - subprocess.call([ 'pip', 'install', '-r', 'requirements.txt', '--force-reinstall' ]) + subprocess.call([ shutil.which('pip'), 'install', '-r', 'requirements.txt', '--force-reinstall' ]) if onnxruntime == 'rocm': python_id = 'cp' + str(sys.version_info.major) + str(sys.version_info.minor) @@ -65,13 +65,13 @@ def run(program : ArgumentParser) -> None: wheel_name = 'onnxruntime_rocm-' + onnxruntime_version +'-' + python_id + '-' + python_id + '-linux_x86_64.whl' wheel_path = os.path.join(tempfile.gettempdir(), wheel_name) wheel_url = 'https://repo.radeon.com/rocm/manylinux/rocm-rel-6.2/' + wheel_name - subprocess.call([ 'curl', '--silent', '--location', '--continue-at', '-', '--output', wheel_path, wheel_url ]) - subprocess.call([ 'pip', 'uninstall', 'onnxruntime', wheel_path, '-y', '-q' ]) - subprocess.call([ 'pip', 'install', wheel_path, '--force-reinstall' ]) + subprocess.call([ shutil.which('curl'), '--silent', '--location', '--continue-at', '-', '--output', wheel_path, wheel_url ]) + subprocess.call([ shutil.which('pip'), 'uninstall', 'onnxruntime', wheel_path, '-y', '-q' ]) + subprocess.call([ shutil.which('pip'), 'install', wheel_path, '--force-reinstall' ]) os.remove(wheel_path) else: - subprocess.call([ 'pip', 'uninstall', 'onnxruntime', onnxruntime_name, '-y', '-q' ]) - subprocess.call([ 'pip', 'install', onnxruntime_name + '==' + onnxruntime_version, '--force-reinstall' ]) + subprocess.call([ shutil.which('pip'), 'uninstall', 'onnxruntime', onnxruntime_name, '-y', '-q' ]) + subprocess.call([ shutil.which('pip'), 'install', onnxruntime_name + '==' + onnxruntime_version, '--force-reinstall' ]) if onnxruntime == 'cuda' and has_conda: library_paths = [] @@ -88,7 +88,7 @@ def run(program : ArgumentParser) -> None: ]) library_paths = [ library_path for library_path in library_paths if os.path.exists(library_path) ] - subprocess.call([ 'conda', 'env', 'config', 'vars', 'set', 'LD_LIBRARY_PATH=' + os.pathsep.join(library_paths) ]) + subprocess.call([ shutil.which('conda'), 'env', 'config', 'vars', 'set', 'LD_LIBRARY_PATH=' + os.pathsep.join(library_paths) ]) if is_windows(): if os.getenv('PATH'): diff --git a/facefusion/wording.py b/facefusion/wording.py index e5a9a454..ea236782 100755 --- a/facefusion/wording.py +++ b/facefusion/wording.py @@ -4,6 +4,7 @@ WORDING : Dict[str, Any] =\ { 'conda_not_activated': 'Conda is not activated', 'python_not_supported': 'Python version is not supported, upgrade to {version} or higher', + 'curl_not_installed': 'CURL is not installed', 'ffmpeg_not_installed': 'FFMpeg is not installed', 'creating_temp': 'Creating temporary resources', 'extracting_frames': 'Extracting frames with a resolution of {resolution} and {fps} frames per second', diff --git a/install.py b/install.py index 19a63e82..7aef9446 100755 --- a/install.py +++ b/install.py @@ -1,11 +1,12 @@ #!/usr/bin/env python3 import os +import shutil import subprocess os.environ['SYSTEM_VERSION_COMPAT'] = '0' os.environ['PIP_BREAK_SYSTEM_PACKAGES'] = '1' -subprocess.call([ 'pip', 'install', 'inquirer', '-q' ]) +subprocess.call([ shutil.which('pip'), 'install', 'inquirer', '-q' ]) from facefusion import installer