|
| 1 | +# Visualize Module Dependencies |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +> [!NOTE] |
| 6 | +> This codemod helps developers understand module relationships by creating a visual representation of import dependencies between different parts of the codebase. |
| 7 | +
|
| 8 | +## How the Visualization Script Works |
| 9 | + |
| 10 | +The script analyzes module dependencies in several key steps: |
| 11 | + |
| 12 | +1. **Graph Initialization** |
| 13 | + ```python |
| 14 | + G = nx.DiGraph() |
| 15 | + list_apps = ["src/sentry/api", "src/sentry/auth", "src/sentry/flags"] |
| 16 | + for app in list_apps: |
| 17 | + G.add_node(app, metadata={'color':'red'}) |
| 18 | + ``` |
| 19 | + - Creates a directed graph using NetworkX |
| 20 | + - Initializes nodes for each major application module |
| 21 | + - Sets up metadata for visualization |
| 22 | + |
| 23 | +2. **Import Analysis** |
| 24 | + ```python |
| 25 | + for file in codebase.files: |
| 26 | + if app in file.filepath: |
| 27 | + for import_statement in file.import_statements: |
| 28 | + # Analyze imports and build edges |
| 29 | + ``` |
| 30 | + - Scans through all files in specified modules |
| 31 | + - Analyzes import statements |
| 32 | + - Creates edges based on module dependencies |
| 33 | + |
| 34 | +3. **Graph Cleanup** |
| 35 | + ```python |
| 36 | + nodes_to_remove = [node for node, degree in G.degree() if degree == 1] |
| 37 | + G.remove_nodes_from(nodes_to_remove) |
| 38 | + ``` |
| 39 | + - Removes isolated nodes |
| 40 | + - Cleans up the graph for better visualization |
| 41 | + - Focuses on meaningful dependencies |
| 42 | + |
| 43 | +## Why This Makes Architecture Analysis Easy |
| 44 | + |
| 45 | +1. **Automated Dependency Detection** |
| 46 | + - Automatically finds module relationships |
| 47 | + - Identifies import patterns |
| 48 | + - No manual tracking needed |
| 49 | + |
| 50 | +2. **Visual Representation** |
| 51 | + - Clear visualization of dependencies |
| 52 | + - Easy to identify clusters |
| 53 | + - Highlights potential architectural issues |
| 54 | + |
| 55 | +3. **Simplified Analysis** |
| 56 | + - Quick overview of codebase structure |
| 57 | + - Helps identify tightly coupled modules |
| 58 | + - Assists in refactoring decisions |
| 59 | + |
| 60 | +## Common Dependency Patterns |
| 61 | + |
| 62 | +### Module Dependencies |
| 63 | +```python |
| 64 | +# The script will detect dependencies like: |
| 65 | +from src.sentry.api import endpoint # Creates edge from current module to api |
| 66 | +from src.sentry.auth import tokens # Creates edge from current module to auth |
| 67 | +``` |
| 68 | + |
| 69 | +### Visualization Output |
| 70 | +``` |
| 71 | +DiGraph with n nodes and m edges where: |
| 72 | +- Nodes represent major modules |
| 73 | +- Edges show import relationships |
| 74 | +- Node colors indicate module types |
| 75 | +``` |
| 76 | + |
| 77 | +## Key Benefits to Note |
| 78 | + |
| 79 | +1. **Better Architecture Understanding** |
| 80 | + - Clear view of module relationships |
| 81 | + - Identifies dependency patterns |
| 82 | + - Helps spot architectural issues |
| 83 | + |
| 84 | +2. **Refactoring Support** |
| 85 | + - Identifies tightly coupled modules |
| 86 | + - Helps plan refactoring |
| 87 | + - Shows impact of changes |
| 88 | + |
| 89 | +3. **Documentation Aid** |
| 90 | + - Visual documentation of architecture |
| 91 | + - Easy to share and discuss |
| 92 | + - Helps onboard new developers |
| 93 | + |
| 94 | +## Running the Visualization |
| 95 | + |
| 96 | +```bash |
| 97 | +# Install Codegen and dependencies |
| 98 | +pip install codegen networkx |
| 99 | + |
| 100 | +# Run the visualization |
| 101 | +python run.py |
| 102 | +``` |
| 103 | + |
| 104 | +The script will: |
| 105 | +1. Initialize the codebase |
| 106 | +2. Analyze module dependencies |
| 107 | +3. Create a dependency graph |
| 108 | +4. Output the visualization through codegen.sh |
| 109 | + |
| 110 | +## Customization Options |
| 111 | + |
| 112 | +You can customize the analysis by: |
| 113 | +- Modifying the `list_apps` to include different modules |
| 114 | +- Adjusting node metadata and colors |
| 115 | +- Adding additional filtering criteria |
| 116 | + |
| 117 | +## Learn More |
| 118 | + |
| 119 | +- [NetworkX Documentation](https://networkx.org/) |
| 120 | +- [Python Import System](https://docs.python.org/3/reference/import.html) |
| 121 | +- [Codegen Documentation](https://docs.codegen.com) |
| 122 | +- [Graph visualization](https://docs.codegen.com/building-with-codegen/codebase-visualization) |
| 123 | + |
| 124 | +## Contributing |
| 125 | + |
| 126 | +Feel free to submit issues and enhancement requests! Contributions to improve the visualization or add new features are welcome. |
0 commit comments