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