def generate_version_py(packagename=None, version=None, release=None, debug=None,
                        uses_git=None, srcdir='.'):
    """
    Generate a version.py file in the package with version information, and
    update developer version strings.

    This function should normally be called without any arguments. In this case
    the package name and version is read in from the ``setup.cfg`` file (from
    the ``name`` or ``package_name`` entry and the ``version`` entry in the
    ``[metadata]`` section).

    If the version is a developer version (of the form ``3.2.dev``), the
    version string will automatically be expanded to include a sequential
    number as a suffix (e.g. ``3.2.dev13312``), and the updated version string
    will be returned by this function.

    Based on this updated version string, a ``version.py`` file will be
    generated inside the package, containing the version string as well as more
    detailed information (for example the major, minor, and bugfix version
    numbers, a ``release`` flag indicating whether the current version is a
    stable or developer version, and so on.
    """

    if packagename is not None:
        warnings.warn('The packagename argument to generate_version_py has '
                      'been deprecated and will be removed in future. Specify '
                      'the package name in setup.cfg instead', AstropyDeprecationWarning)

    if version is not None:
        warnings.warn('The version argument to generate_version_py has '
                      'been deprecated and will be removed in future. Specify '
                      'the version number in setup.cfg instead', AstropyDeprecationWarning)

    if release is not None:
        warnings.warn('The release argument to generate_version_py has '
                      'been deprecated and will be removed in future. We now '
                      'use the presence of the "dev" string in the version to '
                      'determine whether this is a release', AstropyDeprecationWarning)

    # We use ConfigParser instead of read_configuration here because the latter
    # only reads in keys recognized by setuptools, but we need to access
    # package_name below.
    conf = ConfigParser()
    conf.read('setup.cfg')

    if conf.has_option('metadata', 'name'):
        packagename = conf.get('metadata', 'name')
    elif conf.has_option('metadata', 'package_name'):
        # The package-template used package_name instead of name for a while
        warnings.warn('Specifying the package name using the "package_name" '
                      'option in setup.cfg is deprecated - use the "name" '
                      'option instead.', AstropyDeprecationWarning)
        packagename = conf.get('metadata', 'package_name')
    elif packagename is not None:  # deprecated
        pass
    else:
        sys.stderr.write('ERROR: Could not read package name from setup.cfg\n')
        sys.exit(1)

    if conf.has_option('metadata', 'version'):
        version = conf.get('metadata', 'version')
        add_git_devstr = True
    elif version is not None:  # deprecated
        add_git_devstr = False
    else:
        sys.stderr.write('ERROR: Could not read package version from setup.cfg\n')
        sys.exit(1)

    if release is None:
        release = 'dev' not in version

    if not release and add_git_devstr:
        version += get_git_devstr(False)

    if uses_git is None:
        uses_git = not release

    # In some cases, packages have a - but this is a _ in the module. Since we
    # are only interested in the module here, we replace - by _
    packagename = packagename.replace('-', '_')

    try:
        version_module = get_pkg_version_module(packagename)

        try:
            last_generated_version = version_module._last_generated_version
        except AttributeError:
            last_generated_version = version_module.version

        try:
            last_githash = version_module._last_githash
        except AttributeError:
            last_githash = version_module.githash

        current_release = version_module.release
        current_debug = version_module.debug
    except ImportError:
        version_module = None
        last_generated_version = None
        last_githash = None
        current_release = None
        current_debug = None

    if release is None:
        # Keep whatever the current value is, if it exists
        release = bool(current_release)

    if debug is None:
        # Likewise, keep whatever the current value is, if it exists
        debug = bool(current_debug)

    package_srcdir = os.path.join(srcdir, *packagename.split('.'))
    version_py = os.path.join(package_srcdir, 'version.py')

    if (last_generated_version != version or current_release != release or
            current_debug != debug):
        if '-q' not in sys.argv and '--quiet' not in sys.argv:
            log.set_threshold(log.INFO)

        if is_distutils_display_option():
            # Always silence unnecessary log messages when display options are
            # being used
            log.set_threshold(log.WARN)

        log.info('Freezing version number to {0}'.format(version_py))

        with open(version_py, 'w') as f:
            # This overwrites the actual version.py
            f.write(_get_version_py_str(packagename, version, last_githash,
                                        release, debug, uses_git=uses_git))

    return version