def backup_dir(dir, ext='.bak'):
    # type: (str, str) -> str
    """Figure out the name of a directory to back up the given dir to
    (adding .bak, .bak2, etc)"""
    n = 1
    extension = ext
    while os.path.exists(dir + extension):
        n += 1
        extension = ext + str(n)
    return dir + extension
Figure out the name of a directory to back up the given dir to
    (adding .bak, .bak2, etc)
product/gradle-plugin/src/main/python/pip/_internal/utils/misc.py