Spaces:
Running
on
Zero
Running
on
Zero
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) | |