Skip to content

Commit

Permalink
Merge pull request #14 from ros2/regenerate_templated_files_only_when…
Browse files Browse the repository at this point in the history
…_changed

only rewrite templated files when the content has changed
  • Loading branch information
dirk-thomas committed Apr 22, 2015
2 parents 8cd4e57 + b7fcfbc commit af0ed7e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
29 changes: 23 additions & 6 deletions rosidl_generator_dds_idl/rosidl_generator_dds_idl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import em
from io import StringIO
import os

from rosidl_parser import Field
Expand Down Expand Up @@ -56,25 +57,33 @@ def generate_dds_idl(
'%s_.idl' % spec.base_type.type)

try:
# TODO only touch generated file if its content actually changes
ofile = open(generated_file, 'w')
output = StringIO()
data = {'spec': spec, 'subfolders': subfolders}
data.update(functions)
# TODO reuse interpreter
interpreter = em.Interpreter(
output=ofile,
output=output,
options={
em.RAW_OPT: True,
em.BUFFERED_OPT: True,
},
globals=data,
)
interpreter.file(open(template_file_msg))
content = output.getvalue()
interpreter.shutdown()
except Exception:
os.remove(generated_file)
raise

# only overwrite file if necessary
if os.path.exists(generated_file):
with open(generated_file, 'r') as h:
if h.read() == content:
continue
with open(generated_file, 'w') as h:
h.write(content)

elif extension == '.srv':
srv_spec = parse_service_file(pkg_name, idl_file)
request_fields = [
Expand Down Expand Up @@ -134,25 +143,33 @@ def generate_dds_idl(
for spec, generated_file in generated_files:

try:
# TODO only touch generated file if its content actually changes
ofile = open(generated_file, 'w')
output = StringIO()
data = {'spec': spec, 'subfolders': subfolders}
data.update(functions)
# TODO reuse interpreter
interpreter = em.Interpreter(
output=ofile,
output=output,
options={
em.RAW_OPT: True,
em.BUFFERED_OPT: True,
},
globals=data,
)
interpreter.file(open(template_file_msg))
content = output.getvalue()
interpreter.shutdown()
except Exception:
os.remove(generated_file)
raise

# only overwrite file if necessary
if os.path.exists(generated_file):
with open(generated_file, 'r') as h:
if h.read() == content:
continue
with open(generated_file, 'w') as h:
h.write(content)

return 0


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import em
from io import StringIO
import os

from rosidl_parser import parse_message_file, parse_service_file
Expand Down Expand Up @@ -48,44 +49,60 @@ def generate_cpp(pkg_name, ros_interface_files, deps, output_dir, template_dir):
generated_file = os.path.join(output_dir, generated_filename % spec.base_type.type)

try:
# TODO only touch generated file if its content actually changes
ofile = open(generated_file, 'w')
output = StringIO()
# TODO reuse interpreter
interpreter = em.Interpreter(
output=ofile,
output=output,
options={
em.RAW_OPT: True,
em.BUFFERED_OPT: True,
},
globals={'spec': spec},
)
interpreter.file(open(template_file))
content = output.getvalue()
interpreter.shutdown()
except Exception:
os.remove(generated_file)
raise

# only overwrite file if necessary
if os.path.exists(generated_file):
with open(generated_file, 'r') as h:
if h.read() == content:
continue
with open(generated_file, 'w') as h:
h.write(content)

elif extension == '.srv':
spec = parse_service_file(pkg_name, ros_interface_file)
for template_file, generated_filename in mapping_srvs.items():
generated_file = os.path.join(output_dir, generated_filename % spec.srv_name)

try:
# TODO only touch generated file if its content actually changes
ofile = open(generated_file, 'w')
output = StringIO()
# TODO reuse interpreter
interpreter = em.Interpreter(
output=ofile,
output=output,
options={
em.RAW_OPT: True,
em.BUFFERED_OPT: True,
},
globals={'spec': spec},
)
interpreter.file(open(template_file))
content = output.getvalue()
interpreter.shutdown()
except Exception:
os.remove(generated_file)
raise

# only overwrite file if necessary
if os.path.exists(generated_file):
with open(generated_file, 'r') as h:
if h.read() == content:
continue
with open(generated_file, 'w') as h:
h.write(content)

return 0

0 comments on commit af0ed7e

Please sign in to comment.