def align_trees(a_files, b_files): """ Given two sequences of File objects 'a' and 'b', return a tuple of two integers that represent the number path segments to remove respectively from a File path in 'a' or a File path in 'b' to obtain the equal paths for two files that are the same in 'a' and 'b'. """ # we need to find one uniquly named file that exists in 'a' and 'b'. a_names = defaultdict(list) for a_file in a_files: a_names[a_file.name].append(a_file) a_uniques = {k: v[0] for k, v in a_names.items() if len(v) == 1} b_names = defaultdict(list) for b_file in b_files: b_names[b_file.name].append(b_file) b_uniques = {k: v[0] for k, v in b_names.items() if len(v) == 1} candidate_found = False for a_name, a_unique in a_uniques.items(): if a_name not in b_uniques: continue b_unique = b_uniques.get(a_name) if a_unique and a_unique.sha1 == b_unique.sha1: candidate_found = True break if not candidate_found: raise AlignmentException if a_unique.path == b_unique.path: return 0, 0 common_suffix, common_segments = paths.common_path_suffix( a_unique.path, b_unique.path) a_segments = len(paths.split(a_unique.path)) b_segments = len(paths.split(b_unique.path)) return a_segments - common_segments, b_segments - common_segments
def test_common_path_suffix_two_root(self): test = paths.common_path_suffix('/', '/') assert (None, 0) == test
def test_common_path_suffix_return_None_if_no_common_suffix2(self): test = paths.common_path_suffix('/', '/a/b/c') assert (None, 0) == test
def test_common_path_suffix_handles_relative_subpath(self): test = paths.common_path_suffix('zsds/adsds/a/b/b/c', 'a//a/d//b/c') assert ('b/c', 2) == test
def test_common_path_suffix_find_subpath(self): test = paths.common_path_suffix('/z/b/c', '/a/b/c') assert ('b/c', 2) == test
def test_common_path_suffix(self): test = paths.common_path_suffix('/a/b/c', '/a/b/c') assert ('a/b/c', 3) == test
def test_common_path_suffix_two_root(self): test = paths.common_path_suffix("/", "/") assert (None, 0) == test
def test_common_path_suffix_return_None_if_no_common_suffix2(self): test = paths.common_path_suffix("/", "/a/b/c") assert (None, 0) == test
def test_common_path_suffix_root_empty(self): test = paths.common_path_suffix('/', '') assert (None, 0) == test
def test_common_path_suffix_empty_root(self): test = paths.common_path_suffix('', '/') assert (None, 0) == test
def test_common_path_suffix_return_None_if_no_common_suffix(self): test = paths.common_path_suffix('/a/b/c', '/') assert (None, 0) == test
def test_common_path_suffix_ignore_and_strip_trailing_slash(self): test = paths.common_path_suffix('zsds/adsds/a/b/b/c/', 'a//a/d//b/c/') assert ('b/c', 2) == test
def test_common_path_suffix_handles_relative_path(self): test = paths.common_path_suffix('a/b', 'a/b') assert ('a/b', 2) == test
def test_common_path_suffix_absolute_relative(self): test = paths.common_path_suffix('a/b/c', '/a/b/c') assert ('a/b/c', 3) == test
def test_common_path_suffix_ignore_and_strip_trailing_slash(self): test = paths.common_path_suffix("zsds/adsds/a/b/b/c/", "a//a/d//b/c/") assert ("b/c", 2) == test
def test_common_path_suffix_empty_root(self): test = paths.common_path_suffix('', '/') assert test == (None, 0)
def test_common_path_suffix_match_only_whole_segments(self): # only segments are honored, commonality within segment is ignored test = paths.common_path_suffix("this/is/aaaa/great/path", "this/is/aaaaa/great/path") assert ("great/path", 2) == test
def test_common_path_suffix_root_empty(self): test = paths.common_path_suffix('/', '') assert test == (None, 0)
def test_common_path_suffix_empty_empty(self): test = paths.common_path_suffix("", "") assert (None, 0) == test
def test_common_path_suffix(self): test = paths.common_path_suffix("/a/b/c", "/a/b/c") assert ("a/b/c", 3) == test
def test_common_path_suffix_absolute_relative(self): test = paths.common_path_suffix("a/b/c", "/a/b/c") assert ("a/b/c", 3) == test
def test_common_path_suffix_find_subpath(self): test = paths.common_path_suffix("/z/b/c", "/a/b/c") assert ("b/c", 2) == test
def test_common_path_suffix_handles_relative_path(self): test = paths.common_path_suffix("a/b", "a/b") assert ("a/b", 2) == test
def test_common_path_suffix_match_only_whole_segments(self): # only segments are honored, commonality within segment is ignored test = paths.common_path_suffix('this/is/aaaa/great/path', 'this/is/aaaaa/great/path') assert ('great/path', 2) == test
def test_common_path_suffix_handles_relative_subpath(self): test = paths.common_path_suffix("zsds/adsds/a/b/b/c", "a//a/d//b/c") assert ("b/c", 2) == test
def test_common_path_suffix_empty_empty(self): test = paths.common_path_suffix('', '') assert (None, 0) == test