1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import struct
16+ import fcntl
1517import os
18+ import sys
1619
1720from rosidl_cmake import convert_camel_case_to_lower_case_underscore
1821from rosidl_cmake import expand_template
2124from rosidl_parser import parse_message_file
2225from rosidl_parser import parse_service_file
2326from rosidl_parser import validate_field_types
27+ from shutil import copyfile
2428from pathlib import Path
2529
2630
@@ -33,6 +37,34 @@ def generate_micro_ros_agent_xml_support(args):
3337 'get_header_filename_from_msg_name' : convert_camel_case_to_lower_case_underscore ,
3438 }
3539
40+
41+ # Set file format
42+ file_format = ".xml"
43+ ros2_prefix = "rt/"
44+
45+
46+ # Check destination dir
47+ dest_dir = args ['output_dir' ]
48+ if not os .path .exists (dest_dir ):
49+ os .makedirs (dest_dir )
50+
51+
52+ # Check source dir
53+ srcs_dir = os .path .join (dest_dir , "srcs" )
54+ if not os .path .exists (srcs_dir ):
55+ os .makedirs (srcs_dir )
56+
57+
58+ # Copy all included xml files
59+ for filename in os .listdir (args ['template_dir' ]):
60+ if filename .endswith (file_format ):
61+ template_src_path = os .path .join (args ['template_dir' ], filename )
62+ template_dest_path = os .path .join (srcs_dir , filename )
63+ if not os .path .isfile (template_dest_path ):
64+ copyfile (template_src_path , template_dest_path )
65+
66+
67+ # Iterate throw all msgs/srvs
3668 for idl_file in args ['ros_interface_files' ]:
3769 extension = os .path .splitext (idl_file )[1 ]
3870 if extension == '.msg' :
@@ -50,97 +82,84 @@ def generate_micro_ros_agent_xml_support(args):
5082 data .update (functions )
5183
5284
53- # Make destinatino dir
54- if not os .path .exists (args ['output_dir' ]):
55- os .makedirs (args ['output_dir' ])
56-
57- # Check if publixher exists
58- pub_file_path = os .path .join (args ['output_dir' ], 'publisher.xml' )
59- pub_file = Path (pub_file_path )
60- if not pub_file .is_file ():
61- publ = open (pub_file_path , 'a' )
62- publ .write ("<profiles>\n " )
63- else :
64- publ = open (pub_file_path )
65- lines = publ .readlines ()
66- publ .close ()
67- publ = open (pub_file_path ,'w' )
68- publ .writelines ([item for item in lines [:- 1 ]])
69- #publ = open(pub_file_path, "w")
70- #publ.seek(publ.seek(-1,2) - len("</profiles>\n"))
71-
72-
73-
74- publ .write (" <publisher profile_name=\" default_xrce_publisher_profile\" >\n " )
75- publ .write (" <topic>\n " )
76- publ .write (" <kind>NO_KEY</kind>\n " )
77- publ .write (" <name>%sPubSubTopic</name>\n " % (spec .msg_name ))
78- publ .write (" <dataType>%s::%s::dds_::%s_</dataType>\n " % (spec .base_type .pkg_name , subfolder , spec .msg_name ))
79- publ .write (" <historyQos>\n " )
80- publ .write (" <kind>KEEP_LAST</kind>\n " )
81- publ .write (" <depth>5</depth>\n " )
82- publ .write (" </historyQos>\n " )
83- publ .write (" <durability>\n " )
84- publ .write (" <kind>TRANSIENT_LOCAL</kind>\n " )
85- publ .write (" </durability>\n " )
86- publ .write (" </topic>\n " )
87- publ .write (" </publisher>\n " )
88- publ .write ("</profiles>\n " )
89- publ .close ()
90-
91-
92-
93- # Check if subcriber exists
94- subs_file_path = os .path .join (args ['output_dir' ], 'subscriber.xml' )
95- subs_file = Path (subs_file_path )
96- if not subs_file .is_file ():
97- subs = open (subs_file_path , 'a' )
98- subs .write ("<profiles>\n " )
99- else :
100- subs = open (subs_file_path )
101- lines = subs .readlines ()
102- subs .close ()
103- subs = open (subs_file_path ,'w' )
104- subs .writelines ([item for item in lines [:- 1 ]])
105-
106-
107- subs .write (" <subscriber profile_name=\" default_xrce_subscriber_profile\" >\n " )
108- subs .write (" <topic>\n " )
109- subs .write (" <kind>NO_KEY</kind>\n " )
110- subs .write (" <name>%sPubSubTopic</name>\n " % (spec .msg_name ))
111- subs .write (" <dataType>%s::%s::dds_::%s_</dataType>\n " % (spec .base_type .pkg_name , subfolder , spec .msg_name ))
112- subs .write (" <historyQos>\n " )
113- subs .write (" <kind>KEEP_LAST</kind>\n " )
114- subs .write (" <depth>5</depth>\n " )
115- subs .write (" </historyQos>\n " )
116- subs .write (" <durability>\n " )
117- subs .write (" <kind>TRANSIENT_LOCAL</kind>\n " )
118- subs .write (" </durability>\n " )
119- subs .write (" </topic>\n " )
120- subs .write (" </subscriber>\n " )
121- subs .write ("</profiles>\n " )
122- subs .close ()
123-
124-
125- # Check if topic exists
126- topi_file_path = os .path .join (args ['output_dir' ], 'topic.xml' )
127- topi_file = Path (topi_file_path )
128- if not topi_file .is_file ():
129- topi = open (topi_file_path , 'a' )
130- topi .write ("<dds>\n " )
131- else :
132- topi = open (topi_file_path )
133- lines = topi .readlines ()
134- topi .close ()
135- topi = open (topi_file_path ,'w' )
136- topi .writelines ([item for item in lines [:- 1 ]])
137-
138- topi .write (" <topic>\n " )
139- topi .write (" <name>%sPubSubTopic</name>\n " % (spec .msg_name ))
140- topi .write (" <dataType>%s::%s::dds_::%s_</dataType>\n " % (spec .base_type .pkg_name , subfolder , spec .msg_name ))
141- topi .write (" </topic>\n " )
142- topi .write ("</dds>\n " )
143- topi .close ()
85+ # Generate source file path
86+ src_file = os .path .join (srcs_dir , "%s_%s_%s.xml" % (spec .base_type .pkg_name , subfolder , spec .msg_name ))
87+
88+
89+ # Publisher
90+ file_content = " <publisher profile_name=\" %s_%s_%s_p\" >\n " % (spec .base_type .pkg_name , subfolder , spec .msg_name )
91+ file_content += " <topic profile_name=\" %s_%s_%s_t\" >\n " % (spec .base_type .pkg_name , subfolder , spec .msg_name )
92+ file_content += " <kind>NO_KEY</kind>\n "
93+ file_content += " <name>%s%s_%s_%s</name>\n " % (ros2_prefix , spec .base_type .pkg_name , subfolder , spec .msg_name )
94+ file_content += " <dataType>%s::%s::dds_::%s_</dataType>\n " % (spec .base_type .pkg_name , subfolder , spec .msg_name )
95+ file_content += " <historyQos>\n "
96+ file_content += " <kind>KEEP_LAST</kind>\n "
97+ file_content += " <depth>5</depth>\n "
98+ file_content += " </historyQos>\n "
99+ file_content += " <durability>\n "
100+ file_content += " <kind>TRANSIENT_LOCAL</kind>\n "
101+ file_content += " </durability>\n "
102+ file_content += " </topic>\n "
103+ file_content += " </publisher>\n "
104+
105+
106+ # Subscriber
107+ file_content += " <subscriber profile_name=\" %s_%s_%s_s\" >\n " % (spec .base_type .pkg_name , subfolder , spec .msg_name )
108+ file_content += " <topic profile_name=\" %s_%s_%s_t\" >\n " % (spec .base_type .pkg_name , subfolder , spec .msg_name )
109+ file_content += " <kind>NO_KEY</kind>\n "
110+ file_content += " <name>%s%s_%s_%s</name>\n " % (ros2_prefix , spec .base_type .pkg_name , subfolder , spec .msg_name )
111+ file_content += " <dataType>%s::%s::dds_::%s_</dataType>\n " % (spec .base_type .pkg_name , subfolder , spec .msg_name )
112+ file_content += " <historyQos>\n "
113+ file_content += " <kind>KEEP_LAST</kind>\n "
114+ file_content += " <depth>5</depth>\n "
115+ file_content += " </historyQos>\n "
116+ file_content += " <durability>\n "
117+ file_content += " <kind>TRANSIENT_LOCAL</kind>\n "
118+ file_content += " </durability>\n "
119+ file_content += " </topic>\n "
120+ file_content += " </subscriber>\n "
121+
122+
123+ # Topic
124+ file_content += " <topic profile_name=\" %s_%s_%s_t\" >\n " % (spec .base_type .pkg_name , subfolder , spec .msg_name )
125+ file_content += " <name>%s%s_%s_%s</name>\n " % (ros2_prefix , spec .base_type .pkg_name , subfolder , spec .msg_name )
126+ file_content += " <dataType>%s::%s::dds_::%s_</dataType>\n " % (spec .base_type .pkg_name , subfolder , spec .msg_name )
127+ file_content += " </topic>\n "
128+
129+
130+ # Write file content
131+ fd1 = open (src_file , "w" )
132+ fcntl .lockf (fd1 , fcntl .LOCK_EX )
133+ fd1 .write (file_content )
134+ fd1 .close ()
135+
136+
137+ # Open collect file
138+ collec_file = os .path .join (args ['output_dir' ], "DEFAULT_FASTRTPS_PROFILES.xml" )
139+ fd2 = open (collec_file , "w" )
140+ fcntl .lockf (fd2 , fcntl .LOCK_EX )
141+
142+
143+ # Generate head
144+ fd2 .write ("<profiles>\n " )
145+
146+
147+ # Append all files contents
148+ for filename in os .listdir (srcs_dir ):
149+ if filename .endswith (".xml" ):
150+ fd3 = open (os .path .join (srcs_dir , filename ), "r+" )
151+ fcntl .lockf (fd3 , fcntl .LOCK_EX )
152+ fd2 .write (fd3 .read ())
153+ fd3 .close ()
154+
155+
156+ # Generate tail
157+ fd2 .write ("</profiles>\n " )
158+
159+
160+ # Close file
161+ fd2 .close ()
162+
144163
145164 #elif extension == '.srv':
146165
@@ -153,4 +172,4 @@ def generate_micro_ros_agent_xml_support(args):
153172 #f = open(os.path.join(args['output_dir'], 'demofile.txt'), 'w+')
154173 #f.write("%s\n" % spec)
155174 #f.close()
156- return 0
175+ return 0
0 commit comments