import sys
import time
import os
import paramiko
from pygments.lexers import shell
import zipfile
import json
baseconfig =None
# baseconfig={
# "ip": "xxxxxxx",
# "port": xx,
# "username": "x",
# "password": "xxx",
# "remotedir": "/xx/xx/xxx"
# }
with open("pyconfig","r") as f:
baseconfig = json.load(f)
print("===========开始npm打包===========")
os.system('npm run build')
print("===========npm打包完成,开始生成zip===========")
# 开始打包为zip
output_name = "dist.zip"
if os.path.exists(output_name):
os.remove(output_name)
zip = zipfile.ZipFile(output_name, "w", zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk("./dist"):
for file in files:
if str(file).startswith("~$"):
continue
filepath = os.path.join(root, file)
writepath = os.path.relpath(filepath)
zip.write(filepath, writepath)
zip.close()
print("===========生成zip结束===========")
jarpath = sys.path[0] + "/" + output_name
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=baseconfig["ip"], port=baseconfig["port"], username=baseconfig["username"],
password=baseconfig["password"])
ssh.get_transport()
sftp = paramiko.SFTPClient.from_transport(ssh.get_transport())
remotedir = baseconfig["remotedir"]
sftp.put(output_name, remotedir + output_name)
print("===========上传成功===========")
shell = "cd %s && rm -rf static dist index.html favicon.ico && unzip %s && mv dist/* ./" % (remotedir, output_name)
stdin, stdout, stderr = ssh.exec_command(shell)
print(stderr.readline())
print(stdout.read())
print("===========重启成功===========")
exit(0)