yerang commited on
Commit
8379304
โ€ข
1 Parent(s): 5a51b84

Create setup_environment.py

Browse files
Files changed (1) hide show
  1. 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)