-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathfind_unused_templates.py
187 lines (157 loc) · 7.17 KB
/
find_unused_templates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import django
from pathlib import Path
import re
from collections import defaultdict
import json
# Setup Django environment
def setup_django():
project_root = Path(__file__).resolve().parent
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'apps.common.settings.development')
import sys
sys.path.append(str(project_root))
django.setup()
def find_all_templates():
"""Find all template files in the project."""
from django.conf import settings
template_files = set()
template_dirs = []
# Get template directories from settings
for template_setting in settings.TEMPLATES:
template_dirs.extend(template_setting.get('DIRS', []))
if template_setting.get('APP_DIRS', False):
for app_config in django.apps.apps.get_app_configs():
template_dir = os.path.join(app_config.path, 'templates')
if os.path.exists(template_dir):
template_dirs.append(template_dir)
# Find all template files
for template_dir in template_dirs:
for root, _, files in os.walk(template_dir):
for file in files:
if file.endswith(('.html', '.htm', '.django')):
full_path = os.path.join(root, file)
relative_path = os.path.relpath(full_path, template_dir)
template_files.add(relative_path)
return template_files
def find_template_usage_in_code():
"""Find template references in Python files."""
template_usage = set()
python_files = []
# Find all Python files
for root, _, files in os.walk('.'):
if 'env' in root or 'venv' in root or '.git' in root:
continue
for file in files:
if file.endswith('.py'):
python_files.append(os.path.join(root, file))
# Patterns to search for
patterns = [
r'template_name\s*=\s*[\'"]([^\'"]+)[\'"]',
r'template_name_suffix\s*=\s*[\'"]([^\'"]+)[\'"]',
r'render\([^,]+,\s*[\'"]([^\'"]+)[\'"]',
r'get_template\([\'"]([^\'"]+)[\'"]',
r'select_template\(\[[^\]]+\]\)',
]
for file_path in python_files:
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
for pattern in patterns:
matches = re.finditer(pattern, content)
for match in matches:
if len(match.groups()) > 0:
template_usage.add(match.group(1))
except Exception as e:
print(f"Error processing {file_path}: {str(e)}")
return template_usage
def find_template_usage_in_templates():
"""Find template references within templates."""
template_usage = set()
# Get template directories from settings
from django.conf import settings
template_dirs = []
for template_setting in settings.TEMPLATES:
template_dirs.extend(template_setting.get('DIRS', []))
if template_setting.get('APP_DIRS', False):
for app_config in django.apps.apps.get_app_configs():
template_dir = os.path.join(app_config.path, 'templates')
if os.path.exists(template_dir):
template_dirs.append(template_dir)
# Find and process templates
for template_dir in template_dirs:
for root, _, files in os.walk(template_dir):
for file in files:
if file.endswith(('.html', '.htm', '.django')):
try:
full_path = os.path.join(root, file)
with open(full_path, 'r', encoding='utf-8') as f:
content = f.read()
# Look for extends tags
extends_matches = re.finditer(r'{%\s*extends\s*[\'"]([^\'"]+)[\'"]', content)
for match in extends_matches:
template_usage.add(match.group(1))
# Look for include tags
include_matches = re.finditer(r'{%\s*include\s*[\'"]([^\'"]+)[\'"]', content)
for match in include_matches:
template_usage.add(match.group(1))
except Exception as e:
relative_path = os.path.relpath(full_path, template_dir)
print(f"Error processing template {relative_path}: {str(e)}")
return template_usage
def find_dynamic_template_loading():
"""Find potential dynamic template loading patterns."""
dynamic_patterns = defaultdict(list)
for root, _, files in os.walk('.'):
if 'env' in root or 'venv' in root or '.git' in root:
continue
for file in files:
if file.endswith('.py'):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Look for dynamic template loading patterns
patterns = [
r'get_template\(.*\+.*\)',
r'select_template\(.*\+.*\)',
r'render\([^,]+,\s*[^\'"][^,]+\)',
r'template_name\s*=\s*[^\'"][^,\n]+'
]
for pattern in patterns:
matches = re.finditer(pattern, content)
for match in matches:
dynamic_patterns[file_path].append(match.group(0))
except Exception as e:
print(f"Error checking dynamic loading in {file_path}: {str(e)}")
return dynamic_patterns
def analyze_templates():
"""Main analysis function."""
print("Setting up Django...")
setup_django()
print("\nFinding all templates...")
all_templates = find_all_templates()
print("\nFinding template usage in Python code...")
code_usage = find_template_usage_in_code()
print("\nFinding template usage in templates...")
template_usage = find_template_usage_in_templates()
print("\nChecking for dynamic template loading...")
dynamic_loading = find_dynamic_template_loading()
# Combine all usage
used_templates = code_usage.union(template_usage)
# Find potentially unused templates
unused_templates = all_templates - used_templates
# Write results
results = {
'all_templates': list(all_templates),
'used_templates': list(used_templates),
'unused_templates': list(unused_templates),
'dynamic_loading_patterns': {k: v for k, v in dynamic_loading.items()}
}
with open('template_analysis.json', 'w') as f:
json.dump(results, f, indent=2)
print("\nAnalysis complete! Results written to template_analysis.json")
print(f"\nFound {len(unused_templates)} potentially unused templates")
print("\nWARNING: Please verify results before deleting any templates!")
print("Special attention needed for files with dynamic template loading.")
if __name__ == '__main__':
analyze_templates()