统计ajx中重复图片

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

def get_file_md5(filename):
    m = md5()
    a_file = open(filename, 'rb') #需要使用二进制格式读取文件内容
    m.update(a_file.read())
    a_file.close()
    return m.hexdigest()

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'

    
    
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文件数量
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))







def printPngFile2(fileList):
 
    subList = []
    suffix = ".page"
    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 '.page文件总数' + str(len(subList)) + ', 去重后:' + str(len(list(set(subList)) )) 

#给图片路径判断是否是共享业务
def isSharedBundle(path):

    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'
    ];
    for bundle in sharedBundle[:]:
        if bundle in path:
            return 1
    return 0


#给图片路径判断是否是驾车/自主出行业务
def isDriveBundle(path):

    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'
                ];
    for bundle in driveBundle[:]:
        if bundle in path:
            return 1
    return 0

#给图片路径判断是否是信息服务业务
def isInfoBundle(path):

    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']
    for bundle in infoBundle[:]:
        if bundle in path:
            return 1
    return 0

#给图片路径判断是否是动态UI支撑业务
def isdyUIBundle(path):

    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']
    for bundle in dyUIBundle[:]:
        if bundle in path:
            return 1
    return 0

#给图片路径判断是否是QA业务
def isQABundle(path):

    qaBundle = ['amap_bundle_service_test']
    for bundle in qaBundle[:]:
        if bundle in path:
            return 1
    return 0

#给图片路径判断是否是基础地图业务
def isBaseBundle(path):

    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'
            ]
    for bundle in baseBundle[:]:
        if bundle in path:
            return 1
    return 0

#给图片路径判断是否是运营活动业务
def isActiveBundle(path):

    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']
    for bundle in activeBundle[:]:
        if bundle in path:
            return 1
    return 0

#给图片路径判断是否是资源resource业务
def isResBundle(path):

    resBundle = ['amap_bundle_resource']
    for bundle in resBundle[:]:
        if bundle in path:
            return 1
    return 0
                      
#统计ajx中重复图片
def printAjxPngRepeatFile(fileList):
    pathList = []
    subList = []
    repeatList = []
    suffix = ".png"
    fsize = 0
    totalSaveSize = 0;
    sharedBundleSize = 0
    activedBundleSize = 0
    baseBundleSize = 0
    driveBundleSize = 0
    dyUIBundleSize = 0
    infoBundleSize = 0
    resBundleSize = 0
    sharedBundleList = []
    activeBundleList = []
    baseBundleList = []
    driveBundleList = []
    dyUIBundleList = []
    infoBundleList = []
    resBundleList = []
    for ff in fileList[:]:
        ##print(ff)
        if ff.endswith(suffix):
            if ff[ff.rfind('/')+1:] in subList:
                #print(ff)
                repeatList.append(ff[ff.rfind('/')+1:])
            pathList.append(ff)
            subList.append(ff[ff.rfind('/')+1:])
   
    print(str(len(repeatList)))
    for repeat in repeatList[:]:
        currentRepeatSize = 0;
        for path in pathList[:]:
            if path.endswith(repeat):
                currentRepeatSize = os.path.getsize(path);
                print(path + "        size = " + str(currentRepeatSize)+"byte")
                fsize += currentRepeatSize
                if(isSharedBundle(path)):
                    sharedBundleSize+=currentRepeatSize
                    sharedBundleList.append(path)
                    continue;
                if(isActiveBundle(path)):
                    activedBundleSize+=currentRepeatSize
                    activeBundleList.append(path)
                    continue;
                if(isDriveBundle(path)):
                    driveBundleSize+=currentRepeatSize
                    driveBundleList.append(path)
                    continue;
                if(isInfoBundle(path)):
                    infoBundleSize+=currentRepeatSize
                    infoBundleList.append(path)
                    continue;
                if(isBaseBundle(path)):
                    baseBundleSize+=currentRepeatSize
                    baseBundleList.append(path)
                    continue;
                if(isdyUIBundle(path)):
                    dyUIBundleSize+=currentRepeatSize
                    dyUIBundleList.append(path)
                    continue;
                if(isResBundle(path)):#排除是resBundle里面图片
                    resBundleSize+=currentRepeatSize
                    resBundleList.append(path)
                    continue;
                
                
        print "-------------------------------图片:"+repeat+",累计占用空间:" + str(fsize)+"字节,累计可节约空间:"+ str(fsize-currentRepeatSize)+"字节"
        totalSaveSize+=(fsize - currentRepeatSize);
        fsize = 0

    
    print "pathList Size:"+ str(len(pathList))
    print "一,所有ajx图片结论:"
    print 'png文件总数' + str(len(subList)) +"张"+ ', 去重后' + str(len(list(set(subList ))))+"张"+",所有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("【资源res】"+"累计重复次数:"+str(len(resBundleList))+"次"+",累计重复图片总大小:" + ("%.3f" % (resBundleSize*1.0/1024/1024))+"M")
    
    print("三,各业务线bundle重复图片详细数据如下:")
    print("1.【共享业务】重复图片详细数据:")
    for bundle in sharedBundleList[:]:
        print(bundle)
    print("2.【活动业务】重复图片详细数据:")
    for bundle in activeBundleList[:]:
        print(bundle)
    print("3.【驾车业务】重复图片详细数据:")
    for bundle in driveBundleList[:]:
        print(bundle)
    print("4.【信息业务】重复图片详细数据:")
    for bundle in infoBundleList[:]:
        print(bundle)
    print("5.【基础地图业务】重复图片详细数据:")
    for bundle in baseBundleList[:]:
        print(bundle)
    print("6.【动态ui业务】重复图片详细数据:")
    for bundle in dyUIBundleList[:]:
        print(bundle)
    print("7.【资源res】重复图片详细数据:")
    for bundle in resBundleList[:]:
        print(bundle)

#统计各业务线.page文件个数
def printSuffixFile(fileList):
    pathList = []
    md5List = []
    repeatList = []
    suffix = ".page"
    fsize = 0
    totalSaveSize = 0;
    sharedBundleSize = 0
    activedBundleSize = 0
    baseBundleSize = 0
    driveBundleSize = 0
    dyUIBundleSize = 0
    infoBundleSize = 0
    resBundleSize = 0
    sharedBundleList = []
    activeBundleList = []
    baseBundleList = []
    driveBundleList = []
    dyUIBundleList = []
    infoBundleList = []
    resBundleList = []
    qaBundleList = []
    for ff in fileList[:]:
        ##print(ff)
        if ff.endswith(suffix):
            pathList.append(ff)
   
    for path in pathList[:]:
        if(isSharedBundle(path)):
            sharedBundleList.append(path)
            continue;
        if(isActiveBundle(path)):
            activeBundleList.append(path)
            continue;
        if(isDriveBundle(path)):
            driveBundleList.append(path)
            continue;
        if(isInfoBundle(path)):
            infoBundleList.append(path)
            continue;
        if(isBaseBundle(path)):
            baseBundleList.append(path)
            continue;
        if(isdyUIBundle(path)):
            dyUIBundleList.append(path)
            continue;
        if(isResBundle(path)):#排除是resBundle里面图片
            resBundleList.append(path)
            continue;
        if(isQABundle(path)):#排除是resBundle里面图片
            qaBundleList.append(path)
            continue;
 
    print "所有业务线.page文件累计:"+ (str(len(pathList)))+"个"
    print("\n")
    print "二,各业务线重复图片资源统计结论如下:"
    print("【共享业务】"+".page文件一共"+str(len(sharedBundleList))+"个")
    print("【活动业务】"+".page文件一共"+str(len(activeBundleList))+"个")
    print("【驾车业务】"+".page文件一共"+str(len(driveBundleList))+"个")
    print("【信息业务】"+".page文件一共"+str(len(infoBundleList))+"个")
    print("【基础地图业务】"+".page文件一共"+str(len(baseBundleList))+"个")
    print("【动态ui业务】"+".page文件一共"+str(len(dyUIBundleList))+"个")
    print("【资源res】"+".page文件一共"+str(len(resBundleList))+"个")
    print("【QA业务】"+".page文件一共"+str(len(qaBundleList))+"个")
    
    print("三,各业务线.page文件详细数据如下:")
    print("1.【共享业务】.page文件详细数据:")
    for bundle in sharedBundleList[:]:
        print(bundle)
    print("2.【活动业务】.page文件详细数据:")
    for bundle in activeBundleList[:]:
        print(bundle)
    print("3.【驾车业务】.page文件详细数据:")
    for bundle in driveBundleList[:]:
        print(bundle)
    print("4.【信息业务】.page文件详细数据:")
    for bundle in infoBundleList[:]:
        print(bundle)
    print("5.【基础地图业务】.page文件详细数据:")
    for bundle in baseBundleList[:]:
        print(bundle)
    print("6.【动态ui业务】.page文件详细数据:")
    for bundle in dyUIBundleList[:]:
        print(bundle)
    print("7.【资源res】.page文件详细数据:")
    for bundle in resBundleList[:]:
        print(bundle)
    print("8.【QA业务】.page文件详细数据:")
    for bundle in qaBundleList[:]:
        print(bundle)
    


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")

    printPngFile2(flist)
    #printAjxPngRepeatFile(flist)
    printSuffixFile(flist)




你可能感兴趣的:(python)