def upload_assets(user, secret, bucket, _retries=5):
    access_key = user if user else os.environ['AWS_ACCESS_KEY_ID']
    access_secret = secret if secret else os.environ['AWS_SECRET_ACCESS_KEY']
    bucket = bucket if bucket else os.environ['S3_BUCKET_NAME']

    retries = 0

    import subprocess
    # build assets and capture the output
    print 'Building assets...'
    proc = subprocess.Popen(
        ['python', 'manage.py', 'assets', 'build'],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT
    )
    proc.wait()

    print 'Connecting to S3...'
    conn, bucket = connect_to_s3(access_key, access_secret, bucket)
    print 'Uploading files...'

    for path in proc.communicate()[0].split('\n')[:-1]:
        key = path.split('public')[1]
        print 'Uploading {}'.format(key)
        while retries <= _retries:
            try:
                upload_file(key, bucket, root=current_app.config['APP_DIR'] + '/static')
                break
            except Exception, e:
                print 'Error: {}'.format(e), 'Retrying...'
                retries += 1
        if retries > _retries:
            print 'File {} did not upload'.format(key)
        retries = 0
def upload_assets(user, secret, bucket, _retries=5):
    access_key = user if user else os.environ["AWS_ACCESS_KEY_ID"]
    access_secret = secret if secret else os.environ["AWS_SECRET_ACCESS_KEY"]
    bucket = bucket if bucket else os.environ["S3_BUCKET_NAME"]

    retries = 0

    import subprocess

    # build assets and capture the output
    print "Building assets..."
    proc = subprocess.Popen(
        ["python", "manage.py", "assets", "build"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT
    )
    proc.wait()

    print "Connecting to S3..."
    conn, bucket = connect_to_s3(access_key, access_secret, bucket)
    print "Uploading files..."

    for path in proc.communicate()[0].split("\n")[:-1]:
        key = path.split("public")[1]
        print "Uploading {}".format(key)
        while retries <= _retries:
            try:
                upload_file(key, bucket, root=current_app.config["APP_DIR"] + "/static")
                break
            except Exception, e:
                print "Error: {}".format(e), "Retrying..."
                retries += 1
        if retries > _retries:
            print "File {} did not upload".format(key)
        retries = 0
def upload_costars_contract(_file):
    '''Upload a COSTARS pdf document to S3

    Arguments:
        _file: A werkzeug `FileStorage`_ object

    Returns:
        A two-tuple of (the name of the uploaded file, the path/url to the file)
    '''
    filename = secure_filename(_file.filename)

    if current_app.config['UPLOAD_S3']:
        try:
            turn_off_sqlalchemy_events()
            conn, bucket = connect_to_s3(
                current_app.config['AWS_ACCESS_KEY_ID'],
                current_app.config['AWS_SECRET_ACCESS_KEY'], 'costars')

            file_href = upload_file(filename,
                                    bucket,
                                    input_file=_file,
                                    prefix='/',
                                    from_file=True)
            return filename, file_href
        except Exception:
            raise
        finally:
            turn_on_sqlalchemy_events()
            refresh_search_view()

    else:
        try:
            os.mkdir(current_app.config['UPLOAD_DESTINATION'])
        except:
            pass

        filepath = os.path.join(current_app.config['UPLOAD_DESTINATION'],
                                filename)
        _file.save(filepath)
        return filename, filepath
def upload_costars_contract(_file):
    filename = secure_filename(_file.filename)

    if current_app.config['UPLOAD_S3']:
        conn, bucket = connect_to_s3(
            current_app.config['AWS_ACCESS_KEY_ID'],
            current_app.config['AWS_SECRET_ACCESS_KEY'],
            'costars'
        )

        file_href = upload_file(filename, bucket, input_file=_file, prefix='/', from_file=True)
        return filename, file_href

    else:
        try:
            os.mkdir(current_app.config['UPLOAD_DESTINATION'])
        except:
            pass

        filepath = os.path.join(current_app.config['UPLOAD_DESTINATION'], filename)
        _file.save(filepath)
        return filename, filepath
def upload_costars_contract(_file):
    '''Upload a COSTARS pdf document to S3

    Arguments:
        _file: A werkzeug `FileStorage`_ object

    Returns:
        A two-tuple of (the name of the uploaded file, the path/url to the file)
    '''
    filename = secure_filename(_file.filename)

    if current_app.config['UPLOAD_S3']:
        try:
            turn_off_sqlalchemy_events()
            conn, bucket = connect_to_s3(
                current_app.config['AWS_ACCESS_KEY_ID'],
                current_app.config['AWS_SECRET_ACCESS_KEY'],
                'costars'
            )

            file_href = upload_file(filename, bucket, input_file=_file, prefix='/', from_file=True)
            return filename, file_href
        except Exception:
            raise
        finally:
            turn_on_sqlalchemy_events()
            refresh_search_view()

    else:
        try:
            os.mkdir(current_app.config['UPLOAD_DESTINATION'])
        except:
            pass

        filepath = os.path.join(current_app.config['UPLOAD_DESTINATION'], filename)
        _file.save(filepath)
        return filename, filepath
            try:
                upload_file(key, bucket, root=current_app.config['APP_DIR'] + '/static')
                break
            except Exception, e:
                print 'Error: {}'.format(e), 'Retrying...'
                retries += 1
        if retries > _retries:
            print 'File {} did not upload'.format(key)
        retries = 0

    for fontfile in os.listdir(os.path.join(current_app.config['APP_DIR'], 'static', 'fonts')):
        print 'Uploading {}'.format(fontfile)
        while retries <= _retries:
            try:
                upload_file(
                    fontfile, bucket, root=current_app.config['APP_DIR'] + '/static/fonts',
                    prefix='/static/fonts'
                )
                break
            except Exception, e:
                print 'Error: {}'.format(e), 'Retrying...'
                retries += 1
        if retries > _retries:
            print 'File {} did not upload'.format(fontfile)
        retries = 0

    print 'Uploading images...'
    for root, _, files in os.walk(current_app.config['APP_DIR'] + '/static/img'):
        for filepath in files:
            print 'Uploading {}'.format(filepath)
            while retries <= _retries:
                try:
                upload_file(key, bucket, root=current_app.config["APP_DIR"] + "/static")
                break
            except Exception, e:
                print "Error: {}".format(e), "Retrying..."
                retries += 1
        if retries > _retries:
            print "File {} did not upload".format(key)
        retries = 0

    print "Uploading images..."
    for root, _, files in os.walk(current_app.config["APP_DIR"] + "/static/img"):
        for filepath in files:
            print "Uploading {}".format(filepath)
            while retries <= _retries:
                try:
                    upload_file(filepath, bucket, root=root, prefix="/static/img/")
                    break
                except Exception, e:
                    print "Error: {}".format(e), "Retrying..."
                    retries += 1
            retries = 0

    return


@manager.command
def all_clear():
    status = AppStatus.query.first()
    status.status = "ok"
    status.last_updated = datetime.datetime.now()
    status.message = None
            try:
                upload_file(key, bucket, root=current_app.config["APP_DIR"] + "/static")
                break
            except Exception, e:
                print "Error: {}".format(e), "Retrying..."
                retries += 1
        if retries > _retries:
            print "File {} did not upload".format(key)
        retries = 0

    for fontfile in os.listdir(os.path.join(current_app.config["APP_DIR"], "static", "fonts")):
        print "Uploading {}".format(fontfile)
        while retries <= _retries:
            try:
                upload_file(
                    fontfile, bucket, root=current_app.config["APP_DIR"] + "/static/fonts", prefix="/static/fonts"
                )
                break
            except Exception, e:
                print "Error: {}".format(e), "Retrying..."
                retries += 1
        if retries > _retries:
            print "File {} did not upload".format(fontfile)
        retries = 0

    print "Uploading images..."
    for root, _, files in os.walk(current_app.config["APP_DIR"] + "/static/img"):
        for filepath in files:
            print "Uploading {}".format(filepath)
            while retries <= _retries:
                try: