def is_pypi_listed(pkg):
    if not os.path.exists(options.target_build_dir):
        mkdir(options.target_build_dir)
    listing = os.path.join(options.target_build_dir, '.' + pkg + '_list_test')
    try:
        urlretrieve(pypi_url(pkg, False), listing)
        return True
    except (DownloadError, URLError, HTTPError, ContentTooShortError):
        return False
def available_versions(what, website, pattern, archives=False):
    pre = pattern.split('*')[0]
    post = pattern.split('*')[-1]
    ex = None
    try:
        listing = os.path.join(options.target_build_dir, '.' + what + '_list')
        urlretrieve(website + '/', listing)
        versions = []
        f = open(listing, 'r')
        contents = f.read()
        f.close()
        idx = contents.find(pre, 0)
        l = len(pre)
        while idx >= 0:
            endl = contents.find('\n', idx)
            end = contents.find(post, idx)
            if archives:
                end_t = contents.find('.tar', idx, endl)
                end_z = contents.find('.zip', idx, endl)
                if end_t > 0 and end_z > 0:
                    end = min(end_t, end_z)
                else:
                    end = max(end_t, end_z)
            if end > 0 and end < endl:
                versions.append(contents[idx+l:end])
            idx = contents.find(pre, end)
        return versions
    except (DownloadError, URLError, HTTPError, ContentTooShortError) as e:
        ex = e

    ## Default to whatever is in the third_party directory
    file_list = glob.glob(os.path.join(options.download_dir, pattern))
    if len(file_list) > 0:
        pre = os.path.join(options.download_dir, pre)
        version_list = []
        for f in file_list:
            for archive in archive_types:
                if archive in f:
                    version_list.append(f[len(pre):-(len(post) + len(archive))])
                    break
        return version_list
    if ex:
        raise ex
    raise DownloadError('No PyPi version of ' + which + ' available.')
def pypi_archive(which, version):
    try:
        all_versions = available_versions(which, pypi_url(which),
                                          which + '-*', True)
        if version is None or not version in all_versions:
            print('Warning: version ' + str(version) + ' of ' + which +
                  ' is not available.')
            version = earliest_pypi_version(which)
    except:
        raise DownloadError('No PyPi version of ' + which + ' available.')
    if version is None:
        raise DownloadError('No PyPi version of ' + which + ' available.')
    ex = None
    try:
        if not os.path.exists(options.target_build_dir):
            mkdir(options.target_build_dir)
        listing = os.path.join(options.target_build_dir, '.' + which + '_list')
        if not os.path.exists(listing):
            urlretrieve(pypi_url(which), listing)
        f = open(listing, 'r')
        contents = f.read()
        f.close()
        l = len(which + '-' + version)
        idx = contents.find(which + '-' + version)
        while idx >= 0:
            for archive in archive_types:
                if contents[idx+l:].startswith(archive):
                    return archive
            idx = contents.find(which + '-' + version, idx+1)
        raise DownloadError('Invalid PyPi page for ' + which + '.')
    except (DownloadError, URLError, HTTPError, ContentTooShortError) as e:
        ex = e

    ## Default to whatever is in the third_party directory
    file_list = glob.glob(os.path.join(options.download_dir, which + '*'))
    for f in file_list:
        for archive in archive_types:
            if archive in f:
                return archive
    if ex:
        raise ex
    raise DownloadError('No PyPi version of ' + which + ' available.')