forked from Jatz/PeopleCodeTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractCallStack.py
More file actions
327 lines (256 loc) · 18.9 KB
/
ExtractCallStack.py
File metadata and controls
327 lines (256 loc) · 18.9 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import sublime, sublime_plugin, re
class ExtractpccallstackCommand(sublime_plugin.TextCommand):
# This method is only used for debugging
def replaceViewContent(self, viewToReplace, replaceString):
viewToReplaceAllTextRegion = sublime.Region(0, viewToReplace.size())
viewToReplace.replace(self.edit, viewToReplaceAllTextRegion, replaceString)
viewToReplace.sel().clear()
def run(self, edit):
self.edit = edit
# Grab the current view contents and store it in a new file
originalView = self.view;
originalViewContent = originalView.substr(sublime.Region(0, originalView.size()))
newView = originalView.window().new_file()
# First remove all lines that have a call method, call getter, or call setter followed by a start-ext, since the start-ext is sufficient
# For example: the following call method line will be ignored since there is a start-ext immediately after it:
# PSAPPSRV.4556 (2426) 1-8760 14.05.07 0.000000 call method SSF_CFS:SSF_CFQKey.SSFQKeyString #params=7
# PSAPPSRV.4556 (2426) 1-8761 14.05.07 0.000000 >>> start-ext Nest=01 SSFQKeyString SSF_CFS.SSF_CFQKey.OnExecute getter
newViewString = re.sub(r'(?m)(?:.*call\s+(getter|setter|method|constructor).*)\n(.*>>>\sstart-ext.*)', r'\2 \1', originalViewContent)
# Add an end-get for relevant lines
# I.e. Find all internal call getters and add end-gets. This is done by matching line number before the call getter and after the getter method has finished
# Note: This is really ugly, but I couldn't find a better way to do this. Plus it's all 'easily' done in one line of code
newViewString = re.sub(r'(\d+:.*)(\n.*call getter\s+(?:\w+:?)+\.\w+[\s\S]*?)(.*)(\1)', r'\1\2\3end-get;', newViewString)
# Remove unnecessary DoModals
newViewString = re.sub(r'(?m)(\d+:.*DoModal\(.*)([\s\S]+?EndModal[\s\S]+?)(\1)', r'\1\2', newViewString)
# Add other code to also extract here e.g. RemoteCall calls, etc.
additionalCodeToExtractWithoutIndent = ['RemoteCall', '%IntBroker.SyncRequest', '%IntBroker.Publish']
# Add other code to also extract and indent here e.g. Transfer etc.
additionalCodeToExtractWithNonClosingIndent = ['TransferExact', 'TransferPage', 'Transfer', '%Response.RedirectURL', 'ViewURL']
# Add other code to also extract and indent and then close indent here e.g. DoModal, etc.
additionalCodeToExtractWithClosingIndent = ['DoModalComponent', 'DoModal']
# Different variations of the same thing used by different regexs throughout this tool
additionalCodeToExtract1 = '|'.join(additionalCodeToExtractWithoutIndent) + '|' + '|'.join(additionalCodeToExtractWithNonClosingIndent) + '|' + '|'.join(additionalCodeToExtractWithClosingIndent) # Transfer|TransferExact|DoModalComponent etc.
additionalCodeToExtract2 = '.*|'.join(additionalCodeToExtractWithoutIndent) + '.*|' + '.*|'.join(additionalCodeToExtractWithNonClosingIndent) + '.*|' + '.*|'.join(additionalCodeToExtractWithClosingIndent) # Transfer.*|TransferExact.*|DoModalComponent etc.
additionalCodeToExtract1 = 'OVERRIDECODETOPREVENTUNTESTEDNEWFEATURES'
additionalCodeToExtract2 = 'OVERRIDECODETOPREVENTUNTESTEDNEWFEATURES'
# Extract all lines containing start, end, Nest=, call int, call private, call method, End-Function, end-get, end-set and end-method
# Note: we ignore call constructor and call setter
str_list = re.findall(r'(?:(?:.*(?:start|end|resume|reend).*Nest=.*)|(?:.*call (?:int|private|method|getter|setter).*)|.*End-Function.*|.*end-get.*|.*end-set.*|.*end-method.*|.*\d+:.*?(?:%s)\(.*)' % additionalCodeToExtract1, newViewString, re.MULTILINE)
newViewString = '\n'.join(str_list)
# Get the unique session numbers and store them in a list
sessionNos = re.findall(r'PSAPPSRV.\d+\s+\((\d+)\)', newViewString, re.MULTILINE)
sessionNos = sorted(set(sessionNos))
sessionCount = 1
for sessionNo in sessionNos:
# print('sessionNos %s' % sessionNos)
# Extract only those lines relating to the sessionNo
str_list = re.findall(r'PSAPPSRV\.\d+\s+\(%s\).*' % sessionNo, newViewString, re.MULTILINE)
sessionSpecificString = '\n'.join(str_list)
# Remove header junk for each of the lines
str_list = re.findall(r'(?:(?:(?:start|end|resume|reend).*Nest=.*)|(?:call (?:int|private|method|getter|setter).*)|End-Function.*|end-get.*|end-set.*|end-method.*|%s)' % additionalCodeToExtract2, sessionSpecificString, re.MULTILINE)
#str_list = re.findall(r'(?:(?:(?:start|end|resume|reend).*Nest=.*)|(?:call (?:int|private|method|getter|setter).*)|End-Function.*|end-get.*|end-set.*)', sessionSpecificString, re.MULTILINE)
sessionSpecificString = '\n'.join(str_list)
# Get all the Nest values and store them in a list and store the lowest Nest value
nestNos = re.findall(r'Nest=(\d+)', newViewString, re.MULTILINE)
nestNos = sorted(set(nestNos))
lowestNestValue = int(min(nestNos))
# store lines in a list so that we can iterate through each line
lines = sessionSpecificString.split('\n')
sessionSpecificString = ''
lastCall = ''
nestLevel = 0
nestLevelOffset = 0
# extContext will store a list of contexts to keep track of all the start and start-ext calls
extContext = []
# resumeContext will store a list of contexts to keep track of all the resume calls
resumeContext = []
# Perform initial formatting based on Nest value
for lineContents in lines:
# extract Nest value from lineContents
# print(lineContents)
match = re.search(r'(start-ext|start|end-ext|end|resume|reend)\s+Nest=(\d+)', lineContents)
if match:
prevNestLevel = nestLevel
nestLevel = nestLevelOffset + int(match.group(2)) - lowestNestValue
# Always ensure that the next nest level is only indented by 1 tab, and not more
if (nestLevel - prevNestLevel) > 1:
nestLevel = prevNestLevel + 1
startIndex = 0
# If within a resume context
if resumeContext:
# The resume code already adds an extra tab, so don't need to indent here
nestLevel -= 1
for x in range(startIndex,nestLevel):
lineContents = '\t' + lineContents
sessionSpecificString = sessionSpecificString + lineContents + '\n'
if match.group(1) == 'start':
lastCall = 'start'
# E.g. >>> start Nest=12 DERIVED_ADDR.ADDRESSLONG.RowInit
matchExt = re.search(r'start\s+Nest=(?:\d+).*?((?:\w+\.?)+)', lineContents)
# print('contents of extContext before push: %s' % extContext)
extContext.append(matchExt.group(1))
if match.group(1) == 'start-ext':
lastCall = 'start'
# keep track of start-ext location so that we can append the location to call private and call int lines
# E.g. >>> start-ext Nest=14 ActivePrograms_SCT SSR_STUDENT_RECORDS.SR_StudentData.StudentActivePrograms.OnExecute
matchExt = re.search(r'start-ext\s+Nest=(?:\d+)\s+\w+\s+((?:\w+\.?)+)', lineContents)
# print('contents of extContext before push: %s' % extContext)
extContext.append(matchExt.group(1))
if match.group(1) == 'resume':
lastCall = 'resume'
resumeExt = re.search(r'resume\s+Nest=(?:\d+)\s*\.?\s*((?:\w+\.?)+)', lineContents)
resumeContext.append(resumeExt.group(1))
if match.group(1) == 'end-ext':
lastCall = 'end'
# remove the last element from extContext
# print('contents of extContext before pop: %s' % extContext)
extContext.pop()
if match.group(1) == 'end':
lastCall = 'end'
# remove the last element from extContext
# print('contents of extContext before pop: %s' % extContext)
extContext.pop()
if match.group(1) == 'reend':
lastCall = 'reend'
# remove the last element from resumeContext
resumeContext.pop()
else:
match = re.search(r'(call (int|setter|getter|private|method)|End-Function|end-get|end-set|end-method|%s)' % additionalCodeToExtract1, lineContents)
#match = re.search(r'(call (int|setter|getter|private|method)|End-Function|end-get|end-set)', lineContents)
if match:
if lastCall == 'start' or (lastCall[:4] == 'call'):
nestLevel += 1
# if first 4 chars are 'call'
if match.group(1)[:4] == 'call':
if match.group(1) == 'call method':
lastCall = 'callMethod'
# Rearrange lines so that the method is at the start (like all other lines)
lineContents = re.sub(r'call method\s+((?:\w+:?)+)\.(\w+)', r'call \2 \1.OnExecute', lineContents)
if match.group(1) == 'call getter':
lastCall = 'callGetter'
# Rearrange the call getter so that it is in the same format as all the other calls
lineContents = re.sub(r'call getter\s+((?:\w+:?)+)\.(\w+)', r'call getter \2 \1.OnExecute', lineContents)
if match.group(1) == 'call setter':
lastCall = 'callSetter'
# Remove extra space after call setter and also rearrange setter so that it is at the start (like all other lines)
# E.g. call setter EO:CA:Address.EditPageHeader
lineContents = re.sub(r'call setter\s+((?:\w+:?)+)\.(\w+)', r'call setter \2 \1.OnExecute', lineContents)
if match.group(1) == 'call private':
lastCall = 'callPrivate'
# Now we need to append the last value in extContext (i.e. extContext[-1])
if len(extContext) > 0:
lineContents = re.sub(r'call private\s+(\w+)', r'call \1 %s' % extContext[-1], lineContents)
if match.group(1) == 'call int':
lastCall = 'callInt'
# Now we need to append the last value in extContext (i.e. extContext[-1])
if len(extContext) > 0:
lineContents = re.sub(r'call int\s+(\w+)', r'call \1 %s' % extContext[-1], lineContents)
else:
if match.group(1) in additionalCodeToExtractWithNonClosingIndent or match.group(1) in additionalCodeToExtractWithoutIndent or match.group(1) in additionalCodeToExtractWithClosingIndent:
lastCall = 'additionalCode'
lineContents = re.sub(r'(.*)', r'call \1', lineContents)
else:
if match.group(1)[:3].lower() == 'end':
if match.group(1) == 'End-Function':
lastCall ='endFunction'
if match.group(1) == 'end-get':
lastCall = 'endGet'
if match.group(1) == 'end-set':
lastCall = 'endSet'
if match.group(1) == 'end-method':
lastCall = 'endMethod'
nestLevel -= 1
startIndex = 0
for x in range(startIndex,nestLevel + nestLevelOffset):
lineContents = '\t' + lineContents
if match.group(1) in additionalCodeToExtractWithNonClosingIndent:
nestLevelOffset += 2
sessionSpecificString = sessionSpecificString + lineContents + '\n'
prevLineContents = lineContents
# Remove Nest from each line since we no longer need it
sessionSpecificString = re.sub(r'\s+Nest=\d+', '', sessionSpecificString)
# Remove unnecessary trailer junk (e.g. params= or #params=)
sessionSpecificString = re.sub(r'Dur=.*', '', sessionSpecificString)
sessionSpecificString = re.sub(r'[\s]#?params=\d+', '', sessionSpecificString)
#Are there any resume or reend statements?
#If so, then reformat the session specific string based on the resume and reend statements
found = re.search('(resume|reend)\s(.*)', sessionSpecificString)
if found:
# first remove any dots straight after the resume/reend (if there are any)
sessionSpecificString = re.sub(r'(resume|reend)\s+\.\s+(.*)', r'\1 \2', sessionSpecificString)
# Store remaining text line by line in a dict called results
lines = sessionSpecificString.split('\n')
results = {}
lineNo = 1
for lineContents in lines:
results[lineNo] = lineContents
lineNo = lineNo + 1
resumeResults = {}
reendResults = {}
endResults = {}
# Find resume, reend and end statements and store the line numbers in a dict along with the results
for lineNo, line in results.items():
match = re.search(r'(resume|reend|end)\s.*?((?:\w+\.?)+((?:\s(?:\w+\.?)+)?))', line)
if match:
if match.group(1) == 'resume':
resumeResults[lineNo] = match.group(2)
if match.group(1) == 'reend':
reendResults[lineNo] = match.group(2)
if match.group(1) == 'end':
endResults[lineNo] = match.group(2)
# Add a tab to each line before resume until you reach an end for that event
for resumeResultLineNo, resumeResultLine in resumeResults.items():
# If there is actually an end within the same session, then format all lines after the end and before the resume
if resumeResultLine in endResults.values():
for x in range(resumeResultLineNo,0, -1):
match = re.search(r'end\s+%s' % resumeResultLine, results[x])
if match:
break;
else:
if x != resumeResultLineNo:
results[x] = '\t' + results[x]
# Add a tab to each line before reend until you reach a resume for that event
for reendResultLineNo, reendResultLine in reendResults.items():
for x in range(reendResultLineNo,0, -1):
match = re.search(r'resume\s.*?%s' % reendResultLine, results[x])
if match:
break;
else:
if x != reendResultLineNo:
results[x] = '\t' + results[x]
# store results in sessionSpecificString
sessionSpecificString = ''
for lineNo, line in results.items():
sessionSpecificString = sessionSpecificString + results[lineNo] + '\n'
# Clean lines
# Remove the end-ext, End-Function, end-method, end and reend calls, since we no longer need them
str_list = re.findall(r'(?:.*(?:start|resume).*)|.*(?:call\s.*).*|.*(?:%s).*' % additionalCodeToExtract1, sessionSpecificString, re.MULTILINE)
sessionSpecificString = '\n'.join(str_list)
# Prefix all calls with suffix .OnExecute (apart from getter, setter and app engine steps) with call method
sessionSpecificString = re.sub(r'(?m)call\s(?!(?:getter|setter))(.*\.OnExecute)$', r'call method \1', sessionSpecificString)
# Prefix all remaining calls with call function
sessionSpecificString = re.sub(r'(?m)call\s(?!(?:method|getter|setter|%s))(.*(?!\.OnExecute))$' % additionalCodeToExtract1, r'call function \1', sessionSpecificString)
# Rename eligible call methods to call constructors
sessionSpecificString = re.sub(r'(?m)(?m)call\smethod\s+(\w+)(\s.*\1.OnExecute)', r'call constructor \1\2', sessionSpecificString)
# Rename to call function those start-ext calls that do not have getters, setters or constructors appended
sessionSpecificString = re.sub(r'(?m)start-ext\s(\w+)\s(\w+\.\w+\.\w+)$', r'call function \1 \2', sessionSpecificString)
# Replace any remaining start-ext with calls based on the last word in the line
# e.g. start-ext isSaveWarning PT_NAV2.NavOptions.OnExecute getter
# becomes call getter isSaveWarning PT_NAV2.NavOptions.OnExecute
sessionSpecificString = re.sub('start-ext\s(\w+)\s(.*)\s(constructor|method|getter|setter)', r'call \3 \1 \2', sessionSpecificString)
# Finally rename CI functions
sessionSpecificString = re.sub(r'(?m)start-ext\s(\w+)\s(\w+\.\w+)$', r'call function \1 \2', sessionSpecificString)
# Replace any colons with dots
sessionSpecificString = re.sub(':', r'.', sessionSpecificString)
# We now have the complete callstack for the session
# Insert the call stack in the new view
newViewAllTextRegion = sublime.Region(0, newView.size())
if sessionCount == 1:
newView.insert(edit, newViewAllTextRegion.end(), 'Session %s:\n' % sessionNo + sessionSpecificString)
else:
newView.insert(edit, newViewAllTextRegion.end(), '\n\nSession %s:\n' % sessionNo + sessionSpecificString)
newView.sel().clear()
# Increment session count, go to next sessionNo if available
sessionCount += 1
# Set Syntax to PeopleCodeCallStack syntax
newView.set_syntax_file('Packages/PeopleCodeTools/PeopleCodeCallStack.tmLanguage')