am aa771b2c: am 92582aed: am e641e248: Use gcc for mips target.
[platform/upstream/VK-GL-CTS.git] / scripts / khr_util / format.py
1 # -*- coding: utf-8 -*-
2
3 from itertools import chain
4
5 INL_HEADER_TMPL = """\
6 /* WARNING: This is auto-generated file. Do not modify, since changes will
7  * be lost! Modify the generating script instead.
8  *
9  * Generated from {registryName} revision {revision}.
10  */\
11 """
12
13 def genInlHeader (registryName, revision):
14         return INL_HEADER_TMPL.format(
15                 registryName    = registryName,
16                 revision                = str(revision))
17
18 def genInlHeaderForSource (registrySource):
19         return genInlHeaderForSource(registrySource.getFilename(), registrySource.getRevision())
20
21 def nextMod (val, mod):
22         if val % mod == 0:
23                 return val + mod
24         else:
25                 return int(val/mod)*mod + mod
26
27 def indentLines (lines):
28         tabSize = 4
29
30         # Split into columns
31         lineColumns = [line.split("\t") for line in lines if line is not None]
32         if len(lineColumns) == 0:
33                 return
34
35         numColumns = max(len(line) for line in lineColumns)
36
37         # Figure out max length per column
38         columnLengths = [nextMod(max(len(line[ndx]) for line in lineColumns if len(line) > ndx), tabSize) for ndx in range(numColumns)]
39
40         for line in lineColumns:
41                 indented = []
42                 for columnNdx, col in enumerate(line[:-1]):
43                         colLen  = len(col)
44                         while colLen < columnLengths[columnNdx]:
45                                 col             += "\t"
46                                 colLen   = nextMod(colLen, tabSize)
47                         indented.append(col)
48
49                 # Append last col
50                 indented.append(line[-1])
51                 yield "".join(indented)
52
53 def writeLines (filename, lines):
54         with open(filename, 'wb') as f:
55                 for line in lines:
56                         if line is not None:
57                                 f.write(line)
58                                 f.write('\n')
59         print filename
60
61 def writeInlFile (filename, header, source):
62         writeLines(filename, chain([header], source))