Ticket #7786: jinja_template_deps.html

File jinja_template_deps.html, 2.2 KB (added by mpatel, 11 years ago)

Simple Jinja template dependency graph worksheet. Not a patch.

Line 
1
2
3{{{id=0|
4import os, subprocess
5from sagenb.misc.misc import DATA, SAGENB_VERSION
6///
7}}}
8
9{{{id=1|
10html_template_dir = os.path.join(DATA, 'sage', 'html')
11pre = '{% '
12extends = pre + 'extends'
13include = pre + 'include'
14post = ' %}'
15def shorten(x):
16    x = x.replace('accounts/', 'A/')
17    x = x.replace('notebook/', 'N/')
18    x = x.replace('settings/', 'S/')   
19    x = x.replace('worksheets/', 'W/')
20    x = x.replace('.html', '')
21    x = x.replace('.tmpl', '.t')
22    return x
23///
24}}}
25
26{{{id=2|
27deps = {}
28for root, dirs, files in os.walk(html_template_dir):
29    for filename in files:
30        path = os.path.join(root, filename)
31        fd = open(path, 'r')
32        lines = fd.readlines()
33        fd.close()
34        for li in lines:
35            for hier in [extends, include]:
36                if hier in li:
37                    i = li.find(hier)
38                    j = li.find(post)
39                    if i == -1 or j == -1:
40                        continue
41                    base = os.path.basename(root)
42                    base = '' if base == 'html' else base + '/'
43                    upper = shorten(base + filename)
44                    lower = shorten(li[i + len(hier) + 7:j - len(post) + 2])
45                    try:
46                        deps[upper].append(lower)
47                    except KeyError:
48                        deps[upper] = [lower]
49///
50}}}
51
52{{{id=3|
53ext_col = '#abcdef'
54inc_col = '#fedcba'
55vcols = {ext_col: [], inc_col: []}
56for d in deps:
57    if d.endswith('.t'):
58        vcols[inc_col].append(d)
59    else:
60        vcols[ext_col].append(d)
61vcols[ext_col].append('base')
62vcols[inc_col].append('N/render_cell.t')
63///
64}}}
65
66{{{id=4|
67g = DiGraph(deps).reverse()
68g.add_vertex('base')
69g.add_vertex('N/render_worksheet.t')
70print g.is_directed_acyclic()
71p = Poset(g)
72p.plot(vertex_size=1500, vertex_colors=vcols).show(aspect_ratio=1.0, figsize=20)
73///
74}}}
75
76{{{id=5|
77loc = ''
78format = 'png'
79gv_base = os.path.join(loc, 'jinja_deps-' + SAGENB_VERSION)
80gv_dot = gv_base + '.dot'
81g.graphviz_to_file_named(gv_dot)
82cmd = 'dot ' + gv_dot + ' -T' + format + ' -o ' + gv_base + '.' + format + ' '
83opts = '-Gratio=auto -Gsize=50,10'
84ret = subprocess.call(cmd + opts, shell=True)
85///
86}}}
87
88{{{id=6|
89
90///
91}}}