码迷,mamicode.com
首页 > 编程语言 > 详细

python自动发布

时间:2021-06-02 14:09:42      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:end   自动发布   pass   walk   shc   文件   one   自动   science   

import os

import paramiko

baseconfig = {
    "ip": "192.168.68.58",
    "port": 22,
    "username": "root",
    "password": "",
    "localdir": "E:/code/java/science-api/target/classes",
    "remotedir": "/home/api",
    "startsplit": "target",
    "exclude": ["info", "Controller"]
}


# 遍历所有文件夹下的文件
def walkFiles(path, endpoint=None):
    file_list = []
    for root, dirs, files in os.walk(path):
        for file in files:
            file_path = os.path.join(root, file)
            if endpoint:
                if file_path.endswith(endpoint):
                    file_list.append(file_path)
            else:
                file_list.append(file_path)
    return file_list


def getRemotedir(f):
    if baseconfig["startsplit"] != "":
        p = f.split(baseconfig["startsplit"])[1].replace("\\", "/")
        return p


def checkExclude(file):
    filename = os.path.split(file)[1]
    filename = filename.lower()
    for regstr in baseconfig["exclude"]:
        if filename.__contains__(regstr.lower()):
            return True
    return False


def upload(files):
    if len(files) == 0:
        return
    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())
    for f in files:
        if checkExclude(f):
            print("跳过:%s" % f)
            continue
        p = baseconfig["remotedir"] + getRemotedir(f)
        remotedir = os.path.split(p)[0]
        stdin, stdout, stderr = ssh.exec_command("ls " + remotedir)
        if stdout.readline() == ‘‘:
            stdin, stdout, stderr = ssh.exec_command("mkdir -p " + remotedir)
            stdout.readline()
        print("上传:%s至%s" % (f, p))
        sftp.put(f, p)
    ssh.close()


if __name__ == __main__:
    files = walkFiles(baseconfig["localdir"], endpoint=".class")
    upload(files)

 

python自动发布

标签:end   自动发布   pass   walk   shc   文件   one   自动   science   

原文地址:https://www.cnblogs.com/wujf/p/14821492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!