Spaces:
Running
on
Zero
Running
on
Zero
Create setup_environment.py
Browse files- setup_environment.py +147 -0
setup_environment.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
+
import sys
|
4 |
+
|
5 |
+
|
6 |
+
# pip ์
๋ฐ์ดํธ
|
7 |
+
def upgrade_pip():
|
8 |
+
"""pip๋ฅผ ์ต์ ๋ฒ์ ์ผ๋ก ์
๊ทธ๋ ์ด๋"""
|
9 |
+
print("Upgrading pip...")
|
10 |
+
subprocess.run([sys.executable, "-m", "pip", "install", "--upgrade", "pip"], check=True)
|
11 |
+
|
12 |
+
|
13 |
+
def set_pythonpath():
|
14 |
+
"""PYTHONPATH ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ (stf-tools์ ์ค์น ๊ฒฝ๋ก๋ฅผ ๊ธฐ๋ฐ์ผ๋ก)"""
|
15 |
+
|
16 |
+
# pip show stf-tools ๋ช
๋ น์ด ์คํ
|
17 |
+
try:
|
18 |
+
result = subprocess.run(['pip', 'show', 'stf-alternative'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
19 |
+
|
20 |
+
if result.returncode != 0:
|
21 |
+
print("stf-tools ํจํค์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
22 |
+
return
|
23 |
+
|
24 |
+
# ์ถ๋ ฅ์์ Location ์ฐพ๊ธฐ
|
25 |
+
location_line = next((line for line in result.stdout.splitlines() if line.startswith("Location:")), None)
|
26 |
+
|
27 |
+
if location_line:
|
28 |
+
# Location์์ ๊ฒฝ๋ก ์ถ์ถ
|
29 |
+
location_path = location_line.split("Location: ")[1].strip()
|
30 |
+
print('location_path===',location_path)
|
31 |
+
else:
|
32 |
+
print("Location ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
33 |
+
return
|
34 |
+
|
35 |
+
except Exception as e:
|
36 |
+
print(f"ํจํค์ง ๊ฒฝ๋ก๋ฅผ ๊ฐ์ ธ์ค๋ ์ค ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}")
|
37 |
+
return
|
38 |
+
|
39 |
+
# ํ์ฌ PYTHONPATH ํ์ธ
|
40 |
+
current_pythonpath = os.environ.get("PYTHONPATH", "")
|
41 |
+
|
42 |
+
# ์๋ก ์ถ๊ฐํ PYTHONPATH ๊ฒฝ๋ก (stf-tools ์์น ๊ธฐ๋ฐ)
|
43 |
+
new_pythonpath = location_path
|
44 |
+
|
45 |
+
# ๊ธฐ์กด PYTHONPATH๊ฐ ์์ผ๋ฉด ์ถ๊ฐํ๊ณ , ์์ผ๋ฉด ์๋ก ์ค์
|
46 |
+
if current_pythonpath:
|
47 |
+
new_pythonpath = f"{new_pythonpath}:{current_pythonpath}"
|
48 |
+
|
49 |
+
# PYTHONPATH ์ค์
|
50 |
+
os.environ["PYTHONPATH"] = new_pythonpath
|
51 |
+
print(f"PYTHONPATH set to: {os.environ['PYTHONPATH']}")
|
52 |
+
|
53 |
+
|
54 |
+
# ํ์ฌ ๋๋ ํ ๋ฆฌ ์ ์ฅ
|
55 |
+
original_dir = os.getcwd()
|
56 |
+
|
57 |
+
def run_pip_install(path):
|
58 |
+
"""ํด๋น ๊ฒฝ๋ก๋ก ์ด๋ํ ํ pip install . ์คํ"""
|
59 |
+
if os.path.exists(os.path.join(path, 'setup.py')) or os.path.exists(os.path.join(path, 'pyproject.toml')):
|
60 |
+
print(f"Installing dependencies in {path} using pip install .")
|
61 |
+
os.chdir(path)
|
62 |
+
try:
|
63 |
+
result = subprocess.run([sys.executable, "-m", "pip", "install", "--user", "."], check=True, stderr=subprocess.PIPE, text=True)
|
64 |
+
print(result.stdout)
|
65 |
+
except subprocess.CalledProcessError as e:
|
66 |
+
print(f"Error occurred while installing in {path}: {e.stderr}")
|
67 |
+
else:
|
68 |
+
print(f"No setup.py or pyproject.toml found in {path}, skipping...")
|
69 |
+
|
70 |
+
|
71 |
+
def check_stf_alternative_installed():
|
72 |
+
# pip list | grep stf-alternative ๋ช
๋ น์ ์คํ
|
73 |
+
result = subprocess.run(['pip', 'list'], stdout=subprocess.PIPE, text=True)
|
74 |
+
|
75 |
+
# ์ถ๋ ฅ์์ stf-alternative ํจํค์ง๊ฐ ์๋์ง ํ์ธ
|
76 |
+
if "stf-alternative" in result.stdout:
|
77 |
+
print("stf-alternative ํจํค์ง๊ฐ ์ค์น๋์ด ์์ต๋๋ค.")
|
78 |
+
else:
|
79 |
+
print("stf-alternative ํจํค์ง๊ฐ ์ค์น๋์ง ์์์ต๋๋ค.")
|
80 |
+
|
81 |
+
# ์ถ๋ ฅ์์ stf-alternative ํจํค์ง๊ฐ ์๋์ง ํ์ธ
|
82 |
+
if "stf-tools" in result.stdout:
|
83 |
+
print("stf-tools ํจํค์ง๊ฐ ์ค์น๋์ด ์์ต๋๋ค.")
|
84 |
+
else:
|
85 |
+
print("stf-tools ํจํค์ง๊ฐ ์ค์น๋์ง ์์์ต๋๋ค.")
|
86 |
+
|
87 |
+
|
88 |
+
# ์ถ๊ฐ๋ ํจ์๋ค: 'libcublasLt.so.11'์ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ LD_LIBRARY_PATH์ ์ถ๊ฐํ๋ ํจ์
|
89 |
+
def find_library(library_name):
|
90 |
+
try:
|
91 |
+
result = subprocess.run(['find', '/usr', '-name', library_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
92 |
+
library_path = result.stdout.decode('utf-8').strip()
|
93 |
+
|
94 |
+
if library_path:
|
95 |
+
library_dir = os.path.dirname(library_path)
|
96 |
+
return library_dir
|
97 |
+
else:
|
98 |
+
print(f"๋ผ์ด๋ธ๋ฌ๋ฆฌ {library_name}์(๋ฅผ) ์ฐพ์ ์ ์์ต๋๋ค.")
|
99 |
+
return None
|
100 |
+
except Exception as e:
|
101 |
+
print(f"์ค๋ฅ ๋ฐ์: {e}")
|
102 |
+
return None
|
103 |
+
|
104 |
+
def add_to_ld_library_path(library_dir):
|
105 |
+
if library_dir:
|
106 |
+
ld_library_path = os.environ.get('LD_LIBRARY_PATH', '')
|
107 |
+
if library_dir not in ld_library_path:
|
108 |
+
os.environ['LD_LIBRARY_PATH'] = library_dir + ':' + ld_library_path
|
109 |
+
print(f"{library_dir}์ด(๊ฐ) LD_LIBRARY_PATH์ ์ถ๊ฐ๋์์ต๋๋ค.")
|
110 |
+
else:
|
111 |
+
print(f"{library_dir}์ด(๊ฐ) ์ด๋ฏธ LD_LIBRARY_PATH์ ํฌํจ๋์ด ์์ต๋๋ค.")
|
112 |
+
else:
|
113 |
+
print("์ ํจํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฒฝ๋ก๊ฐ ์์ต๋๋ค.")
|
114 |
+
|
115 |
+
|
116 |
+
# ์ ์ฒด ํ๊ฒฝ ์ค์ ํ๋ก์ธ์ค ์คํ ํจ์
|
117 |
+
def initialize_environment():
|
118 |
+
"""ํ๊ฒฝ ์ค์ ์ ์ํ ์ ์ฒด ํ๋ก์ธ์ค ์คํ"""
|
119 |
+
print('aaaaaaaaaaaa')
|
120 |
+
|
121 |
+
# pip ์
๊ทธ๋ ์ด๋ ์คํ
|
122 |
+
upgrade_pip()
|
123 |
+
|
124 |
+
# ํจ์ ํธ์ถ
|
125 |
+
print('11111111')
|
126 |
+
check_stf_alternative_installed()
|
127 |
+
|
128 |
+
# stf-api-alternative์์ pip install . ์คํ
|
129 |
+
run_pip_install("/home/user/app/stf/stf-api-alternative")
|
130 |
+
|
131 |
+
# stf-api-tools์์ pip install . ์คํ
|
132 |
+
run_pip_install("/home/user/app/stf/stf-api-tools")
|
133 |
+
|
134 |
+
# PYTHONPATH ์ค๏ฟฝ๏ฟฝ๏ฟฝ
|
135 |
+
set_pythonpath()
|
136 |
+
|
137 |
+
# 'libcublasLt.so.11' ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฒฝ๋ก ์ฐพ๊ธฐ ๋ฐ LD_LIBRARY_PATH์ ์ถ๊ฐ
|
138 |
+
library_name = 'libcublasLt.so.*'
|
139 |
+
library_dir = find_library(library_name)
|
140 |
+
add_to_ld_library_path(library_dir)
|
141 |
+
|
142 |
+
# ํจ์ ํธ์ถ
|
143 |
+
print('222222')
|
144 |
+
check_stf_alternative_installed()
|
145 |
+
|
146 |
+
# ์๋ ์์
๋๋ ํ ๋ฆฌ๋ก ๋์์ด
|
147 |
+
os.chdir(original_dir)
|