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}")