1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2021 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19import os
20import sys
21import subprocess
22from PyInquirer import prompt
23
24
25def enable_option(file_name):
26    option_list = []
27    try:
28        with open('.config', 'r') as config_file:
29            for line in config_file:
30                if line.startswith('CONFIG_'):
31                    str1 = line.split('=')
32                    option_list.append(str1[0][7:])
33    except IOError:
34        print('No config file')
35        return
36
37    if os.path.exists('.config'):
38        os.remove('.config')
39
40    file_data = ''
41    with open(file_name, 'r') as gni_file:
42        for line in gni_file:
43            if '=' in line:
44                str1 = line.split('=')
45                if str1[0].strip() in option_list:
46                    line = str1[0] + '= true\n'
47                else:
48                    line = str1[0] + '= false\n'
49            file_data += line
50
51    flags = os.O_WRONLY | os.O_CREAT
52    modes = stat.S_IWUSR | stat.S_IRUSR
53    with os.fdopen(os.open(file_name, flags, modes), 'w')as gni_file
54        gni_file.write(file_data)
55
56
57def ask_option():
58    options_prompt = {
59        'type': 'list',
60        'name': 'option',
61        'message': 'Which platform do you want to config?',
62        'default': 'standard',
63        'choices': ['standard', 'small', 'mini']
64    }
65    answers = prompt(options_prompt)
66    return answers['option']
67
68
69def main():
70    print('##### Welcome to Dsoftbus #####')
71    option = ask_option()
72    subprocess.call(['menuconfig'])
73    file_gni = './adapter/default_config/feature_config/platform/config.gni'
74    if (option == 'standard'):
75        file_gni = file_gni.replace('platform', 'standard')
76    elif (option == 'small'):
77        file_gni = file_gni.replace('platform', 'small')
78    else:
79        file_gni = file_gni.replace('platform', 'mini')
80    enable_option(file_gni)
81
82
83if __name__ == '__main__':
84    sys.exit(main())