-
Notifications
You must be signed in to change notification settings - Fork 54
/
make-dot.py
executable file
·95 lines (84 loc) · 4.23 KB
/
make-dot.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
#!/usr/bin/env python3
import re
import glob
import os
import collections
statuscolordict = {'Active':'green', 'WIP':'darkorange1', 'Deprecated':'red'}
def main():
bundledict = build_bundle_dictionary()
statusbundledict = build_statusbundle_dictionary(bundledict)
subgraphs = generate_subgraphs(statusbundledict)
edges = generate_edges(bundledict)
print_dot_output(subgraphs, edges)
def build_bundle_dictionary():
bundledict = {}
namepattern = re.compile('(?<=\[TITLE\]: ).+')
statuspattern = re.compile('(?<=\[STATUS\]: ).+')
includepattern = re.compile('(?<=include\().+(?=\))')
packagepattern = re.compile('^[^\(\)#\\n]+$', re.MULTILINE)
for filename in glob.glob(os.path.dirname(os.path.realpath(__file__)) + '/bundles/*'):
with open(filename, 'r') as bundlefile:
bundledef = bundlefile.read()
if namepattern.search(bundledef) != None and statuspattern.search(bundledef) != None:
bundlename = namepattern.findall(bundledef)[0]
bundlestatus = statuspattern.findall(bundledef)[0]
bundleincludes = includepattern.findall(bundledef)
bundlepackages = packagepattern.findall(bundledef)
bundledict[bundlename] = Bundle(bundlename, bundlestatus, bundleincludes, bundlepackages)
with open('packages', 'r') as package_file:
re_strip_comment = re.compile("#.*$")
for line in package_file:
line = re_strip_comment.sub("", line)
line = line.strip()
if not line:
continue
bundledict[line] = Bundle(line, 'Active', [], line)
return bundledict
def build_statusbundle_dictionary(bundledict):
statusbundledict = collections.defaultdict(list)
for bundlename in bundledict.keys():
statusbundledict[bundledict[bundlename].status].append(bundlename)
return statusbundledict
def generate_subgraphs(statusbundledict):
subgraphs = []
for status in statusbundledict.keys():
if status in statuscolordict:
subgraphnodes = ['node [color = ' + statuscolordict[status] + ']', 'label = <<b>' + status.upper() + '</b>>']
for bundle in statusbundledict[status]:
subgraphnodes.append('"' + bundle + '";')
subgraphs.append('subgraph cluster_' + status.lower() + ' {\n\t\t' + '\n\t\t'.join(subgraphnodes) + '\n\t}')
return subgraphs
def generate_edges(bundledict):
edges = []
for bundlename, bundle in bundledict.items():
for includename in bundle.includes:
includebundle = bundledict[includename]
edges.append('"' + bundlename + '" -> "' + includename + '" [color = ' + determine_edge_color(bundle, includebundle) + ', tooltip = "' + bundlename + ' -> ' + includename + '"];')
return edges
def determine_edge_color(bundle, includebundle):
if bundle.status in statuscolordict and includebundle.status in statuscolordict:
bundlecolor = statuscolordict[bundle.status]
includebundlecolor = statuscolordict[includebundle.status]
if bundlecolor == includebundlecolor:
return bundlecolor
else:
return includebundlecolor
else:
return 'black'
def print_dot_output(subgraphs, edges):
fontname = 'sans-serif'
print('digraph {')
print('\tgraph [layout = dot, fontname = "' + fontname + '", penwidth = 4]')
print('\tnode [fontname = "' + fontname + '", style = filled, fontcolor = white]')
print('\tedge [fontname = "' + fontname + '", penwidth = 2]')
print('\t' + '\n\t'.join(subgraphs))
print('\t' + '\n\t'.join(edges))
print('}')
class Bundle:
def __init__(self, name, status, includes, packages):
self.name = name
self.status = status
self.includes = includes
self.packages = packages
if __name__ == "__main__":
main()