2019-11-23 14:21:58 -04:00
|
|
|
#!/usr/bin/env python3
|
2014-12-01 11:39:27 -04:00
|
|
|
#############################################################################
|
|
|
|
#
|
2018-09-11 13:25:45 -03:00
|
|
|
# Copyright (C) 2013-2018 PX4 Pro Development Team. All rights reserved.
|
2014-12-01 11:39:27 -04:00
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
|
|
|
#
|
|
|
|
# 1. Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in
|
|
|
|
# the documentation and/or other materials provided with the
|
|
|
|
# distribution.
|
|
|
|
# 3. Neither the name PX4 nor the names of its contributors may be
|
|
|
|
# used to endorse or promote products derived from this software
|
|
|
|
# without specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
#
|
|
|
|
#############################################################################
|
|
|
|
|
|
|
|
"""
|
2019-10-18 06:23:32 -03:00
|
|
|
px_generate_uorb_topic_files.py
|
2016-05-06 11:42:35 -03:00
|
|
|
Generates c/cpp header/source files for uorb topics from .msg (ROS syntax)
|
2014-12-01 11:39:27 -04:00
|
|
|
message files
|
|
|
|
"""
|
2019-11-23 14:21:58 -04:00
|
|
|
|
2014-12-01 11:39:27 -04:00
|
|
|
import os
|
2014-12-04 05:39:24 -04:00
|
|
|
import shutil
|
|
|
|
import filecmp
|
2014-12-01 11:39:27 -04:00
|
|
|
import argparse
|
2015-10-12 11:47:09 -03:00
|
|
|
import sys
|
2018-09-04 17:49:00 -03:00
|
|
|
import errno
|
2017-05-16 12:30:56 -03:00
|
|
|
|
2015-02-06 03:54:19 -04:00
|
|
|
try:
|
2018-09-11 13:28:47 -03:00
|
|
|
import em
|
2015-02-08 07:54:16 -04:00
|
|
|
except ImportError as e:
|
2020-01-15 04:58:09 -04:00
|
|
|
print("Python import error: ", e)
|
2018-09-11 13:28:47 -03:00
|
|
|
print('''
|
2020-01-15 04:58:09 -04:00
|
|
|
Required python package empy not installed.
|
2015-02-13 19:43:49 -04:00
|
|
|
|
2020-01-15 04:58:09 -04:00
|
|
|
Please run:
|
|
|
|
pip3 install --user empy
|
|
|
|
''')
|
|
|
|
exit(1)
|
2015-05-24 05:21:39 -03:00
|
|
|
|
2020-01-15 04:58:09 -04:00
|
|
|
try:
|
|
|
|
import genmsg.template_tools
|
|
|
|
except ImportError as e:
|
|
|
|
print("Python import error: ", e)
|
|
|
|
print('''
|
|
|
|
Required python package pyros-genmsg not installed.
|
2015-02-13 19:43:49 -04:00
|
|
|
|
2020-01-15 04:58:09 -04:00
|
|
|
Please run:
|
|
|
|
pip3 install --user pyros-genmsg
|
2015-02-13 19:43:49 -04:00
|
|
|
''')
|
2018-09-11 13:28:47 -03:00
|
|
|
exit(1)
|
2014-12-01 11:39:27 -04:00
|
|
|
|
2020-01-15 04:58:09 -04:00
|
|
|
|
2016-05-06 11:42:35 -03:00
|
|
|
__author__ = "Sergey Belash, Thomas Gubler, Beat Kueng"
|
|
|
|
__copyright__ = "Copyright (C) 2013-2016 PX4 Development Team."
|
2014-12-01 11:39:27 -04:00
|
|
|
__license__ = "BSD"
|
|
|
|
__email__ = "thomasgubler@gmail.com"
|
|
|
|
|
|
|
|
|
2019-06-10 13:36:16 -03:00
|
|
|
TEMPLATE_FILE = ['msg.h.em', 'msg.cpp.em']
|
|
|
|
TOPICS_LIST_TEMPLATE_FILE = 'uORBTopics.cpp.em'
|
2016-05-06 11:42:35 -03:00
|
|
|
OUTPUT_FILE_EXT = ['.h', '.cpp']
|
2017-01-13 11:43:48 -04:00
|
|
|
INCL_DEFAULT = ['std_msgs:./msg/std_msgs']
|
2016-04-25 18:36:13 -03:00
|
|
|
PACKAGE = 'px4'
|
|
|
|
TOPICS_TOKEN = '# TOPICS '
|
2019-06-10 13:36:16 -03:00
|
|
|
IDL_TEMPLATE_FILE = 'msg.idl.em'
|
2014-12-01 11:39:27 -04:00
|
|
|
|
2019-10-18 06:23:32 -03:00
|
|
|
CONSTRAINED_FLASH = False
|
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
|
2017-05-16 12:30:56 -03:00
|
|
|
class MsgScope:
|
|
|
|
NONE = 0
|
|
|
|
SEND = 1
|
|
|
|
RECEIVE = 2
|
2014-12-01 11:39:27 -04:00
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
|
2016-04-25 18:36:13 -03:00
|
|
|
def get_multi_topics(filename):
|
2018-09-11 13:28:47 -03:00
|
|
|
"""
|
|
|
|
Get TOPICS names from a "# TOPICS" line
|
|
|
|
"""
|
|
|
|
ofile = open(filename, 'r')
|
|
|
|
text = ofile.read()
|
|
|
|
result = []
|
|
|
|
for each_line in text.split('\n'):
|
|
|
|
if each_line.startswith(TOPICS_TOKEN):
|
|
|
|
topic_names_str = each_line.strip()
|
|
|
|
topic_names_str = topic_names_str.replace(TOPICS_TOKEN, "")
|
|
|
|
result.extend(topic_names_str.split(" "))
|
|
|
|
ofile.close()
|
|
|
|
return result
|
|
|
|
|
2016-04-25 18:36:13 -03:00
|
|
|
|
2016-05-06 11:42:35 -03:00
|
|
|
def get_msgs_list(msgdir):
|
|
|
|
"""
|
|
|
|
Makes list of msg files in the given directory
|
|
|
|
"""
|
|
|
|
return [fn for fn in os.listdir(msgdir) if fn.endswith(".msg")]
|
2016-04-25 18:36:13 -03:00
|
|
|
|
2016-05-06 11:42:35 -03:00
|
|
|
|
2018-08-30 18:32:14 -03:00
|
|
|
def generate_output_from_file(format_idx, filename, outputdir, package, templatedir, includepath):
|
2018-09-11 13:28:47 -03:00
|
|
|
"""
|
|
|
|
Converts a single .msg file to an uorb header/source file
|
|
|
|
"""
|
|
|
|
msg_context = genmsg.msg_loader.MsgContext.create_default()
|
|
|
|
full_type_name = genmsg.gentools.compute_full_type_name(
|
|
|
|
package, os.path.basename(filename))
|
|
|
|
spec = genmsg.msg_loader.load_msg_from_file(
|
|
|
|
msg_context, filename, full_type_name)
|
|
|
|
field_name_and_type = {}
|
|
|
|
for field in spec.parsed_fields():
|
|
|
|
field_name_and_type.update({field.name: field.type})
|
|
|
|
# assert if the timestamp field exists
|
|
|
|
try:
|
|
|
|
assert 'timestamp' in field_name_and_type
|
|
|
|
except AssertionError:
|
|
|
|
print("[ERROR] uORB topic files generator:\n\tgenerate_output_from_file:\tNo 'timestamp' field found in " +
|
|
|
|
spec.short_name + " msg definition!")
|
|
|
|
exit(1)
|
|
|
|
# assert if the timestamp field is of type uint64
|
|
|
|
try:
|
|
|
|
assert field_name_and_type.get('timestamp') == 'uint64'
|
|
|
|
except AssertionError:
|
|
|
|
print("[ERROR] uORB topic files generator:\n\tgenerate_output_from_file:\t'timestamp' field in " + spec.short_name +
|
|
|
|
" msg definition is not of type uint64 but rather of type " + field_name_and_type.get('timestamp') + "!")
|
|
|
|
exit(1)
|
|
|
|
topics = get_multi_topics(filename)
|
|
|
|
if includepath:
|
|
|
|
search_path = genmsg.command_line.includepath_to_dict(includepath)
|
|
|
|
else:
|
|
|
|
search_path = {}
|
|
|
|
genmsg.msg_loader.load_depends(msg_context, spec, search_path)
|
|
|
|
md5sum = genmsg.gentools.compute_md5(msg_context, spec)
|
|
|
|
if len(topics) == 0:
|
|
|
|
topics.append(spec.short_name)
|
|
|
|
em_globals = {
|
|
|
|
"file_name_in": filename,
|
|
|
|
"md5sum": md5sum,
|
|
|
|
"search_path": search_path,
|
|
|
|
"msg_context": msg_context,
|
|
|
|
"spec": spec,
|
2019-10-18 06:23:32 -03:00
|
|
|
"topics": topics,
|
|
|
|
"constrained_flash": CONSTRAINED_FLASH
|
2018-09-11 13:28:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Make sure output directory exists:
|
|
|
|
if not os.path.isdir(outputdir):
|
|
|
|
os.makedirs(outputdir)
|
|
|
|
|
|
|
|
template_file = os.path.join(templatedir, TEMPLATE_FILE[format_idx])
|
|
|
|
output_file = os.path.join(outputdir, spec.short_name +
|
|
|
|
OUTPUT_FILE_EXT[format_idx])
|
|
|
|
|
|
|
|
return generate_by_template(output_file, template_file, em_globals)
|
|
|
|
|
2016-04-25 18:36:13 -03:00
|
|
|
|
2019-10-02 06:40:37 -03:00
|
|
|
def generate_idl_file(filename_msg, msg_dir, alias, outputdir, templatedir, package, includepath, fastrtpsgen_version, ros2_distro, ids):
|
2018-09-11 13:28:47 -03:00
|
|
|
"""
|
|
|
|
Generates an .idl from .msg file
|
|
|
|
"""
|
2019-06-02 12:12:50 -03:00
|
|
|
msg = os.path.join(msg_dir, filename_msg + ".msg")
|
|
|
|
|
|
|
|
if (alias != ""):
|
|
|
|
em_globals = get_em_globals(
|
2019-10-02 06:40:37 -03:00
|
|
|
msg, alias, package, includepath, ids, fastrtpsgen_version, ros2_distro, MsgScope.NONE)
|
2019-06-02 12:12:50 -03:00
|
|
|
spec_short_name = alias
|
|
|
|
else:
|
|
|
|
em_globals = get_em_globals(
|
2019-10-02 06:40:37 -03:00
|
|
|
msg, "", package, includepath, ids, fastrtpsgen_version, ros2_distro, MsgScope.NONE)
|
2019-06-02 12:12:50 -03:00
|
|
|
spec_short_name = em_globals["spec"].short_name
|
2017-05-16 12:30:56 -03:00
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
# Make sure output directory exists:
|
|
|
|
if not os.path.isdir(outputdir):
|
|
|
|
os.makedirs(outputdir)
|
2017-05-16 12:30:56 -03:00
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
template_file = os.path.join(templatedir, IDL_TEMPLATE_FILE)
|
2019-10-02 11:00:01 -03:00
|
|
|
if 1.5 <= fastrtpsgen_version <= 1.7:
|
2019-09-09 08:39:39 -03:00
|
|
|
output_file = os.path.join(outputdir, IDL_TEMPLATE_FILE.replace(
|
|
|
|
"msg.idl.em", str(spec_short_name + "_.idl")))
|
|
|
|
else:
|
|
|
|
output_file = os.path.join(outputdir, IDL_TEMPLATE_FILE.replace(
|
|
|
|
"msg.idl.em", str(spec_short_name + ".idl")))
|
2018-09-11 13:28:47 -03:00
|
|
|
|
|
|
|
return generate_by_template(output_file, template_file, em_globals)
|
2017-05-16 12:30:56 -03:00
|
|
|
|
|
|
|
|
2019-06-02 12:12:50 -03:00
|
|
|
def generate_uRTPS_general(filename_send_msgs, filename_alias_send_msgs, filename_receive_msgs, filename_alias_receive_msgs,
|
2019-10-02 06:40:37 -03:00
|
|
|
msg_dir, outputdir, templatedir, package, includepath, ids, fastrtpsgen_version, ros2_distro, template_name):
|
2018-09-11 13:28:47 -03:00
|
|
|
"""
|
|
|
|
Generates source file by msg content
|
|
|
|
"""
|
2019-08-04 14:54:04 -03:00
|
|
|
send_msgs = list(os.path.join(msg_dir, msg + ".msg")
|
|
|
|
for msg in filename_send_msgs)
|
|
|
|
receive_msgs = list(os.path.join(msg_dir, msg + ".msg")
|
|
|
|
for msg in filename_receive_msgs)
|
|
|
|
|
2019-11-23 14:21:58 -04:00
|
|
|
alias_send_msgs = list([os.path.join(
|
|
|
|
msg_dir, msg[1] + ".msg"), list(msg[0].keys())[0]] for msg in filename_alias_send_msgs)
|
2019-08-04 14:54:04 -03:00
|
|
|
|
2019-11-23 14:21:58 -04:00
|
|
|
alias_receive_msgs = list([os.path.join(
|
|
|
|
msg_dir, msg[1] + ".msg"), list(msg[0].keys())[0]] for msg in filename_alias_receive_msgs)
|
2019-06-02 12:12:50 -03:00
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
em_globals_list = []
|
2019-06-02 12:12:50 -03:00
|
|
|
if send_msgs:
|
|
|
|
em_globals_list.extend([get_em_globals(
|
2019-10-02 06:40:37 -03:00
|
|
|
f, "", package, includepath, ids, fastrtpsgen_version, ros2_distro, MsgScope.SEND) for f in send_msgs])
|
2019-06-02 12:12:50 -03:00
|
|
|
|
|
|
|
if alias_send_msgs:
|
2018-09-11 13:28:47 -03:00
|
|
|
em_globals_list.extend([get_em_globals(
|
2019-10-02 06:40:37 -03:00
|
|
|
f[0], f[1], package, includepath, ids, fastrtpsgen_version, ros2_distro, MsgScope.SEND) for f in alias_send_msgs])
|
2018-09-11 13:28:47 -03:00
|
|
|
|
2019-06-02 12:12:50 -03:00
|
|
|
if receive_msgs:
|
2018-09-11 13:28:47 -03:00
|
|
|
em_globals_list.extend([get_em_globals(
|
2019-10-02 06:40:37 -03:00
|
|
|
f, "", package, includepath, ids, fastrtpsgen_version, ros2_distro, MsgScope.RECEIVE) for f in receive_msgs])
|
2019-06-02 12:12:50 -03:00
|
|
|
|
|
|
|
if alias_receive_msgs:
|
|
|
|
em_globals_list.extend([get_em_globals(
|
2019-10-02 06:40:37 -03:00
|
|
|
f[0], f[1], package, includepath, ids, fastrtpsgen_version, ros2_distro, MsgScope.RECEIVE) for f in alias_receive_msgs])
|
2017-05-16 12:30:56 -03:00
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
merged_em_globals = merge_em_globals_list(em_globals_list)
|
2019-06-02 12:12:50 -03:00
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
# Make sure output directory exists:
|
|
|
|
if not os.path.isdir(outputdir):
|
|
|
|
os.makedirs(outputdir)
|
2017-05-16 12:30:56 -03:00
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
template_file = os.path.join(templatedir, template_name)
|
|
|
|
output_file = os.path.join(
|
2019-06-10 13:36:16 -03:00
|
|
|
outputdir, template_name.replace(".em", ""))
|
2017-05-16 12:30:56 -03:00
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
return generate_by_template(output_file, template_file, merged_em_globals)
|
2017-05-16 12:30:56 -03:00
|
|
|
|
|
|
|
|
2019-10-02 06:40:37 -03:00
|
|
|
def generate_topic_file(filename_msg, msg_dir, alias, outputdir, templatedir, package, includepath, ids, fastrtpsgen_version, ros2_distro, template_name):
|
2018-09-11 13:28:47 -03:00
|
|
|
"""
|
2019-06-02 12:12:50 -03:00
|
|
|
Generates a sources and headers from .msg file
|
2018-09-11 13:28:47 -03:00
|
|
|
"""
|
2019-06-02 12:12:50 -03:00
|
|
|
msg = os.path.join(msg_dir, filename_msg + ".msg")
|
|
|
|
|
|
|
|
if (alias):
|
|
|
|
em_globals = get_em_globals(
|
2019-10-02 06:40:37 -03:00
|
|
|
msg, alias, package, includepath, ids, fastrtpsgen_version, ros2_distro, MsgScope.NONE)
|
2019-06-02 12:12:50 -03:00
|
|
|
spec_short_name = alias
|
|
|
|
else:
|
|
|
|
em_globals = get_em_globals(
|
2019-10-02 06:40:37 -03:00
|
|
|
msg, "", package, includepath, ids, fastrtpsgen_version, ros2_distro, MsgScope.NONE)
|
2019-06-02 12:12:50 -03:00
|
|
|
spec_short_name = em_globals["spec"].short_name
|
2018-09-11 13:28:47 -03:00
|
|
|
|
|
|
|
# Make sure output directory exists:
|
|
|
|
if not os.path.isdir(outputdir):
|
|
|
|
os.makedirs(outputdir)
|
2017-05-16 12:30:56 -03:00
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
template_file = os.path.join(templatedir, template_name)
|
|
|
|
output_file = os.path.join(
|
2019-06-10 13:36:16 -03:00
|
|
|
outputdir, spec_short_name + "_" + template_name.replace(".em", ""))
|
2017-05-16 12:30:56 -03:00
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
return generate_by_template(output_file, template_file, em_globals)
|
2017-05-16 12:30:56 -03:00
|
|
|
|
|
|
|
|
2019-10-02 06:40:37 -03:00
|
|
|
def get_em_globals(filename_msg, alias, package, includepath, ids, fastrtpsgen_version, ros2_distro, scope):
|
2018-09-11 13:28:47 -03:00
|
|
|
"""
|
|
|
|
Generates em globals dictionary
|
|
|
|
"""
|
|
|
|
msg_context = genmsg.msg_loader.MsgContext.create_default()
|
|
|
|
full_type_name = genmsg.gentools.compute_full_type_name(
|
|
|
|
package, os.path.basename(filename_msg))
|
|
|
|
spec = genmsg.msg_loader.load_msg_from_file(
|
|
|
|
msg_context, filename_msg, full_type_name)
|
|
|
|
topics = get_multi_topics(filename_msg)
|
|
|
|
if includepath:
|
|
|
|
search_path = genmsg.command_line.includepath_to_dict(includepath)
|
|
|
|
else:
|
|
|
|
search_path = {}
|
|
|
|
genmsg.msg_loader.load_depends(msg_context, spec, search_path)
|
|
|
|
md5sum = genmsg.gentools.compute_md5(msg_context, spec)
|
|
|
|
if len(topics) == 0:
|
|
|
|
topics.append(spec.short_name)
|
|
|
|
em_globals = {
|
|
|
|
"file_name_in": filename_msg,
|
|
|
|
"md5sum": md5sum,
|
|
|
|
"search_path": search_path,
|
|
|
|
"msg_context": msg_context,
|
|
|
|
"spec": spec,
|
|
|
|
"topics": topics,
|
2018-09-22 12:32:56 -03:00
|
|
|
"ids": ids,
|
2018-09-24 11:03:48 -03:00
|
|
|
"scope": scope,
|
2019-06-02 12:12:50 -03:00
|
|
|
"package": package,
|
2019-09-09 08:39:39 -03:00
|
|
|
"alias": alias,
|
2019-10-02 06:40:37 -03:00
|
|
|
"fastrtpsgen_version": fastrtpsgen_version,
|
2019-09-09 08:39:39 -03:00
|
|
|
"ros2_distro": ros2_distro
|
2018-09-11 13:28:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return em_globals
|
|
|
|
|
2017-05-16 12:30:56 -03:00
|
|
|
|
|
|
|
def merge_em_globals_list(em_globals_list):
|
|
|
|
"""
|
|
|
|
Merges a list of em_globals to a single dictionary where each attribute is a list
|
|
|
|
"""
|
|
|
|
if len(em_globals_list) < 1:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
merged_em_globals = {}
|
|
|
|
for name in em_globals_list[0]:
|
2018-09-11 13:28:47 -03:00
|
|
|
merged_em_globals[name] = [em_globals[name]
|
|
|
|
for em_globals in em_globals_list]
|
2017-05-16 12:30:56 -03:00
|
|
|
|
|
|
|
return merged_em_globals
|
|
|
|
|
|
|
|
|
2016-05-06 11:42:35 -03:00
|
|
|
def generate_by_template(output_file, template_file, em_globals):
|
2018-09-11 13:28:47 -03:00
|
|
|
"""
|
|
|
|
Invokes empy intepreter to geneate output_file by the
|
|
|
|
given template_file and predefined em_globals dict
|
|
|
|
"""
|
|
|
|
# check if folder exists:
|
|
|
|
folder_name = os.path.dirname(output_file)
|
|
|
|
if not os.path.exists(folder_name):
|
|
|
|
os.makedirs(folder_name)
|
|
|
|
|
|
|
|
ofile = open(output_file, 'w')
|
|
|
|
# todo, reuse interpreter
|
|
|
|
interpreter = em.Interpreter(output=ofile, globals=em_globals, options={
|
|
|
|
em.RAW_OPT: True, em.BUFFERED_OPT: True})
|
|
|
|
try:
|
|
|
|
interpreter.file(open(template_file))
|
|
|
|
except OSError as e:
|
2016-04-25 18:36:13 -03:00
|
|
|
ofile.close()
|
2018-09-11 13:28:47 -03:00
|
|
|
os.remove(output_file)
|
|
|
|
raise
|
|
|
|
interpreter.shutdown()
|
|
|
|
ofile.close()
|
|
|
|
return True
|
2014-12-01 11:39:27 -04:00
|
|
|
|
|
|
|
|
2018-08-30 18:32:14 -03:00
|
|
|
def convert_dir(format_idx, inputdir, outputdir, package, templatedir):
|
2018-09-11 13:28:47 -03:00
|
|
|
"""
|
|
|
|
Converts all .msg files in inputdir to uORB header/source files
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Find the most recent modification time in input dir
|
|
|
|
maxinputtime = 0
|
|
|
|
for f in os.listdir(inputdir):
|
|
|
|
fni = os.path.join(inputdir, f)
|
|
|
|
if os.path.isfile(fni):
|
|
|
|
it = os.path.getmtime(fni)
|
|
|
|
if it > maxinputtime:
|
|
|
|
maxinputtime = it
|
|
|
|
|
|
|
|
# Find the most recent modification time in output dir
|
|
|
|
maxouttime = 0
|
|
|
|
if os.path.isdir(outputdir):
|
|
|
|
for f in os.listdir(outputdir):
|
|
|
|
fni = os.path.join(outputdir, f)
|
|
|
|
if os.path.isfile(fni):
|
|
|
|
it = os.path.getmtime(fni)
|
|
|
|
if it > maxouttime:
|
|
|
|
maxouttime = it
|
|
|
|
|
|
|
|
# Do not generate if nothing changed on the input
|
|
|
|
if (maxinputtime != 0 and maxouttime != 0 and maxinputtime < maxouttime):
|
|
|
|
return False
|
|
|
|
|
|
|
|
includepath = INCL_DEFAULT + [':'.join([package, inputdir])]
|
|
|
|
for f in os.listdir(inputdir):
|
|
|
|
# Ignore hidden files
|
|
|
|
if f.startswith("."):
|
|
|
|
continue
|
|
|
|
|
|
|
|
fn = os.path.join(inputdir, f)
|
|
|
|
# Only look at actual files
|
|
|
|
if not os.path.isfile(fn):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if fn[-4:].lower() != '.msg':
|
|
|
|
continue
|
|
|
|
|
|
|
|
generate_output_from_file(
|
|
|
|
format_idx, fn, outputdir, package, templatedir, includepath)
|
|
|
|
return True
|
2015-07-12 10:51:20 -03:00
|
|
|
|
2014-12-04 05:39:24 -04:00
|
|
|
|
2015-09-07 21:37:45 -03:00
|
|
|
def copy_changed(inputdir, outputdir, prefix='', quiet=False):
|
2018-09-11 13:28:47 -03:00
|
|
|
"""
|
|
|
|
Copies files from inputdir to outputdir if they don't exist in
|
|
|
|
ouputdir or if their content changed
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Make sure output directory exists:
|
|
|
|
if not os.path.isdir(outputdir):
|
|
|
|
os.makedirs(outputdir)
|
|
|
|
|
|
|
|
for input_file in os.listdir(inputdir):
|
|
|
|
fni = os.path.join(inputdir, input_file)
|
|
|
|
if os.path.isfile(fni):
|
|
|
|
# Check if input_file exists in outpoutdir, copy the file if not
|
|
|
|
fno = os.path.join(outputdir, prefix + input_file)
|
|
|
|
if not os.path.isfile(fno):
|
|
|
|
shutil.copy(fni, fno)
|
|
|
|
if not quiet:
|
|
|
|
print("{0}: new header file".format(fno))
|
|
|
|
continue
|
|
|
|
|
|
|
|
if os.path.getmtime(fni) > os.path.getmtime(fno):
|
|
|
|
# The file exists in inputdir and outputdir
|
|
|
|
# only copy if contents do not match
|
|
|
|
if not filecmp.cmp(fni, fno):
|
|
|
|
shutil.copy(fni, fno)
|
|
|
|
if not quiet:
|
|
|
|
print("{0}: updated".format(input_file))
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not quiet:
|
|
|
|
print("{0}: unchanged".format(input_file))
|
2014-12-04 05:39:24 -04:00
|
|
|
|
2015-01-23 05:33:18 -04:00
|
|
|
|
2018-08-30 18:32:14 -03:00
|
|
|
def convert_dir_save(format_idx, inputdir, outputdir, package, templatedir, temporarydir, prefix, quiet=False):
|
2018-09-11 13:28:47 -03:00
|
|
|
"""
|
|
|
|
Converts all .msg files in inputdir to uORB header files
|
|
|
|
Unchanged existing files are not overwritten.
|
|
|
|
"""
|
|
|
|
# Create new headers in temporary output directory
|
|
|
|
convert_dir(format_idx, inputdir, temporarydir, package, templatedir)
|
|
|
|
if generate_idx == 1:
|
|
|
|
generate_topics_list_file(inputdir, temporarydir, templatedir)
|
|
|
|
# Copy changed headers from temporary dir to output dir
|
|
|
|
copy_changed(temporarydir, outputdir, prefix, quiet)
|
|
|
|
|
2014-12-01 11:39:27 -04:00
|
|
|
|
2016-05-06 11:42:35 -03:00
|
|
|
def generate_topics_list_file(msgdir, outputdir, templatedir):
|
2018-09-11 13:28:47 -03:00
|
|
|
# generate cpp file with topics list
|
|
|
|
msgs = get_msgs_list(msgdir)
|
|
|
|
multi_topics = []
|
|
|
|
for msg in msgs:
|
|
|
|
msg_filename = os.path.join(msgdir, msg)
|
|
|
|
multi_topics.extend(get_multi_topics(msg_filename))
|
|
|
|
tl_globals = {"msgs": msgs, "multi_topics": multi_topics}
|
|
|
|
tl_template_file = os.path.join(templatedir, TOPICS_LIST_TEMPLATE_FILE)
|
|
|
|
tl_out_file = os.path.join(
|
2019-06-10 13:36:16 -03:00
|
|
|
outputdir, TOPICS_LIST_TEMPLATE_FILE.replace(".em", ""))
|
2018-09-11 13:28:47 -03:00
|
|
|
generate_by_template(tl_out_file, tl_template_file, tl_globals)
|
|
|
|
|
2017-05-16 12:30:56 -03:00
|
|
|
|
2016-08-25 08:28:44 -03:00
|
|
|
def generate_topics_list_file_from_files(files, outputdir, templatedir):
|
2018-09-11 13:28:47 -03:00
|
|
|
# generate cpp file with topics list
|
|
|
|
filenames = [os.path.basename(
|
|
|
|
p) for p in files if os.path.basename(p).endswith(".msg")]
|
|
|
|
multi_topics = []
|
|
|
|
for msg_filename in files:
|
|
|
|
multi_topics.extend(get_multi_topics(msg_filename))
|
|
|
|
tl_globals = {"msgs": filenames, "multi_topics": multi_topics}
|
|
|
|
tl_template_file = os.path.join(templatedir, TOPICS_LIST_TEMPLATE_FILE)
|
|
|
|
tl_out_file = os.path.join(
|
2019-06-10 13:36:16 -03:00
|
|
|
outputdir, TOPICS_LIST_TEMPLATE_FILE.replace(".em", ""))
|
2018-09-11 13:28:47 -03:00
|
|
|
generate_by_template(tl_out_file, tl_template_file, tl_globals)
|
|
|
|
|
2017-01-13 11:43:48 -04:00
|
|
|
|
2018-08-30 18:32:14 -03:00
|
|
|
def append_to_include_path(path_to_append, curr_include, package):
|
2017-01-13 11:43:48 -04:00
|
|
|
for p in path_to_append:
|
2018-08-30 18:32:14 -03:00
|
|
|
curr_include.append('%s:%s' % (package, p))
|
2017-01-13 11:43:48 -04:00
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
|
2014-12-01 11:39:27 -04:00
|
|
|
if __name__ == "__main__":
|
2018-09-11 13:28:47 -03:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Convert msg files to uorb headers/sources')
|
|
|
|
parser.add_argument('--headers', help='Generate header files',
|
|
|
|
action='store_true')
|
|
|
|
parser.add_argument('--sources', help='Generate source files',
|
|
|
|
action='store_true')
|
|
|
|
parser.add_argument('-d', dest='dir', help='directory with msg files')
|
|
|
|
parser.add_argument('-f', dest='file',
|
|
|
|
help="files to convert (use only without -d)",
|
|
|
|
nargs="+")
|
|
|
|
parser.add_argument('-i', dest="include_paths",
|
|
|
|
help='Additional Include Paths', nargs="*",
|
|
|
|
default=None)
|
|
|
|
parser.add_argument('-e', dest='templatedir',
|
|
|
|
help='directory with template files',)
|
|
|
|
parser.add_argument('-k', dest='package', default=PACKAGE,
|
|
|
|
help='package name')
|
|
|
|
parser.add_argument('-o', dest='outputdir',
|
|
|
|
help='output directory for header files')
|
|
|
|
parser.add_argument('-t', dest='temporarydir',
|
|
|
|
help='temporary directory')
|
|
|
|
parser.add_argument('-p', dest='prefix', default='',
|
|
|
|
help='string added as prefix to the output file '
|
|
|
|
' name when converting directories')
|
|
|
|
parser.add_argument('-q', dest='quiet', default=False, action='store_true',
|
|
|
|
help='string added as prefix to the output file '
|
|
|
|
' name when converting directories')
|
2019-10-18 06:23:32 -03:00
|
|
|
parser.add_argument('--constrained-flash', dest='constrained_flash', default=False, action='store_true',
|
|
|
|
help='set to save flash space')
|
2018-09-11 13:28:47 -03:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.include_paths:
|
|
|
|
append_to_include_path(args.include_paths, INCL_DEFAULT, args.package)
|
|
|
|
|
2019-10-18 06:23:32 -03:00
|
|
|
CONSTRAINED_FLASH = args.constrained_flash
|
|
|
|
|
2018-09-11 13:28:47 -03:00
|
|
|
if args.headers:
|
|
|
|
generate_idx = 0
|
|
|
|
elif args.sources:
|
|
|
|
generate_idx = 1
|
|
|
|
else:
|
|
|
|
print('Error: either --headers or --sources must be specified')
|
|
|
|
exit(-1)
|
|
|
|
if args.file is not None:
|
|
|
|
for f in args.file:
|
|
|
|
generate_output_from_file(
|
|
|
|
generate_idx, f, args.temporarydir, args.package, args.templatedir, INCL_DEFAULT)
|
|
|
|
if generate_idx == 1:
|
|
|
|
generate_topics_list_file_from_files(
|
|
|
|
args.file, args.outputdir, args.templatedir)
|
|
|
|
copy_changed(args.temporarydir, args.outputdir,
|
|
|
|
args.prefix, args.quiet)
|
|
|
|
elif args.dir is not None:
|
|
|
|
convert_dir_save(
|
|
|
|
generate_idx,
|
|
|
|
args.dir,
|
|
|
|
args.outputdir,
|
|
|
|
args.package,
|
|
|
|
args.templatedir,
|
|
|
|
args.temporarydir,
|
|
|
|
args.prefix,
|
|
|
|
args.quiet)
|