import paramiko
import os
import sys
import time
import zipfile

hostname=""
username=""
password=""
port=222

if sys.argv[3] == 'pro':
 hostname="000.luojigou.vip"
 username="admin"
 password='FUpnUQ8aLpngnEW'
else:
 hostname="local.luojigou.vip"
 username="root" 
 password='RDefC4Rh&I0VN1j1'
 port=22
############################## 
############################## 
############################## 

transport = paramiko.Transport((hostname, port))

transport.connect(username=username, password=password)

############################## 
############################## 
############################## 

sftp = paramiko.SFTPClient.from_transport(transport)

# transport.close(),
############################## 
############################## 
############################## 

# 创建SSH对象
ssh = paramiko.SSHClient()

# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname=hostname, port=port, username=username, password=password,allow_agent=False,look_for_keys=False)

############################## 
##############################
##############################

print("os.getcwd():",  os.getcwd())
local_path = os.getcwd() + "/dist/build" + "/h5.zip"

target_file_name = sys.argv[2]

remote_path = sys.argv[1]

remote_file_name = remote_path + target_file_name

print("本地文件地址: " + local_path )
print("远程目录地址： " + remote_file_name)

srmdir_all_folder = os.getcwd() + '/dist/build/h5'  # 文件夹路径

zip_file_path = srmdir_all_folder + ".zip"  # 压缩文件路径

print("打包中.....🍗")
def make_zip(source_dir, output_filename):
    zipf = zipfile.ZipFile(output_filename, 'w')    
    pre_len = len(os.path.dirname(source_dir))
    for parent, _, filenames in os.walk(source_dir):
        for filename in filenames:
            pathfile = os.path.join(parent, filename)
            arcname = pathfile[pre_len:].strip(os.path.sep)     #相对路径
            zipf.write(pathfile, arcname.replace('/h5', ''), zipfile.ZIP_DEFLATED)
    zipf.close()

make_zip(srmdir_all_folder, zip_file_path)

time.sleep(1)
print("打包完成.....😎")

print("上传中.....💪")
sftp.put(local_path,  remote_file_name + "/h5.zip")

time.sleep(1)

cmd1 = "unzip -o -d " + remote_file_name + " " + remote_file_name + "/h5.zip"

stdin, stdout, stderr= ssh.exec_command(cmd1)

result = stdout.read()

print("上传成功🎉🎉🎉🎉")

transport.close()

ssh.close()


