def sync_cmd(master, backup, no_commit): master_dir = data.loaded_dir(master) backup_dir = data.loaded_dir(backup) diff = dirdiff.full_diff_dirs(master_dir, backup_dir, full_paths=True) logger.info("Setting master directory to %s", master) logger.info("Setting backup directory to %s", backup) if no_commit: logger.info("No-commit enabled. No changes will be committed!") contents_to_copy = diff.files.changed | diff.files.missing | diff.subdirs.missing for f in contents_to_copy: master_copy = Path(f) backup_copy = Path(backup) / master_copy.name if no_commit: content_type = "directory" if master_copy.is_dir() else "file" logger.info("Will copy %s %s >>>> %s", content_type, fspath(master_copy), fspath(backup_copy)) else: if master_copy.is_dir(): shutil.copytree(fspath(master_copy), fspath(backup_copy)) content_type = "directory" else: shutil.copy(fspath(master_copy), fspath(backup_copy)) content_type = "file" logger.info("Copied %s %s >>>> %s", content_type, fspath(master_copy), fspath(backup_copy))
def test_dir_with_files_and_subdir(tmpdir): # GIVEN temp_path = Path(tmpdir) expected = sut.Dir( temp_path.name, fspath(temp_path), {"one", "two", "three"} ) subdir_name = "sublevel1" expected.subdirs = { subdir_name: sut.Dir( subdir_name, fspath(temp_path/subdir_name), {"file1.txt", "file2.tsk", "file3.js"}, ) } create_dir(temp_path, expected) # WHEN actual = sut.loaded_dir(temp_path) # THEN assert expected == actual
def test_dir_with_multiple_subdir_levels(tmpdir): # GIVEN temp_path = Path(tmpdir) expected = helpers.dir_schemas.multiple_subdir_levels(temp_path) create_dir(temp_path, expected) # WHEN actual = sut.loaded_dir(temp_path) # THEN assert expected == actual
def test_dir_with_only_files(tmpdir): # GIVEN temp_path = Path(tmpdir) expected = sut.Dir( temp_path.name, fspath(temp_path), {"one", "two", "three"} ) create_dir(temp_path, expected) # WHEN actual = sut.loaded_dir(temp_path) # THEN assert expected == actual
def test_full_diff_dirs(diff_fix): # GIVEN src_dir, cmp_dir = diff_fix src_dir = helpers.dir_schemas.multiple_subdir_levels(src_dir.fullpath) cmp_dir = helpers.dir_schemas.multiple_subdir_levels(cmp_dir.fullpath) create_dir(src_dir.fullpath, src_dir, empty_files=False, seed="SRC") create_dir(cmp_dir.fullpath, cmp_dir, empty_files=False, seed="CMP") # WHEN src_dir = loaded_dir(src_dir.fullpath) cmp_dir = loaded_dir(cmp_dir.fullpath) diff = sut.full_diff_dirs(src_dir, cmp_dir) # THEN assert diff.files.changed assert not diff.files.missing assert not diff.files.new assert not diff.files.shared assert not diff.subdirs.missing assert not diff.subdirs.new assert not diff.subdirs.shared
def compare_cmd(src, target): src_dir = data.loaded_dir(src) target_dir = data.loaded_dir(target) res = dirdiff.full_diff_dirs(src_dir, target_dir) pp.pprint(asdict(res))