def compare2DirDifferents(path_dir1, path_dir2): #返回值 different_dirs_cnt = 0 different_files_cnt = 0 only_dir1_dirs = set() only_dir2_dirs = set() only_dir1_files = set() only_dir2_files = set() modified_files = set() print("开始比较目录差异") path1dirs, path1files = walkPathDir(path_dir1) print("目录1包含:", len(path1dirs), "个子目录,", len(path1files), "个文件") path2dirs, path2files = walkPathDir(path_dir2) print("目录2包含:", len(path2dirs), "个子目录,", len(path2files), "个文件") #耗时比较操作,用协程分片回调进度 yield compare_different_files_of_dirs(path1files, path_dir1, path_dir2, only_dir1_files, modified_files) yield compare_different_files_of_dirs(path2files, path_dir2, path_dir1, only_dir2_files, modified_files) compare_different_dirs_of_dirs(path1dirs, path_dir1, path_dir2, only_dir1_dirs) compare_different_dirs_of_dirs(path2dirs, path_dir2, path_dir1, only_dir2_dirs) # 遍历目录的文件,得到相异的文件,加上相异目录,构建树 only_dir1_dirs = list(only_dir1_dirs) only_dir2_dirs = list(only_dir2_dirs) only_dir1_files = list(only_dir1_files) only_dir2_files = list(only_dir2_files) modified_files = list(modified_files) different_dirs_cnt = len(only_dir1_dirs) + len(only_dir2_dirs) different_files_cnt = len(only_dir1_files) + len(only_dir2_files) + len( modified_files) only_dir1_tree = createFileTree(dir1_tree_name, path_dir1, only_dir1_files, only_dir1_dirs) only_dir2_tree = createFileTree(dir2_tree_name, path_dir2, only_dir2_files, only_dir2_dirs) modified_tree = createFileTree(modified_tree_name, "modify_root", modified_files, []) compareResult = { "different_dirs_cnt": different_dirs_cnt, "different_files_cnt": different_files_cnt, "only_dir1_tree": only_dir1_tree, "only_dir2_tree": only_dir2_tree, "modified_tree": modified_tree, "path_dir1": path_dir1, "path_dir2": path_dir2, } from compareCommand import CompareCommand from mymvc.GameFacade import GameFacade GameFacade().sendNotification(CompareCommand.hide_compare_progress_cmd) GameFacade().sendNotification(CompareCommand.show_compare_result_cmd, compareResult)
def registViewMediator(cls, view, mediatorCls): mediator = mediatorCls(view) #删除旧的mediator和ui oldMediator = GameFacade().retrieveMediator(mediatorCls.NAME) if oldMediator: GameFacade.getInstance().removeMediator(mediatorCls.NAME) try: oldMediator.viewComponent.destroy() except: print("旧view component已删除") GameFacade().registerMediator(mediator) #绑定删除 oldViewDestroy = None if hasattr(view, "destroy"): oldViewDestroy = getattr(view, "destroy") def onViewDestroy(self, destroyWindow=True, destroySubWindows=True): if oldViewDestroy: oldViewDestroy(destroyWindow, destroySubWindows) if mediatorCls.NAME and mediatorCls.NAME != "" and isinstance( mediatorCls.NAME, str): GameFacade.getInstance().removeMediator(mediatorCls.NAME) from types import MethodType view.destroy = MethodType(onViewDestroy, view)
def RemoveFileEvent(self, item): print("remove file event", item.text(0)) item_data = self.getClickFileData(item) node_path = item_data.open_path + "/" + item_data.file_name if self.treedata and isinstance(self.treedata, Tree): treenode = self.treedata.find_child_by_path(node_path) open_path = item_data.open_path edit_tree_data = add_to_edit_tree_data(open_path=open_path, tree_node=treenode) GameFacade.getInstance().sendNotification( EditCommand.remove_file_from_target_dir_cmd, edit_tree_data)
def AddFileEvent(self, item): print("add file event", item.text(0)) item_data = self.getClickFileData(item) node_path = item_data.open_path + "/" + item_data.file_name if self.treedata and isinstance(self.treedata, Tree): treenode = self.treedata.find_child_by_path(node_path) open_path = item_data.open_path if open_path.find(modified_tree_root) > -1: open_path = open_path.replace(modified_tree_root, self.dir1) edit_tree_data = add_to_edit_tree_data(open_path=open_path, tree_node=treenode) GameFacade.getInstance().sendNotification( EditCommand.add_file_to_target_dir_cmd, edit_tree_data)
def compare_different_files_of_dirs(pathfiles, path_dir1, path_dir2, only_dir_files, modified_files): frame_compare_cnt = 0 compare_cnt = 0 for pathfile in pathfiles: relativePath = getRelativePath(pathfile, path_dir1) if relativePath in only_dir_files or relativePath in modified_files: continue if isExistInOtherDir(pathfile, path_dir1, path_dir2): #两个目录都存在文件 dir2file = pathfile.replace(path_dir1, path_dir2) if diffTwoFiles(pathfile, dir2file) == 0: #完全相同 pass else: #两文件不同,加入修改目录 modified_files.add(relativePath) else: #目录1独有 only_dir_files.add(relativePath) frame_compare_cnt += 1 compare_cnt += 1 if frame_compare_cnt >= 3: print("this frame has done:", (compare_cnt / len(pathfiles))) from mymvc.GameFacade import GameFacade from compareCommand import CompareCommand GameFacade().sendNotification( CompareCommand.show_compare_progress_cmd, round(100 * compare_cnt / len(pathfiles), 2)) yield frame_compare_cnt = 0
def singleClickHandler(self, item): send_data = self.getClickFileData(item) print("single click:", send_data.open_path, item.text(0)) GameFacade().sendNotification(EditCommand.single_click_file_cmd, send_data)
def remove(cls): for command in CompareCommand.commands: from mymvc.GameFacade import GameFacade GameFacade().removeCommand(command)
def regist(cls): for command in CompareCommand.commands: from mymvc.GameFacade import GameFacade GameFacade().registerCommand(command, CompareCommand)
def RemoveFileEvent(self): item_data = self.preview_data print("remove file event", item_data) treenode = TreeNode(item_data.file_name) edit_tree_data = add_to_edit_tree_data(open_path=item_data.open_path, tree_node=treenode) GameFacade.getInstance().sendNotification(EditCommand.remove_file_from_target_dir_cmd, edit_tree_data)
def onExportButtonClick(self): from editCommand import EditCommand GameFacade().sendNotification(EditCommand.export_edit_trees_cmd)
def onBackButtonClick(self): from compareCommand import CompareCommand GameFacade().sendNotification(CompareCommand.back_to_select_cmd)
def onViewDestroy(self, destroyWindow=True, destroySubWindows=True): if oldViewDestroy: oldViewDestroy(destroyWindow, destroySubWindows) if mediatorCls.NAME and mediatorCls.NAME != "" and isinstance( mediatorCls.NAME, str): GameFacade.getInstance().removeMediator(mediatorCls.NAME)
# -*- coding: utf-8 -*- import sys sys.path.append("./compare2dir") from mymvc.GameFacade import GameFacade if __name__ == "__main__": GameFacade().startUp()