#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#
# Copyright (C) 2019 created by djliu
import os
import sys
import shutil
from hashlib import md5
if sys.getdefaultencoding() != 'utf-8':
reload(sys)
sys.setdefaultencoding('utf-8')
#遍歷當前目錄(子目錄)下所有文件
def getAllFile(path, fileList):
dirList = []
for ff in os.listdir(path):
wholepath = os.path.join(path, ff)
if os.path.isdir(wholepath):
dirList.append(wholepath)
if os.path.isfile(wholepath):
fileList.append(wholepath)
for dir in dirList:
getAllFile(dir, fileList)
#獲取文件md5
def get_file_md5(filename):
m = md5()
a_file = open(filename, 'rb') #需要使用二進制格式讀取文件內容
m.update(a_file.read())
a_file.close()
return m.hexdigest()
#獲取文件md5
def get_file_md5_1(filename):
if not os.path.isfile(filename):
return
myhash = md5()
f = open(filename,'rb')
while True:
b = f.read(65536)
if not b :
break
myhash.update(b)
f.close()
return myhash.hexdigest()
#統計路徑下各後綴文件數量大小
def printSufFilePath(fileList, suffixList):
for suffix in suffixList:
subList = []
fsize = 0
for ff in fileList[:]:
if ff.endswith(suffix):
subList.append(ff)
fsize += os.path.getsize(ff)
print suffix + ': number:' + str(len(subList)) + ', size:' + str(fsize/1024) + ' KB'
#統計含有2x,3x的png圖片數量大小
def printPngFile(fileList, size):
subList = []
suffix = ".png"
fsize = 0
for ff in fileList[:]:
if ff.endswith(suffix):
if size in ff:
subList.append(ff)
fsize += os.path.getsize(ff)
print size + suffix + ': number:' + str(len(subList)) + ', size:' + str(fsize/1024) + ' KB'
return subList
#統計png文件數量分佈區間(0,MidSize),[MidSize,upSize),[upSize,)
def printPngSize(fileList,upSize,MidSize):
print("所有png圖片一共:"+ str(len(fileList)))
subList = []
suffix = ".png"
fsize = 0
upCount = 0
midCount = 0
litCount = 0
for ff in fileList[:]:
if ff.endswith(suffix):
if (upSize*1024) <= os.path.getsize(ff):
upCount+=1
elif (MidSize*1024) <= os.path.getsize(ff):
midCount += 1
else:
litCount += 1
print("大於"+str(upSize)+"KB的圖片:"+str(upCount))
print("大於"+str(MidSize)+"KB的圖片:"+str(midCount))
print("小於"+str(MidSize)+"KB的圖片:"+str(litCount))
#拷貝png到jpgImage,並打印
def copyPrintPngFile(fileList):
subList = []
suffix = ".png"
fsize = 0
for ff in fileList[:]:
##print(ff)
if ff.endswith(suffix):
shutil.copyfile(ff,"./jpgImage/"+ff[ff.rfind('/')+1:])
print(ff[ff.rfind('/')+1:])
subList.append(ff[ff.rfind('/')+1:])
print 'png文件總數' + str(len(subList)) + ', 去重後:' + str(len(list(set(subList)) ))
#給圖片路徑集合判斷是否是共享業務
def isSharedBundle(list):
sharedBundle = ['amap_bundle_lib_sharetrip',
'amap_bundle_lib_freeride',
'amap_bundle_newfreeride',
'amap_bundle_taxifeedback',
'amap_bundle_lib_taxi',
'amap_bundle_taxibill',
'amap_bundle_taxi',
'amap_bundle_schoolbus',
'amap_bundle_resource'
];
for path in list[:]:
#當前path是否是該業務
currPathBusiness = False
for bundle in sharedBundle[:]:
if bundle in path:
currPathBusiness = True
break
if(currPathBusiness == False):
return False
return True
#給圖片路徑集合判斷是否是駕車/自主出行業務
def isDriveBundle(list):
driveBundle = ['amap_bundle_horus',
'amap_bundle_navi_end',
'amap_bundle_mock',
'amap_bundle_lib_trip_config',
'amap_bundle_test_quality',
'amap_bundle_lib_realbus',
'amap_bundle_realbus',
'amap_bundle_tripgroup',
'amap_bundle_motorbike',
'amap_bundle_drive_1',
'amap_bundle_lib_drivecommon',
'amap_bundle_busnavi',
'amap_bundle_buscard',
'amap_bundle_globalvoice',
'amap_bundle_ride',
'amap_bundle_foot',
'amap_bundle_routecommute',
'amap_bundle_air_ticket',
'amap_bundle_lib_userinsight',
'amap_bundle_resource'
];
for path in list[:]:
#當前path是否是該業務
currPathBusiness = False
for bundle in driveBundle[:]:
if bundle in path:
currPathBusiness = True
break
if(currPathBusiness == False):
return False
return True
#給圖片路徑集合判斷是否是信息服務業務
def isInfoBundle(list):
infoBundle = ['amap_bundle_community',
'amap_bundle_information_ecology',
'amap_bundle_travel',
'amap_bundle_walkman',
'amap_bundle_lib_aux_information',
'amap_bundle_comment',
'amap_bundle_cardui',
'amap_bundle_lib_eyrie',
'amap_bundle_idqmax',
'amap_bundle_landing_page',
'amap_bundle_search',
'amap_bundle_idqplus',
'amap_bundle_poi',
'amap_bundle_environment',
'amap_bundle_lib_information',
'amap_bundle_nearby',
'amap_bundle_scenic_area',
'amap_bundle_search_around',
'amap_bundle_resource']
for path in list[:]:
#當前path是否是該業務
currPathBusiness = False
for bundle in infoBundle[:]:
if bundle in path:
currPathBusiness = True
break
if(currPathBusiness == False):
return False
return True
#給圖片路徑集合判斷是否是動態UI支撐業務
def isdyUIBundle(list):
dyUIBundle = ['amap_bundle_lib_eaux',
'amap_bundle_ajx_rdtest',
'amap_bundle_test',
'amap_bundle_lib_app',
'amap_bundle_lib_1',
'amap_bundle_dynamic_ui',
'amap_bundle_lib_aux',
'amap_bundle_lib_map_engine',
'amap_bundle_resource']
for path in list[:]:
#當前path是否是該業務
currPathBusiness = False
for bundle in dyUIBundle[:]:
if bundle in path:
currPathBusiness = True
break
if(currPathBusiness == False):
return False
return True
#給圖片路徑集合判斷是否是QA業務
def isQABundle(list):
qaBundle = ['amap_bundle_service_test','amap_bundle_resource']
for path in list[:]:
#當前path是否是該業務
currPathBusiness = False
for bundle in qaBundle[:]:
if bundle in path:
currPathBusiness = True
break
if(currPathBusiness == False):
return False
return True
#給圖片路徑集合判斷是否是基礎地圖業務
def isBaseBundle(list):
baseBundle = ['amap_bundle_lib_recommended_service',
'amap_bundle_common_feedback',
'amap_bundle_basemap',
'amap_bundle_setting',
'amap_bundle_fre_loc',
'amap_bundle_offline',
'amap_bundle_locationselect',
'amap_bundle_carowner',
'amap_bundle_messagebox',
'amap_bundle_secureaide',
'amap_bundle_convoy',
'amap_bundle_basemap_feedback',
'amap_bundle_basemap_route',
'amap_bundle_agroup',
'amap_bundle_small_biz',
'amap_bundle_toolbox',
'amap_bundle_bus_feedback',
'amap_bundle_laboratory',
'amap_bundle_lib_featurecard',
'amap_bundle_quickservice',
'amap_bundle_trafficevent',
'amap_bundle_lib_basemap',
'amap_bundle_drive_achievement',
'amap_bundle_user_achievement',
'amap_bundle_contribution',
'amap_bundle_account',
'amap_bundle_feedback',
'amap_bundle_mine',
'amap_bundle_order_home',
'amap_bundle_share',
'amap_bundle_resource'
]
for path in list[:]:
#當前path是否是該業務
currPathBusiness = False
for bundle in baseBundle[:]:
if bundle in path:
currPathBusiness = True
break
if(currPathBusiness == False):
return False
return True
#給圖片路徑集合判斷是否是運營活動業務
def isActiveBundle(list):
activeBundle = ['amap_bundle_activity_earthquake_map',
'amap_bundle_activity_fun_trip',
'amap_bundle_activity_migration_map',
'amap_bundle_activity_fireworks',
'amap_bundle_activity_small_bridge',
'amap_bundle_resource']
for path in list[:]:
#當前path是否是該業務
currPathBusiness = False
for bundle in activeBundle[:]:
if bundle in path:
currPathBusiness = True
break
if(currPathBusiness == False):
return False
return True
#給圖片路徑集合判斷是否是資源resource業務
def isResBundle(list):
resBundle = ['amap_bundle_resource']
for path in list[:]:
#當前path是否是該業務
currPathBusiness = False
for bundle in resBundle[:]:
if bundle in path:
currPathBusiness = True
break
if(currPathBusiness == False):
return False
return True
#打印業務線重複圖片詳情
def printBundleDetail(list):
md5Curr =""
for path in list[:]:
md5Temp = get_file_md5(path)
if(md5Curr == md5Temp):
print(path)
else:
print '-------------------------------重複圖片(取md5):'+ md5Temp+",大小:"+str(os.path.getsize(path))+'字節,重複情況如下:'
print path
md5Curr = md5Temp
#統計project源碼中重複png圖片
def printProjectPngRepeatFile(fileList):
pathList = []
fixedList = set()
repeatList = set()
suffix = ".png"
totalSaveSize = 0;
sharedBundleSize = 0
activedBundleSize = 0
baseBundleSize = 0
driveBundleSize = 0
dyUIBundleSize = 0
infoBundleSize = 0
resBundleSize = 0
qaBundleSize = 0
beyondBundleSize = 0
beyondBundleList=[]
sharedBundleList = []
activeBundleList = []
baseBundleList = []
driveBundleList = []
dyUIBundleList = []
infoBundleList = []
resBundleList = []
qaBundleList = []
for ff in fileList[:]:
##print(ff)
if ff.endswith(suffix):
md5File = get_file_md5(ff)
if md5File in fixedList:
repeatList.add(md5File)
pathList.append(ff)
fixedList.add(md5File)
print("repeatList Size:"+str(len(set(repeatList))))
print("fixedList Size:"+str(len((set(fixedList)))))
print "pathList Size:"+ str(len(pathList))
for repeat in repeatList:
currentSize = 0
currentList = []
for path in pathList[:]:
if get_file_md5(path) == repeat:
currentList.append(path)
size = os.path.getsize(path)
print(path + " size = " + str(size)+"byte")
currentSize += size
print "-------------------------------圖片(取md5):"+repeat+",累計佔用空間:" + str(currentSize)+"字節,累計可節約空間:"+ str(currentSize-size)+"字節"
totalSaveSize+=(currentSize - size)
if(isSharedBundle(currentList)):
sharedBundleList += currentList
sharedBundleSize += currentSize
continue
if(isActiveBundle(currentList)):
activeBundleList += currentList
activedBundleSize += currentSize
continue
if(isDriveBundle(currentList)):
driveBundleList += currentList
driveBundleSize += currentSize
continue
if(isInfoBundle(currentList)):
infoBundleList += currentList
infoBundleSize += currentSize
continue
if(isBaseBundle(currentList)):
baseBundleList += currentList
baseBundleSize += currentSize
continue
if(isdyUIBundle(currentList)):
dyUIBundleList += currentList
dyUIBundleSize += currentSize
continue
if(isQABundle(currentList)):
qaBundleList += currentList
qaBundleSize += currentSize
continue
#都不是說明是誇業務
beyondBundleList += currentList
beyondBundleSize += currentSize
print "一,所有ajx圖片結論:"
print 'png文件總數' + str(len(pathList)) +"張"+ ', 去重後' + str(len(fixedList))+"張"+",所有png圖片重複"+ str(len(repeatList))+"張."
print "所有png圖片累計可節約:"+ ("%.3f" % (totalSaveSize*1.0/1024/1024))+"M"
print("\n")
print "二,各業務線重複圖片資源統計結論如下:"
print("【共享業務】"+"累計重複次數:"+str(len(sharedBundleList))+"次"+",累計重複圖片總大小:" + ("%.3f" % (sharedBundleSize*1.0/1024/1024))+"M")
print("【活動業務】"+"累計重複次數:"+str(len(activeBundleList))+"次"+",累計重複圖片總大小:" + ("%.3f" % (activedBundleSize*1.0/1024/1024))+"M")
print("【駕車業務】"+"累計重複次數:"+str(len(driveBundleList))+"次"+",累計重複圖片總大小:" + ("%.3f" % (driveBundleSize*1.0/1024/1024))+"M")
print("【信息業務】"+"累計重複次數:"+str(len(infoBundleList))+"次"+",累計重複圖片總大小:" + ("%.3f" % (infoBundleSize*1.0/1024/1024))+"M")
print("【基礎地圖業務】"+"累計重複次數:"+str(len(baseBundleList))+"次"+",累計重複圖片總大小:" + ("%.3f" % (baseBundleSize*1.0/1024/1024))+"M")
print("【動態ui業務】"+"累計重複次數:"+str(len(dyUIBundleList))+"次"+",累計重複圖片總大小:" + ("%.3f" % (dyUIBundleSize*1.0/1024/1024))+"M")
print("【QA業務】"+"累計重複次數:"+str(len(qaBundleList))+"次"+",累計重複圖片總大小:" + ("%.3f" % (qaBundleSize*1.0/1024/1024))+"M")
print("【跨業務】"+"累計重複次數:"+str(len(beyondBundleList))+"次"+",累計重複圖片總大小:" + ("%.3f" % (beyondBundleSize*1.0/1024/1024))+"M")
print("三,各業務線bundle重複圖片詳細數據如下:")
print("1.【共享業務】重複圖片詳細數據:")
printBundleDetail(sharedBundleList)
print("2.【活動業務】重複圖片詳細數據:")
printBundleDetail(activeBundleList)
print("3.【駕車業務】重複圖片詳細數據:")
printBundleDetail(driveBundleList)
print("4.【信息業務】重複圖片詳細數據:")
printBundleDetail(infoBundleList)
print("5.【基礎地圖業務】重複圖片詳細數據:")
printBundleDetail(baseBundleList)
print("6.【動態ui業務】重複圖片詳細數據:")
printBundleDetail(dyUIBundleList)
print("7.【QA業務】重複圖片詳細數據:")
printBundleDetail(qaBundleList)
print("8.【跨業務】重複圖片詳細數據:")
printBundleDetail(beyondBundleList)
if __name__ == '__main__':
flist = []
findpath = r'./App_Size'
getAllFile(findpath, flist)
print('allfile:', len(flist)) # filepath下的文件總數
typelist = ['.png','.jpg','.svg','.gif','.zip','.dat','.js','.xml','.css','.txt','.json','.config']
#printPngSize(flist,100,50)
#printSufFilePath(flist, typelist)
#list2x = printPngFile(flist,"@2x")
#list3x = printPngFile(flist,"@3x")
#copyPrintPngFile(flist)
printProjectPngRepeatFile(flist)
統計project中重複圖片
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.