This example demonstrates how to use Codegen to automatically analyze and visualize module dependencies in Python codebases. The script creates a directed graph showing relationships between different modules, making it easier to understand code architecture and dependencies.
Note
This codemod helps developers understand module relationships by creating a visual representation of import dependencies between different parts of the codebase.
The script analyzes module dependencies in several key steps:
-
Graph Initialization
G = nx.DiGraph() list_apps = ["src/sentry/api", "src/sentry/auth", "src/sentry/flags"] for app in list_apps: G.add_node(app, metadata={"color": "red"})
- Creates a directed graph using NetworkX
- Initializes nodes for each major application module
- Sets up metadata for visualization
-
Import Analysis
for file in codebase.files: if app in file.filepath: for import_statement in file.import_statements: # Analyze imports and build edges
- Scans through all files in specified modules
- Analyzes import statements
- Creates edges based on module dependencies
-
Graph Cleanup
nodes_to_remove = [node for node, degree in G.degree() if degree == 1] G.remove_nodes_from(nodes_to_remove)
- Removes isolated nodes
- Cleans up the graph for better visualization
- Focuses on meaningful dependencies
-
Automated Dependency Detection
- Automatically finds module relationships
- Identifies import patterns
- No manual tracking needed
-
Visual Representation
- Clear visualization of dependencies
- Easy to identify clusters
- Highlights potential architectural issues
-
Simplified Analysis
- Quick overview of codebase structure
- Helps identify tightly coupled modules
- Assists in refactoring decisions
# The script will detect dependencies like:
from src.sentry.api import endpoint # Creates edge from current module to api
from src.sentry.auth import tokens # Creates edge from current module to authDiGraph with n nodes and m edges where:
- Nodes represent major modules
- Edges show import relationships
- Node colors indicate module types
-
Better Architecture Understanding
- Clear view of module relationships
- Identifies dependency patterns
- Helps spot architectural issues
-
Refactoring Support
- Identifies tightly coupled modules
- Helps plan refactoring
- Shows impact of changes
-
Documentation Aid
- Visual documentation of architecture
- Easy to share and discuss
- Helps onboard new developers
# Install Codegen and dependencies
pip install codegen networkx
# Run the visualization
python run.pyThe script will:
- Initialize the codebase
- Analyze module dependencies
- Create a dependency graph
- Output the visualization through codegen.sh
You can customize the analysis by:
- Modifying the
list_appsto include different modules - Adjusting node metadata and colors
- Adding additional filtering criteria
Feel free to submit issues and enhancement requests! Contributions to improve the visualization or add new features are welcome.