实现细节都在代码里面, 帮助 -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 os
import sys
import 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_dict
def 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:])
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-12 05:37:53