Convert a git tag version string into something compatible with `PEP-440 <https://www.python.org/dev/peps/pep-0440/>`_.

    :param version: The input version string, normally directly out of git describe.
    :return: PEP-440 version string

    Usage::

      >>> convert_to_pypi_version('r1.0.1')  # Normal Releases
      1.0.1
      >>> convert_to_pypi_version('r1.0.1-dev1')  # Dev Releases
      1.0.1.dev1
      >>> convert_to_pypi_version('r1.0.1-a1')  # Alpha Releases
      1.0.1a1
      >>> convert_to_pypi_version('r1.0.1-b4')  # Beta Releases
      1.0.1b4
      >>> convert_to_pypi_version('r1.0.1-rc2')  # RC Releases
      1.0.1rc2
      >>> convert_to_pypi_version('r1.0.1-12-geaea7b6')  # Post Releases
      1.0.1.post12

def convert_to_pypi_version(version):
    """
    Convert a git tag version string into something compatible with `PEP-440 <https://www.python.org/dev/peps/pep-0440/>`_.

    :param version: The input version string, normally directly out of git describe.
    :return: PEP-440 version string

    Usage::

      >>> convert_to_pypi_version('r1.0.1')  # Normal Releases
      1.0.1
      >>> convert_to_pypi_version('r1.0.1-dev1')  # Dev Releases
      1.0.1.dev1
      >>> convert_to_pypi_version('r1.0.1-a1')  # Alpha Releases
      1.0.1a1
      >>> convert_to_pypi_version('r1.0.1-b4')  # Beta Releases
      1.0.1b4
      >>> convert_to_pypi_version('r1.0.1-rc2')  # RC Releases
      1.0.1rc2
      >>> convert_to_pypi_version('r1.0.1-12-geaea7b6')  # Post Releases
      1.0.1.post12
    """
    v = re.search('^[r,v]{0,1}(?P<final>[0-9\.]+)(\-(?P<pre>(a|b|rc)[0-9]+))?(\-(?P<dev>dev[0-9]+))?(\-(?P<post>[0-9]+))?(\-.+)?$', version)
    if not v:
        return __default_version__

    # https://www.python.org/dev/peps/pep-0440/#final-releases
    version = v.group('final')

    # https://www.python.org/dev/peps/pep-0440/#pre-releases
    if v.group('pre'):
        version += v.group('pre')

    # https://www.python.org/dev/peps/pep-0440/#developmental-releases
    if v.group('dev'):
        version += '.%s' % v.group('dev')

    # https://www.python.org/dev/peps/pep-0440/#post-releases
    if v.group('post'):
        version += '.post%s' % v.group('post')

    return version