88
99from __future__ import with_statement
1010from string import Template
11- import re , fnmatch , os , sys , codecs , pickle
11+ import re , fnmatch , os , sys , codecs , pickle , io
1212
1313class 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
@@ -216,33 +216,82 @@ def callback_count(self):
216216 return sum (len (module .callbacks ) for module in self .modules .values ())
217217
218218 def write (self ):
219- output = os .path .join (self .output , 'clar.suite' )
219+ wrote_suite = self .write_suite ()
220+ wrote_header = self .write_header ()
220221
221- if not self .should_generate (output ):
222+ if wrote_suite or wrote_header :
223+ self .save_cache ()
224+ return True
225+
226+ return False
227+
228+ def write_output (self , fn , data ):
229+ if not self .maybe_generate (fn ):
222230 return False
223231
224- with open (output , 'w' ) as data :
232+ current = None
233+
234+ try :
235+ with open (fn , 'r' ) as input :
236+ current = input .read ()
237+ except OSError :
238+ pass
239+ except IOError :
240+ pass
241+
242+ if current == data :
243+ return False
244+
245+ with open (fn , 'w' ) as output :
246+ output .write (data )
247+
248+ return True
249+
250+ def write_suite (self ):
251+ suite_fn = os .path .join (self .output , 'clar.suite' )
252+
253+ with io .StringIO () as suite_file :
225254 modules = sorted (self .modules .values (), key = lambda module : module .name )
226255
227256 for module in modules :
228257 t = Module .DeclarationTemplate (module )
229- data .write (t .render ())
258+ suite_file .write (t .render ())
230259
231260 for module in modules :
232261 t = Module .CallbacksTemplate (module )
233- data .write (t .render ())
262+ suite_file .write (t .render ())
234263
235264 suites = "static struct clar_suite _clar_suites[] = {" + ',' .join (
236265 Module .InfoTemplate (module ).render () for module in modules
237266 ) + "\n };\n "
238267
239- data .write (suites )
268+ suite_file .write (suites )
240269
241- data .write ("static const size_t _clar_suite_count = %d;\n " % self .suite_count ())
242- data .write ("static const size_t _clar_callback_count = %d;\n " % self .callback_count ())
270+ suite_file .write (u "static const size_t _clar_suite_count = %d;\n " % self .suite_count ())
271+ suite_file .write (u "static const size_t _clar_callback_count = %d;\n " % self .callback_count ())
243272
244- self .save_cache ()
245- return True
273+ return self .write_output (suite_fn , suite_file .getvalue ())
274+
275+ return False
276+
277+ def write_header (self ):
278+ header_fn = os .path .join (self .output , 'clar_suite.h' )
279+
280+ with io .StringIO () as header_file :
281+ header_file .write (u"#ifndef _____clar_suite_h_____\n " )
282+ header_file .write (u"#define _____clar_suite_h_____\n " )
283+
284+ modules = sorted (self .modules .values (), key = lambda module : module .name )
285+
286+ for module in modules :
287+ t = Module .DeclarationTemplate (module )
288+ header_file .write (t .render ())
289+
290+ header_file .write (u"#endif\n " )
291+
292+ return self .write_output (header_fn , header_file .getvalue ())
293+
294+ return False
246295
247296if __name__ == '__main__' :
248297 from optparse import OptionParser
@@ -263,5 +312,5 @@ def write(self):
263312 suite .load (options .force )
264313 suite .disable (options .excluded )
265314 if suite .write ():
266- print ("Written `clar.suite` (%d tests in %d suites)" % (suite .callback_count (), suite .suite_count ()))
315+ print ("Written `clar.suite`, `clar_suite.h` (%d tests in %d suites)" % (suite .callback_count (), suite .suite_count ()))
267316
0 commit comments