-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevent_flow_interface.py
144 lines (125 loc) · 4.91 KB
/
event_flow_interface.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
from data_structures import AObject, AField
import subprocess
import re
import random
class EventFlowInterface(object):
"""
Abstracts Event Framework parsing.
"""
TOPIC_RE = re.compile("dispatcher.(publish|publish_with_header|register).Topics.([A-Z_]+)")
def _generate_random_color(self, mixr, mixg, mixb):
"""
mix in colors to random color
"""
r = random.random()
g = random.random()
b = random.random()
r = (r + mixr) / 2
g = (g + mixg) / 2
b = (b + mixb) / 2
return (r, g, b)
def __init__(self):
pass
def load_aobjects(self, directory, source_re, dest_re):
"""
Loads event dispatch and registration into AObjects
"""
# maps filenames/topic to aobjects
aobjects = {}
# maps filenames/topic to map of destination filename/topic
visited = {}
command_list = ["|",
"grep -v Binary",
"|",
"grep -v site_media",
"|",
"grep -v tests.py",
">"]
publish_output_filename = "publish.out"
publish_command = " ".join(["grep -r dispatcher.publish(_with_header)?.Topics %s" % directory] + command_list + [publish_output_filename])
self._do_command(publish_command)
self._extract_objects_and_arrows(publish_output_filename,
aobjects,
visited,
is_publishing=True)
register_output_filename = "register.out"
register_command = " ".join(["grep -r dispatcher.register.Topics %s" % directory] + command_list + [register_output_filename])
self._do_command(register_command)
self._extract_objects_and_arrows(register_output_filename,
aobjects,
visited,
is_publishing=False)
return aobjects.values()
def _extract_objects_and_arrows(self,
output_filename,
aobjects,
visited,
is_publishing=True):
# example line
#/Users/lucy/sandbox/ck/cloudkick/helen/agent_cull_service.py: dispatcher.publish(Topics.HELEN_NODE_LOOKUP, event, service=SERVICE)
f = open(output_filename)
for line in f.readlines():
parts = line.split()
# remove .py:
filename_parts = parts[0].split("/")
filename = filename_parts[-2] + "/" + filename_parts[-1][:-4]
topic = None
for part in parts:
search = EventFlowInterface.TOPIC_RE.search(part)
if search:
topic = search.groups()[1]
if not topic:
print "ERR: NO TOPIC FOUND for", line
continue
if filename in aobjects:
faobject = aobjects[filename]
else:
faobject = AObject(name=filename,
color=self._generate_random_color(1, 1, 1),
shape="Circle")
aobjects[filename] = faobject
visited[filename] = {}
if topic in aobjects:
taobject = aobjects[topic]
else:
taobject = AObject(name=topic)
aobjects[topic] = taobject
visited[topic] = {}
if is_publishing:
source = faobject
dest = taobject
else:
source = taobject
dest = faobject
if dest.name in visited[source.name]:
# we've seen this combo before
continue
else:
visited[source.name][dest.name] = 1
afield = AField(name="fieldname",
type="bam",
dest=dest)
source.add_field(field=afield)
def _do_command(self, command):
print
print "THE COMMAND:"
print command
print
process = subprocess.Popen(command,
shell=True,
stdout=subprocess.PIPE)
stdoutdata, stderrdata = process.communicate()
if stderrdata:
print "STDERRDATA:"
print stderrdata
#except subprocess.CalledProcessError:
if stdoutdata:
print "STDOUTDATA:"
print stdoutdata
def pretty_print(self, aobjects):
for m in aobjects:
print m.name
for f in m.fields:
print " ", f.name, f.type
if f.dest:
print " ", f.dest.name