统计project中重复图片

#!/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)




你可能感兴趣的:(python)