#!/usr/bin/env python
""" Download wheels from upstream URL, upload with twine

WARNING: this script deprecated, we will remove it soon.

Please use "wheel-uploader" script instead.
"""
from __future__ import print_function, absolute_import

import os
from os.path import join as pjoin, splitext, exists, expanduser
import sys
import re
from optparse import OptionParser, Option
if sys.version_info[0] >= 3:
    from urllib.request import urlopen, urlretrieve
else:
    from urllib import urlretrieve
    from urllib2 import urlopen
from subprocess import check_call, PIPE

# From `pip install beautifulsoup4`
from bs4 import BeautifulSoup

__version__ = '0.1'

RACKSPACE_URL='http://wheels.scikit-image.org'

WHEEL_RE = re.compile('^.*\.whl$')

def get_wheel_names(url):
    """ Get wheel names from HTML directory listing
    """
    html = urlopen(url).read()
    soup = BeautifulSoup(html)
    # This is almost certainly specific to the Rackspace directory
    cells = soup.findAll('td', attrs='colname', text=WHEEL_RE)
    if cells:
        return [cell.text for cell in cells]
    # Works for e.g. Apache directory listing, looking in contents of links
    cells = soup.findAll('a', text=WHEEL_RE)
    return [cell.text for cell in cells]


def main():
    parser = OptionParser(
        usage="%s WHEEL_ROOT\n\n" % sys.argv[0] + __doc__,
        version="%prog " + __version__)
    parser.add_option(
        Option("-u", "--wheel-url-dir", type='string', default=RACKSPACE_URL,
               help="URL for web directory containing wheels for uploading "
               "[default %default]"))
    parser.add_option(
        Option("-w", "--wheel-dir",
               action="store", type='string', default=os.getcwd(),
               help="Directory to store downloaded wheels [defaults to "
               "current working directory]"))
    parser.add_option(
        Option("-t", "--wheel-type",
               action="store", type='string', default="macosx",
               help="Platform type of wheels to download "
               "[default %default], one of 'macosx', 'win', 'linux'"))
    parser.add_option(
        Option("-c", "--clobber",
               action="store_true",
               help="Overwrite pre-existing wheels"))
    parser.add_option(
        Option("-n", "--no-twine",
               action="store_true",
               help="Do not upload wheels with twine"))
    parser.add_option(
        Option("-v", "--verbose",
               action="store_true",
               help="Give more feedback"))
    (opts, wheel_roots) = parser.parse_args()
    wheel_dir = expanduser(opts.wheel_dir)
    wheel_url_dir = opts.wheel_url_dir
    if not wheel_url_dir.endswith('/'):
        wheel_url_dir += '/'
    if len(wheel_roots) < 1:
        parser.print_help()
        sys.exit(1)
    try:
        check_call(['twine', '-h'], stdout=PIPE)
    except OSError:
        raise RuntimeError('Please install "twine" utility')
    wheel_names = get_wheel_names(wheel_url_dir)
    copied_wheels = []
    found_wheels = []
    for wheel_root in wheel_roots:
        for wheel_name in wheel_names:
            if not wheel_name.startswith(wheel_root):
                continue
            root, ext = splitext(wheel_name)
            project, version, pyv, pycv, plat = root.split('-')
            if not opts.wheel_type in plat:
                continue
            wheel_url = wheel_url_dir + wheel_name
            wheel_path = pjoin(wheel_dir, wheel_name)
            found_wheels.append(wheel_path)
            if exists(wheel_path) and not opts.clobber:
                if opts.verbose:
                    print('Not overwriting {0}'.format(wheel_path))
                continue
            if opts.verbose:
                print("Downloading {0} to {1}".format(wheel_url, wheel_path))
            urlretrieve(wheel_url, wheel_path)
            copied_wheels.append(wheel_path)
        if found_wheels:
            if opts.no_twine:
                if opts.verbose:
                    print("Found wheels but not uploading because of "
                          "--no-twine flag\n{0}".format(
                          '\n'.join(found_wheels)))
            else:
                check_call(['twine', 'upload'] + found_wheels)
        elif opts.verbose:
            print('Found no wheels at {0} for {1} and type {2}'.format(
                opts.wheel_url_dir, ', '.join(wheel_roots), opts.wheel_type))


if __name__ == '__main__':
    print("Please use 'wheel-uploader' instead of this script")
    main()
