forked from dreamworksanimation/USD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfGenCode.py
304 lines (262 loc) · 10.7 KB
/
gfGenCode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python
#
# Copyright 2016 Pixar
#
# Licensed under the Apache License, Version 2.0 (the "Apache License")
# with the following modification; you may not use this file except in
# compliance with the Apache License and the following modification to it:
# Section 6. Trademarks. is deleted and replaced with:
#
# 6. Trademarks. This License does not grant permission to use the trade
# names, trademarks, service marks, or product names of the Licensor
# and its affiliates, except as required to comply with Section 4(c) of
# the License and to reproduce the content of the NOTICE file.
#
# You may obtain a copy of the Apache License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the Apache License with the above modification is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the Apache License for the specific
# language governing permissions and limitations under the Apache License.
#
########################################################################
# Code generation script for GfVec, GfRange, GfQuat (and in future, GfMatrix)
# classes.
#
# Run this script manually to update the source code that's checked in. Run
# with --validate to compare what would be generated with the existing code. If
# it differs, this script will print a diff and error out.
#
import os, sys, itertools
from argparse import ArgumentParser
from jinja2 import Environment, FileSystemLoader
from jinja2.exceptions import TemplateError, TemplateSyntaxError
# Write filePath if its content differs from \p content. If unchanged, print a
# message indicating that. If filePath is not writable, print a diff.
def _WriteFile(filePath, content, verbose=True):
import difflib
# If file currently exists and content is unchanged, do nothing.
existingContent = '\n'
content = (content + '\n'
if content and not content.endswith('\n') else content)
if os.path.exists(filePath):
existingContent = open(filePath, 'r').read()
if existingContent == content:
if verbose:
print '\tunchanged %s' % filePath
return
# Otherwise attempt to write to file.
try:
with open(filePath, 'w') as curfile:
curfile.write(content)
if verbose:
print '\t wrote %s' % filePath
except IOError as ioe:
print '\t', ioe
print 'Diff:'
print '\n'.join(difflib.unified_diff(existingContent.split('\n'),
content.split('\n')))
def IsFloatingPoint(t):
return t in ['double', 'float', 'GfHalf']
def RankScalar(s):
# Return a numeric rank for a scalar type. We allow implicit conversions to
# scalars of greater rank.
return dict([(t, n) for n, t in
enumerate(['int', 'GfHalf', 'float', 'double'])])[s]
def AllowImplicitConversion(src, dst):
# Somewhat questionably, we always allow ints to implicitly convert to
# floating point types.
return RankScalar(src) <= RankScalar(dst)
def MakeListFn(defaultN):
def List(fmt, sep=', ', num=None):
num = num if num is not None else defaultN
return sep.join([fmt % {'i': i} for i in range(num)])
return List
def MakeMatrixFn(defaultN):
def Matrix(fmt, sep=', ', indent=0, diagFmt=None, num=None):
num = num if num is not None else defaultN
diagFmt = diagFmt if diagFmt is not None else fmt
def GetFmt(row, col):
return diagFmt if row == col else fmt
strs = [(sep.join([GetFmt(i,j) % {'i':i,'j':j} for j in range(num)])) \
for i in range(num)]
indentStr = (' ' * indent)
lineSep = sep + ('\n' + indentStr if '\n' not in sep else '')
result = lineSep.join(strs)
return result
return Matrix
def GenerateFromTemplates(env, templates, suffix, outputPath, verbose=True):
for tmpl in templates:
tmplName = tmpl % '.template'
try:
_WriteFile(os.path.join(outputPath, tmpl % suffix),
env.get_template(tmplName).render(), verbose)
except TemplateSyntaxError as err:
print >>sys.stderr, \
'Syntax Error: {0.name}:{0.lineno}: {0.message}'.format(err)
except TemplateError as err:
print >>sys.stderr, \
'Template Error: {}: {}'.format(err, tmplName)
def ScalarSuffix(scl):
if scl == 'GfHalf':
return 'h'
else:
return scl[0]
def VecName(dim, scl):
return 'GfVec%s%s' % (dim, ScalarSuffix(scl))
def Eps(scl):
return '0.001' if scl == 'GfHalf' else 'GF_MIN_VECTOR_LENGTH'
########################################################################
# GfVec
def GetVecSpecs():
scalarTypes = ['double', 'float', 'GfHalf', 'int']
dimensions = [2, 3, 4]
vecSpecs = sorted(
[dict(SCL=scl,
DIM=dim,
SUFFIX=str(dim) + ScalarSuffix(scl),
VEC=VecName(dim, scl),
EPS=Eps(scl),
LIST=MakeListFn(dim),
VECNAME=VecName,
SCALAR_SUFFIX=ScalarSuffix,
SCALARS=scalarTypes)
for scl, dim in itertools.product(scalarTypes, dimensions)],
key=lambda d: RankScalar(d['SCL']))
return dict(templates=['vec%s.h', 'vec%s.cpp', 'wrapVec%s.cpp'],
specs=vecSpecs)
########################################################################
# GfRange
def GetRangeSpecs():
def RngName(dim, scl):
return 'GfRange%s%s' % (dim, ScalarSuffix(scl))
def MinMaxType(dim, scl):
return scl if dim == 1 else VecName(dim, scl)
def MinMaxParm(dim, scl):
t = MinMaxType(dim, scl)
return t + ' ' if dim == 1 else 'const %s &' % t
scalarTypes = ['double', 'float']
dimensions = [1, 2, 3]
rngSpecs = sorted(
[dict(SCL=scl,
MINMAX=MinMaxType(dim, scl),
MINMAXPARM=MinMaxParm(dim, scl),
DIM=dim,
SUFFIX=str(dim) + ScalarSuffix(scl),
RNG=RngName(dim, scl),
RNGNAME=RngName,
SCALARS=scalarTypes,
LIST=MakeListFn(dim))
for scl, dim in itertools.product(scalarTypes, dimensions)],
key=lambda d: RankScalar(d['SCL']))
return dict(templates=['range%s.h', 'range%s.cpp', 'wrapRange%s.cpp'],
specs=rngSpecs)
########################################################################
# GfQuat
def GetQuatSpecs():
def QuatName(scl):
return 'GfQuat%s' % ScalarSuffix(scl)
scalarTypes = ['double', 'float', 'GfHalf']
quatSpecs = sorted(
[dict(SCL=scl,
SUFFIX=ScalarSuffix(scl),
QUAT=QuatName(scl),
QUATNAME=QuatName,
SCALAR_SUFFIX=ScalarSuffix,
SCALARS=scalarTypes,
LIST=MakeListFn(4))
for scl in scalarTypes],
key=lambda d: RankScalar(d['SCL']))
return dict(templates=['quat%s.h', 'quat%s.cpp', 'wrapQuat%s.cpp'],
specs=quatSpecs)
########################################################################
# GfMatrix
def GetMatrixSpecs(dim):
def MatrixName(dim, scl):
return 'GfMatrix%s%s' % (dim, ScalarSuffix(scl))
scalarTypes = ['double', 'float']
dimensions = [dim]
matrixSpecs = sorted(
[dict(SCL=scl,
DIM=i,
FILESUFFIX=ScalarSuffix(scl),
SUFFIX=str(i) + ScalarSuffix(scl),
MAT=MatrixName(i, scl),
LIST=MakeListFn(i),
MATRIX=MakeMatrixFn(i),
MATNAME=MatrixName,
SCALARS=scalarTypes)
for scl, i in itertools.product(scalarTypes, dimensions)],
key=lambda d: RankScalar(d['SCL']))
return dict(templates=['matrix%s%%s.h' % dim,
'matrix%s%%s.cpp' % dim,
'wrapMatrix%s%%s.cpp' % dim ], specs=matrixSpecs)
def GetMatrix2Specs():
return GetMatrixSpecs(dim = 2)
def GetMatrix3Specs():
return GetMatrixSpecs(dim = 3)
def GetMatrix4Specs():
return GetMatrixSpecs(dim = 4)
# Check that each file in dstDir matches the corresponding file in srcDir.
def ValidateFiles(srcDir, dstDir):
import difflib
missing = []
diffs = []
for dstFile in [os.path.join(dstDir, f) for f in os.listdir(dstDir)
if os.path.isfile(os.path.join(dstDir, f))]:
srcFile = os.path.join(srcDir, os.path.basename(dstFile))
if not os.path.isfile(srcFile):
missing.append(srcFile)
continue
dstContent, srcContent = open(dstFile).read(), open(srcFile).read()
if dstContent != srcContent:
diff = '\n'.join(difflib.unified_diff(
srcContent.split('\n'),
dstContent.split('\n'),
'Source ' + os.path.basename(srcFile),
'Generated ' + os.path.basename(dstFile)))
diffs.append(diff)
continue
if missing or diffs:
msg = []
if missing:
msg.append('*** Missing Generated Files: ' + ', '.join(missing))
if diffs:
msg.append('*** Differing Generated Files:\n' + '\n'.join(diffs))
raise RuntimeError('\n' + '\n'.join(msg))
if __name__ == '__main__':
ap = ArgumentParser(
description='Generate source code for GfVec, GfRange, GfQuat.')
ap.add_argument('--validate', action='store_true')
ap.add_argument('--dstDir', default=os.curdir)
ap.add_argument('--srcDir', default=os.curdir)
args = ap.parse_args()
stdEnv = dict(UPPER=str.upper, LOWER=str.lower,
ALLOW_IMPLICIT_CONVERSION=AllowImplicitConversion,
IS_FLOATING_POINT=IsFloatingPoint)
if args.validate:
# Make a temporary directory for results.
import tempfile
args.dstDir = tempfile.mkdtemp()
try:
for s in [GetVecSpecs(), GetRangeSpecs(), GetQuatSpecs(),
GetMatrix2Specs(), GetMatrix3Specs(), GetMatrix4Specs()]:
env = Environment(loader=FileSystemLoader(args.srcDir),
trim_blocks=True)
env.globals.update(**stdEnv)
templates, specs = s['templates'], s['specs']
for spec in specs:
env.globals.update(**spec)
GenerateFromTemplates(
env, templates, spec.get('FILESUFFIX', spec['SUFFIX']),
args.dstDir, verbose=not args.validate)
if args.validate:
ValidateFiles(args.srcDir, args.dstDir)
finally:
# Remove the temporary directory.
if args.validate:
import shutil
shutil.rmtree(args.dstDir)