-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·191 lines (153 loc) · 6.35 KB
/
test.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
188
189
#!/usr/bin/python
import sys
sys.path.append("~/dot/codesearch-py")
import codesearch
class SymbolDefinition:
skipped = set()
processed = set()
def __init__(self, signature, display_name):
self.signature = signature
self.display_name = display_name
self.references = dict()
def FindReferences(self, cs):
print "Finding references of " + self.display_name
if self.display_name in SymbolDefinition.processed:
return
#SymbolDefinition.processed.add(self.display_name)
node = codesearch.XrefNode.FromSignature(cs, self.signature)
edges = node.GetEdges(codesearch.EdgeEnumKind.DECLARES)
for edge in edges:
try:
definitions = edge.GetRelatedDefinitions()
for do in definitions:
definition = do.GetEdges(codesearch.EdgeEnumKind.HAS_DEFINITION)
if definition[0].GetXrefKind() != codesearch.NodeEnumKind.CLASS:
continue
try:
name = definition[0].GetDisplayName()
if name == self.display_name:
continue
if name in self.references:
continue
if name in SymbolDefinition.skipped:
continue
definition = do.GetEdges(codesearch.EdgeEnumKind.HAS_DEFINITION)
if definition[0].GetXrefKind() != codesearch.NodeEnumKind.CLASS:
print "- skipping " + name + " " + str(definition[0].GetXrefKind())
SymbolDefinition.skipped.add(name)
continue
print "+ adding " + name
self.references[name] = SymbolDefinition(do.GetSignature(), name)
except Exception as err1:
print "Definition Exception: " + str(err1)
continue
except Exception as err:
print "Edge Exception: " + str(err)
continue
def PrintReferences(self):
for reference in self.references.keys():
print self.display_name + " -> " + reference
for reference in self.references.values():
reference.PrintReferences()
def PrintDigraph(self):
print "digraph {"
self.PrintReferences()
print "}"
def ResolveReferences(self, cs, level):
if level <= 0:
return
self.FindReferences(cs)
for reference in self.references.values():
reference.ResolveReferences(cs, level - 1)
# The plugin needs to locate a local Chromium checkout. We are passing '.' as a
# path inside the source directory, which works if the current directory is
# inside the Chromium checkout. The configuration mechanism is likely to change.
cs = codesearch.CodeSearch(a_path_inside_source_dir='../chrome/android/src')
sig = cs.GetSignatureForSymbol('../chrome/android/src/net/http/http_network_transaction.cc', \
'HttpNetworkTransaction')
#node = codesearch.XrefNode.FromSignature(cs, sig)
#print node
#network_transaction = SymbolDefinition(sig, 'HttpNetworkTransaction');
#network_transaction.FindReferences(cs)
#network_transaction.ResolveReferences(cs,2)
#network_transaction.PrintDigraph()
#sys.exit(0)
def GetFileForClass(cs, class_name):
response = cs.SendRequestToServer(codesearch.CompoundRequest(
search_request=[
codesearch.SearchRequest(query='class:' + class_name)
]))
assert isinstance(response, codesearch.CompoundResponse)
assert hasattr(response, 'search_response')
assert isinstance(response.search_response, list)
assert isinstance(response.search_response[0], codesearch.SearchResponse)
return response.search_response[0].search_result[0].top_file.file.name
file_prefix = '../chrome/android/'
def GetSignatureForClass(cs, class_name):
file_name = GetFileForClass(cs, class_name)
sig = cs.GetSignatureForSymbol(file_prefix + file_name, class_name)
return sig
print GetSignatureForClass(cs, "HttpNetworkTransaction")
print GetSignatureForClass(cs, "net::UrlRequestContext")
print GetSignatureForClass(cs, "CronetEngine")
print GetSignatureForClass(cs, "CronetEngine.Builder")
sys.exit(0)
# The backend takes a CompoundRequest object ...
response = cs.SendRequestToServer(codesearch.CompoundRequest(
search_request=[
codesearch.SearchRequest(query='class:CronetEngine')
]))
# ... and returns a CompoundResponse
assert isinstance(response, codesearch.CompoundResponse)
# Both CompoundRequest and CompoundResponse are explained in
# codesearch/messages.py. Since our request was a |search_request| which is a
# list of SearchRequest objects, our CompoundResponse object is going to have a
# |search_response| field ...
assert hasattr(response, 'search_response')
# containing a list of SearchResponse objects ...
assert isinstance(response.search_response, list)
assert isinstance(response.search_response[0], codesearch.SearchResponse)
# We can now examine the contents of the SearchResponse object to see what the
# server sent us. The fields are explained in message.py.
for search_result in response.search_response[0].search_result:
print search_result
assert isinstance(search_result, codesearch.SearchResult)
print search_result.top_file.file.name
# definition = search_result.GetEdges(codesearch.EdgeEnumKind.HAS_DEFINITION)
# print "***********" + str(definition)
if not hasattr(search_result, 'snippet'):
print "no snippet"
continue
for snippet in search_result.snippet:
assert isinstance(snippet, codesearch.Snippet)
# Just print the text of the search result snippet.
print snippet.text.text
# if definition[0].GetXrefKind() != codesearch.NodeEnumKind.CLASS:#
# print "- skipping " + name + " " + str(definition[0].GetXrefKind())
# SymbolDefinition.skipped.add(name)
# continue
sys.exit(0)
edges = node.GetEdges(codesearch.EdgeEnumKind.DECLARES)
#edges = node.GetEdges(codesearch.EdgeEnumKind.EXTENDS)
for edge in edges:
#print edge
print "***line:***" + edge.single_match.line_text
#print "***sig:***" + edge.single_match.signature
#declared_by = edge.GetEdges(codesearch.EdgeEnumKind.DECLARED_BY)
#for db in declared_by:
# print "***declared_by:***" + db.single_match.line_text
#definition_of = edge.GetEdges(codesearch.EdgeEnumKind.HAS_TYPE)
try:
definition_of = edge.GetRelatedDefinitions()
for do in definition_of:
try:
print "***Related Definition of:***" + str(do)
print "***Related Definition Name:***" + do.GetDisplayName()
except:
continue
except:
continue
#edges = node.GetEdges(codesearch.EdgeEnumKind.DECLARED_BY)
#for edge in edges:
# print edge
print node.GetDisplayName()