def process_branch_directories()

in helpers/drop_platform_support.py [0:0]


def process_branch_directories(platform_version: int) -> Changes:
    branch_dirs = []
    for root, dirs, files in os.walk("."):
        if "branches" in dirs:
            branch_dirs.append(Path(root).absolute() / "branches")

    changes = {}
    for branch_dir in branch_dirs:
        # Delete all files related to dropping platform
        old_platform_dir = branch_dir / str(platform_version)
        changes.update({path: (FileModification.Delete, None) for path in old_platform_dir.rglob('*') if path.is_file()})

        # Try to find files with the same content and move them to common module directory
        sub_dirs = [child for child in branch_dir.glob("*") if
                    child.is_dir() and PLATFORM_DIR_REGEX.match(child.name) and not child.name == str(platform_version)]
        file_dicts = []
        for sub_dir in sub_dirs:
            file_dicts.append({f.relative_to(sub_dir): f.read_bytes() for f in sub_dir.rglob('*') if f.is_file()})
        # It's possible collect nothing. For example, because `branch_dir` was a part of `.git` directory
        # in this case, just continue. Otherwise, `reduce` will fail
        if not file_dicts:
            continue
        same_files = reduce(dict_intersection, file_dicts)

        main_dir = branch_dir.parent
        for (relative_path, content) in same_files.items():
            path_in_main_module = main_dir / relative_path
            # It's possible to have a file with the same name in the main module,
            # and we don't want to override it because it most likely will break compilation
            if not path_in_main_module.exists():
                # Add file to main directory
                changes[path_in_main_module] = (FileModification.Change, content)
                # Delete all files from remaining platform-specific directories
                for sub_dir in sub_dirs:
                    changes[sub_dir / relative_path] = (FileModification.Delete, None)

    return changes