'Deduce the version number of the downloaded package from its filename.'

def _version(self):

    def version_of_archive(filename, package_name):
        for ext in ARCHIVE_EXTENSIONS:
            if filename.endswith(ext):
                filename = filename[:(- len(ext))]
                break
        if is_git_sha(filename):
            filename = ((package_name + '-') + filename)
        if (not filename.lower().replace('_', '-').startswith(package_name.lower())):
            give_up(filename, package_name)
        return filename[(len(package_name) + 1):]
    def version_of_wheel(filename, package_name):
        (whl_package_name, version, _rest) = filename.split('-', 2)
        our_package_name = re.sub('[^\\w\\d.]+', '_', package_name, re.UNICODE)
        if (whl_package_name != our_package_name):
            give_up(filename, whl_package_name)
        return version
    def give_up(filename, package_name):
        raise RuntimeError(("The    archive    '%s'    didn't    start    with    the    package    name    '%s',    so    I    couldn't    figure    out    the    version    number.    My    bad;    improve    me." % (filename, package_name)))
    get_version = (version_of_wheel if self._downloaded_filename().endswith('.whl') else version_of_archive)
    return get_version(self._downloaded_filename(), self._project_name())
