Skip to content

Commit 4ec39cb

Browse files
committed
Adds modules dependencies examples
1 parent 8f992fb commit 4ec39cb

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import codegen
2+
from codegen import Codebase
3+
from codegen.sdk.enums import ProgrammingLanguage
4+
import networkx as nx
5+
6+
@codegen.function("visualize-modules-dependencies")
7+
def run(codebase: Codebase):
8+
9+
# Create a directed graph
10+
G = nx.DiGraph()
11+
12+
list_apps = ["src/sentry/api", "src/sentry/auth", "src/sentry/flags"]
13+
# Get the specific file for balance
14+
for app in list_apps:
15+
G.add_node(app, metadata={'color':'red'})
16+
17+
for app in list_apps:
18+
for file in codebase.files:
19+
if app in file.filepath:
20+
# Iterate over all import statements in the file
21+
for import_statement in file.import_statements:
22+
# Check if the import statement is importing an app
23+
for imp in import_statement.imports:
24+
# Assuming app imports follow a specific naming convention or structure
25+
if 'app' in imp.name: # Adjust this condition based on your app naming convention
26+
G.add_edge(app, imp.import_statement.source)
27+
28+
nodes_to_remove = [node for node, degree in G.degree() if degree == 1]
29+
30+
# Remove the nodes from the graph
31+
G.remove_nodes_from(nodes_to_remove)
32+
33+
print(G)
34+
print("Use codegen.sh to visualize the graph!")
35+
36+
37+
if __name__ == "__main__":
38+
codebase = Codebase.from_repo('getsentry/sentry', commit="fb0d53b2210cc896fc3e2cf32dae149ea8a8a45a", programming_language=ProgrammingLanguage.PYTHON)
39+
run(codebase)

0 commit comments

Comments
 (0)