Introduce an option for testing binary API
[one.git] / tests / data_plane / vpp_lite_topo / scripts / generate_config.py
1 #!/usr/bin/env python
2
3 """
4 generate_config.py - Generate specific configuration file for VPP from
5                      generic config file
6
7 Usage:
8   ./generate_config.py <directory> <output-file-type>
9
10 where <directory> is a system directory containing generic config file(s)
11     (suffixed with *.config)
12     <output-file-type> is one of 'vat' or 'cli'
13
14 This script looks for *.config files in provided directory and for each
15 generates a specific configuration file based on output file type in form
16 '<filename>.cli' or '<filename>.vat' respectively.
17 """
18
19 import sys
20 import glob
21 import cmd_mappings
22
23
24 def generate_config(file_name, mode):
25   """
26   param file_name:
27   param mode: one of 'vat' or 'cli'
28   """
29   s = ''
30   f = open(file_name, 'r')
31   line_num = 0
32
33   for line in f:
34     line_num += 1
35     line = line.strip()
36     if line == '' or line[0] == '#':
37       continue
38
39     kw = line[: line.index(' ')]
40     args = line[ line.index(' ') + 1:]
41
42     if kw not in cmd_mappings.mappings:
43       raise Exception('Conversion error at {}:{}:\n > {}\nKeyword not found:'
44               ' {}'.format(file_name, line_num, line, kw))
45
46     mapping = cmd_mappings.mappings[kw]
47     try:
48       s = s + mapping.generate(mode, args) + '\n'
49     except Exception as e:
50       raise Exception('Conversion error at {}:{}:\n > {}'
51               .format(file_name, line_num, line))
52
53   return s
54
55
56 def main():
57   if len(sys.argv) != 3:
58     print('Error: expected 2 arguments!')
59     sys.exit(1)
60
61   dir_name = sys.argv[1]
62   config_type = sys.argv[2]
63
64   if config_type != 'vat' and config_type != 'cli':
65     print('Error: expected second parameter one of "vat" or "cli"!')
66     sys.exit(1)
67
68   for f in glob.glob(dir_name + "/*.config"):
69     config = generate_config(f, config_type)
70
71     output_fname = f.replace('.config', '.' + config_type)
72     print('\n* Generated config from {}:'.format(f))
73     print(config)
74     print ('* Saving to {}'.format(output_fname))
75
76     fout = open(output_fname, 'w')
77     fout.write(config);
78
79
80 main()