Skip to content

Commit ae1212f

Browse files
committed
clar: emit clar_suite.h with test declarations
We may want to have test function declarations; produce a header file with (only) the test declarations. Update clar to avoid overwriting the file unnecessarily to avoid bumping timestamps and potentially recompiling unnecessarily.
1 parent 36d696c commit ae1212f

1 file changed

Lines changed: 63 additions & 14 deletions

File tree

generate.py

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from __future__ import with_statement
1010
from string import Template
11-
import re, fnmatch, os, sys, codecs, pickle
11+
import re, fnmatch, os, sys, codecs, pickle, io
1212

1313
class Module(object):
1414
class Template(object):
@@ -147,7 +147,7 @@ def __init__(self, path, output):
147147
self.path = path
148148
self.output = output
149149

150-
def should_generate(self, path):
150+
def maybe_generate(self, path):
151151
if not os.path.isfile(path):
152152
return True
153153

@@ -223,36 +223,85 @@ def callback_count(self):
223223
return sum(len(module.callbacks) for module in self.modules.values())
224224

225225
def write(self):
226-
output = os.path.join(self.output, 'clar.suite')
227-
228226
if not os.path.exists(self.output):
229227
os.makedirs(self.output)
230228

231-
if not self.should_generate(output):
229+
wrote_suite = self.write_suite()
230+
wrote_header = self.write_header()
231+
232+
if wrote_suite or wrote_header:
233+
self.save_cache()
234+
return True
235+
236+
return False
237+
238+
def write_output(self, fn, data):
239+
if not self.maybe_generate(fn):
240+
return False
241+
242+
current = None
243+
244+
try:
245+
with open(fn, 'r') as input:
246+
current = input.read()
247+
except OSError:
248+
pass
249+
except IOError:
250+
pass
251+
252+
if current == data:
232253
return False
233254

234-
with open(output, 'w') as data:
255+
with open(fn, 'w') as output:
256+
output.write(data)
257+
258+
return True
259+
260+
def write_suite(self):
261+
suite_fn = os.path.join(self.output, 'clar.suite')
262+
263+
with io.StringIO() as suite_file:
235264
modules = sorted(self.modules.values(), key=lambda module: module.name)
236265

237266
for module in modules:
238267
t = Module.DeclarationTemplate(module)
239-
data.write(t.render())
268+
suite_file.write(t.render())
240269

241270
for module in modules:
242271
t = Module.CallbacksTemplate(module)
243-
data.write(t.render())
272+
suite_file.write(t.render())
244273

245274
suites = "static struct clar_suite _clar_suites[] = {" + ','.join(
246275
Module.InfoTemplate(module).render() for module in modules
247276
) + "\n};\n"
248277

249-
data.write(suites)
278+
suite_file.write(suites)
250279

251-
data.write("static const size_t _clar_suite_count = %d;\n" % self.suite_count())
252-
data.write("static const size_t _clar_callback_count = %d;\n" % self.callback_count())
280+
suite_file.write(u"static const size_t _clar_suite_count = %d;\n" % self.suite_count())
281+
suite_file.write(u"static const size_t _clar_callback_count = %d;\n" % self.callback_count())
253282

254-
self.save_cache()
255-
return True
283+
return self.write_output(suite_fn, suite_file.getvalue())
284+
285+
return False
286+
287+
def write_header(self):
288+
header_fn = os.path.join(self.output, 'clar_suite.h')
289+
290+
with io.StringIO() as header_file:
291+
header_file.write(u"#ifndef _____clar_suite_h_____\n")
292+
header_file.write(u"#define _____clar_suite_h_____\n")
293+
294+
modules = sorted(self.modules.values(), key=lambda module: module.name)
295+
296+
for module in modules:
297+
t = Module.DeclarationTemplate(module)
298+
header_file.write(t.render())
299+
300+
header_file.write(u"#endif\n")
301+
302+
return self.write_output(header_fn, header_file.getvalue())
303+
304+
return False
256305

257306
if __name__ == '__main__':
258307
from optparse import OptionParser
@@ -277,4 +326,4 @@ def write(self):
277326
suite.load(options.force)
278327
suite.disable(options.excluded)
279328
if suite.write():
280-
print("Written `clar.suite` (%d tests in %d suites)" % (suite.callback_count(), suite.suite_count()))
329+
print("Written `clar.suite`, `clar_suite.h` (%d tests in %d suites)" % (suite.callback_count(), suite.suite_count()))

0 commit comments

Comments
 (0)