Files
Scholar01-ComfyUI-Keyframe/launch_util.py
T
2023-11-10 21:15:46 +08:00

53 lines
1.3 KiB
Python

import importlib.util
import subprocess
import sys
import os
python = sys.executable
def is_installed(package):
try:
spec = importlib.util.find_spec(package)
except ModuleNotFoundError:
return False
return spec is not None
def run_pip(command, desc=None, live=False):
return run(f'"{python}" -m pip {command}', desc=f"Installing {desc}",
errdesc=f"Couldn't install {desc}", live=live)
def run(command, desc=None, errdesc=None, custom_env=None, live: bool = False) -> str:
if desc is not None:
print(desc)
run_kwargs = {
"args": command,
"shell": True,
"env": os.environ if custom_env is None else custom_env,
"encoding": 'utf8',
"errors": 'ignore',
}
if not live:
run_kwargs["stdout"] = run_kwargs["stderr"] = subprocess.PIPE
result = subprocess.run(**run_kwargs)
if result.returncode != 0:
error_bits = [
f"{errdesc or 'Error running command'}.",
f"Command: {command}",
f"Error code: {result.returncode}",
]
if result.stdout:
error_bits.append(f"stdout: {result.stdout}")
if result.stderr:
error_bits.append(f"stderr: {result.stderr}")
raise RuntimeError("\n".join(error_bits))
return (result.stdout or "")