RM-autogen.py: Add support for sorting the entries
[glsdk/host-tools.git] / respart / RM-autogen.py
1 #!/usr/bin/python
2 #
3 # Author: Nikhil Devshatwar <nikhil.nd@ti.com>
4 # Python script to auto generate the Resource Management data
5 # Parses excel sheet and generates different files which need
6 # to be in sync for correct functionality
7 #
8 import argparse
9 import xlrd
10 import xlwt
11 import re
13 COL_COMMENT = 0
14 COL_RES_TYPE = 1
15 COL_SUB_TYPE = 2
16 COL_RES_FIELD = 3
17 COL_RES_START = 6
18 COL_HOST_START = 7
20 ROW_HOST_ID = 0
21 ROW_RES_START = 2
23 comments = {}
25 def gen_rmcfg_data(sharing):
26         global comments
27         rmcfg = []
28         for res in range(ROW_RES_START, sheet.nrows):
30                 comment = sheet.cell_value(res, COL_COMMENT)
31                 restype = sheet.cell_value(res, COL_RES_TYPE)
32                 subtype = sheet.cell_value(res, COL_SUB_TYPE)
33                 start = sheet.cell_value(res, COL_RES_START)
34                 if (restype == '' or subtype == '' or start == ''):
35                         continue
36                 start = int(start)
37                 comments[(restype, subtype)] = comment
39                 for host in range(COL_HOST_START, sheet.ncols):
41                         #print ("##v(%d, %d) = '%s'" % (res, host, sheet.cell_value(res, host)))
42                         host_id = sheet.cell_value(ROW_HOST_ID, host).split('\n')[0]
43                         if (re.match("HOST_ID_.*", host_id) == None):
44                                 continue
46                         num = sheet.cell_value(res, host)
47                         if (num == '' or int(num) == 0):
48                                 continue
49                         num = int(num)
51                         rmcfg.append((start, num, restype, subtype, host_id))
53                         for pair in sharing:
54                                 if (host_id != pair[0]):
55                                         continue
57                                 shared_host = pair[1]
58                                 rmcfg.append((start, num, restype, subtype, shared_host))
61                         start += int(num)
62         return rmcfg
64 def print_rmcfg(rmcfg):
65         comment_templ = '''
66                 /* %s */\n'''
67         rmconfig_templ = '''\
68                 {
69                         .start_resource = %d,
70                         .num_resource = %d,
71                         .type = RESASG_UTYPE (%s,
72                                         %s),
73                         .host_id = %s,
74                 },\n'''
75         output = ""
77         def custom_key(entry):
78                 (start, num, restype, subtype, host) = entry
79                 restype = soc.const_values[restype]
80                 subtype = soc.const_values[subtype]
81                 host = soc.const_values[host]
82                 utype = (restype << soc.RESASG_TYPE_SHIFT) | (subtype << soc.RESASG_SUBTYPE_SHIFT)
83                 val = (utype << 24) | (start << 8) | (host << 0)
84                 return val
86         sorted_rmcfg = sorted(rmcfg, key=custom_key)
88         comment = None
89         for entry in sorted_rmcfg:
90                 (start, num, restype, subtype, host) = entry
92                 if (comment != comments[(restype, subtype)]):
93                         comment = comments[(restype, subtype)]
94                         output += comment_templ % comment
95                 output += rmconfig_templ % (start, num, restype, subtype, host)
96         return output
98 ################################################################################
99 ##                          Main program starts here                          ##
100 ################################################################################
102 parser = argparse.ArgumentParser(prog='RM-autogen.py', formatter_class=argparse.RawTextHelpFormatter,
103         description='RM-autogen.py - Auto generate the Resource Management data')
105 parser.add_argument('-f', '--format', required=True, dest='format',
106         action='store', choices=["boardconfig", "rtos_rmcfg", "jailhouse_cell_config"],
107         help='format to select the output file')
109 parser.add_argument('-o', '--output', required=True, dest='output',
110         action='store',
111         help='output file name')
113 parser.add_argument('-s', '--share', dest='share', default=[],
114         action='append', nargs=2, metavar=('HOST_ID_A', 'HOST_ID_B'),
115         help='Share resource with HOST_ID_A for HOST_ID_B')
117 parser.add_argument('workbook', help='Input excel sheet with assigned resources')
119 args = parser.parse_args()
120 print(args)
122 workbook = xlrd.open_workbook(args.workbook)
123 sheet = workbook.sheet_by_index(0)
125 #sheet.nrows = 9
126 if (args.format == 'boardconfig'):
127         boardconfig = gen_rmcfg_data(args.share)
128         print ("Total entries = %d" % len(boardconfig))
129         data = print_rmcfg(boardconfig)
130 else:
131         print ("ERROR: format %s not supported")
132         exit(1)
135 ofile = open(args.output, "w")
136 ofile.write(data)
137 ofile.close()
139 # END OF FILE