from itertools import product
import os
import subprocess

import click

py_versions = ('2.7', '3.4')
npy_versions = ('1.9', '1.10')
zipline_path = os.path.join(
    os.path.dirname(__file__),
    '..',
    'conda',
    'zipline',
)


def mkargs(py_version, npy_version, output=False):
    return {
        'args': [
            'conda',
            'build',
            zipline_path,
            '-c', 'quantopian',
            '--python=%s' % py_version,
            '--numpy=%s' % npy_version,
        ] + (['--output'] if output else []),
        'stdout': subprocess.PIPE,
        'stderr': subprocess.PIPE,
    }


@click.command()
@click.option(
    '--upload',
    is_flag=True,
    default=False,
    help='Upload packages after building',
)
@click.option(
    '--upload-only',
    is_flag=True,
    default=False,
    help='Upload the last built packages without rebuilding.',
)
@click.option(
    '--user',
    default='quantopian',
    help='The anaconda account to upload to.',
)
def main(upload, upload_only, user):
    if upload_only:
        # if you are only uploading you shouldn't need to specify both flags
        upload = True
    procs = (
        (
            py_version,
            npy_version,
            (subprocess.Popen(**mkargs(py_version, npy_version))
             if not upload_only else
             None),
        )
        for py_version, npy_version in product(py_versions, npy_versions)
    )
    status = 0
    files = []
    for py_version, npy_version, proc in procs:
        if not upload_only:
            out, err = proc.communicate()
            if proc.returncode:
                status = 1
                print('build failure: python=%s numpy=%s\n%s' % (
                    py_version,
                    npy_version,
                    err.decode('utf-8'),
                ))
        elif upload:
            files.append(subprocess.Popen(
                **mkargs(py_version, npy_version, output=True)
            ).communicate()[0].decode('utf-8').strip())

    if not status and upload:
        for f in files:
            p = subprocess.Popen(
                ['anaconda', 'upload', '-u', user, f],
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL,
            )
            out, err = p.communicate()
            if p.returncode:
                print('failed to upload: %s\n%s' % (f, err.decode('utf-8')))
    return status


if __name__ == '__main__':
    exit(main())

@@ -889,32 +889,207 @@
 %0A@click.option(%0A
+    '--allow-partial-uploads',%0A    is_flag=True,%0A    default=False,%0A    help='Upload any packages that were built even if some of the builds'%0A    ' failed.',%0A)%0A@click.option(%0A
     '--user',%0A  
@@ -1190,16 +1190,39 @@
 ad_only,
+ allow_partial_uploads,
  user):%0A
@@ -2035,157 +2035,591 @@
     
-elif upload:%0A            files.append(subprocess.Popen(%0A                **mkargs(py_version, npy_version, output=True)%0A            ).communicate()%5B0%5D
+        # don't add the filename to the upload list if the build%0A                # fails%0A                continue%0A%0A        if upload:%0A            p = subprocess.Popen(%0A                **mkargs(py_version, npy_version, output=True)%0A            )%0A            out, err = p.communicate()%0A            if p.returncode:%0A                status = 1%0A                print(%0A                    'failed to get the output name for python=%25s numpy=%25s%5Cn'%0A                    '%25s' %25 (py_version, npy_version, err.decode('utf-8')),%0A                )%0A            else:%0A                files.append(out
 .dec
@@ -2644,24 +2644,25 @@
 ())%0A%0A    if 
+(
 not status a
@@ -2659,16 +2659,42 @@
 t status
+ or allow_partial_uploads)
  and upl
@@ -2971,32 +2971,193 @@
 f p.returncode:%0A
+                # only change the status to failure if we are not allowing%0A                # partial uploads%0A                status %7C= not allow_partial_uploads%0A
                 

allow partial uploads in conda_build_matrix