|
| 1 | +# Copyright 2021 Open Source Robotics Foundation, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import contextlib |
| 16 | +import glob |
| 17 | +import json |
| 18 | +import os |
| 19 | +import tempfile |
| 20 | + |
| 21 | + |
| 22 | +def package_name_from_include_path(path): |
| 23 | + return os.path.basename(os.path.dirname(os.path.dirname(path))) |
| 24 | + |
| 25 | + |
| 26 | +def dependencies_from_include_paths(include_paths): |
| 27 | + return list({ |
| 28 | + f'{package_name_from_include_path(path)}:{path}' |
| 29 | + for include_path in include_paths |
| 30 | + for path in glob.iglob(f'{include_path}/**/*.idl', recursive=True) |
| 31 | + }) |
| 32 | + |
| 33 | + |
| 34 | +def idl_tuples_from_interface_files(interface_files): |
| 35 | + idl_tuples = [] |
| 36 | + for path in interface_files: |
| 37 | + if ':' not in path: |
| 38 | + prefix = os.getcwd() |
| 39 | + stem = path |
| 40 | + else: |
| 41 | + prefix, _, stem = path.partition(':') |
| 42 | + prefix = os.path.realpath(prefix) |
| 43 | + if os.path.isabs(stem): |
| 44 | + raise ValueError() |
| 45 | + idl_tuples.append(f'{prefix}:{stem}') |
| 46 | + return idl_tuples |
| 47 | + |
| 48 | + |
| 49 | +@contextlib.contextmanager |
| 50 | +def legacy_generator_arguments_file( |
| 51 | + package_name, interface_files, |
| 52 | + include_paths, templates_path, |
| 53 | + output_path |
| 54 | +): |
| 55 | + idl_tuples = idl_tuples_from_interface_files(interface_files) |
| 56 | + interface_dependencies = dependencies_from_include_paths(include_paths) |
| 57 | + with tempfile.NamedTemporaryFile(mode='w') as tmp: |
| 58 | + tmp.write(json.dumps({ |
| 59 | + 'package_name': package_name, |
| 60 | + 'output_dir': output_path, |
| 61 | + 'template_dir': templates_path, |
| 62 | + 'idl_tuples': idl_tuples, |
| 63 | + 'ros_interface_dependencies': interface_dependencies, |
| 64 | + 'target_dependencies': [] |
| 65 | + })) |
| 66 | + tmp.flush() |
| 67 | + yield tmp.name |
0 commit comments