• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2021-2024 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import os
19import shutil
20import logging
21from pathlib import Path
22from typing import Set, List
23
24from runner.logger import Log
25from runner.plugins.ets.utils.exceptions import InvalidFileFormatException, InvalidFileStructureException, \
26    UnknownTemplateException
27from runner.plugins.ets.ets_templates.benchmark import Benchmark, TEMPLATE_EXTENSION
28
29_LOGGER = logging.getLogger("runner.plugins.ets.ets_templates.ets_templates_generator")
30
31
32class EtsTemplatesGenerator:
33    def __init__(self, root_path: Path, gen_path: Path) -> None:
34        self.__root_path = root_path
35        self.__output_path = gen_path
36        self.generated_tests: List[str] = []
37
38        if not self.__root_path.is_dir():
39            Log.exception_and_raise(_LOGGER, f"{str(self.__root_path.absolute())} must be a directory")
40
41    def dfs(self, path: Path, seen: Set) -> None:
42        if not path.exists() or path in seen:
43            return
44        seen.add(path)
45
46        if path.is_dir():
47            for i in sorted(path.iterdir()):
48                self.dfs(i, seen)
49        elif path.suffix == TEMPLATE_EXTENSION:
50            self.__generate_test(path)
51
52    def generate(self) -> List[str]:
53        Log.all(_LOGGER, "Starting generate test")
54        if self.__output_path.exists():
55            shutil.rmtree(self.__output_path)
56        try:
57            self.dfs(self.__root_path, set())
58        except InvalidFileFormatException as inv_format_exp:
59            Log.exception_and_raise(_LOGGER, inv_format_exp.message)
60        except InvalidFileStructureException as inv_fs_exp:
61            Log.exception_and_raise(_LOGGER, inv_fs_exp.message)
62        except UnknownTemplateException as unknown_template_exp:
63            Log.default(_LOGGER, f"\t {repr(unknown_template_exp.exception)}")
64            Log.exception_and_raise(_LOGGER, f"{unknown_template_exp.filepath}: exception while processing template:")
65        Log.all(_LOGGER, "Generation finished!")
66        return self.generated_tests
67
68    def __generate_test(self, path: Path) -> None:
69        test_full_name = os.path.relpath(path, self.__root_path)
70        output = self.__output_path / test_full_name
71        bench = Benchmark(path, output, test_full_name)
72        self.generated_tests.extend(bench.generate())
73