22import glob
33import re
44import argparse
5+ import filecmp
6+ import shutil
57from os import path
68from jinja2 import Environment , FileSystemLoader
79
@@ -22,12 +24,12 @@ def main():
2224 help = 'input dir(s) that contains mros2 app code (required)' )
2325
2426 args = parser .parse_args ()
25- outdir = args .outdir
2627 indir = args .indir
28+ outdir = args .outdir
2729 file = []
28-
2930 for id in indir :
3031 file = file + glob .glob (os .path .join (id , "*.cpp" ))
32+
3133 for f in file :
3234 print (' Analyzing \' {}\' to generate...' .format (f ))
3335 with open (f , 'r' ) as m_f :
@@ -56,8 +58,18 @@ def main():
5658 env = Environment (loader = FileSystemLoader (path .dirname (__file__ )))
5759 template = env .get_template ('templates.tpl' )
5860 datatext = template .render ({ "includeFiles" :includeFiles , "pubMsgTypes" :pubMsgTypes , "subMsgTypes" :subMsgTypes })
59- with open (os .path .join (outdir + "/templates.hpp" ), "wb" ) as f :
61+
62+ outfile_path = os .path .join (outdir , "templates.hpp" )
63+ outtemp_path = os .path .join (outdir , "templates.hpp.tmp" )
64+ if (not os .path .isfile (outfile_path )):
65+ with open (outfile_path , "wb" ) as f :
66+ f .write (datatext .encode ('utf-8' ))
67+ with open (outtemp_path , "wb" ) as f :
6068 f .write (datatext .encode ('utf-8' ))
69+ if filecmp .cmp (outtemp_path , outfile_path , shallow = True ):
70+ os .remove (outtemp_path )
71+ else :
72+ shutil .move (outtemp_path , outfile_path )
6173
6274 print ('Generate {}/template.hpp done.' .format (outdir ))
6375
0 commit comments