Exemplo n.º 1
0
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
Exemplo n.º 2
0
 def test_common_path_suffix_two_root(self):
     test = paths.common_path_suffix('/', '/')
     assert (None, 0) == test
Exemplo n.º 3
0
 def test_common_path_suffix_return_None_if_no_common_suffix2(self):
     test = paths.common_path_suffix('/', '/a/b/c')
     assert (None, 0) == test
Exemplo n.º 4
0
 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
Exemplo n.º 5
0
 def test_common_path_suffix_find_subpath(self):
     test = paths.common_path_suffix('/z/b/c', '/a/b/c')
     assert ('b/c', 2) == test
Exemplo n.º 6
0
 def test_common_path_suffix(self):
     test = paths.common_path_suffix('/a/b/c', '/a/b/c')
     assert ('a/b/c', 3) == test
Exemplo n.º 7
0
 def test_common_path_suffix_two_root(self):
     test = paths.common_path_suffix("/", "/")
     assert (None, 0) == test
Exemplo n.º 8
0
 def test_common_path_suffix_return_None_if_no_common_suffix2(self):
     test = paths.common_path_suffix("/", "/a/b/c")
     assert (None, 0) == test
Exemplo n.º 9
0
 def test_common_path_suffix_root_empty(self):
     test = paths.common_path_suffix('/', '')
     assert (None, 0) == test
Exemplo n.º 10
0
 def test_common_path_suffix_empty_root(self):
     test = paths.common_path_suffix('', '/')
     assert (None, 0) == test
Exemplo n.º 11
0
 def test_common_path_suffix_return_None_if_no_common_suffix(self):
     test = paths.common_path_suffix('/a/b/c', '/')
     assert (None, 0) == test
Exemplo n.º 12
0
 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
Exemplo n.º 13
0
 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
Exemplo n.º 14
0
 def test_common_path_suffix_handles_relative_path(self):
     test = paths.common_path_suffix('a/b', 'a/b')
     assert ('a/b', 2) == test
Exemplo n.º 15
0
 def test_common_path_suffix_find_subpath(self):
     test = paths.common_path_suffix('/z/b/c', '/a/b/c')
     assert ('b/c', 2) == test
Exemplo n.º 16
0
 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
Exemplo n.º 17
0
 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
Exemplo n.º 18
0
 def test_common_path_suffix_empty_root(self):
     test = paths.common_path_suffix('', '/')
     assert test == (None, 0)
Exemplo n.º 19
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
Exemplo n.º 20
0
 def test_common_path_suffix_root_empty(self):
     test = paths.common_path_suffix('/', '')
     assert test == (None, 0)
Exemplo n.º 21
0
 def test_common_path_suffix_empty_empty(self):
     test = paths.common_path_suffix("", "")
     assert (None, 0) == test
Exemplo n.º 22
0
 def test_common_path_suffix(self):
     test = paths.common_path_suffix("/a/b/c", "/a/b/c")
     assert ("a/b/c", 3) == test
Exemplo n.º 23
0
 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
Exemplo n.º 24
0
 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
Exemplo n.º 25
0
 def test_common_path_suffix_handles_relative_path(self):
     test = paths.common_path_suffix('a/b', 'a/b')
     assert ('a/b', 2) == test
Exemplo n.º 26
0
 def test_common_path_suffix_find_subpath(self):
     test = paths.common_path_suffix("/z/b/c", "/a/b/c")
     assert ("b/c", 2) == test
Exemplo n.º 27
0
 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
Exemplo n.º 28
0
 def test_common_path_suffix_handles_relative_path(self):
     test = paths.common_path_suffix("a/b", "a/b")
     assert ("a/b", 2) == test
Exemplo n.º 29
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
Exemplo n.º 30
0
 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
Exemplo n.º 31
0
 def test_common_path_suffix_empty_empty(self):
     test = paths.common_path_suffix('', '')
     assert (None, 0) == test
Exemplo n.º 32
0
 def test_common_path_suffix(self):
     test = paths.common_path_suffix('/a/b/c', '/a/b/c')
     assert ('a/b/c', 3) == test