File size: 5,359 Bytes
cd87751
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa596e5
 
 
 
 
cd87751
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61e8130
cd87751
fa596e5
 
cd87751
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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("/tmp/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)