From 24b79a9e4d6681ed57ff3fc62a1acd19c148020d Mon Sep 17 00:00:00 2001 From: Anders Roxell Date: Wed, 13 Dec 2023 12:22:44 +0100 Subject: [PATCH] add squad-download-attachments Signed-off-by: Anders Roxell --- squad-download-attachments | 108 +++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100755 squad-download-attachments diff --git a/squad-download-attachments b/squad-download-attachments new file mode 100755 index 0000000..a72402d --- /dev/null +++ b/squad-download-attachments @@ -0,0 +1,108 @@ +#!/usr/bin/env python3 + +import argparse +import json +import logging +import os +import sys +from squad_client.core.api import SquadApi +from squad_client.core.models import ALL, Squad, TestRun, Environment +from squad_client.utils import getid +from squad_client.shortcuts import download_attachments +import contextlib +import lzma +import tarfile +import glob + +SquadApi.configure(cache=3600, url="https://qa-reports.linaro.org/") + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + + +def arg_parser(): + parser = argparse.ArgumentParser(description="download attachments in a testrun") + + parser.add_argument( + "--group", + required=True, + help="squad group", + ) + + parser.add_argument( + "--project", + required=True, + help="squad project", + ) + + parser.add_argument( + "--build", + required=True, + help="squad build", + ) + + return parser + + +def run(): + args = arg_parser().parse_args() + + group = Squad().group(args.group) + if group is None: + logger.error(f"Get group failed. Group not found: '{args.group}'.") + return -1 + + project = group.project(args.project) + if project is None: + logger.error(f"Get project failed. Project not found: '{args.project}'.") + return -1 + + build = project.build(args.build) + if build is None: + logger.error(f"Get build failed. Build not found: '{args.build}'.") + return -1 + + environments = project.environments(count=ALL, ordering="slug").values() + if not environments: + logger.error("Get environments failed. No environments found.") + return -1 + + suites = project.suites(count=ALL, ordering="slug").values() + if not suites: + logger.error("Get suites failed. No suites found.") + return -1 + + attachment_dir = 'stored_attachments' + if not os.path.exists(attachment_dir): + os.makedirs(attachment_dir) + print(f"Created dir: {attachment_dir}") + os.chdir(attachment_dir) + testruns = build.testruns() + for testrun in testruns: + if not TestRun(testrun).attachments: + continue + env_name = Environment(getid((TestRun(testrun).environment))).slug + dirname = env_name+"_"+str(TestRun(testrun).id) + if "qemu-" in dirname: + if not os.path.exists(dirname): + os.makedirs(dirname) + print(f"Created dir: {dirname}") + os.chdir(dirname) + download_attachments(TestRun(testrun)) + os.chdir('..') + + file = glob.glob(f"{dirname}/mmtests-*.tar.xz") + with contextlib.closing(lzma.LZMAFile(file[0])) as xz: + with tarfile.open(fileobj=xz) as f: + f.extractall(dirname) + + file = glob.glob(f"{dirname}/output/*.json") + file_write = file[0].replace("/output", "") + with open(file[0], mode="r") as read_file: + pjson = json.dumps(json.load(read_file), sort_keys=True, indent=4) + with open(file_write, mode="w") as write_file: + write_file.write(pjson) + + +if __name__ == "__main__": + sys.exit(run())