IOS,ios8
實現細節都在代碼裡面, 協助 -h.
# -*- coding: utf-8 -*-"""檢查IOS應用圖片是否使用1. 讀取有效檔案: 圖片(.png, .jpg), 檔案(.h, .m, .xib, .c, .cpp, .json);2. 在檔案中搜尋圖片, 去掉尾碼符和@2x, 適應字串拼接, 捨棄最後'_''-'之後的部分;注意: 會遺漏字串拼接的情況, 如a_b_%zi_c_d, 需要檢查之後再刪除.時間複雜度O(n^2)-find, 每個檔案尋找每個圖片;"""__author__ = 'C.L.Wang'import osimport sysimport getopt# 列出檔案夾內所有圖片def list_dictionary_images(root_dir): names_list = [] paths_list = [] for parent, dirNames, fileNames in os.walk(root_dir): for name in fileNames: ext = ['.png', '.jpg'] if name.endswith(tuple(ext)): names_list.append(name) paths_list.append(os.path.join(parent, name)) return [paths_list, names_list]# 列出檔案夾內所有代碼def list_dictionary_codes(root_dir): paths_list = [] for parent, dirNames, fileNames in os.walk(root_dir): for name in fileNames: ext = ['.h', '.m', '.xib', '.json', '.c', '.cpp', '.mm', '.md'] if name.endswith(tuple(ext)): paths_list.append(os.path.join(parent, name)) return paths_list# 在檔案中尋找名稱def search_name_in_file(search_name, file_name): # 拼接字串特例 short_name_1 = search_name[::-1].split('_', 1) short_name_2 = search_name[::-1].split('-', 1) if len(short_name_1) == 2: search_name = short_name_1[1][::-1] elif len(short_name_2) == 2: search_name = short_name_2[1][::-1] # 引用映像特例 search_name = '"' + search_name file_object = open(file_name) try: all_the_text = file_object.read() if all_the_text.find(search_name) > 0: return True else: return False finally: file_object.close()# 正常化名稱def normalize_name(raw_name): raw_name = raw_name.replace('@2x', '') raw_name = raw_name.replace('.png', '') raw_name = raw_name.replace('.jpg', '') return raw_name# 列出檔案中未出現的名稱def list_unused_names(names_dict, file_name): for name, path in names_dict.items(): if search_name_in_file(normalize_name(name), file_name): del names_dict[name] return names_dict# 尋找未使用的圖片def search_unused_images(image_folder, file_folder): [image_paths, image_names] = list_dictionary_images(image_folder) file_paths = list_dictionary_codes(file_folder) images_dict = dict(zip(image_names, image_paths)) for file_path in file_paths: images_dict = list_unused_names(images_dict, file_path) return images_dictdef main(argv): image_dir = '' # 圖片檔案夾 code_dir = '' # 代碼檔案夾 out_put = '' # 輸出檔案 result = list() try: opts, args = getopt.getopt(argv, "hi:c:o:", ["image_dir=", "code_dir=", "out_put="]) except getopt.GetoptError: print 'SearchUnusedImages.py -i <image_dir> -c <code_dir> -o <out_put>' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'SearchUnusedImages.py -i <image_dir> -c <code_dir> -o <out_put>' sys.exit() elif opt in ("-i", "--image_dir"): image_dir = arg elif opt in ("-c", "--code_dir"): code_dir = arg elif opt in ("-o", "--out_put"): out_put = arg out_file = open(out_put, 'w') print 'scan start.' for name, path in search_unused_images(image_dir, code_dir).items(): result.append(path) for sort_path in sorted(result): print >> out_file, sort_path print 'scan over.' out_file.close()if __name__ == "__main__": main(sys.argv[1:])
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。