def create_file(self, ignore=False, is_folder=False): ignore_patterns = [] if ignore: ignore_patterns = [".*\\.txt"] file_changes.add_folder(self.abs_folder_path, exclude_regexes=ignore_patterns) if is_folder: rel_file_path = "dummy" os.mkdir(os.path.join(self.abs_folder_path, rel_file_path)) else: rel_file_path = "test.txt" abs_file_path = os.path.join(self.abs_folder_path, rel_file_path) with open(abs_file_path, "w") as f: f.write("Hello" * 10) time.sleep(1) expected_changes = {} if is_folder: action = gen_json.ACTION_MKDIR else: action = gen_json.ACTION_PULL gen_json._add_new_change_entry(expected_changes, paths.normalize_path(rel_file_path), action, is_folder) data = gen_json._get_json_data() actual_changes = data[self.abs_folder_path]["changes"] if ignore: self.assertEqual({}, actual_changes) else: expected_changes[rel_file_path]["timestamp"] = actual_changes[rel_file_path]["timestamp"] self.assertEqual(expected_changes, actual_changes)
def test_add_folder(self): file_changes.start_observing() path, include, exclude = h_get_dummy_folder_data() file_changes.add_folder(self.path, include, exclude) time.sleep(1) self.assertEqual(1, len(file_changes.watchers)) self.assertEqual([self.path], gen_json.get_all_synced_folders_paths())
def setUp(self): file_changes_json.init_file(empty=True) self.abs_folder_path = h_create_empty_dummy_folder() self.rel_file_path = "test.txt" self.abs_file_path = os.path.join(self.abs_folder_path, self.rel_file_path) with open(self.abs_file_path, "w"): pass file_changes.start_observing() file_changes.add_folder(self.abs_folder_path)
def test_add_single_ignores(self): file_changes.start_observing() file_changes.add_folder(self.path) rel_path = paths.normalize_path("test.txt") time.sleep(0.3) file_changes.add_single_ignores(self.path, [rel_path]) with open(os.path.join(self.path, rel_path), "w+") as f: f.write("Hello" * 100) folder = file_changes_json.get_folder_entry(self.path) self.assertEqual(0, len(folder["changes"]))
def add_sync_folder( abs_local_path: NormalizedPath, remote_name: str, include_regexes: List[str] = (".*", ), exclude_regexes: List[str] = (), merge_method: merge_folders.MergeMethod = merge_folders.MergeMethods. DEFAULT ) -> Status: """Adds a synchronization between a local folder and a server folder. The content of both folders is merged, so that both folders are identical.""" success = file_changes.add_folder(abs_local_path, include_regexes, exclude_regexes, remote_name) if not success: return Status.fail( "Folder can not be added locally. It is nested in an existing folder or wraps " "around an existing folder") new_added = net_interface.server.add_folder(remote_name) if new_added: merge_method = merge_folders.MergeMethods.TAKE_1 status = merge_folders.merge_folders(abs_local_path, remote_name, merge_method) if not status.was_successful(): return status return Status.success("Successfully added new sync folder pair.")
def test_create_many(self): ignore_patterns = pattern_parser.parse_patterns("*.pyc, *ignore*") file_changes.add_folder(self.abs_folder_path, exclude_regexes=ignore_patterns) time.sleep(0.5) # root: 10 files + 1 folder for i in range(10): rel_file_path = f"file_{i}.txt" abs_file_path = os.path.join(self.abs_folder_path, rel_file_path) with open(abs_file_path, "w") as f: f.write("HeHe" * 10) time.sleep(0.1) rel_folder_path = "folder_1" folder_1_path = os.path.join(self.abs_folder_path, rel_folder_path) os.mkdir(folder_1_path) # folder_1: 1 ignore folder + 1 ignore_file.pyc + 1 not_ignore.txt rel_folder_path = "ignore" ignore_folder_path = os.path.join(folder_1_path, rel_folder_path) os.mkdir(ignore_folder_path) abs_file_path = os.path.join(folder_1_path, "file.pyc") with open(abs_file_path, "w"): pass abs_file_path = os.path.join(folder_1_path, "not_Iggnore_file.txt") with open(abs_file_path, "w"): pass # ignore folder: 1 file1.txt abs_file_path = os.path.join(ignore_folder_path, "file_1.txt") with open(abs_file_path, "w"): pass time.sleep(2) folder = file_changes_json.get_folder_entry(self.abs_folder_path) changes = folder["changes"] num_files = 0 num_folders = 0 for change in changes.values(): num_folders += change["is_directory"] num_files += not change["is_directory"] self.assertEqual(11, num_files) self.assertEqual(1, num_folders)
def test_remove_folder(self): file_changes.start_observing() file_changes.add_folder(self.path) file_changes.remove_folder_from_watching(self.path) self.assertEqual(0, len(file_changes.watchers))
def test_start_observing(self): file_changes_json.init_file() path, include, exclude = h_get_dummy_folder_data() file_changes.add_folder(self.path, include, exclude) file_changes.start_observing() self.assertEqual(1, len(file_changes.watchers))
def h_watch_dummy_folder(folder_name: str): abs_path = os.path.join(client_paths.LOCAL_CLIENT_DATA, folder_name) os.makedirs(abs_path, exist_ok=True) file_changes.add_folder(abs_path, remote_name=folder_name)