162306a36Sopenharmony_ci'''
262306a36Sopenharmony_cibuild ebpf program
362306a36Sopenharmony_ci'''
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciimport os
662306a36Sopenharmony_ciimport signal
762306a36Sopenharmony_cifrom string import Template
862306a36Sopenharmony_ciimport subprocess
962306a36Sopenharmony_ciimport time
1062306a36Sopenharmony_cifrom TdcPlugin import TdcPlugin
1162306a36Sopenharmony_cifrom tdc_config import *
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ciclass SubPlugin(TdcPlugin):
1462306a36Sopenharmony_ci    def __init__(self):
1562306a36Sopenharmony_ci        self.sub_class = 'buildebpf/SubPlugin'
1662306a36Sopenharmony_ci        self.tap = ''
1762306a36Sopenharmony_ci        super().__init__()
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci    def pre_suite(self, testcount, testidlist):
2062306a36Sopenharmony_ci        super().pre_suite(testcount, testidlist)
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci        if self.args.buildebpf:
2362306a36Sopenharmony_ci            self._ebpf_makeall()
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci    def post_suite(self, index):
2662306a36Sopenharmony_ci        super().post_suite(index)
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci        self._ebpf_makeclean()
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci    def add_args(self, parser):
3162306a36Sopenharmony_ci        super().add_args(parser)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci        self.argparser_group = self.argparser.add_argument_group(
3462306a36Sopenharmony_ci            'buildebpf',
3562306a36Sopenharmony_ci            'options for buildebpfPlugin')
3662306a36Sopenharmony_ci        self.argparser_group.add_argument(
3762306a36Sopenharmony_ci            '--nobuildebpf', action='store_false', default=True,
3862306a36Sopenharmony_ci            dest='buildebpf',
3962306a36Sopenharmony_ci            help='Don\'t build eBPF programs')
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci        return self.argparser
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci    def _ebpf_makeall(self):
4462306a36Sopenharmony_ci        if self.args.buildebpf:
4562306a36Sopenharmony_ci            self._make('all')
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci    def _ebpf_makeclean(self):
4862306a36Sopenharmony_ci        if self.args.buildebpf:
4962306a36Sopenharmony_ci            self._make('clean')
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci    def _make(self, target):
5262306a36Sopenharmony_ci        command = 'make -C {} {}'.format(self.args.NAMES['EBPFDIR'], target)
5362306a36Sopenharmony_ci        proc = subprocess.Popen(command,
5462306a36Sopenharmony_ci            shell=True,
5562306a36Sopenharmony_ci            stdout=subprocess.PIPE,
5662306a36Sopenharmony_ci            stderr=subprocess.PIPE,
5762306a36Sopenharmony_ci            env=os.environ.copy())
5862306a36Sopenharmony_ci        (rawout, serr) = proc.communicate()
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci        if proc.returncode != 0 and len(serr) > 0:
6162306a36Sopenharmony_ci            foutput = serr.decode("utf-8")
6262306a36Sopenharmony_ci        else:
6362306a36Sopenharmony_ci            foutput = rawout.decode("utf-8")
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci        proc.stdout.close()
6662306a36Sopenharmony_ci        proc.stderr.close()
6762306a36Sopenharmony_ci        return proc, foutput
68