LivePortrait2 / setup_environment.py
yerang's picture
Create setup_environment.py
cd87751 verified
import os
import subprocess
import sys
# pip ์—…๋ฐ์ดํŠธ
def upgrade_pip():
"""pip๋ฅผ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ"""
print("Upgrading pip...")
subprocess.run([sys.executable, "-m", "pip", "install", "--upgrade", "pip"], check=True)
def set_pythonpath():
"""PYTHONPATH ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ • (stf-tools์˜ ์„ค์น˜ ๊ฒฝ๋กœ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ)"""
# pip show stf-tools ๋ช…๋ น์–ด ์‹คํ–‰
try:
result = subprocess.run(['pip', 'show', 'stf-alternative'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode != 0:
print("stf-tools ํŒจํ‚ค์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
return
# ์ถœ๋ ฅ์—์„œ Location ์ฐพ๊ธฐ
location_line = next((line for line in result.stdout.splitlines() if line.startswith("Location:")), None)
if location_line:
# Location์—์„œ ๊ฒฝ๋กœ ์ถ”์ถœ
location_path = location_line.split("Location: ")[1].strip()
print('location_path===',location_path)
else:
print("Location ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
return
except Exception as e:
print(f"ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {e}")
return
# ํ˜„์žฌ PYTHONPATH ํ™•์ธ
current_pythonpath = os.environ.get("PYTHONPATH", "")
# ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•  PYTHONPATH ๊ฒฝ๋กœ (stf-tools ์œ„์น˜ ๊ธฐ๋ฐ˜)
new_pythonpath = location_path
# ๊ธฐ์กด PYTHONPATH๊ฐ€ ์žˆ์œผ๋ฉด ์ถ”๊ฐ€ํ•˜๊ณ , ์—†์œผ๋ฉด ์ƒˆ๋กœ ์„ค์ •
if current_pythonpath:
new_pythonpath = f"{new_pythonpath}:{current_pythonpath}"
# PYTHONPATH ์„ค์ •
os.environ["PYTHONPATH"] = new_pythonpath
print(f"PYTHONPATH set to: {os.environ['PYTHONPATH']}")
# ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ ์ €์žฅ
original_dir = os.getcwd()
def run_pip_install(path):
"""ํ•ด๋‹น ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•œ ํ›„ pip install . ์‹คํ–‰"""
if os.path.exists(os.path.join(path, 'setup.py')) or os.path.exists(os.path.join(path, 'pyproject.toml')):
print(f"Installing dependencies in {path} using pip install .")
os.chdir(path)
try:
result = subprocess.run([sys.executable, "-m", "pip", "install", "--user", "."], check=True, stderr=subprocess.PIPE, text=True)
print(result.stdout)
except subprocess.CalledProcessError as e:
print(f"Error occurred while installing in {path}: {e.stderr}")
else:
print(f"No setup.py or pyproject.toml found in {path}, skipping...")
def check_stf_alternative_installed():
# pip list | grep stf-alternative ๋ช…๋ น์„ ์‹คํ–‰
result = subprocess.run(['pip', 'list'], stdout=subprocess.PIPE, text=True)
# ์ถœ๋ ฅ์—์„œ stf-alternative ํŒจํ‚ค์ง€๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
if "stf-alternative" in result.stdout:
print("stf-alternative ํŒจํ‚ค์ง€๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.")
else:
print("stf-alternative ํŒจํ‚ค์ง€๊ฐ€ ์„ค์น˜๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
# ์ถœ๋ ฅ์—์„œ stf-alternative ํŒจํ‚ค์ง€๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
if "stf-tools" in result.stdout:
print("stf-tools ํŒจํ‚ค์ง€๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.")
else:
print("stf-tools ํŒจํ‚ค์ง€๊ฐ€ ์„ค์น˜๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
# ์ถ”๊ฐ€๋œ ํ•จ์ˆ˜๋“ค: 'libcublasLt.so.11'์˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„ LD_LIBRARY_PATH์— ์ถ”๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜
def find_library(library_name):
try:
result = subprocess.run(['find', '/usr', '-name', library_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
library_path = result.stdout.decode('utf-8').strip()
if library_path:
library_dir = os.path.dirname(library_path)
return library_dir
else:
print(f"๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ {library_name}์„(๋ฅผ) ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
return None
except Exception as e:
print(f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}")
return None
def add_to_ld_library_path(library_dir):
if library_dir:
ld_library_path = os.environ.get('LD_LIBRARY_PATH', '')
if library_dir not in ld_library_path:
os.environ['LD_LIBRARY_PATH'] = library_dir + ':' + ld_library_path
print(f"{library_dir}์ด(๊ฐ€) LD_LIBRARY_PATH์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
else:
print(f"{library_dir}์ด(๊ฐ€) ์ด๋ฏธ LD_LIBRARY_PATH์— ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.")
else:
print("์œ ํšจํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฒฝ๋กœ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
# ์ „์ฒด ํ™˜๊ฒฝ ์„ค์ • ํ”„๋กœ์„ธ์Šค ์‹คํ–‰ ํ•จ์ˆ˜
def initialize_environment():
"""ํ™˜๊ฒฝ ์„ค์ •์„ ์œ„ํ•œ ์ „์ฒด ํ”„๋กœ์„ธ์Šค ์‹คํ–‰"""
print('aaaaaaaaaaaa')
# pip ์—…๊ทธ๋ ˆ์ด๋“œ ์‹คํ–‰
upgrade_pip()
# ํ•จ์ˆ˜ ํ˜ธ์ถœ
print('11111111')
check_stf_alternative_installed()
# stf-api-alternative์—์„œ pip install . ์‹คํ–‰
run_pip_install("/home/user/app/stf/stf-api-alternative")
# stf-api-tools์—์„œ pip install . ์‹คํ–‰
run_pip_install("/home/user/app/stf/stf-api-tools")
# PYTHONPATH ์„ค์ •
set_pythonpath()
# 'libcublasLt.so.11' ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฒฝ๋กœ ์ฐพ๊ธฐ ๋ฐ LD_LIBRARY_PATH์— ์ถ”๊ฐ€
library_name = 'libcublasLt.so.*'
library_dir = find_library(library_name)
add_to_ld_library_path(library_dir)
# ํ•จ์ˆ˜ ํ˜ธ์ถœ
print('222222')
check_stf_alternative_installed()
# ์›๋ž˜ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ๋กœ ๋Œ์•„์˜ด
os.chdir(original_dir)