c871585915acb30faff5bd59ecb2754307d56a4f
[bottlenecks.git] / utils / parser.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 '''This file realize the function of how to parser a config file.
11 This contain Two part:
12 Frist is Init some variables the will be used.
13 Second is reading config file.'''
14
15 import os
16 import yaml
17
18
19 class Parser():
20
21     bottlenecks_config = {}
22     bottlenecks_test = {}
23
24     @classmethod
25     def config_init(cls):
26         cls.code_dir = os.path.dirname(os.path.abspath(__file__))
27         cls.root_dir = os.path.dirname(cls.code_dir)
28         cls.test_dir = os.path.join(cls.root_dir, 'testsuites')
29         config_dir = os.path.join(
30             cls.root_dir,
31             'config',
32             'config.yaml')
33
34         with open(config_dir) as file:
35             config_info = yaml.load(file)
36             common_config = config_info['common_config']
37             cls.bottlenecks_config["releng_dir"] = common_config["releng_dir"]
38             cls.bottlenecks_config["fetch_os"] = common_config["fetch_os_file"]
39             cls.bottlenecks_config["log_dir"] = common_config['log_dir']
40             cls.bottlenecks_config["rc_dir"] = common_config['rc_dir']
41             cls.config_dir_check(cls.bottlenecks_config["log_dir"])
42
43     @classmethod
44     def story_read(cls, testcase, story_name):
45         story_dir = os.path.join(
46             cls.test_dir,
47             testcase,
48             'testsuite_story',
49             story_name)
50         with open(story_dir) as file:
51             story_parser = yaml.load(file)
52         for case_name in story_parser['testcase']:
53             Parser.testcase_read(cls, testcase, case_name)
54
55         return cls.bottlenecks_test
56
57     @classmethod
58     def testcase_read(cls, testcase, testcase_name):
59
60         testcase_dir = os.path.join(
61             cls.test_dir,
62             testcase,
63             'testcase_cfg',
64             testcase_name)
65         testcase_local = testcase_dir + ".yaml"
66         with open(testcase_local) as f:
67             cls.bottlenecks_test[testcase_name] = yaml.load(f)
68
69         return cls.bottlenecks_test
70
71     @classmethod
72     def config_dir_check(cls, dirname):
73         if dirname is None:
74             dirname = '/tmp/'
75         if not os.path.exists(dirname):
76             os.makedirs(dirname)
77
78     @staticmethod
79     def config_parser(testcase_cfg, parameters):
80         test_cfg = testcase_cfg['test_config']
81         stack_cfg = testcase_cfg['stack_config']
82         # TO-DO add cli parameters to stack_config.
83         return test_cfg, stack_cfg
84