-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_api.py
More file actions
171 lines (135 loc) · 5.26 KB
/
generate_api.py
File metadata and controls
171 lines (135 loc) · 5.26 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
#!/usr/bin/env python3
import argparse
import os
from pathlib import Path
import re
import subprocess
import time
import yaml
EXCLUDE_PATTERNS = re.compile(
r"(describe_parameters|get_parameter_types|get_parameters|list_parameters|set_parameters|rosout|parameter_events)"
)
def find_ros2_executables(workspace="install"):
executables = []
install_path = Path(workspace)
if not install_path.exists():
raise FileNotFoundError(f"No install directory found at {workspace}")
for pkg_dir in install_path.iterdir():
lib_path = pkg_dir / "lib" / pkg_dir.name
if not lib_path.exists():
continue
for exe in lib_path.iterdir():
if os.access(exe, os.X_OK) and exe.is_file():
executables.append(
{"package": pkg_dir.name, "executable": exe.name, "path": str(exe)}
)
return executables
def run_cmd(cmd):
"""Run a shell command and return stdout lines."""
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
return result.stdout.strip().splitlines()
def write_api_to_file(
api: dict,
machine_output_file: str = "public_api.yaml",
human_output_file: str = "PUBLIC_API.md",
):
"""
Writes the public api for a given ROS2 node to a file
Inputs:
api: Distionary containing the api for each node
machine_output_file: String. Name of the to write the machine-readable api file to
human_output_file: String. Name of the to write the human-readable api file to
"""
# Machine readable version
with open(machine_output_file, "w") as f:
yaml.dump(api, f, sort_keys=False)
# Human readable version
md_lines = ["# Public API - ROS2 Interfaces"]
for node in api.keys():
md_lines += ["## " + node]
for interface in api[node].keys():
md_lines += ["### " + interface, "| Name | Type |", "| :--- | :--- |"]
for api_element in api[node][interface]:
md_lines += [f"| {api_element['name']} | {api_element['type']} |"]
md_lines += [""]
with open(human_output_file, "w") as f:
f.write("\n".join(md_lines))
def parse_node_api(api: dict = {}):
"""
Parse the api for the currently running nodes into a dictionary
"""
node_list = run_cmd(["ros2", "node", "list"])
for node in node_list:
node_info = run_cmd(["ros2", "node", "info", node])
# Add node name to the api dict (not necessarily the same as the exec name)
node_name = node_info.pop(0)
if node_name in api.keys():
continue
api[node_name] = {}
# Iterate over the info
curr_key = ""
for line in node_info:
if line.isspace() or not line:
continue
if (
"subscribers:" in line.lower()
or "publishers:" in line.lower()
or "service servers:" in line.lower()
or "service clients:" in line.lower()
or "action servers:" in line.lower()
or "action clients:" in line.lower()
):
curr_key = line
if curr_key not in api[node_name].keys():
api[node_name][curr_key] = []
continue
# Don't include it if the interface is one of the default interfaces
if EXCLUDE_PATTERNS.search(line):
continue
try:
api_element = line.split(": ")
api_element = {"name": api_element[0].strip(), "type": api_element[1]}
except IndexError as e:
print(e.what())
raise ValueError("Unable to parse API. Check parse script")
api[node_name][curr_key].append(api_element)
return api
def extract_api(human_readable_file: str, machine_readable_file: str):
"""
Parses the ROS2 API for a given workspace, which is defined as
the set of all ROS2 interfaces for the executables in the workspace.
This includes parameters, publishers, subscribers, topic names,
topic types, services, service types.
"""
# Find the ROS2 executables
execs = find_ros2_executables()
# TODO: Figure out which executables need special running configuration
# Run each executable and extract the API
api = {}
for exe in execs:
p = subprocess.Popen(["ros2", "run", exe["package"], exe["executable"]])
time.sleep(3) # Wait for the node to spool up
# Parse API
api = parse_node_api(api)
# Stop executables
p.terminate()
write_api_to_file(api, machine_readable_file, human_readable_file)
print(f"Public API written to: {machine_readable_file} and {human_readable_file}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-f",
"--human-readable-file",
type=str,
default="PUBLIC_API.md",
help="Path to write the generated human-readable API to.",
)
parser.add_argument(
"-m",
"--machine-readable-file",
type=str,
default="public_api.yaml",
help="Path to write the generated machine-readable API to.",
)
args = parser.parse_args()
extract_api(args.human_readable_file, args.machine_readable_file)