• 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)

    有追求,才有动力!

    向每一个软件工程师致敬!

    by wujf

    mail:921252375@qq.com

  • 相关阅读:
    技术分享 | app自动化测试(Android)–App 控件交互
    测试人生 | 疫情之下工资翻了2倍多,这4个月学习比工作8年学到的还多
    23 树——学习红黑树的捷径
    红黑树代码实现及注释
    工作的第二和第三年(201807~202005)
    使用nodejs的wxmnode模块,开发一个微信自动监控提醒功能,做个天气预报。
    难蚌
    基于.NetCore开发博客项目 StarBlog (12) Razor页面动态编译
    .NetCore实现图片缩放与裁剪 基于ImageSharp
    Serverless 时代下微服务应用全托管解决方案
  • 原文地址:https://www.cnblogs.com/wujf/p/14821492.html
Copyright © 2020-2023  润新知