Create a base-26 symlink in `path` pointing to `source`.

    If such a symlink already exists, it is returned. Note that there is a small
    chance that this function may create a new symlink when there is already one
    pointed at `source`.

    Assumes `path` only contains base26 symlinks.

    Returns:
        str: Path to created symlink.

def create_unique_base26_symlink(path, source):
    """Create a base-26 symlink in `path` pointing to `source`.

    If such a symlink already exists, it is returned. Note that there is a small
    chance that this function may create a new symlink when there is already one
    pointed at `source`.

    Assumes `path` only contains base26 symlinks.

    Returns:
        str: Path to created symlink.
    """
    retries = 0

    while True:
        # if a link already exists that points at `source`, return it
        name = find_matching_symlink(path, source)
        if name:
            return os.path.join(path, name)

        # get highest current symlink in path
        names = [
            x for x in os.listdir(path)
            if os.path.islink(os.path.join(path, x))
        ]

        if names:
            prev = max(names)
        else:
            prev = None

        linkname = get_next_base26(prev)
        linkpath = os.path.join(path, linkname)

        # attempt to create the symlink
        try:
            os.symlink(source, linkpath)
            return linkpath

        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        # if we're here, the same named symlink was created in parallel
        # somewhere. Try again up to N times.
        #
        if retries > 10:
            raise RuntimeError(
                "Variant shortlink not created - there was too much contention.")
        retries += 1