python自动发布

复制代码
import os

import paramiko

baseconfig = {
    "ip": "121.4.38.187",
    "port": 22,
    "username": "",
    "password": "",
    "localdir": "E:\\javawork\\moodapi\\target\\classes",
    "remotedir": "/www/wwwroot/zshapi",
    "startsplit": "target",
    "exclude": [],
    "include": ["Info"],
    "fielExt": ".class",
    "succExec": "",
    "skipDircheck": False
}


# 遍历所有文件夹下的文件
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 checkInclude(file):
    filename = os.path.split(file)[1]
    filename = filename.lower()
    for regstr in baseconfig["include"]:
        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
        if len(baseconfig["include"]) > 0:
            if checkInclude(f) == False:
                continue
        p = baseconfig["remotedir"] + getRemotedir(f)
        if baseconfig["skipDircheck"] == False:
            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)
    if baseconfig["succExec"] != "":
        stdin, stdout, stderr = ssh.exec_command(baseconfig["succExec"])
        stdout.readline()
    ssh.close()


if __name__ == '__main__':
    files = walkFiles(baseconfig["localdir"], endpoint=baseconfig["fielExt"])
    upload(files)
复制代码

 

posted @   wujf  阅读(97)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2020-05-28 java优化几个小步骤
2018-05-28 过期算法
2018-05-28 负载均衡算法
2018-05-28 spring boot druid mybatis多数据源
点击右上角即可分享
微信分享提示