統計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)




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章