import re


def strip_name_suffixes(path, version):
    """Most tarballs contain a package name followed by a version number.
    However, some also contain extraneous information in-between the name
    and version:

    * ``rgb-1.0.6``
    * ``converge_install_2.3.16``
    * ``jpegsrc.v9b``

    These strings are not part of the package name and should be ignored.
    This function strips the version number and any extraneous suffixes
    off and returns the remaining string. The goal is that the name is
    always the last thing in ``path``:

    * ``rgb``
    * ``converge``
    * ``jpeg``

    Args:
        path (str): The filename or URL for the package
        version (str): The version detected for this URL

    Returns:
        str: The ``path`` with any extraneous suffixes removed
    """
    # NOTE: This could be done with complicated regexes in parse_name_offset
    # NOTE: The problem is that we would have to add these regexes to every
    # NOTE: single name regex. Easier to just strip them off permanently

    suffix_regexes = [
        # Strip off the version and anything after it

        # name-ver
        # name_ver
        # name.ver
        r'[._-][rvV]?' + str(version) + '.*',

        # namever
        r'V?' + str(version) + '.*',

        # Download type
        r'install',
        r'[Ss]rc',
        r'(open)?[Ss]ources?',
        r'[._-]open',
        r'[._-]archive',
        r'[._-]std',
        r'[._-]bin',
        r'Software',

        # Download version
        r'release',
        r'snapshot',
        r'distrib',
        r'everywhere',
        r'latest',

        # Arch
        r'Linux(64)?',
        r'x86_64',

        # VCS
        r'0\+bzr',

        # License
        r'gpl',

        # Needs to come before and after gpl, appears in both orders
        r'[._-]x11',
        r'gpl',
    ]

    for regex in suffix_regexes:
        # Remove the suffix from the end of the path
        # This may be done multiple times
        path = re.sub('[._-]?' + regex + '$', '', path)

    return path