From f4076b67073bb4d57b5841d0976b9224495e0640 Mon Sep 17 00:00:00 2001 From: takasehideki Date: Tue, 19 Sep 2023 22:35:23 +0900 Subject: [PATCH] do not generate templates.hpp if it is same as the previous --- mros2_header_generator/templates_generator.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/mros2_header_generator/templates_generator.py b/mros2_header_generator/templates_generator.py index d154e46..b41555e 100644 --- a/mros2_header_generator/templates_generator.py +++ b/mros2_header_generator/templates_generator.py @@ -2,6 +2,8 @@ import glob import re import argparse +import filecmp +import shutil from os import path from jinja2 import Environment, FileSystemLoader @@ -22,12 +24,12 @@ def main(): help='input dir(s) that contains mros2 app code (required)') args = parser.parse_args() - outdir = args.outdir indir = args.indir + outdir = args.outdir file = [] - for id in indir: file = file + glob.glob(os.path.join(id, "*.cpp")) + for f in file: print(' Analyzing \'{}\' to generate...'.format(f)) with open(f, 'r') as m_f: @@ -56,8 +58,18 @@ def main(): env = Environment(loader=FileSystemLoader(path.dirname(__file__))) template = env.get_template('templates.tpl') datatext = template.render({ "includeFiles":includeFiles, "pubMsgTypes":pubMsgTypes, "subMsgTypes":subMsgTypes }) - with open(os.path.join(outdir + "/templates.hpp"), "wb") as f: + + outfile_path = os.path.join(outdir, "templates.hpp") + outtemp_path = os.path.join(outdir, "templates.hpp.tmp") + if (not os.path.isfile(outfile_path)): + with open(outfile_path, "wb") as f: + f.write(datatext.encode('utf-8')) + with open(outtemp_path, "wb") as f: f.write(datatext.encode('utf-8')) + if filecmp.cmp(outtemp_path, outfile_path, shallow=True): + os.remove(outtemp_path) + else: + shutil.move(outtemp_path, outfile_path) print('Generate {}/template.hpp done.'.format(outdir))