#! usr/bin/env python
# -*- coding: utf-8 -*-
#实现对文件的自定义命名的并批量复制,脚本并未对异常情况进行处理
import os
import datetime
import time
import fnmatch
import re
import shutil
time = time.strftime("%Y%m%d%H%M%S") #定义时间的输出格式
files = ["dday-collect", "dday-launcher", "dday-mediaserver",
"dday-music", "dday-navigation", "dday-navigation-amap-concept",
"dday-ota", "dday-podcast", "dday-system", "dday-voice",
"tsd-bluetooth", "tsd-cardvr", "tsd-news", "tsd-settings"]
toPath='/home/uftp/upload/999.版本发布'
toPath_debug = toPath + "/debug/"
#print toPath_debug
toPath_release = toPath + "/release/"
os.chdir(toPath_debug)
path1 = "Version_debug_" + time
os.mkdir(path1)
os.chdir(toPath_release)
path2 = "Version_release_" + time
os.mkdir(path2)
# print path
# files=["dday-bluetooth","dday-cardvr"]
os.system("cd ..")
# filedir = 'E:\\Program Files\\Jenkins\\workspace\\dday-bluetooth\\build\\outputs\\apk'
for file in files: #根据files遍历文件夹下的子文件夹
filedir="/home/tuyou/jenkins/workspace/"+file+"/build/outputs/apk/"
#filedir = "E:\\LOG\\12"
for parent, dirnames, filenames in os.walk(filedir): #os.walk的使用
for filename in filenames:
# 匹配release版本APK文件
# print filename
if fnmatch.fnmatch(filename, "*release.apk"): #fnmatch 的使用
filereleaseapk = filedir + "/" + filename
# print filereleaseapk
aapt = os.popen("aapt d badging " + filereleaseapk) #获取apk 的信息
s = aapt.read()
pattern = re.compile("versionName='(.*)\.(.*)\.(.*).*'", re.I) #正则匹配包信息中的versionName版本号
re1 = re.findall(pattern, s)
version = str(re1[0][0] + "." + re1[0][1] + "." + re1[0][2])
# print filereleaseapk
filename1 = os.path.splitext(filename)[0]
toPath1 = toPath_release + path2 + '/' + filename1 + "-" + version + "-" + time + ".apk"
# print toPath1
shutil.copyfile(filereleaseapk, toPath1)
# print filereleaseapk
if fnmatch.fnmatch(filename, "*debug.apk"):
filedebugapk = filedir + "/" + filename
aapt = os.popen("aapt d badging " + filedebugapk)
s = aapt.read()
pattern = re.compile("versionName='(.*)\.(.*)\.(.*).*'", re.I)
re1 = re.findall(pattern, s)
version = str(re1[0][0] + "." + re1[0][1] + "." + re1[0][2])
filename1 = os.path.splitext(filename)[0]
toPath2 = toPath_debug + path1 + '/' + filename1 + "-" + version + "-" + time + ".apk"
# toPath2 =toPath + "/debug/" + "Version_debug_" + time+'/' + filename1 + "-" + version + "-" + time+".apk"
shutil.copyfile(filedebugapk, toPath2) #shutil模块 拷贝
# print filedebugapk
# print filedir
# print file
print "传输完成!-------------------------!"
实现匹配文件(并自定义命名)的批量拷贝,后续上传改良版
最新推荐文章于 2022-12-23 14:58:28 发布