def test_get_regex_matcher(self): for reg in TestGetMethods.regex: matcher = get_regex_matcher(reg) for path in TestGetMethods.paths: basename = os.path.basename(path) fully_matched_part = None matches = re.match(reg + "$", basename) if matches: fully_matched_part = matches.group(0) self.assertEqual(matcher(path), fully_matched_part)
def synchronized_remove( self, paths_to_remove, lock_trash, lock_remove, queue, regex=None ): with lock_trash: make_trash_if_not_exist(self.trash_location) regex_matcher = return_true if regex: regex_matcher = get_regex_matcher(regex) for path in paths_to_remove: path_info_object = TrashInfo(path, self.trash_location) correct_path = path_info_object.initial_path try: self._run_remove_checks(correct_path, self.trash_location) except SmartError as error: path_info_object.errors.append(error) queue.put(path_info_object) continue items_to_remove = self._get_paths_to_remove_from_tree( correct_path, regex_matcher ) for item in items_to_remove: if item == correct_path: info_object = path_info_object else: info_object = TrashInfo(item, self.trash_location) try: self._run_access_checks( info_object.initial_path, self.trash_location ) except SmartError as error: info_object.errors.append(error) queue.put(info_object) continue if not self.dry_run: if check_path_existance(info_object.path_in_trash): method_to_solve = getattr(solve, self.conflict_policy) method_to_solve(info_object) if not info_object.errors: with lock_remove: self._smart_remove(info_object) queue.put(info_object) queue.cancel_join_thread()
def test_file_regex(self): regex_dir = create_empty_directory("regex") file_template = "file.*" file_names = [ "file.py", "file$", "file$$", "file1231321", r"\file1", "abracadabra", "lalal" ] file_re = get_regex_matcher(file_template) regex_paths = [] file_paths = [] for name in file_names: path = create_file_in_dir(name, regex_dir) file_paths.append(path) if file_re(name): regex_paths.append(os.path.abspath(path)) removed = self.trash.remove(file_paths, regex=file_template) for rm in removed: self.assertIn(rm.initial_path, regex_paths)
def test_dir_regex(self): regex_dir = create_empty_directory("regex_dir") dir_template = ".+dir123" dir_names = [ "dir123", "ajdhadir123", "asjdhsjdir123skjfhkds", r"$$$$$dir123", "$$dir123", "abr" ] dir_re = get_regex_matcher(dir_template) regex_paths = [] dir_paths = [] for name in dir_names: path = create_dir_in_dir(name, regex_dir) dir_paths.append(path) if dir_re(name): regex_paths.append(os.path.abspath(path)) self.trash.remove_mode = const.REMOVE_EMPTY_DIRECTORY_MODE removed = self.trash.remove(dir_paths, regex=dir_template) for rm in removed: self.assertIn(rm.initial_path, regex_paths)
def test_tree_match_rehex_partially(self): regex_dir = create_empty_directory("regex_tree_part") paths = [] regex_paths = [] tree_template = r"4+part.*" matcher = get_regex_matcher(tree_template) tree = create_dir_in_dir("444part098765", regex_dir) file1_in_tree = create_file_in_dir("444partads", tree) file2_in_tree = create_file_in_dir("partkasjd", tree) dir_in_tree = create_dir_in_dir("hello", tree) file_in_dir_tree = create_file_in_dir("4444parthello", dir_in_tree) paths = [ tree, file1_in_tree, file2_in_tree, dir_in_tree, file_in_dir_tree ] for path in paths: if matcher(path): regex_paths.append(os.path.abspath(path)) self.trash.remove_mode = const.REMOVE_TREE_MODE result = self.trash.remove([tree], regex=tree_template) for res in result: self.assertIn(res.initial_path, regex_paths)
def remove(self, paths_to_remove, regex=None): """Remove paths in list to trash. Remove objects which are located on paths_to_remove. If 'regex' is specified then remove objects with name which is matches regelar ecpression in 'regex'. Take into consideration 'remove_mode', 'dry_run' and 'confirm_removal'. Args: paths_to_remove (list of str) regex (str) Returns: list of TrashInfo: Removed object is mapped to TrashInfo object. If there are exceptions they will append to TrashInfo object attribute. Errors in TrashInfo objects : AccessError: 1) Removable object parent directory has no read rights. 2) Removable object parent directory has no wright or execute or both rights. 3) Removable object is "fifo" or "socket". 3) Removable object is root, directory in root or mount point. ExistError: 1) Removable object doesn't exist. ModeError: 1) Try to remove directory or not empty directory when remove_mode = "file". 2) Try to remove empty directory when remove_mode = "directory". SysError: Remove into itself. """ make_trash_if_not_exist(self.trash_location) result_info_objects = [] regex_matcher = return_true if regex: regex_matcher = get_regex_matcher(regex) for path in paths_to_remove: path_info_object = TrashInfo(path, self.trash_location) correct_path = path_info_object.initial_path try: self._run_remove_checks(correct_path, self.trash_location) except SmartError as error: path_info_object.errors.append(error) result_info_objects.append(path_info_object) continue items_to_remove = self._get_paths_to_remove_from_tree( correct_path, regex_matcher ) for item in items_to_remove: if item == correct_path: info_object = path_info_object else: info_object = TrashInfo(item, self.trash_location) try: self._run_access_checks( info_object.initial_path, self.trash_location ) except SmartError as error: info_object.errors.append(error) result_info_objects.append(info_object) continue if not self.dry_run: if check_path_existance(info_object.path_in_trash): method_to_solve = getattr(solve, self.conflict_policy) method_to_solve(info_object) if not info_object.errors: self._smart_remove(info_object) result_info_objects.append(info_object) return result_info_objects