• 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

  • 相关阅读:
    k近邻 KNN
    聚类之k-means
    支持向量机SVM、优化问题、核函数
    [THUSC 2016] 补退选 (Trie树)
    [CQOI2016] 手机号码 (数位dp)
    [CQOI2012] 交换棋子 (费用流)
    [SCOI2016] 背单词 (Trie树)
    [JSOI2009] 球队收益 (费用流)
    [BZOJ1878][SDOI2009] HH的项链 (树状数组)
    [BZOJ2151]种树
  • 原文地址:https://www.cnblogs.com/wujf/p/14821492.html
Copyright © 2020-2023  润新知