|
1 |
| -# This script produces the `vendor` module inside `rclrs` by copying |
2 |
| -# the generated code for the `rosgraph_msgs` and `rcl_interfaces` packages and |
3 |
| -# its dependency `builtin_interfaces` and adjusting the submodule paths in the |
4 |
| -# code. |
| 1 | +#!/usr/bin/env python3 |
| 2 | +# This script produces the `vendor` module inside `rclrs` by copying the |
| 3 | +# generated code for the `rosgraph_msgs`, `rcl_interfaces`, and `action_msgs` |
| 4 | +# packages and their dependencies `builtin_interfaces` and |
| 5 | +# `unique_identifier_msgs` and adjusting the submodule paths in the code. |
5 | 6 | # If these packages, or the `rosidl_generator_rs`, get changed, you can
|
6 | 7 | # update the `vendor` module by running this script.
|
7 |
| -# The purpose is to avoid an external dependency on `rcl_interfaces`, which |
8 |
| -# is not published on crates.io. |
| 8 | +# The purpose is to avoid an external dependency on these message packages, |
| 9 | +# which are not published on crates.io. |
9 | 10 |
|
10 | 11 | import argparse
|
11 | 12 | from pathlib import Path
|
12 | 13 | import shutil
|
13 | 14 | import subprocess
|
14 | 15 |
|
| 16 | +vendored_packages = [ |
| 17 | + "action_msgs", |
| 18 | + "builtin_interfaces", |
| 19 | + "rcl_interfaces", |
| 20 | + "rosgraph_msgs", |
| 21 | + "unique_identifier_msgs", |
| 22 | +] |
| 23 | + |
15 | 24 | def get_args():
|
16 |
| - parser = argparse.ArgumentParser(description='Vendor the rcl_interfaces, builtin_interfaces and rosgraph_msgs packages into rclrs') |
| 25 | + parser = argparse.ArgumentParser(description='Vendor message packages into rclrs') |
17 | 26 | parser.add_argument('install_base', metavar='install_base', type=Path,
|
18 | 27 | help='the install base (must have non-merged layout)')
|
19 | 28 | return parser.parse_args()
|
20 | 29 |
|
21 |
| -def adjust(pkg, text): |
22 |
| - text = text.replace('builtin_interfaces::', 'crate::vendor::builtin_interfaces::') |
23 |
| - text = text.replace('rcl_interfaces::', 'crate::vendor::rcl_interfaces::') |
24 |
| - text = text.replace('rosgraph_msgs::', 'crate::vendor::rosgraph_msgs::') |
25 |
| - text = text.replace('crate::msg', f'crate::vendor::{pkg}::msg') |
26 |
| - text = text.replace('crate::srv', f'crate::vendor::{pkg}::srv') |
| 30 | +def adjust(current_package, text): |
| 31 | + for pkg in vendored_packages: |
| 32 | + text = text.replace(f'{pkg}::', f'crate::vendor::{pkg}::') |
| 33 | + text = text.replace('crate::msg', f'crate::vendor::{current_package}::msg') |
| 34 | + text = text.replace('crate::srv', f'crate::vendor::{current_package}::srv') |
| 35 | + text = text.replace('crate::action', f'crate::vendor::{current_package}::action') |
27 | 36 | return text
|
28 | 37 |
|
29 | 38 | def copy_adjusted(pkg, src, dst):
|
30 | 39 | dst.write_text(adjust(pkg, src.read_text()))
|
31 | 40 | subprocess.check_call(['rustfmt', str(dst)])
|
32 | 41 |
|
33 |
| -mod_contents = """//! Created by {} |
34 |
| -#![allow(dead_code)] |
35 |
| -#![allow(clippy::derive_partial_eq_without_eq)] |
36 |
| -
|
37 |
| -pub mod builtin_interfaces; |
38 |
| -pub mod rcl_interfaces; |
39 |
| -pub mod rosgraph_msgs; |
40 |
| -""".format(Path(__file__).name) |
41 |
| - |
42 | 42 | def main():
|
43 | 43 | args = get_args()
|
44 | 44 | assert args.install_base.is_dir(), "Install base does not exist"
|
45 |
| - assert (args.install_base / 'builtin_interfaces').is_dir(), "Install base does not contain builtin_interfaces" |
46 |
| - assert (args.install_base / 'rcl_interfaces').is_dir(), "Install base does not contain rcl_interfaces" |
47 |
| - assert (args.install_base / 'rosgraph_msgs').is_dir(), "Install base does not contain rosgraph_msgs" |
| 45 | + for pkg in vendored_packages: |
| 46 | + assert (args.install_base / pkg).is_dir(), f"Install base does not contain {pkg}" |
48 | 47 | rclrs_root = Path(__file__).parent
|
49 | 48 | vendor_dir = rclrs_root / 'src' / 'vendor'
|
50 | 49 | if vendor_dir.exists():
|
51 | 50 | shutil.rmtree(vendor_dir)
|
52 |
| - for pkg in ['builtin_interfaces', 'rcl_interfaces', 'rosgraph_msgs']: |
| 51 | + for pkg in vendored_packages: |
53 | 52 | src = args.install_base / pkg / 'share' / pkg / 'rust' / 'src'
|
54 | 53 | dst = vendor_dir / pkg
|
55 | 54 | dst.mkdir(parents=True)
|
56 | 55 | copy_adjusted(pkg, src / 'msg.rs', dst / 'msg.rs')
|
57 | 56 | if (src / 'srv.rs').is_file():
|
58 | 57 | copy_adjusted(pkg, src / 'srv.rs', dst / 'srv.rs')
|
| 58 | + if (src / 'action.rs').is_file(): |
| 59 | + copy_adjusted(pkg, src / 'action.rs', dst / 'action.rs') |
59 | 60 | copy_adjusted(pkg, src / 'lib.rs', dst / 'mod.rs') # Rename lib.rs to mod.rs
|
60 |
| - (vendor_dir / 'mod.rs').write_text(mod_contents) |
61 |
| - |
62 | 61 |
|
| 62 | + mod_contents = "//! Created by {}\n".format(Path(__file__).name) |
| 63 | + mod_contents += "#![allow(dead_code)]\n" |
| 64 | + mod_contents += "#![allow(clippy::derive_partial_eq_without_eq)]\n" |
| 65 | + mod_contents += "\n" |
| 66 | + for pkg in vendored_packages: |
| 67 | + mod_contents += f"pub mod {pkg};\n" |
| 68 | + (vendor_dir / 'mod.rs').write_text(mod_contents) |
63 | 69 |
|
64 | 70 | if __name__ == '__main__':
|
65 | 71 | main()
|
0 commit comments