Created
January 28, 2024 14:50
-
-
Save yt/1a14b54f81b2c1abd501b1337142614c to your computer and use it in GitHub Desktop.
Shows ros dependency graph of a catkin workspacae
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
import os | |
import networkx as nx | |
import matplotlib.pyplot as plt | |
def parse_package_xml(file_path): | |
tree = ET.parse(file_path) | |
root = tree.getroot() | |
package_name = root.find('name').text | |
dependencies = [dep.text for dep in root.findall('depend')] + [exec_dep.text for exec_dep in root.findall('exec_depend')] | |
return package_name, list(set(dependencies)) # Remove duplicates | |
def create_dependency_graph(package_dependencies): | |
G = nx.DiGraph() | |
# Collect all known package names | |
all_packages = set(package_dependencies.keys()) | |
for package, deps in package_dependencies.items(): | |
if package.endswith('msgs'): | |
continue | |
if package not in G: | |
G.add_node(package) # Add package node only if it's a known package | |
for dep in deps: | |
if dep in all_packages: # Filter: Add only if dep is also a known package | |
if dep.endswith('msgs'): | |
continue | |
G.add_node(dep) # Add dependency node | |
G.add_edge(package, dep) # Add edge from package to dependency | |
return G | |
def main(): | |
package_dependencies = {} | |
for root, dirs, files in os.walk('/path/to/catkin_ws'): | |
for file in files: | |
if file == 'package.xml': | |
file_path = os.path.join(root, file) | |
package_name, deps = parse_package_xml(file_path) | |
package_dependencies[package_name] = deps | |
# Now package_dependencies contains all the dependencies | |
dependency_graph = create_dependency_graph(package_dependencies) | |
pos = nx.kamada_kawai_layout(dependency_graph) | |
plt.figure(figsize=(20, 20)) # Increase the size of the graph | |
nx.draw(dependency_graph, pos, with_labels=True, node_size=50, font_size=10) | |
plt.show() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment