|
| 1 | +"""Metadata generation for created execution script sets.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import shlex |
| 6 | +import sys |
| 7 | +from datetime import datetime |
| 8 | + |
| 9 | +from .validation import normalize_intermediate_entries, require_positive_int |
| 10 | + |
| 11 | + |
| 12 | +def collect_metadata_node_names(json_content): |
| 13 | + """Collect host names, node names, and topic counts for metadata.txt.""" |
| 14 | + host_names = [] |
| 15 | + publisher_names = [] |
| 16 | + subscriber_names = [] |
| 17 | + intermediate_names = [] |
| 18 | + topic_names = set() |
| 19 | + |
| 20 | + for host_dict in json_content["hosts"]: |
| 21 | + host_names.append(host_dict["host_name"]) |
| 22 | + for node in host_dict.get("nodes", []): |
| 23 | + node_name = node["node_name"] |
| 24 | + if node.get("publisher"): |
| 25 | + publisher_names.append(node_name) |
| 26 | + for publisher in node["publisher"]: |
| 27 | + topic_names.add(publisher["topic_name"]) |
| 28 | + if node.get("subscriber"): |
| 29 | + subscriber_names.append(node_name) |
| 30 | + for subscriber in node["subscriber"]: |
| 31 | + topic_names.add(subscriber["topic_name"]) |
| 32 | + if "intermediate" in node: |
| 33 | + intermediate_names.append(node_name) |
| 34 | + intermediate_entries = normalize_intermediate_entries( |
| 35 | + node["intermediate"], node_name |
| 36 | + ) |
| 37 | + for intermediate_entry in intermediate_entries: |
| 38 | + for publisher in intermediate_entry.get("publisher", []): |
| 39 | + topic_names.add(publisher["topic_name"]) |
| 40 | + for subscriber in intermediate_entry.get("subscriber", []): |
| 41 | + topic_names.add(subscriber["topic_name"]) |
| 42 | + |
| 43 | + return host_names, publisher_names, subscriber_names, intermediate_names, topic_names |
| 44 | + |
| 45 | + |
| 46 | +def collect_topic_runtime_config(json_content): |
| 47 | + """Collect topic -> payload/period/publisher_count from topology.""" |
| 48 | + topic_cfg = {} |
| 49 | + |
| 50 | + def add_publisher_topic(entry, context): |
| 51 | + topic = entry.get("topic_name") |
| 52 | + if not topic: |
| 53 | + raise ValueError(f"{context}: missing topic_name") |
| 54 | + payload_size = require_positive_int(entry, "payload_size", context) |
| 55 | + period_ms = require_positive_int(entry, "period_ms", context) |
| 56 | + |
| 57 | + cfg = topic_cfg.setdefault( |
| 58 | + topic, |
| 59 | + { |
| 60 | + "payload_size": payload_size, |
| 61 | + "period_ms": period_ms, |
| 62 | + "publisher_count": 0, |
| 63 | + }, |
| 64 | + ) |
| 65 | + if cfg["payload_size"] != payload_size or cfg["period_ms"] != period_ms: |
| 66 | + raise ValueError( |
| 67 | + f"Inconsistent payload/period for topic '{topic}' in topology JSON" |
| 68 | + ) |
| 69 | + cfg["publisher_count"] += 1 |
| 70 | + |
| 71 | + for host in json_content.get("hosts", []): |
| 72 | + for node in host.get("nodes", []): |
| 73 | + node_name = node.get("node_name", "?") |
| 74 | + for pub_idx, publisher in enumerate(node.get("publisher", []) or []): |
| 75 | + add_publisher_topic( |
| 76 | + publisher, f"node '{node_name}' publisher[{pub_idx}]" |
| 77 | + ) |
| 78 | + |
| 79 | + if "intermediate" in node: |
| 80 | + intermediate_entries = normalize_intermediate_entries( |
| 81 | + node["intermediate"], node_name |
| 82 | + ) |
| 83 | + for entry_idx, inter in enumerate(intermediate_entries): |
| 84 | + for pub_idx, publisher in enumerate(inter.get("publisher", []) or []): |
| 85 | + add_publisher_topic( |
| 86 | + publisher, |
| 87 | + f"node '{node_name}' intermediate[{entry_idx}] publisher[{pub_idx}]", |
| 88 | + ) |
| 89 | + |
| 90 | + return topic_cfg |
| 91 | + |
| 92 | + |
| 93 | +def unique_in_order(items): |
| 94 | + """Remove duplicates while preserving the original order.""" |
| 95 | + return list(dict.fromkeys(items)) |
| 96 | + |
| 97 | + |
| 98 | +def generate_metadata_file( |
| 99 | + json_content, json_path, rmw, ws_dir, project_root, scenario_dir |
| 100 | +): |
| 101 | + """Generate <ws-dir>/latest/metadata.txt.""" |
| 102 | + latest_dir = os.path.join(project_root, ws_dir, "latest") |
| 103 | + metadata_path = os.path.join(latest_dir, "metadata.txt") |
| 104 | + |
| 105 | + ( |
| 106 | + host_names, |
| 107 | + publisher_names, |
| 108 | + subscriber_names, |
| 109 | + intermediate_names, |
| 110 | + topic_names, |
| 111 | + ) = collect_metadata_node_names(json_content) |
| 112 | + host_names = unique_in_order(host_names) |
| 113 | + publisher_names = unique_in_order(publisher_names) |
| 114 | + subscriber_names = unique_in_order(subscriber_names) |
| 115 | + intermediate_names = unique_in_order(intermediate_names) |
| 116 | + topic_runtime_cfg = collect_topic_runtime_config(json_content) |
| 117 | + |
| 118 | + all_nodes = [ |
| 119 | + node |
| 120 | + for host in json_content["hosts"] |
| 121 | + for node in host.get("nodes", []) |
| 122 | + ] |
| 123 | + node_count = len(all_nodes) |
| 124 | + |
| 125 | + qos = json_content.get("qos", {}) |
| 126 | + timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") |
| 127 | + sections = [ |
| 128 | + [ |
| 129 | + "# --- 1. general info ---", |
| 130 | + f"command: {shlex.join(sys.argv)}", |
| 131 | + f"timestamp: {timestamp}", |
| 132 | + f"json: {os.path.basename(json_path)}", |
| 133 | + f"json_path: {json_path}", |
| 134 | + f"ws_dir: {ws_dir}", |
| 135 | + f"scenario_dir: {scenario_dir}", |
| 136 | + ], |
| 137 | + [ |
| 138 | + "# --- 2. test config ---", |
| 139 | + f"rmw: {rmw}", |
| 140 | + f"qos_history: {qos.get('history', 'KEEP_LAST')}", |
| 141 | + f"qos_depth: {qos.get('depth', 1)}", |
| 142 | + f"qos_reliability: {qos.get('reliability', 'RELIABLE')}", |
| 143 | + ], |
| 144 | + [ |
| 145 | + "# --- 3. topology stats ---", |
| 146 | + f"host_count: {len(host_names)}", |
| 147 | + f"node_count: {node_count}", |
| 148 | + f"publisher_count: {len(publisher_names)}", |
| 149 | + f"subscriber_count: {len(subscriber_names)}", |
| 150 | + f"intermediate_count: {len(intermediate_names)}", |
| 151 | + f"topic_count: {len(topic_names)}", |
| 152 | + f"hosts: {', '.join(host_names)}", |
| 153 | + f"publishers: {', '.join(publisher_names)}", |
| 154 | + f"subscribers: {', '.join(subscriber_names)}", |
| 155 | + f"intermediates: {', '.join(intermediate_names)}", |
| 156 | + f"topics: {', '.join(sorted(topic_names))}", |
| 157 | + ( |
| 158 | + "topic_runtime_json: " |
| 159 | + f"{json.dumps(topic_runtime_cfg, separators=(',', ':'), sort_keys=True)}" |
| 160 | + ), |
| 161 | + ], |
| 162 | + ] |
| 163 | + |
| 164 | + with open(metadata_path, "w") as f: |
| 165 | + f.write("\n\n".join("\n".join(section) for section in sections) + "\n") |
0 commit comments