def check_use_layout_res(self): """根据检查出的 java 文件检查使用的布局资源""" java_files = filex.read_lines(self.rv_java_files_path, ignore_line_separator=True) pattern = re.compile(r'(?<!setContentView\()R\.layout\.(.*?)[;),]') name_pattern = re.compile(r'^(?!fragment|dialog|high|pop|layout|address)') xml_name_list = [] for file in java_files: content = filex.read(file) all_match = re.findall(pattern, content) if all_match: print('%s 找到布局使用' % file) for match in all_match: if re.search(name_pattern, match): print(match) if 'item' not in match: print('不包含 item') if match not in xml_name_list: xml_name_list.append(match) else: print('过滤', match) print('共使用了 %d 个文件' % len(xml_name_list)) print('查找对应的 xml 文件') files = filex.list_file(self.work_space, 'xml$') xml_file_list = [] for xml_name in xml_name_list: for file in files: name = os.path.splitext(os.path.split(file)[1])[0] if xml_name == name: xml_file_list.append(file) break print('共找到 %d 个文件' % len(xml_file_list)) filex.write_lines(self.item_xml_files_path, xml_file_list, add_line_separator=True)
def check_java_file(java_file, rv_xml_files): content = filex.read(java_file) for file in rv_xml_files: layout = 'R.layout.' + file if layout in content: print('%s 使用了资源文件 %s' % (java_file, file)) return True return False
def read_cookies_and_token_from_file(self): """从保存的文件读取""" cookies = netx.parse_cookies_from_file(self.cookies_file) if not cookies: print('读取 cookie 失败', self.cookies_file) token = filex.read(self.token_file) if not token: print('读取 token 失败', self.token_file) return cookies, token
def check_adapter_files(self): """检查 adapter """ java_file_list = filex.list_file(self.work_space, 'java$') print('共有 %d 个 java 文件' % len(java_file_list)) adapter_file_list = [] for java_file in java_file_list: name = 'RecyclerView.Adapter' if 'extends ' + name in filex.read(java_file): print('%s 继承了 %s' % (java_file, name)) adapter_file_list.append(java_file) self.check_subclass(java_file_list, adapter_file_list) return adapter_file_list
def check_rv_xml_files(self): files = filex.list_file(self.work_space, 'xml$') length = len(files) print('共有文件 %d 个' % length) rv_xml_files = [] for i in range(length): file = files[i] print('%d/%d %s' % (i + 1, length, file)) # 检查文件中是否包含 recycler view if 'RecyclerView' in filex.read(file): rv_xml_files.append(file) print('共有 %d 个文件包含 recycler view' % len(rv_xml_files)) filex.write_lines(self.rv_xml_files_path, rv_xml_files, add_line_separator=True)
def check_subclass(self, all_file_list, result_file_list): """检查子类""" children_file_list = [] for file in all_file_list: if file not in result_file_list: content = filex.read(file) # 这里可以优化为,只检查新添加的 children 中的文件,而不需要全部再检查 for result_file in result_file_list: name = os.path.splitext(os.path.split(result_file)[1])[0] if 'extends ' + name in content: print('%s 继承了 %s' % (file, name)) children_file_list.append(file) break print('共找到 %d 个子类' % (len(children_file_list))) if len(children_file_list) > 0: # 找到子类,继续 result_file_list.extend(children_file_list) print('当前大小 %d' % len(result_file_list)) self.check_subclass(all_file_list, result_file_list)