1 # -*- coding: utf-8 -*-
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
7 # Copyright 2015 The Android Open Source Project
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
21 #-------------------------------------------------------------------------
24 from itertools import chain
26 INL_HEADER_TMPL = """\
27 /* WARNING: This is auto-generated file. Do not modify, since changes will
28 * be lost! Modify the generating script instead.
30 * Generated from {registryName} revision {revision}.
34 def genInlHeader (registryName, revision):
35 return INL_HEADER_TMPL.format(
36 registryName = registryName,
37 revision = str(revision))
39 def genInlHeaderForSource (registrySource):
40 return genInlHeaderForSource(registrySource.getFilename(), registrySource.getRevision())
42 def nextMod (val, mod):
46 return int(val/mod)*mod + mod
48 def indentLines (lines):
52 lineColumns = [line.split("\t") for line in lines if line is not None]
53 if len(lineColumns) == 0:
56 numColumns = max(len(line) for line in lineColumns)
58 # Figure out max length per column
59 columnLengths = [nextMod(max(len(line[ndx]) for line in lineColumns if len(line) > ndx), tabSize) for ndx in range(numColumns)]
61 for line in lineColumns:
63 for columnNdx, col in enumerate(line[:-1]):
65 while colLen < columnLengths[columnNdx]:
67 colLen = nextMod(colLen, tabSize)
71 indented.append(line[-1])
72 yield "".join(indented)
74 def readFile (filename):
75 f = open(filename, 'rb')
80 def writeFileIfChanged (filename, data):
81 if not os.path.exists(filename) or readFile(filename) != data:
82 f = open(filename, 'wb')
86 def writeLines (filename, lines):
92 writeFileIfChanged(filename, text)
95 def writeInlFile (filename, header, source):
96 writeLines(filename, chain([header], source))
98 def normalizeConstant (constant):
99 value = int(constant, base=0)
102 elif value >= 1 << 32:
104 elif value >= 1 << 31:
108 return constant + suffix
110 def commandParams (command):
111 if len(command.params) > 0:
112 return ", ".join(param.declaration for param in command.params)
116 def commandArgs (command):
117 return ", ".join(param.name for param in command.params)