-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.py
126 lines (109 loc) · 4.21 KB
/
server.py
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
import subprocess
import os
import threading
import platform
class Cancelled(Exception):
pass
def get_nodejs_path():
system = platform.system()
if system == "Windows":
try:
node_path = subprocess.check_output(["where", "node"]).decode().strip()
if node_path:
return node_path.split("\n")[0]
except subprocess.CalledProcessError:
return None
elif system == "Linux":
try:
node_path = subprocess.check_output(["which", "node"]).decode().strip()
if node_path:
return node_path
except subprocess.CalledProcessError:
return None
else:
return None
return None
def read_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
return content
except FileNotFoundError:
return ""
def run_node_program(nodepdth,node_script_path):
def shuchu(a):
if(str(a).find("fkhides")==-1):
print(a)
def _run_node_program():
try:
command = [nodepdth, node_script_path]
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding='utf-8',
errors='replace'
)
while True:
output = process.stdout.readline().strip()
if output == '' and process.poll() is not None:
break
if output:
shuchu(f"\33[93mFKServer: {output}\33[0m")
error = process.stderr.read().strip()
if error:
shuchu(f"FKServer: {error}")
if process.returncode!= 0:
print(f"FKServer: {process.returncode}")
except Exception as e:
print(f"FKServer error occurred: {e}")
thread = threading.Thread(target=_run_node_program)
thread.start()
node_lujin = get_nodejs_path()
def setnev(nd):
global node_lujin
if nd:
system = platform.system()
current_path = os.environ.get('PATH', '')
nlijin = nd # 初始化 nlijin 变量
if system == "Windows":
nlijin = nd.replace(f'\\node.exe',f'')
nlijin = nlijin.replace(f'/node.exe',f'')
os.environ["PATH"] = nlijin + ";" + current_path
else:
nlijin = nlijin.replace(f'/bin/node',f'/bin')
nlijin = nlijin.replace(f'\\bin/node',f'\\bin')
os.environ["PATH"] = nlijin + ":" + current_path
node_lujin = "node"
if(not node_lujin):
ndpath = os.path.join(os.path.dirname(__file__), 'server/node.txt')
node_lujin = read_file(ndpath)
setnev(node_lujin)
if (node_lujin):
node_lujin = node_lujin.strip()
else:
ndpath = os.path.join(os.path.dirname(__file__), 'server/node.txt')
print(f'未检测到NodeJs,你可以在 {ndpath} 文件中写入node程序路径,手动配置NodeJs路径')
node_lujin = ""
if (node_lujin):
serpath = os.path.join(os.path.dirname(__file__), 'server/')
if (os.path.exists(serpath+'node_modules')==True):
node_script_path = os.path.join(serpath, 'suanli.js')
run_node_program(node_lujin,node_script_path)
else:
print(f'正在安装算力环境...')
os.chdir(serpath)
result = subprocess.run('npm install --registry=https://registry.npmmirror.com', shell=True, capture_output=True, text=True)
if result.returncode == 0:
node_script_path = os.path.join(serpath, 'suanli.js')
run_node_program(node_lujin,node_script_path)
else:
print(f'算力环境自动安装失败:请在server目录下执行 npm install 命令安装算力环境')
else:
config_path = os.path.join(os.path.dirname(__file__), "server/data.json")
try:
os.remove(config_path)
except Exception as e:
pass
print(f"系统未安装NodeJs,无法运行FKServer云算力环境")