#
# Archives, to the specified folder, the logged output generated by a benchmark
# run.
#
# @author A. Shawn Bandy
import os
import zipfile
import datetime
import requests
# Follows closely from:
# http://stackoverflow.com/a/34153816
#
# Paths to the log folders are generated by TFB and where those files
# should be archived.
#
path_in = os.path.abspath(os.path.normpath(os.path.expanduser(os.path.join( \
    os.environ['TFB_REPOPARENT'], os.environ['TFB_REPONAME'], \
    'results'))))
date_time = datetime.datetime.now()
dt_folder = date_time.strftime('%Y%m%d%H%M%S')
path_out = os.path.abspath(os.path.join(os.environ['TFB_LOGSFOLDER'], \
    dt_folder))

if not os.path.exists(path_out):
    os.makedirs(path_out)

zip_path = path_out + '/results.zip'

zip_file = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED)

for root, dirs, files in os.walk(path_in):
    for file in files:
        zip_file.write(os.path.join(root, file))

zip_file.close()

results_upload_uri = os.environ['TFB_UPLOADURI']

if results_upload_uri != None:
    with open(zip_path, 'rb') as file_to_upload:
        requests.post(
            results_upload_uri,
            headers={'Content-Type': 'application/zip'},
            data=file_to_upload)
