def dirCompare(old_path, new_path, new_dict, removed_dict, changed_dict): afiles = [] bfiles = [] for root, dirs, files in os.walk(old_path): for f in files: afiles.append(root + os.sep + f) for root, dirs, files in os.walk(new_path): for f in files: bfiles.append(root + os.sep + f) apathlen = len(old_path) aafiles = [] for f in afiles: aafiles.append(f[apathlen:]) # 去掉afiles中文件名的apath bpathlen = len(new_path) bbfiles = [] for f in bfiles: bbfiles.append(f[bpathlen:]) afiles = aafiles bfiles = bbfiles setA = set(afiles) setB = set(bfiles) # 处理共有文件 commonfiles = setA & setB for of in sorted(commonfiles): size_changed = utils.get_path_size(str(new_path + of)) - utils.get_path_size(str(old_path + of)) if size_changed != 0: changed_dict[of] = size_changed # 处理仅出现在一个目录中的文件 onlyFiles = setA ^ setB aonlyFiles = [] bonlyFiles = [] for of in onlyFiles: if of in afiles: aonlyFiles.append(of) elif of in bfiles: bonlyFiles.append(of) # print "#only in ", old_path for of in sorted(aonlyFiles): removed_dict[of] = utils.get_path_size(str(old_path + of)) # print "#only in ", new_path for of in sorted(bonlyFiles): # print of + " size:" + get_size_in_nice_string(get_path_size(str(new_path + of))) new_dict[of] = utils.get_path_size(str(new_path + of))
def walk_dict(parent, jdict, size_dict=None, method_dict=None, root_name=None): for (d, x) in jdict.items(): path = os.path.join(parent, d) size = utils.get_path_size(path) key = d if root_name is not None: key = utils.get_folder_name(path, root_name) # print("key:" + key) if size_dict is not None and isinstance(size_dict, dict): size_dict[key] = size count = None if method_dict is not None: count = get_method_counts_in_file(path) if count is not None: # print("d:" + d + " count:" + count) method_dict[key] = count method_count = "method:" if count is None: method_count = "" else: method_count += str(count) print("path:%-30s | size: %-12s | %-17s" % ( key, utils.get_size_in_nice_string(size), method_count)) if isinstance(x, dict): walk_dict(path, x, size_dict, method_dict, root_name) else: pass
def get_apk_data(apk_single): # 获取没有拓展名的文件名 apk_dir = os.path.split(apk_single)[-1].split(".")[0] apk_size = utils.get_path_size(apk_single) print("apk_single:%s size: %s" % (apk_dir, utils.get_size_in_nice_string(apk_size))) # 解压文件夹以便分析 print("start to unzip apk single") utils.surely_rmdir(apk_dir) utils.unzip_dir(apk_single, apk_dir) print("unzip apk info:") check_unzipped_apk(apk_single, apk_dir) json_object = get_json_from_file("apk_tree"); data_string = json.dumps(json_object) jdict = json.loads(data_string) # 键值对保存要查文件的大小,用于后面对比 apk_obj = dict(); method_dict = dict(); print("") print("") # 输出其中指定文件的大小 print("============%s==============" % apk_dir) walk_dict(apk_dir, jdict, apk_obj, method_dict, apk_dir) print("============%s==============" % apk_dir) # 清除临时解压的apk文件夹 utils.surely_rmdir(apk_dir)
def check_image_limit(apk, apk_dir, limit=40000): count = 0 for parent, dir_names, filenames in os.walk(apk_dir): for filename in filenames: # 获取文件的绝对路径 path = os.path.join(parent, filename) # 过滤文件类型 if not os.path.splitext(filename)[1] in LIMIT_IMAGE_FORMAT: continue # 判断文件存在 if not filename: continue file_size = utils.get_path_size(path) if long(file_size) > long(limit): image_path = utils.get_folder_name(parent, apk_dir) + os.sep + filename print('IMAGE:%s size:%s' % (image_path, utils.get_size_in_nice_string(file_size))) count += 1 if count > 0: print("These files may be too large.(larger than %s)" % utils.get_size_in_nice_string(int(limit)))
def compare_apk(apk_old, apk_new, top=None): # 获取没有拓展名的文件名 old_apk_dir = os.path.split(apk_old)[-1].split(".")[0] new_apk_dir = os.path.split(apk_new)[-1].split(".")[0] print("old_apk_dir:" + old_apk_dir) print("new_apk_dir:" + new_apk_dir) old_size = utils.get_path_size(apk_old) new_size = utils.get_path_size(apk_new) print("apk_old:%s size: %s" % (apk_old, utils.get_size_in_nice_string(old_size))) print("apk_new:%s size: %s" % (apk_new, utils.get_size_in_nice_string(new_size))) print("") # 解压文件夹以便分析 utils.surely_rmdir(old_apk_dir) utils.surely_rmdir(new_apk_dir) print("start to unzip apk old") utils.unzip_dir(apk_old, old_apk_dir) print("start to unzip apk new") utils.unzip_dir(apk_new, new_apk_dir) print("") print("unzip apk info:") check_unzipped_apk(apk_old, old_apk_dir) check_unzipped_apk(apk_new, new_apk_dir) json_object = get_json_from_file("apk_tree"); data_string = json.dumps(json_object) jdict = json.loads(data_string) # 键值对保存要查文件的大小,用于后面对比 old_apk_obj = dict() new_apk_obj = dict() # 方法数dict new_method_dict = dict() old_method_dict = dict() print("") print("") # 输出其中指定文件的大小 print("============%s==============" % old_apk_dir) walk_dict(old_apk_dir, jdict, old_apk_obj, old_method_dict, old_apk_dir) print("============%s==============" % old_apk_dir) print("") print("") print("============%s==============" % new_apk_dir) walk_dict(new_apk_dir, jdict, new_apk_obj, new_method_dict, new_apk_dir) print("============%s==============" % new_apk_dir) print("") print("") print("============compare result==============") # compare compare_dict(new_apk_obj, old_apk_obj, new_method_dict, old_method_dict) print("============compare result==============") print("") print("") # 检查出apk种新添的文件 # 文件dict,用于检查新文件 new_file_dict = dict() removed_file_dict = dict() changed_file_dict = dict() dirCompare(old_apk_dir, new_apk_dir, new_file_dict, removed_file_dict, changed_file_dict) print_top_dict(new_file_dict, top, "new file") print("") print("") print_top_dict(removed_file_dict, top, "removed file") print("") print("") print_top_dict(changed_file_dict, top, "changed file") # 清除临时解压的apk文件夹 utils.surely_rmdir(old_apk_dir) utils.surely_rmdir(new_apk_dir)
def check_unzipped_apk(apk_name, apk_path): file_count = sum([len(x) for _, _, x in os.walk(apk_path)]) floder_size = utils.get_size_in_nice_string(utils.get_path_size(apk_path)) print("apk:%s size: %s files: %s" % (apk_name, floder_size, file_count))