From 23fb36cd8aae52ce3550d60fc35768c1de72bab2 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Mon, 13 Jan 2020 23:18:37 -0700 Subject: [PATCH] Added upload-logs script --- scripts/upload-logs | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100755 scripts/upload-logs diff --git a/scripts/upload-logs b/scripts/upload-logs new file mode 100755 index 00000000..aea4d318 --- /dev/null +++ b/scripts/upload-logs @@ -0,0 +1,90 @@ +#!/usr/bin/env python +# vim: sts=2 sw=2 ts=2 +"""Wizard Kit: Upload Logs""" + +import datetime +import os +import pathlib + +import pytz +import requests + +import wk + + +# STATIC VARIABLES +LOG_DIR = pathlib.Path('~/Logs').expanduser().resolve() +PLATFORM = wk.std.PLATFORM.replace('Darwin', 'macOS') +TIMEZONE = pytz.timezone(wk.cfg.main.LINUX_TIME_ZONE) +NOW = datetime.datetime.now(tz=TIMEZONE) + + +# Safety check +if PLATFORM not in ('macOS', 'Linux'): + raise OSError(f'This script is not supported under {PLATFORM}') + + +# Functions +def main(): + """Upload logs for review.""" + lines = [] + try_and_print = wk.std.TryAndPrint() + + # Set log + wk.log.update_log_path(dest_name='Upload-Logs', timestamp=True) + + # Instructions + wk.std.print_success(f'{wk.cfg.main.KIT_NAME_FULL}: Upload Logs') + wk.std.print_standard('') + wk.std.print_standard('Please state the reason for the review.') + wk.std.print_info(' End note with an empty line.') + wk.std.print_standard('') + + # Get reason note + while True: + text = wk.std.input_text('> ') + if not text: + lines.append('') + break + lines.append(text) + with open(f'{LOG_DIR}/__reason__.txt', 'a') as _f: + _f.write('\n'.join(lines)) + + # Compress and upload logs + result = try_and_print.run( + message='Uploading logs...', + function=upload_log_dir, + reason='Review', + ) + if not result['Failed']: + raise SystemExit(1) + + +def upload_log_dir(reason='Testing'): + """Upload compressed log_dir to the crash server.""" + server = wk.cfg.net.CRASH_SERVER + dest = pathlib.Path(f'~/{reason}_{NOW.strftime("%Y-%m-%dT%H%M%S%z")}.txz') + dest = dest.expanduser().resolve() + data = None + + # Compress LOG_DIR (relative to parent dir) + os.chdir(LOG_DIR.parent) + cmd = ['tar', 'caf', dest.name, LOG_DIR.name] + proc = wk.exe.run_program(cmd, check=False) + + # Upload compressed data + url = f'{server["Url"]}/{dest.name}' + result = requests.put( + url, + data=dest.read_bytes(), + headers=server['Headers'], + auth=(server['User'], server['Pass']), + ) + + # Check result + if not result.ok: + raise wk.std.GenericError('Failed to upload logs') + + +if __name__ == '__main__': + main()