#!/usr/bin/env python3

import base64
import os
import requests
import sys
import warnings

if len(sys.argv) != 3:
    print('USAGE: {} JOB-ID XZ-FILE', sys.argv[0])
    print('Upload an xz-compressed file to our Google Drive stash')
    sys.exit(1)

tarball_path = sys.argv[2]

# upload to google drive
app_url = os.environ.get('GDRIVE_LOG_POST_URL')

if not app_url:
    print('Missing application url. Please set GDRIVE_LOG_POST_URL in the environment.')
    sys.exit(1)

with open(tarball_path, 'rb') as tarball:
    post_data = {
        'job_id': sys.argv[1],
        'tarball': base64.b64encode(tarball.read())
    }

with warnings.catch_warnings():
    warnings.simplefilter('ignore')
    response = requests.post(app_url, data=post_data, timeout=10)

print()
print('==============================')
print('== UPLOAD RESPONSE:')
print('==============================')
print(response.text)
print('==============================')
print()

if not response.text.strip().endswith('successfully'):
    print('UPLOAD FAILED!')
    sys.exit(1)
