美文网首页
Ftp上传和下载Apk脚本

Ftp上传和下载Apk脚本

作者: 江ls | 来源:发表于2020-11-19 15:06 被阅读0次
    通过 gradle重命名并上传apk
    • 在module里build.gradle的android闭包中添加
    configurations {
         ftpAntTask
    }
    

    dependencies闭包中添加

    ftpAntTask("org.apache.ant:ant-commons-net:1.8.4") {
        module("commons-net:commons-net:1.4.1") {
             dpendencies "oro:oro:2.0.8:jar"
        }
    }
    
    • 添加拷贝task
    String outName
    task assembleAndCopyApk(type: Copy) {
        // 拷贝apk
        from(originalFilePath) 
        into(destFilePath)
        // 重命名
        outName = "${releaseTime()}.apk"
        rename(orginalFileName, outName)
        // 编译命令
        dependsOn xxx
    }
    
    • 添加releaseTime函数
    def releaseTime() {
        return new Date().format("MMdd_HHmm")
    }
    
    • 添加FTP上传task
    task uploadToFtp {
        // 先执行编译,拷贝,重命名
        dependsOn 'assembleAndCopyApk'
        doLast {
             ant {
                    taskdef(name: 'ftp', 
                        classname:  'org.apache.tools.ant.taskdefs.optional.net.FTP', 
                        classpath: configurations.ftpAntTask.asPath)
                    ftp (server: "xxx.xxx.xxx.xxx",  //ftp地址
                          userid: "xxxx",  //用户名
                          password: "xxxx", //密码
                          remoteDir: "/") {
                          fileset(dir: outputDir) {
                                  include(name: outName)
                          }
                      }
             }
        }
    uploadToFtp.setGroup(group)
    
    使用python脚本下载和安装apk
    from ftplib import FTP
    import os, sys, string, datetime, time
    import socket
    
    class MYFTP:
        def __init__(self, rootdir_local, hostaddr, username, password, remotedir, port=21):
            self.hostaddr = hostaddr
            self.username = username
            self.password = password
            self.remotedir = remotedir
            self.port = port
            self.ftp = FTP()
            self.file_list = []
    
            self.rootdir_local = rootdir_local
            self.local_files = []
            self.remote_files = []
    
        # self.ftp.set_debuglevel(2)
        def __del__(self):
            self.ftp.close()
    
        # self.ftp.set_debuglevel(0)
        def login(self):
            ftp = self.ftp
            try:
                timeout = 300
                socket.setdefaulttimeout(timeout)
                ftp.set_pasv(True)
                print (u'开始连接到 %s' % (self.hostaddr))
                ftp.connect(self.hostaddr, self.port)
                print (u'成功连接到 %s' % (self.hostaddr))
                print (u'开始登录到 %s' % (self.hostaddr))
                ftp.login(self.username, self.password)
                print (u'成功登录到 %s' % (self.hostaddr))
                debug_print(ftp.getwelcome())
            except Exception:
                print (u'连接或登录失败')
            try:
                ftp.cwd(self.remotedir)
            except(Exception):
                print (u'切换目录失败')
    
        def margeFile(self):
            temp = []
            try:
                for f in self.remote_files:
                    if f in self.local_files:
                        temp.append(f)
                for f in temp:
                    self.remote_files.remove(f)
            except Exception as e:
                print (e)
            print (self.remote_files)
    
        def get_localFileList(self):
            localFiles = os.listdir(self.rootdir_local)
            for fileName in localFiles:
                # print ('本地文件列表:', fileName)
                self.local_files.append(fileName)
    
        def get_remoteFileList(self):
            print (u'读取服务器文件列表')
            print (self.ftp.dir())
            try:
                self.remote_files = self.ftp.nlst()
            except Exception as e:
                print (e)
            for name in self.remote_files:
                print ('服务端文件:', name)
    
        def download_file(self, localfile, remotefile):
            debug_print(u'>>>>>>>>>>>>下载文件 %s <<<<<<<<<<<<' % localfile)
            file_handler = open(localfile, 'wb')
            self.ftp.retrbinary(u'RETR %s' % (remotefile), file_handler.write)
            file_handler.close()
    
        def download_marge_files(self):
            name = ''
            for f in self.remote_files:
                self.download_file(rootdir_local + '/' + f, f)
                name = f
            print ('下载Apk:', name);
            cmdStr = 'adb install -r F:/%s' %name
            print (cmdStr)
            d = os.system(cmdStr)
            print ('安装APK: ', d)
    
    def debug_print(s):
        print (s)
    
    if __name__ == '__main__':
        timenow = time.localtime()
        datenow = time.strftime('%Y-%m-%d', timenow)
        # 配置如下变量
        hostaddr = 'xxx.xxx.xxx.xxx'  # ftp地址
        username = 'xxx'  # 用户名
        password = 'xxx'  # 密码
        port = 21  # 端口号
        rootdir_local = 'F:/'  # 本地目录
        rootdir_remote = '/'  # 远程目录
    
        f = MYFTP(rootdir_local, hostaddr, username, password, rootdir_remote, port)
        f.login()
        f.get_localFileList()
        f.get_remoteFileList()
        f.margeFile()
        f.download_marge_files()
    

    相关文章

      网友评论

          本文标题:Ftp上传和下载Apk脚本

          本文链接:https://www.haomeiwen.com/subject/bllmvktx.html