-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdjango_unused_templates.py
84 lines (65 loc) · 2.41 KB
/
django_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
"""
Django Unused Templates
by Alexander Meng
Generates a list of unused templates in your current directory and subdirectories
by searching all .html and .py files for the names of your templates.
IF YOU OVERRIDE DJANGO TEMPLATES, THEY MAY APPEAR IN THE LIST AS UNUSED.
"""
import argparse
import fileinput
import os
def get_files(extension, skip):
cmd = f"find . -name '*.{extension}'"
if skip:
for dir in skip:
cmd += f" -not -path './{dir}/*'"
cmd += " | sort"
return [f[2:-1] for f in os.popen(cmd).readlines()]
def get_templates(html_files):
# Templates will only be returned if they are located in a
# /templates/ directory
templates = {}
for html_file in html_files:
try:
template = html_file.rsplit("templates/")[1]
templates[template] = {'file': html_file, 'count': 0}
except IndexError:
# The html file is not in a template directory...
# don't count it as a template
pass
return templates
def get_unused_templates(skip, fast, filename):
html_files = get_files('html', skip)
py_files = get_files('py', skip)
files = py_files + html_files # List of all files
templates = get_templates(html_files)
files_content = None
output = []
# If -f or --fast parameter is given
# load entire data to memory to speed up execution.
# Otherwise reread files on every pass
if fast:
files_content = list(fileinput.input(files))
for template in templates:
if fast:
lines = files_content
else:
lines = fileinput.input(files, openhook=fileinput.hook_encoded("utf-8"))
for line in lines:
if line.find(template) > 0:
templates[template]['count'] += 1
if templates[template]['count'] == 0:
if not filename:
print(templates[template]['file'])
else:
output.append(templates[template]['file'])
if filename:
with open(filename, 'w') as f:
f.writelines('\n'.join(output))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--skip', default=[], nargs='*')
parser.add_argument('-f', '--fast', action='store_true')
parser.add_argument('-o', '--output', default=None)
options = parser.parse_args()
get_unused_templates(options.skip, options.fast, options.output)