import re


def get_cupy_release_for(chainer_version):
    """Returns CuPy version required for the given Chainer version."""

    m = re.search(r'^v(\d)\.(.+)$', chainer_version)
    if m is None:
        raise ValueError(chainer_version)

    chainer_major = int(m.group(1))
    chainer_rest = m.group(2)
    if chainer_major <= 1:
        raise ValueError('Chainer v1 or earlier is unsupported')
    elif 2 <= chainer_major <= 3:
        # Chainer vN requires CuPy v(N-1).
        return 'v{}.{}'.format((chainer_major - 1), chainer_rest)
    else:
        # The same versioning as Chainer.
        return chainer_version