一起来写一个渠道信息验证脚本:
一、分析需求:
1、验证的渠道包的数量
2、验证的信息位置及获取方案
3、结果的输出和展示
二、具体需求
1、根据实际事情,要验证1-n个渠道包
2、渠道信息在资源文件夹中,采用appt解析渠道包
3、结果在cmd中输出并保存到csv文件中,在展示上需要有汇总信息,错误和正确的个数、错误具体内容,在csv中将错误的数据放到最上面
附上代码,大家自己看下。
# -*- coding: utf-8-*-
importos
importcsv
importplatformfromsubprocess
importPopen,PIPE
importsys
passPackage = []
failPackage = []
total = []
count = []
def main(SRC_DIR):
list = os.listdir(SRC_DIR)
filelist = []
website = []
foriinrange(,len(list)):
path = os.path.join(SRC_DIR,list[i])
ifos.path.isfile(path) and path.find('filesubstring') != -1and (os.path.isdir(path)) != True:
filelist.append(path)
elif os.path.isfile(path) and path.find('.apk') != -1and (os.path.isdir(path)) != True:
website.append(path)
getApkPackageChannelInfo(SRC_DIR,filelist,'channel')
getApkPackageChannelInfo(SRC_DIR,website,'website')
print printdecodemethod('验证成功的渠道包: '),passPackage.__len__()
print printdecodemethod('验证失败的渠道包: '),failPackage.__len__(),failPackage
print printdecodemethod('渠道包总数量: '),count[count.__len__()-1]+count[]
total = failPackage+passPackage
writecsv(passPackage.__len__(),failPackage.__len__(),count[count.__len__()-1]+count[],total)
def getApkPackageChannelInfo(SRC_DIR,list,apkorwebsite):
packageinfo =""
ifapkorwebsite =="channel":
count.append(list.__len__())
os.chdir(SRC_DIR)
foriinrange(,len(list)):
try:
packageinfo = list[i].split("__")[1]
print i+1
apkinfo = getChannelInfo(list[i],packageinfo)
ifapkinfo[3]=='pass':
passPackage.append(apkinfo)
else:
failPackage.append(apkinfo)
except Exception:
print'please check package name1111!!!',list[i],list.__len__(),
elif apkorwebsite =="website":
count.append(list.__len__())
forjinrange(,len(list)):
try:
[dirname,packagename] = os.path.split(list[j])
ifpackagename !='xxx.apk':
packageinfo=list[j].split("xxx_")[1].split(".")[]
packageinfo="xxx_"+packageinfo
print count[]+j+1
else:
packageinfo ="website"
print count[]+j+1
except Exception:
print"please check package name!"
websiteinfo = getChannelInfo(list[j],packageinfo)
ifwebsiteinfo[3]=='pass':
passPackage.append(websiteinfo)
else:
failPackage.append(websiteinfo)
def getChannelInfo(apkpath,packageinfo):
ifplatform.system() =="Windows":
command ='aapt l '+ apkpath.replace("\\","\\\\") +' | findstr "^META-INF/.*@filename$" '
else:
command ='aapt l '+ apkpath +' | grep ^META-INF/.*@filename.cn$'
#linux or unix等可使用获取完整字符串
#command ='aapt l '+ apkpath +' | grep ^META-INF/.*@filename$ | cut -b 10-'+" | awk -F '@' '' "
try:
temp = Popen(command,shell=True,stdout=PIPE)
channelinfoinapk = temp.communicate()[].split("\r\n")[]
channelinfoinapk = channelinfoinapk.split("META-INF/")[1].split("@xxx.cn")[]
except Exception as err:
print"please check,add adb and aapt to environment path!!!"
packagename=apkpath.split("/")[-1]
ifchannelinfoinapk == packageinfo:
result ='pass'
else:
print"***********check fail !!!**************"
result ='fail'
print'ApkName: ',packagename
print'channelinfoinapk: ',channelinfoinapk
print'ChannelPackageinfo:',packageinfo
print'ChannelInfoCheckResult: ',result
print'\t'
apkinfo = [packagename,channelinfoinapk,packageinfo,result]
returnapkinfo
def writecsv(checkpass,checkfail,total,data):
file2 ='./channelcheckresult.csv'
withopen(file2,'w') as f:
writer = csv.writer(f)
writer.writerow([decodemethod('渠道包验证通过数量','gbk'), checkpass])
writer.writerow([decodemethod('渠道包验证失败数量','gbk'), checkfail])
writer.writerow([decodemethod('渠道包总数量','gbk'), total])
writer.writerow(['ApkName','ChannelInfoInApk','PackageInfo','CheckResult'])
forrowindata:
writer.writerow(row)
def decodemethod(str,encodestr):
encodestr = str.decode("utf-8").encode(encodestr)
returnencodestr
def printdecodemethod(str):
type = sys.getfilesystemencoding()
str = decodemethod(str,type)
returnstr
if__name__ =='__main__':
from sysimportargv
filepath = argv[1]
main(filepath)
ifplatform.system() =="Windows":
print printdecodemethod('生成的结果文件: '),filepath+"\channelcheckresult.csv"
else:
print printdecodemethod('生成的结果文件: ') ,filepath+"/channelcheckresult.csv"
欢迎关注“互联网红柚”。
更多资料分享,技术讨论,一群大咖等着你!
我们的微信群:请扫二维码后,邀请进群哦。
or
领取专属 10元无门槛券
私享最新 技术干货