Files
jcm_test/server/jcm.py

368 lines
11 KiB
Python
Raw Normal View History

2023-08-22 04:30:01 +08:00
# coding=utf-8
#!/bin/python
import os
import sys
import imp
import locale
2023-11-26 20:20:29 +00:00
import platform
2023-08-22 04:30:01 +08:00
OS=''
Versino = "0.0.2"
port = 8888
python = sys.executable
dir = os.getcwd()
2023-11-26 20:20:29 +00:00
OS = platform.system()
2023-08-22 04:30:01 +08:00
OS_ = ''
dev_name = ''
bin = ''
bash = ''
2023-11-26 20:20:29 +00:00
ipaddr = ''
install = ''
2023-08-22 04:30:01 +08:00
iftext = {}
iftext["osname"] = "OS Name:"
iftext["devname"] = "Host Name:"
2023-11-26 20:20:29 +00:00
iftext["netdir"] = "Network Card(s):"
iftext["netname"] = "Connection Name:"
iftext["ip"] = "IP addrees(es)"
2023-08-22 04:30:01 +08:00
text = {}
text["init"] = "init"
text["user"] = "User:"
text["passwor"] = "Password:"
2023-11-26 20:20:29 +00:00
#加载语言文件
2023-08-22 04:30:01 +08:00
try:
language = locale.getdefaultlocale()
language = language[0]
#language = "zh_CN"
2023-11-26 20:20:29 +00:00
if os.path.exists("language/server/init.py/" + language + ".py"):
run = imp.load_source('run',"language/server/init.py/" + language + ".py")
for textname in iftext.keys():
try:
textval = run.text[textname]
iftext[textname] = textval
except Exception as e:
print(e.args)
except Exception as e:
print(e.args)
try:
language = locale.getdefaultlocale()
language = language[0]
#language = "zh_CN"
if os.path.exists("language/server/init.py/" + language + ".py"):
run = imp.load_source('run',"language/server/init.py/" + language + ".py")
for textname in text.keys():
try:
textval = run.text[textname]
text[textname] = textval
except Exception as e:
print(e.args)
2023-08-22 04:30:01 +08:00
except Exception as e:
print(e.args)
if OS != "Windows":
try:
sh = os.popen('uname')
OS = sh.read().split('\n')[0]
#if OS == ''or OS[:7] == 'MINGW32':
osuser = os.popen("id -un").read().split('\n')[0].split('\r')[0]
if osuser != "root":
print("No root user,exit!")
exit()
except Exception as e:
print(e.args)
else:
os.environ['path']= dir + "\\Tools\\.bash\\bin\\;" + os.getenv('path')
python = '"' + python + '"'
bash = dir + "\\Tools\\.bash\\bin\\"
if os.path.exists(dir + "\\Tools\\7z") == False:
os.system("mkdir Tools\\7z")
cmd = "" + "xcopy " + dir + "\\lib\\7z\\* " + dir + "\\Tools\\7z"
os.system(cmd)
if os.path.exists(dir + "\\Tools\\.bash\\bin") == False:
cmd = dir + "\\Tools\\7z\\7z.exe x " + dir + "\\lib\\bash.zip -r -o" + dir + "\\Tools\\"
os.system(cmd)
OS_ = OS
def dpkginstall(name):
2023-11-26 20:20:29 +00:00
pkg = ''
if os.path.exists("/bin/apt"):
if name == "update":
2024-03-03 19:43:51 +00:00
os.system("#apt update")
2023-11-26 20:20:29 +00:00
pkg = "apt install -y "
elif os.path.exists("/bin/yum"):
pkg = "yum install -y "
elif os.path.exists("/bin/opkg"):
if name == "update":
os.system("opkg update")
pkg = "opkg install "
if name == (python.split('/')[-1] + "-pip "):
if os.path.exists("/bin/pip3") == False:
os.system(pkg + " " + name)
elif name == "update":
print()
elif name == (python.split('/')[-1] + "-dev"):
2024-03-03 19:43:51 +00:00
if pkg == "yum install -y ":
2023-11-26 20:20:29 +00:00
os.system(pkg + " " + python.split('/')[-1] + "-devel")
else:
os.system(pkg + " " + name)
elif name == ("gcc"):
if os.path.exists("/bin/gcc") == False:
os.system(pkg + " " + name)
elif name == ("curl"):
if os.path.exists("/bin/curl") == False:
os.system(pkg + " " + name)
2023-08-22 04:30:01 +08:00
def pip(name):
run = 1
if os.path.exists("info.sh"):
os.system("./info.sh '" + name + "'")
2023-08-22 04:30:01 +08:00
print('pip ' + name)
while run == 1:
run = pipinstall(name," ")
2024-03-03 19:43:51 +00:00
if run == 1:
run = pipinstall(name," --break-system-packages ")
2023-08-22 04:30:01 +08:00
if run == 1:
run = pipinstall(name,"https://pypi.tuna.tsinghua.edu.cn/simple")
if run == 1:
link = ''
if name == 'psutil':
link = '==4.4.2'
run = pipinstall(name,link)
if run == 1:
run = pipinstall(name,link + " -i https://pypi.tuna.tsinghua.edu.cn/simple")
2023-11-26 20:20:29 +00:00
2023-08-22 04:30:01 +08:00
def pipinstall(name,link):
sh = os.popen("" + python + " -m pip list | " + bash + "grep '" + name + "'")
2024-03-03 19:43:51 +00:00
shell = sh.read().split("\n")[0].split(' ')
2023-08-22 04:30:01 +08:00
if shell[0] != name:
if link[0] == 'h':
link = ' -i ' + link
2024-03-03 19:43:51 +00:00
os.system(bin + "" + python + " -m pip install " + name + link)
2023-08-22 04:30:01 +08:00
sh = os.popen("" + python + " -m pip list | " + bash + "grep '" + name + "'")
2024-03-03 19:43:51 +00:00
shell = sh.read().split("\n")[0].split(' ')
2023-08-22 04:30:01 +08:00
if shell[0] == name:
pr = ''
2024-03-03 19:43:51 +00:00
for p in shell:
2023-08-22 04:30:01 +08:00
pr = pr + p
if pr == name:
pr = pr + ' '
if os.path.exists("info.sh"):
os.system("./info.sh '" + pr + "'")
2023-08-22 04:30:01 +08:00
print(pr)
return 0
else:
pr = ''
2024-03-03 19:43:51 +00:00
for p in shell:
2023-08-22 04:30:01 +08:00
pr = pr + p
if pr == name:
pr = pr + ' '
if os.path.exists("info.sh"):
os.system("./info.sh '" + pr + "'")
2023-08-22 04:30:01 +08:00
print(pr)
return 0
return 1
def file(dir,new):
if not os.path.exists(dir):
fs = open(dir,"w")
fs.write(new)
fs.close()
if OS == 'Windows':
2023-11-26 20:20:29 +00:00
ifnet = False
ifip = False
ifkg = False
2023-08-22 04:30:01 +08:00
sh = os.popen("systeminfo")
shell = sh.read().split('\n')
for sh in range(len(shell)):
#print('[' + str(sh) + '] ' + shell[sh])
sh = shell[sh].split(' ')
if sh[0] == iftext["osname"]:
OS = sh[-1]
if sh[0] == iftext["devname"]:
dev_name = sh[-1]
2023-11-26 20:20:29 +00:00
if ifnet:
data = ''
kg = 0
if sh[0] != '':
ifnet = False
else:
for ss in sh:
data = data + ss
if ss != '':
data = data + '\t'
else:
kg = kg + 1
sh = data.split('\t')
if ifip:
if ifkg == False or ifkg == kg:
if sh[0][0] == '[':
sh = sh[0].split(' ')
ipaddr = ipaddr + sh[1] + ' '
if ifkg == 0:
ifkg = kg
else:
ifip = False
ipaddr = ipaddr + '\r'
if sh[0] == iftext["ip"]:
ifip = True
if sh[0] == iftext["netname"]:
ipaddr = ipaddr + sh[1] + '\t'
if sh[0] == iftext["netdir"]:
ifnet = True
2023-08-22 04:30:01 +08:00
if OS == '':
if os.path.exists("info.sh"):
os.system("./info.sh '" + text["oserr"] + "'")
2023-08-22 04:30:01 +08:00
print(text["oserr"])
OS = 'Windows'
else:
if os.path.exists('/etc/os-release'):
sh = os.popen("cat /etc/os-release | grep PRETTY_NAME")
shell = sh.read().split('"')
OS = shell[1]
else:
sh = os.popen("uname -n")
shell = sh.read()
OS = shell[:-1]
2023-11-26 20:20:29 +00:00
sh = os.popen('cat /etc/hostname')
dev_name = sh.read().split('\r')[0].split('\n')[0]
2023-08-22 04:30:01 +08:00
if os.path.exists(".logs") == False:
os.mkdir(".logs")
if os.path.exists(".logs/run.py") == False:
os.mkdir(".logs/run.py")
if os.path.exists(".config/main/user") == False:
if os.path.exists(".config") == False:
os.mkdir(".config")
if os.path.exists(".config/main") == False:
os.mkdir(".config/main")
print(text["init"])
user = ''
2024-03-03 19:43:51 +00:00
if len(sys.argv) == 5:
if sys.argv[1] == "hass":
print(sys.argv)
user = sys.argv[3].encode("utf-8")
else:
user = input(text["user"]).encode("utf-8")
password = ''
2024-03-03 19:43:51 +00:00
if len(sys.argv) == 5:
if sys.argv[1] == "hass":
print(sys.argv)
password = sys.argv[4].encode("utf-8")
else:
password = input(text["passwor"]).encode("utf-8")
2023-08-22 04:30:01 +08:00
tools = imp.load_source('tools',"Tools/Tools.py")
tools.newuser(user,password,b"0")
2024-03-03 19:43:51 +00:00
#if OS_ == "Linux":
install = "L"
2023-11-26 20:20:29 +00:00
2023-08-22 04:30:01 +08:00
if os.path.exists(".config/main/port"):
fs = open(".config/main/port", "rb")
ini = fs.read().decode("utf-8")
port = int(ini)
if len(sys.argv) == 5:
if sys.argv[1] == "hass":
print(sys.argv)
port = int(sys.argv[2])
2023-08-22 04:30:01 +08:00
file(".config/main/cookie","\n")
file(".config/sessino","sessinon\n")
file(".config/main/lits.json",'{"name":"'+"127.0.0.1"+'","host":"'+"127.0.0.1"+'"},' + '\n')
OSS = OS.split(' ')
if OS_ == "Windows":
pip("psutil")
pip("pywin32")
2024-03-03 19:43:51 +00:00
pip("requests")
pip("pythonnet")
2023-08-22 04:30:01 +08:00
elif OS == "DSM":
if os.path.exists("server/psutil.py") == False:
os.system('cp lib/psutil.py ./server/')
if os.path.exists("server/_common.py") == False:
os.system('cp lib/_common.py ./server/')
elif OS_ == "Linux":
t = 0
if len(sys.argv) == 5:
if sys.argv[1] == "hass":
t = 1
if t == 0:
dpkginstall("update")
2024-03-03 19:43:51 +00:00
dpkginstall("python3" + "-pip ")
dpkginstall("python3" + "-dev")
dpkginstall("gcc")
dpkginstall("curl")
2023-08-22 04:30:01 +08:00
pip("psutil")
2024-03-03 19:43:51 +00:00
pip("requests")
2023-08-22 04:30:01 +08:00
OS_ = os.popen('if [ "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/.)" ]; then echo "chroot"; else echo "Linux"; fi').read().split('\r')[0].split('\n')[0]
sh = os.popen(python + " -V")
shell = sh.read().split('\n')
theme = "Ace_Admin"
if os.path.exists("info.sh"):
os.system("./info.sh '" + shell[0] + "'")
os.system("./info.sh '" + "JCM " + Versino + "'")
os.system("./info.sh '" + "name: " + dev_name + "'")
os.system("./info.sh '" + "python: " + python + "'")
os.system("./info.sh '" + "dir: " + dir + "'")
os.system("./info.sh '" + "OS: " + OS + "'")
os.system("./info.sh '" + "OS: " + OS_ + "'")
os.system("./info.sh '" + "port: " + str(port) + "'")
os.system("./info.sh '" + "theme: " + theme + "'")
2023-08-22 04:30:01 +08:00
print(shell[0])
print("JCM " + Versino)
print("name: " + dev_name)
print("python: " + python)
print("dir: " + dir)
print("OS: " + OS)
print("OS: " + OS_)
print("port: " + str(port))
print("theme: " + theme)
2024-03-03 19:43:51 +00:00
try:
import psutil
print("Net: ")
net = psutil.net_if_addrs()
for i in net:
addrees = net[i]
addrees = addrees[0]
addrees = addrees.address
print('\t' + i + '\t' + addrees)
except Exception as e:
print(e.args)
2023-08-22 04:30:01 +08:00
info = {}
info["Versino"] = Versino
info["python"] = python
info["dir"] = dir
info["OS"] = OS_
info["OS_name"] = OS
info["port"] = port
info["dev_name"] = dev_name
info["theme"] = theme
2023-11-26 20:20:29 +00:00
info["Headers"] = "Server: JCM/1.0\r\n"
2023-08-22 04:30:01 +08:00
info['debug'] = False
2023-11-26 20:20:29 +00:00
info['tmp'] = '/tmp/jcm'
2023-08-22 04:30:01 +08:00
2023-11-26 20:20:29 +00:00
if os.path.exists(".config/main/debug"):
info['debug'] = True
if dev_name == ' JIANG-G5-5500-P':
info['debug'] = True
2023-08-22 04:30:01 +08:00
if info['debug']:
if os.path.exists("info.sh"):
os.system("./info.sh '" + "DEBUG" + "'")
os.system("error.sh '" + "DEBUG" + "'")
2023-08-22 04:30:01 +08:00
print('Debug')
run = imp.load_source('run',"server/run.py")
2023-11-26 20:20:29 +00:00
if install == "L":
2024-03-03 19:43:51 +00:00
if OS_ == "Linux":
2023-11-26 20:20:29 +00:00
os.system("systemctl start jcm.service")
2024-03-03 19:43:51 +00:00
else:
os.system("dist/boot/boot.exe start")
2023-08-22 04:30:01 +08:00
run.main(info)
print("end")