mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-06-01 02:55:07 +08:00
generate_microRTPS_bridge.py: beautify
This commit is contained in:
@@ -37,11 +37,16 @@
|
|||||||
# IDL for the topic messages. The PX4 msg definitions are used to create the IDL
|
# IDL for the topic messages. The PX4 msg definitions are used to create the IDL
|
||||||
# used by fastrtpsgen using templates.
|
# used by fastrtpsgen using templates.
|
||||||
|
|
||||||
import sys, os, argparse, shutil
|
import sys
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
|
import shutil
|
||||||
import px_generate_uorb_topic_files
|
import px_generate_uorb_topic_files
|
||||||
import subprocess, glob
|
import subprocess
|
||||||
|
import glob
|
||||||
import errno
|
import errno
|
||||||
|
|
||||||
|
|
||||||
def get_absolute_path(arg_parse_dir):
|
def get_absolute_path(arg_parse_dir):
|
||||||
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
||||||
@@ -55,29 +60,47 @@ def get_absolute_path(arg_parse_dir):
|
|||||||
|
|
||||||
return dir
|
return dir
|
||||||
|
|
||||||
default_client_out = get_absolute_path("src/modules/micrortps_bridge/micrortps_client")
|
|
||||||
default_agent_out = get_absolute_path("src/modules/micrortps_bridge/micrortps_agent")
|
default_client_out = get_absolute_path(
|
||||||
|
"src/modules/micrortps_bridge/micrortps_client")
|
||||||
|
default_agent_out = get_absolute_path(
|
||||||
|
"src/modules/micrortps_bridge/micrortps_agent")
|
||||||
default_uorb_templates_dir = "templates/uorb_microcdr"
|
default_uorb_templates_dir = "templates/uorb_microcdr"
|
||||||
default_urtps_templates_dir = "templates/urtps"
|
default_urtps_templates_dir = "templates/urtps"
|
||||||
default_package_name = px_generate_uorb_topic_files.PACKAGE
|
default_package_name = px_generate_uorb_topic_files.PACKAGE
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
|
|
||||||
parser.add_argument("-s", "--send", dest='send', metavar='*.msg', type=str, nargs='+', help="Topics to be sended")
|
parser.add_argument("-s", "--send", dest='send', metavar='*.msg',
|
||||||
parser.add_argument("-r", "--receive", dest='receive', metavar='*.msg', type=str, nargs='+', help="Topics to be received")
|
type=str, nargs='+', help="Topics to be sended")
|
||||||
parser.add_argument("-a", "--agent", dest='agent', action="store_true", help="Flag for generate the agent, by default is true if -c is not specified")
|
parser.add_argument("-r", "--receive", dest='receive', metavar='*.msg',
|
||||||
parser.add_argument("-c", "--client", dest='client', action="store_true", help="Flag for generate the client, by default is true if -a is not specified")
|
type=str, nargs='+', help="Topics to be received")
|
||||||
parser.add_argument("-i", "--generate-idl", dest='gen_idl', action="store_true", help="Flag for generate idl files for each msg")
|
parser.add_argument("-a", "--agent", dest='agent', action="store_true",
|
||||||
parser.add_argument("-j", "--idl-dir", dest='idl_dir', type=str, help="IDL files dir", default='')
|
help="Flag for generate the agent, by default is true if -c is not specified")
|
||||||
parser.add_argument("-m", "--mkdir-build", dest='mkdir_build', action="store_true", help="Flag to create 'build' dir")
|
parser.add_argument("-c", "--client", dest='client', action="store_true",
|
||||||
parser.add_argument("-t", "--topic-msg-dir", dest='msgdir', type=str, help="Topics message dir, by default msg/", default="msg")
|
help="Flag for generate the client, by default is true if -a is not specified")
|
||||||
parser.add_argument("-b", "--uorb-templates-dir", dest='uorb_templates', type=str, help="uORB templates dir, by default msg_dir/templates/uorb_microcdr", default=default_uorb_templates_dir)
|
parser.add_argument("-i", "--generate-idl", dest='gen_idl',
|
||||||
parser.add_argument("-q", "--urtps-templates-dir", dest='urtps_templates', type=str, help="uRTPS templates dir, by default msg_dir/templates/urtps", default=default_urtps_templates_dir)
|
action="store_true", help="Flag for generate idl files for each msg")
|
||||||
parser.add_argument("-p", "--package", dest='package', type=str, help="Msg package naming, by default px4", default=default_package_name)
|
parser.add_argument("-j", "--idl-dir", dest='idl_dir',
|
||||||
parser.add_argument("-o", "--agent-outdir", dest='agentdir', type=str, nargs=1, help="Agent output dir, by default src/modules/micrortps_bridge/micrortps_agent", default=default_agent_out)
|
type=str, help="IDL files dir", default='')
|
||||||
parser.add_argument("-u", "--client-outdir", dest='clientdir', type=str, nargs=1, help="Client output dir, by default src/modules/micrortps_bridge/micrortps_client", default=default_client_out)
|
parser.add_argument("-m", "--mkdir-build", dest='mkdir_build',
|
||||||
parser.add_argument("-f", "--fastrtpsgen-dir", dest='fastrtpsgen', type=str, nargs='?', help="fastrtpsgen installation dir, only needed if fastrtpsgen is not in PATH, by default empty", default="")
|
action="store_true", help="Flag to create 'build' dir")
|
||||||
parser.add_argument("--delete-tree", dest='del_tree', action="store_true", help="Delete dir tree output dir(s)")
|
parser.add_argument("-t", "--topic-msg-dir", dest='msgdir', type=str,
|
||||||
|
help="Topics message dir, by default msg/", default="msg")
|
||||||
|
parser.add_argument("-b", "--uorb-templates-dir", dest='uorb_templates', type=str,
|
||||||
|
help="uORB templates dir, by default msg_dir/templates/uorb_microcdr", default=default_uorb_templates_dir)
|
||||||
|
parser.add_argument("-q", "--urtps-templates-dir", dest='urtps_templates', type=str,
|
||||||
|
help="uRTPS templates dir, by default msg_dir/templates/urtps", default=default_urtps_templates_dir)
|
||||||
|
parser.add_argument("-p", "--package", dest='package', type=str,
|
||||||
|
help="Msg package naming, by default px4", default=default_package_name)
|
||||||
|
parser.add_argument("-o", "--agent-outdir", dest='agentdir', type=str, nargs=1,
|
||||||
|
help="Agent output dir, by default src/modules/micrortps_bridge/micrortps_agent", default=default_agent_out)
|
||||||
|
parser.add_argument("-u", "--client-outdir", dest='clientdir', type=str, nargs=1,
|
||||||
|
help="Client output dir, by default src/modules/micrortps_bridge/micrortps_client", default=default_client_out)
|
||||||
|
parser.add_argument("-f", "--fastrtpsgen-dir", dest='fastrtpsgen', type=str, nargs='?',
|
||||||
|
help="fastrtpsgen installation dir, only needed if fastrtpsgen is not in PATH, by default empty", default="")
|
||||||
|
parser.add_argument("--delete-tree", dest='del_tree',
|
||||||
|
action="store_true", help="Delete dir tree output dir(s)")
|
||||||
|
|
||||||
if len(sys.argv) <= 1:
|
if len(sys.argv) <= 1:
|
||||||
parser.print_usage()
|
parser.print_usage()
|
||||||
@@ -102,7 +125,8 @@ agent = args.agent
|
|||||||
client = args.client
|
client = args.client
|
||||||
mkdir_build = args.mkdir_build
|
mkdir_build = args.mkdir_build
|
||||||
del_tree = args.del_tree
|
del_tree = args.del_tree
|
||||||
px_generate_uorb_topic_files.append_to_include_path({msg_folder}, px_generate_uorb_topic_files.INCL_DEFAULT, package)
|
px_generate_uorb_topic_files.append_to_include_path(
|
||||||
|
{msg_folder}, px_generate_uorb_topic_files.INCL_DEFAULT, package)
|
||||||
agent_out_dir = get_absolute_path(args.agentdir)
|
agent_out_dir = get_absolute_path(args.agentdir)
|
||||||
client_out_dir = get_absolute_path(args.clientdir)
|
client_out_dir = get_absolute_path(args.clientdir)
|
||||||
gen_idl = args.gen_idl
|
gen_idl = args.gen_idl
|
||||||
@@ -117,7 +141,8 @@ if args.fastrtpsgen is None or args.fastrtpsgen == "":
|
|||||||
fastrtpsgen_path = "fastrtpsgen"
|
fastrtpsgen_path = "fastrtpsgen"
|
||||||
else:
|
else:
|
||||||
# Path to fastrtpsgen is explicitly specified
|
# Path to fastrtpsgen is explicitly specified
|
||||||
fastrtpsgen_path = os.path.join(get_absolute_path(args.fastrtpsgen), "/fastrtpsgen")
|
fastrtpsgen_path = os.path.join(
|
||||||
|
get_absolute_path(args.fastrtpsgen), "/fastrtpsgen")
|
||||||
|
|
||||||
# If nothing specified it's generated both
|
# If nothing specified it's generated both
|
||||||
if agent == False and client == False:
|
if agent == False and client == False:
|
||||||
@@ -126,7 +151,8 @@ if agent == False and client == False:
|
|||||||
|
|
||||||
if del_tree:
|
if del_tree:
|
||||||
if agent:
|
if agent:
|
||||||
_continue = str(raw_input("\nFiles in " + agent_out_dir + " will be erased, continue?[Y/n]\n"))
|
_continue = str(raw_input("\nFiles in " + agent_out_dir +
|
||||||
|
" will be erased, continue?[Y/n]\n"))
|
||||||
if _continue == "N" or _continue == "n":
|
if _continue == "N" or _continue == "n":
|
||||||
print("Aborting execution...")
|
print("Aborting execution...")
|
||||||
exit(-1)
|
exit(-1)
|
||||||
@@ -135,7 +161,8 @@ if del_tree:
|
|||||||
shutil.rmtree(agent_out_dir)
|
shutil.rmtree(agent_out_dir)
|
||||||
|
|
||||||
if client:
|
if client:
|
||||||
_continue = str(raw_input("\nFiles in " + client_out_dir + " will be erased, continue?[Y/n]\n"))
|
_continue = str(raw_input(
|
||||||
|
"\nFiles in " + client_out_dir + " will be erased, continue?[Y/n]\n"))
|
||||||
if _continue == "N" or _continue == "n":
|
if _continue == "N" or _continue == "n":
|
||||||
print("Aborting execution...")
|
print("Aborting execution...")
|
||||||
exit(-1)
|
exit(-1)
|
||||||
@@ -159,6 +186,7 @@ uRTPS_PUBLISHER_H_TEMPL_FILE = 'Publisher.h.template'
|
|||||||
uRTPS_SUBSCRIBER_SRC_TEMPL_FILE = 'Subscriber.cpp.template'
|
uRTPS_SUBSCRIBER_SRC_TEMPL_FILE = 'Subscriber.cpp.template'
|
||||||
uRTPS_SUBSCRIBER_H_TEMPL_FILE = 'Subscriber.h.template'
|
uRTPS_SUBSCRIBER_H_TEMPL_FILE = 'Subscriber.h.template'
|
||||||
|
|
||||||
|
|
||||||
def generate_agent(out_dir):
|
def generate_agent(out_dir):
|
||||||
|
|
||||||
if msg_files_send:
|
if msg_files_send:
|
||||||
@@ -205,9 +233,11 @@ def generate_agent(out_dir):
|
|||||||
if not glob.glob(os.path.join(idl_dir, "*.idl")):
|
if not glob.glob(os.path.join(idl_dir, "*.idl")):
|
||||||
raise Exception("No IDL files found in %s" % idl_dir)
|
raise Exception("No IDL files found in %s" % idl_dir)
|
||||||
for idl_file in glob.glob(os.path.join(idl_dir, "*.idl")):
|
for idl_file in glob.glob(os.path.join(idl_dir, "*.idl")):
|
||||||
ret = subprocess.call(fastrtpsgen_path + " -d " + out_dir + "/fastrtpsgen -example x64Linux2.6gcc " + idl_file, shell=True)
|
ret = subprocess.call(fastrtpsgen_path + " -d " + out_dir +
|
||||||
|
"/fastrtpsgen -example x64Linux2.6gcc " + idl_file, shell=True)
|
||||||
if ret:
|
if ret:
|
||||||
raise Exception("fastrtpsgen not found. Specify the location of fastrtpsgen with the -f flag")
|
raise Exception(
|
||||||
|
"fastrtpsgen not found. Specify the location of fastrtpsgen with the -f flag")
|
||||||
rm_wildcard(os.path.join(out_dir, "fastrtpsgen/*PubSubMain*"))
|
rm_wildcard(os.path.join(out_dir, "fastrtpsgen/*PubSubMain*"))
|
||||||
rm_wildcard(os.path.join(out_dir, "fastrtpsgen/makefile*"))
|
rm_wildcard(os.path.join(out_dir, "fastrtpsgen/makefile*"))
|
||||||
rm_wildcard(os.path.join(out_dir, "fastrtpsgen/*Publisher*"))
|
rm_wildcard(os.path.join(out_dir, "fastrtpsgen/*Publisher*"))
|
||||||
@@ -217,21 +247,26 @@ def generate_agent(out_dir):
|
|||||||
cp_wildcard(os.path.join(out_dir, "fastrtpsgen/*"), out_dir)
|
cp_wildcard(os.path.join(out_dir, "fastrtpsgen/*"), out_dir)
|
||||||
if os.path.isdir(os.path.join(out_dir, "fastrtpsgen")):
|
if os.path.isdir(os.path.join(out_dir, "fastrtpsgen")):
|
||||||
shutil.rmtree(os.path.join(out_dir, "fastrtpsgen"))
|
shutil.rmtree(os.path.join(out_dir, "fastrtpsgen"))
|
||||||
cp_wildcard(os.path.join(urtps_templates_dir, "microRTPS_transport.*"), agent_out_dir)
|
cp_wildcard(os.path.join(urtps_templates_dir,
|
||||||
os.rename(os.path.join(out_dir, "microRTPS_agent_CMakeLists.txt"), os.path.join(out_dir, "CMakeLists.txt"))
|
"microRTPS_transport.*"), agent_out_dir)
|
||||||
|
os.rename(os.path.join(out_dir, "microRTPS_agent_CMakeLists.txt"),
|
||||||
|
os.path.join(out_dir, "CMakeLists.txt"))
|
||||||
if (mkdir_build):
|
if (mkdir_build):
|
||||||
mkdir_p(os.path.join(out_dir, "build"))
|
mkdir_p(os.path.join(out_dir, "build"))
|
||||||
os.chdir(prev_cwd_path)
|
os.chdir(prev_cwd_path)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def rm_wildcard(pattern):
|
def rm_wildcard(pattern):
|
||||||
for f in glob.glob(pattern):
|
for f in glob.glob(pattern):
|
||||||
os.remove(f)
|
os.remove(f)
|
||||||
|
|
||||||
|
|
||||||
def cp_wildcard(pattern, destdir):
|
def cp_wildcard(pattern, destdir):
|
||||||
for f in glob.glob(pattern):
|
for f in glob.glob(pattern):
|
||||||
shutil.copy(f, destdir)
|
shutil.copy(f, destdir)
|
||||||
|
|
||||||
|
|
||||||
def mkdir_p(dirpath):
|
def mkdir_p(dirpath):
|
||||||
try:
|
try:
|
||||||
os.makedirs(dirpath)
|
os.makedirs(dirpath)
|
||||||
@@ -241,6 +276,7 @@ def mkdir_p(dirpath):
|
|||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def generate_client(out_dir):
|
def generate_client(out_dir):
|
||||||
|
|
||||||
# Rename work in the default path
|
# Rename work in the default path
|
||||||
@@ -259,10 +295,12 @@ def generate_client(out_dir):
|
|||||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, uRTPS_CLIENT_TEMPL_FILE)
|
package, px_generate_uorb_topic_files.INCL_DEFAULT, uRTPS_CLIENT_TEMPL_FILE)
|
||||||
|
|
||||||
# Final steps to install client
|
# Final steps to install client
|
||||||
cp_wildcard(os.path.join(urtps_templates_dir, "microRTPS_transport.*"), out_dir)
|
cp_wildcard(os.path.join(urtps_templates_dir,
|
||||||
|
"microRTPS_transport.*"), out_dir)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
if agent:
|
if agent:
|
||||||
generate_agent(agent_out_dir)
|
generate_agent(agent_out_dir)
|
||||||
print("\nAgent created in: " + agent_out_dir)
|
print("\nAgent created in: " + agent_out_dir)
|
||||||
|
|||||||
Reference in New Issue
Block a user