1#!/usr/bin/env python3
2# vim: set syntax=python ts=4 :
3# Copyright (c) 2020 Intel Corporation
4# SPDX-License-Identifier: Apache-2.0
5
6import json
7import os
8import tarfile
9
10from twisterlib.statuses import TwisterStatus
11
12
13class Artifacts:
14
15    def __init__(self, env):
16        self.options = env.options
17
18    def make_tarfile(self, output_filename, source_dirs):
19        root = os.path.basename(self.options.outdir)
20        with tarfile.open(output_filename, "w:bz2") as tar:
21            tar.add(self.options.outdir, recursive=False)
22            for d in source_dirs:
23                f = os.path.relpath(d, self.options.outdir)
24                tar.add(d, arcname=os.path.join(root, f))
25
26    def package(self):
27        dirs = []
28        with open(os.path.join(self.options.outdir, "twister.json")) as json_test_plan:
29            jtp = json.load(json_test_plan)
30            for t in jtp['testsuites']:
31                if t['status'] != TwisterStatus.FILTER:
32                    p = t['platform']
33                    normalized  = p.replace("/", "_")
34                    dirs.append(os.path.join(self.options.outdir, normalized, t['name']))
35
36        dirs.extend(
37            [
38                os.path.join(self.options.outdir, "twister.json"),
39                os.path.join(self.options.outdir, "testplan.json")
40                ]
41            )
42        self.make_tarfile(self.options.package_artifacts, dirs)
43