查找iOS工程中未使用到方法脚本 FindSelectorsUnrefs.py

2024-05-15 10:18

本文主要是介绍查找iOS工程中未使用到方法脚本 FindSelectorsUnrefs.py,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

# coding:utf-8import os
import re
import sys
import getoptreserved_prefixs = ["-[", "+["]# 获取入参参数
def input_parameter():opts, args = getopt.getopt(sys.argv[1:], '-a:-p:-w:-b:',['app_path=', 'project_path=', 'black_list_Str', 'white_list_str'])black_list_str = ''white_list_str = ''white_list = []black_list = []# 入参判断for opt_name, opt_value in opts:if opt_name in ('-a', '--app_path'):# .app文件路径app_path = opt_valueif opt_name in ('-p', '--project_path'):# 项目文件路径project_path = opt_valueif opt_name in ('-b', '--black_list_Str'):# 检测黑名单前缀,不检测谁black_list_Str = opt_valueif opt_name in ('-w', '--white_list_str'):# 检测白名单前缀,只检测谁white_list_str = opt_valueif len(black_list_str) > 0:black_list = black_list_str.split(",")if len(white_list_str) > 0:white_list = white_list_str.split(",")if len(white_list) > 0 and len(black_list) > 0:print("\033[0;31;40m白名单【-w】和黑名单【-b】不能同时存在\033[0m")exit(1)# 判断文件路径存不存在if not os.path.exists(project_path):print("\033[0;31;40m输入的项目文件路径【-p】不存在\033[0m")exit(1)app_path = verified_app_path(app_path)if not app_path:exit('输入的app路径不存在,停止运行')return app_path, project_path, black_list, white_listdef verified_app_path(path):if path.endswith('.app'):appname = path.split('/')[-1].split('.')[0]path = os.path.join(path, appname)if appname.endswith('-iPad'):path = path.replace(appname, appname[:-5])if not os.path.isfile(path):return Noneif not os.popen('file -b ' + path).read().startswith('Mach-O'):return Nonereturn path# 获取protocol中所有的方法
def header_protocol_selectors(file_path):# 删除路径前后的空格file_path = file_path.strip()if not os.path.isfile(file_path):return Noneprotocol_sels = set()file = open(file_path, 'r', encoding='utf-8',errors='ignore')is_protocol_area = False# 开始遍历文件内容for line in file.readlines():# 删除注释信息# delete descriptionline = re.sub('\".*\"', '', line)# delete annotationline = re.sub('//.*', '', line)# 检测是否是 @protocol# match @protocolif re.compile('\s*@protocol\s*\w+').findall(line):is_protocol_area = True# match @endif re.compile('\s*@end').findall(line):is_protocol_area = False# match selif is_protocol_area and re.compile('\s*[-|+]\s*\(').findall(line):sel_content_match_result = None# - (CGPoint)convertPoint:(CGPoint)point toCoordinateSpace:(id <UICoordinateSpace>)coordinateSpaceif ':' in line:# match sel with parameters# 【"convertPoint:","toCoordinateSpace:"]sel_content_match_result = re.compile('\w+\s*:').findall(line)else:# - (void)invalidate;# match sel without parameters# invalidate;sel_content_match_result = re.compile('\w+\s*;').findall(line)if sel_content_match_result:# 方法参数拼接# convertPoint:toCoordinateSpace:funcList = ''.join(sel_content_match_result).replace(';', '')protocol_sels.add(funcList)file.close()return protocol_sels# 获取所有protocol定义的方法
def protocol_selectors(path, project_path):print('获取所有的protocol中的方法...')header_files = set()protocol_sels = set()# 获取当前引用的系统库中的方法列表system_base_dir = '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk'# get system librareislines = os.popen('otool -L ' + path).readlines()for line in lines:# 去除首尾空格line = line.strip()# /System/Library/Frameworks/MediaPlayer.framework/MediaPlayer (compatibility version 1.0.0, current version 1.0.0)# /System/Library/Frameworks/MediaPlayer.framework/MediaPlayer# delete description,line = re.sub('\(.*\)', '', line).strip()if line.startswith('/System/Library/'):# [0:-1],获取数组的左起第一个,到倒数最后一个,不包含最后一个,[1,-1)左闭右开library_dir = system_base_dir + '/'.join(line.split('/')[0:-1])if os.path.isdir(library_dir):# 获取当前系统架构中所有的类# 获取合集header_files = header_files.union(os.popen('find %s -name \"*.h\"' % library_dir).readlines())if not os.path.isdir(project_path):exit('Error: project path error')# 获取当前路径下面所有的.h文件路径header_files = header_files.union(os.popen('find %s -name \"*.h\"' % project_path).readlines())for header_path in header_files:# 获取所有查找到的文件下面的protocol方法,这些方法,不能用来统计header_protocol_sels = header_protocol_selectors(header_path)if header_protocol_sels:protocol_sels = protocol_sels.union(header_protocol_sels)return protocol_selsdef imp_selectors(path):print('获取所有的方法,除了setter and getter方法...')# return struct: {'setupHeaderShadowView':['-[TTBaseViewController setupHeaderShadowView]']}#  imp     0x100001260 -[AppDelegate setWindow:] ==>> -[AppDelegate setWindow:],setWindow:re_sel_imp = re.compile('\s*imp\s*0x\w+ ([+|-]\[.+\s(.+)\])')re_properties_start = re.compile('\s*baseProperties 0x\w{9}')re_properties_end = re.compile('\w{16} 0x\w{9} _OBJC_CLASS_\$_(.+)')re_property = re.compile('\s*name\s*0x\w+ (.+)')imp_sels = {}is_properties_area = False# “otool - ov”将输出Objective - C类结构及其定义的方法。for line in os.popen('/usr/bin/otool -oV %s' % path).readlines():results = re_sel_imp.findall(line)if results:#  imp     0x100001260 -[AppDelegate setWindow:] ==>> [-[AppDelegate setWindow:],setWindow:](class_sel, sel) = results[0]if sel in imp_sels:imp_sels[sel].add(class_sel)else:imp_sels[sel] = set([class_sel])else:# delete setter and getter methods as ivar assignment will not trigger them# 删除相关的set方法if re_properties_start.findall(line):is_properties_area = Trueif re_properties_end.findall(line):is_properties_area = Falseif is_properties_area:property_result = re_property.findall(line)if property_result:property_name = property_result[0]if property_name and property_name in imp_sels:# properties layout in mach-o is after func impimp_sels.pop(property_name)# 拼接set方法setter = 'set' + property_name[0].upper() + property_name[1:] + ':'# 干掉set方法if setter in imp_sels:imp_sels.pop(setter)return imp_selsdef ref_selectors(path):print('获取所有被调用的方法...')re_selrefs = re.compile('__TEXT:__objc_methname:(.+)')ref_sels = set()lines = os.popen('/usr/bin/otool -v -s __DATA __objc_selrefs %s' % path).readlines()for line in lines:results = re_selrefs.findall(line)if results:ref_sels.add(results[0])return ref_selsdef ignore_selectors(sel):if sel == '.cxx_destruct':return Trueif sel == 'load':return Truereturn Falsedef filter_selectors(sels):filter_sels = set()for sel in sels:for prefix in reserved_prefixs:if sel.startswith(prefix):filter_sels.add(sel)return filter_selsdef unref_selectors(path, project_path):# 获取所有类的protocol的方法集合protocol_sels = protocol_selectors(path, project_path)# 获取项目所有的引用方法ref_sels = ref_selectors(path)if len(ref_sels) == 0:exit('获取项目所有的引用方法为空....')# 获取所有的方法,除了set方法imp_sels = imp_selectors(path)print("\n")if len(imp_sels) == 0:exit('Error: imp selectors count null')unref_sels = set()for sel in imp_sels:# 所有的方法,忽略白名单if ignore_selectors(sel):continue# 如果当前的方法不在protocol中,也不再引用的方法中,那么认为这个方法没有被用到# protocol sels will not apppear in selrefs sectionif sel not in ref_sels and sel not in protocol_sels:unref_sels = unref_sels.union(filter_selectors(imp_sels[sel]))return unref_sels# 黑白名单过滤
def filtration_list(unref_sels, black_list, white_list):# 黑名单过滤temp_unref_sels = list(unref_sels)if len(black_list) > 0:# 如果黑名单存在,那么将在黑名单中的前缀都过滤掉for unref_sel in temp_unref_sels:for black_prefix in black_list:class_method = "+[%s" % black_prefixinstance_method = "-[%s" % black_prefixif (unref_sel.startswith(class_method) or unref_sel.startswith(instance_method)) and unref_sel in unref_sels:unref_sels.remove(unref_sel)break# 白名单过滤temp_array = []if len(white_list) > 0:# 如果白名单存在,只留下白名单中的部分for unref_sel in unref_sels:for white_prefix in white_list:class_method = "+[%s" % white_prefixinstance_method = "-[%s" % white_prefixif unref_sel.startswith(class_method) or unref_sel.startswith(instance_method):temp_array.append(unref_sel)breakunref_sels = temp_arrayreturn unref_sels# 整理结果,写入文件
def write_to_file(unref_sels):file_name = 'selector_unrefs.txt'f = open(os.path.join(sys.path[0].strip(), file_name), 'w')unref_sels_num_str = '查找到未被使用的方法: %d个\n' % len(unref_sels)print(unref_sels_num_str)f.write(unref_sels_num_str)num = 1for unref_sel in unref_sels:unref_sels_str = '%d : %s' % (num, unref_sel)print(unref_sels_str)f.write(unref_sels_str + '\n')num = num + 1f.close()print('\n项目中未使用方法检测完毕,相关结果存储到当前目录 %s 中' % file_name)print('请在项目中进行二次确认后处理')if __name__ == '__main__':# 获取入参app_path, project_path, black_list, white_list = input_parameter()# 获取未使用方法unref_sels = unref_selectors(app_path, project_path)# 黑白名单过滤unref_sels = filtration_list(unref_sels, black_list, white_list)# 打印写入文件write_to_file(unref_sels)

网上找了几个脚本都有问题,以上是自己修改后测试可以使用的。

使用示例:

python3 FindSelectorsUnrefs.py -a /Users/XXXXXX/Library/Developer/Xcode/DerivedData/XWorld-bhzlzkolmmftbubftspqdqfxqzpo/Build/Products/Debug-iphoneos/XXX.app -p /Users/XXXXX/Documents/GitHub/XXXXXX/XXXXX

运行成功后会在当前目录下生成 selector_unrefs.txt文件 里面是工程中未使用到的方法

先cd到脚本路路径

1. python3 表示使用的python版本

2. FindSelectorsUnrefs.py 表示要执行的文件

3. -a XXXXXXXX 代表需要分析的工程文件中生成的.app路径 在Products的xxxx.app里 Show In Finder可以查看

4. -p 表示工程文件所在的路径

其他扩展参数说明:-w 结果白名单处理,检测结果,只想要以什么开头的类的方法,多个用逗号隔开,比如JD,BD,AL

-b 结果黑名单处理,检测结果,不想要以什么开头的类的方法,多个用逗号隔开,比如Pod,AF,SD

-w 和 -b 不能共存,共存会报错

这篇关于查找iOS工程中未使用到方法脚本 FindSelectorsUnrefs.py的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/991549

相关文章

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

Oracle数据库定时备份脚本方式(Linux)

《Oracle数据库定时备份脚本方式(Linux)》文章介绍Oracle数据库自动备份方案,包含主机备份传输与备机解压导入流程,强调需提前全量删除原库数据避免报错,并需配置无密传输、定时任务及验证脚本... 目录说明主机脚本备机上自动导库脚本整个自动备份oracle数据库的过程(建议全程用root用户)总结

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali