From 03ff2ed4df1035f2d6cc899e799be9528321800d Mon Sep 17 00:00:00 2001 From: andy <1414772332@qq.com> Date: Mon, 20 Jan 2025 19:41:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9F=A5=E6=89=BE=E6=9C=AA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=A4=B4=E6=96=87=E4=BB=B6=E7=9A=84=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- find_headfile.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 find_headfile.py diff --git a/find_headfile.py b/find_headfile.py new file mode 100644 index 0000000..d94c272 --- /dev/null +++ b/find_headfile.py @@ -0,0 +1,65 @@ +import os +import sys +import shutil + + +# 找出未调用的 .h 文件 + +# 定义 .h 文件的目录 +_INC=[ + "inc/hw/reg/riscv3/2/soc/macro", + "driver/src/hw3/inc" + ] + +# 定义调用目录 +_CALL=[ + "driver/inc", + "driver/src/hal" +] + +# 找到指定后缀的文件 +def find_type(path:str,fix:str): + dlist=os.listdir(path) + file_list=[] + for i in dlist: + ps=os.path.join(path, i) + if os.path.isdir(ps): + file_list+=find_type(ps,fix) + pass + else: + if(ps[-len(fix):]==fix): + file_list.append(ps) + return file_list + + +# 如果文件中存在列表中任意字符串则返回true +def check_list_in_file(file:str,str_list:list): + ret=[] + with open(file,mode='r',encoding='utf-8') as f: + lines=f.readlines() + for line in lines: + for item in str_list: + ps=os.path.split(item)[-1] + if(line.find(ps)>=0): + ret.append(item) + return ret + + + +if __name__ == "__main__": + head_list=[] + for item in _INC: + head_list+=find_type(item,".h") + call_list=[] + for item in _CALL: + call_list+=find_type(item,".h") + call_list+=find_type(item,".c") + find_list=[] + for item in call_list: + ret=check_list_in_file(item,head_list) + if(len(ret)>0): + print(f"{item} called {ret}") + find_list+=ret + for item in head_list: + if item not in find_list: + print(f"unused file:{item}")