Skip to content

Commit 7264c5f

Browse files
authored
Merge pull request #52 from mROS-base/refactor_templates
do not generate templates.hpp if it is same as the previously generated contents
2 parents 7a8606d + f4076b6 commit 7264c5f

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

mros2_header_generator/templates_generator.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import glob
33
import re
44
import argparse
5+
import filecmp
6+
import shutil
57
from os import path
68
from 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

Comments
 (0)