-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathros_builder.py
More file actions
331 lines (277 loc) · 12.9 KB
/
ros_builder.py
File metadata and controls
331 lines (277 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"""Builders for auto-generated ROS1 and ROS2 package wrappers."""
import opengen.definitions as og_dfn
import datetime
import logging
import os
import shutil
import sys
import jinja2
def make_dir_if_not_exists(directory):
"""Create ``directory`` if it does not already exist.
:param directory: Path to the directory to create.
:type directory: str
"""
if not os.path.exists(directory):
os.makedirs(directory)
def get_ros_template(template_subdir, name):
"""Load a Jinja template from a ROS-specific template subdirectory.
:param template_subdir: Template subdirectory name, e.g. ``"ros"`` or
``"ros2"``.
:type template_subdir: str
:param name: Template file name.
:type name: str
:return: Loaded Jinja template.
:rtype: jinja2.Template
"""
file_loader = jinja2.FileSystemLoader(og_dfn.templates_subdir(template_subdir))
env = jinja2.Environment(loader=file_loader, autoescape=True)
return env.get_template(name)
class _BaseRosBuilder:
"""
Shared code generation logic for ROS-related packages.
This base class contains the common file-generation pipeline used by both
:class:`RosBuilder` and :class:`ROS2Builder`. Subclasses specialize the
process by providing the package configuration object, template
subdirectory, launch file name, and final user-facing instructions.
:ivar _meta: Optimizer metadata used to render the package templates.
:ivar _build_config: Global build configuration for the generated solver.
:ivar _solver_config: Solver configuration used when rendering node code.
:ivar _logger: Logger dedicated to the concrete builder implementation.
"""
#: Template subdirectory under ``opengen/templates`` used by the builder.
_template_subdir = None
#: Fully-qualified logger name for the concrete builder.
_logger_name = None
#: Short logger tag shown in log messages.
_logger_tag = None
#: Launch file generated by the concrete builder.
_launch_file_name = None
def __init__(self, meta, build_config, solver_config):
"""Initialise a shared ROS package builder.
:param meta: Optimizer metadata.
:param build_config: Build configuration object.
:param solver_config: Solver configuration object.
"""
self._meta = meta
self._build_config = build_config
self._solver_config = solver_config
self._logger = logging.getLogger(self._logger_name)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(1)
c_format = logging.Formatter(
f'[%(levelname)s] <<{self._logger_tag}>> %(message)s')
stream_handler.setFormatter(c_format)
self._logger.setLevel(1)
self._logger.handlers.clear()
self._logger.addHandler(stream_handler)
self._logger.propagate = False
@property
def _ros_config(self):
"""Return the ROS/ROS2 package configuration for the subclass.
:return: ROS configuration object used by the concrete builder.
:raises NotImplementedError: If a subclass does not provide this hook.
"""
raise NotImplementedError
def _template(self, name):
"""Return a template from the builder's template subdirectory.
:param name: Template file name.
:type name: str
:return: Loaded Jinja template.
:rtype: jinja2.Template
"""
return get_ros_template(self._template_subdir, name)
def _target_dir(self):
"""Return the root directory of the generated optimizer project.
:return: Absolute path to the generated optimizer directory.
:rtype: str
"""
return os.path.abspath(
os.path.join(
self._build_config.build_dir,
self._meta.optimizer_name))
def _ros_target_dir(self):
"""Return the root directory of the generated ROS package.
:return: Absolute path to the generated ROS/ROS2 package directory.
:rtype: str
"""
return os.path.abspath(
os.path.join(
self._build_config.build_dir,
self._meta.optimizer_name,
self._ros_config.package_name))
def _generate_ros_dir_structure(self):
"""Create the directory structure for the generated ROS package."""
self._logger.info("Generating directory structure")
target_ros_dir = self._ros_target_dir()
make_dir_if_not_exists(target_ros_dir)
for directory_name in ('include', 'extern_lib', 'src', 'msg', 'config', 'launch'):
make_dir_if_not_exists(os.path.abspath(
os.path.join(target_ros_dir, directory_name)))
def _generate_ros_package_xml(self):
"""Render and write ``package.xml`` for the generated package."""
self._logger.info("Generating package.xml")
target_ros_dir = self._ros_target_dir()
template = self._template('package.xml')
output_template = template.render(meta=self._meta, ros=self._ros_config)
target_rospkg_path = os.path.join(target_ros_dir, "package.xml")
with open(target_rospkg_path, "w", encoding="utf-8") as fh:
fh.write(output_template)
def _generate_ros_cmakelists(self):
"""Render and write the package ``CMakeLists.txt`` file."""
self._logger.info("Generating CMakeLists")
target_ros_dir = self._ros_target_dir()
template = self._template('CMakeLists.txt')
output_template = template.render(meta=self._meta, ros=self._ros_config)
target_rospkg_path = os.path.join(target_ros_dir, "CMakeLists.txt")
with open(target_rospkg_path, "w", encoding="utf-8") as fh:
fh.write(output_template)
def _copy_ros_files(self):
"""Copy generated bindings, static library, and message files."""
self._logger.info("Copying external dependencies")
target_ros_dir = self._ros_target_dir()
header_file_name = self._meta.optimizer_name + '_bindings.hpp'
target_include_filename = os.path.abspath(
os.path.join(target_ros_dir, 'include', header_file_name))
original_include_file = os.path.abspath(
os.path.join(self._target_dir(), header_file_name))
shutil.copyfile(original_include_file, target_include_filename)
if sys.platform == "win32":
lib_file_name = self._meta.optimizer_name + '.lib'
else:
lib_file_name = 'lib' + self._meta.optimizer_name + '.a'
target_lib_file_name = os.path.abspath(
os.path.join(target_ros_dir, 'extern_lib', lib_file_name))
original_lib_file = os.path.abspath(
os.path.join(
self._target_dir(),
'target',
self._build_config.build_mode,
lib_file_name))
shutil.copyfile(original_lib_file, target_lib_file_name)
for message_name in ('OptimizationParameters.msg', 'OptimizationResult.msg'):
original_message = os.path.abspath(
os.path.join(
og_dfn.templates_dir(),
self._template_subdir,
message_name))
target_message = os.path.abspath(
os.path.join(target_ros_dir, 'msg', message_name))
shutil.copyfile(original_message, target_message)
def _generate_ros_params_file(self):
"""Render and write the runtime parameter YAML file."""
self._logger.info("Generating open_params.yaml")
target_ros_dir = self._ros_target_dir()
template = self._template('open_params.yaml')
output_template = template.render(meta=self._meta, ros=self._ros_config)
target_yaml_fname = os.path.join(target_ros_dir, "config", "open_params.yaml")
with open(target_yaml_fname, "w", encoding="utf-8") as fh:
fh.write(output_template)
def _generate_ros_node_header(self):
"""Render and write the generated node header file."""
self._logger.info("Generating open_optimizer.hpp")
target_ros_dir = self._ros_target_dir()
template = self._template('open_optimizer.hpp')
output_template = template.render(
meta=self._meta,
ros=self._ros_config,
solver_config=self._solver_config)
target_rosnode_header_path = os.path.join(
target_ros_dir, "include", "open_optimizer.hpp")
with open(target_rosnode_header_path, "w", encoding="utf-8") as fh:
fh.write(output_template)
def _generate_ros_node_cpp(self):
"""Render and write the generated node implementation file."""
self._logger.info("Generating open_optimizer.cpp")
target_ros_dir = self._ros_target_dir()
template = self._template('open_optimizer.cpp')
output_template = template.render(
meta=self._meta,
ros=self._ros_config,
timestamp_created=datetime.datetime.now())
target_rosnode_cpp_path = os.path.join(target_ros_dir, "src", "open_optimizer.cpp")
with open(target_rosnode_cpp_path, "w", encoding="utf-8") as fh:
fh.write(output_template)
def _generate_ros_launch_file(self):
"""Render and write the package launch file."""
self._logger.info("Generating %s", self._launch_file_name)
target_ros_dir = self._ros_target_dir()
template = self._template(self._launch_file_name)
output_template = template.render(meta=self._meta, ros=self._ros_config)
target_rosnode_launch_path = os.path.join(
target_ros_dir, "launch", self._launch_file_name)
with open(target_rosnode_launch_path, "w", encoding="utf-8") as fh:
fh.write(output_template)
def _generate_ros_readme_file(self):
"""Render and write the generated package README."""
self._logger.info("Generating README.md")
target_ros_dir = self._ros_target_dir()
template = self._template('README.md')
output_template = template.render(ros=self._ros_config)
target_readme_path = os.path.join(target_ros_dir, "README.md")
with open(target_readme_path, "w", encoding="utf-8") as fh:
fh.write(output_template)
def _symbolic_link_info_message(self):
"""Emit final user-facing setup instructions for the generated package.
:raises NotImplementedError: If a subclass does not provide this hook.
"""
raise NotImplementedError
def build(self):
"""
Generate all ROS/ROS2 wrapper files for the current optimizer.
This method creates the package directory structure, copies the
generated solver artefacts, renders all templates, and logs final setup
instructions for the user.
"""
self._generate_ros_dir_structure()
self._generate_ros_package_xml()
self._generate_ros_cmakelists()
self._copy_ros_files()
self._generate_ros_params_file()
self._generate_ros_node_header()
self._generate_ros_node_cpp()
self._generate_ros_launch_file()
self._generate_ros_readme_file()
self._symbolic_link_info_message()
class RosBuilder(_BaseRosBuilder):
"""
Builder for ROS1 package generation.
This specialization uses the ``templates/ros`` template set and the
ROS1-specific configuration stored in
:attr:`opengen.config.build_config.BuildConfiguration.ros_config`.
"""
_template_subdir = 'ros'
_logger_name = 'opengen.builder.RosBuilder'
_logger_tag = 'ROS'
_launch_file_name = 'open_optimizer.launch'
@property
def _ros_config(self):
"""Return the ROS1 package configuration."""
return self._build_config.ros_config
def _symbolic_link_info_message(self):
"""Log the final ROS1 workspace integration instructions."""
target_ros_dir = self._ros_target_dir()
self._logger.info("ROS package was built successfully. Now run:")
self._logger.info("ln -s %s ~/catkin_ws/src/", target_ros_dir)
self._logger.info("cd ~/catkin_ws/; catkin_make")
class ROS2Builder(_BaseRosBuilder):
"""
Builder for ROS2 package generation.
This specialization uses the ``templates/ros2`` template set and the
ROS2-specific configuration stored in
:attr:`opengen.config.build_config.BuildConfiguration.ros2_config`.
"""
_template_subdir = 'ros2'
_logger_name = 'opengen.builder.ROS2Builder'
_logger_tag = 'ROS2'
_launch_file_name = 'open_optimizer.launch.py'
@property
def _ros_config(self):
"""Return the ROS2 package configuration."""
return self._build_config.ros2_config
def _symbolic_link_info_message(self):
"""Log the final ROS2 workspace integration instructions."""
target_ros_dir = self._ros_target_dir()
self._logger.info("ROS2 package was built successfully. Now run:")
self._logger.info("ln -s %s ~/ros2_ws/src/", target_ros_dir)
self._logger.info("cd ~/ros2_ws/; colcon build --packages-select %s",
self._ros_config.package_name)