-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsparkParser.py
executable file
·215 lines (175 loc) · 7.18 KB
/
sparkParser.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
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
#! /usr/bin/python
# Copyright (c) 2016-2018 Stanford University
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""Spark Parser
Scans through a directory containing Java/Scala source files and attempts to
extract statistics.
Usage:
parser.py [-h] LOG_FN ROOT_DIR
Options:
-h --help Show this help messages
LOG_FN Log function to search for in the Java/Scala sources
ROOT_DIR Root directory of sources to scan for *scala and *java
source files and parse their LOG_FN's
"""
from parser import *
from docopt import docopt
import re, os
####
# Below are configuration parameters to be toggled by the library implementer
####
# Since header files are in-lined after the GNU preprocessing step, library
# files can be unintentionally processed. This list is a set of files that
# should be ignored
ignored_files = set([
])
# Given a Java/Scala string that is (optionally) formatted with a .format(...),
# extract the characters in the quotes before the .format(...) invocation. This
# function can handle the special case of multi-formats
# (i.e. ("s1" + "s2").format(...))
#
# \param source
# Java/Scala string to process
# \return
# A single string of the characters contained with the quotes
def extractJavaFmtString(source):
returnString = ""
isInQuotes = False
prevWasEscape = False
for line in source.splitlines(True):
for c in line:
if c == "\"" and not prevWasEscape:
isInQuotes = not isInQuotes
elif isInQuotes:
returnString += c
prevWasEscape = c == "\\"
elif c == '.' or c == ')':
break
return returnString
# Separate a log statement into its constituent parts joined by +
def separateLogFragments(line):
# The algorithm uses the heuristic of assuming that the argument ends
# when it finds a terminating character (either a + or right parenthesis)
# in a position where the relative parenthesis/curly braces/bracket depth is 0
# The latter constraint prevents false positives where function calls are used
# to generate the parameter (i.e. log("number is %d", calculate(a, b)))
parenDepth = 0
curlyDepth = 0
bracketDepth = 0
inQuotes = False
argSrcStr = ""
prevWasEscape = False
fragments = []
for i in range(len(line)):
c = line[i]
argSrcStr = argSrcStr + c
# If it's an escape, we don't care what comes after it
if c == "\\" or prevWasEscape:
prevWasEscape = not prevWasEscape
continue
# Start counting depths
if c == "\"":
inQuotes = not inQuotes
# Don't count curlies and parenthesis when in quotes
if inQuotes:
continue
if c == "{":
curlyDepth = curlyDepth + 1
elif c == "}":
curlyDepth = curlyDepth - 1
elif c == "(":
parenDepth = parenDepth + 1
elif c == ")" and parenDepth > 0:
parenDepth = parenDepth - 1
elif c == "[":
bracketDepth = bracketDepth + 1
elif c == "]":
bracketDepth = bracketDepth - 1
elif (c == "+" or c == ")") and curlyDepth == 0 \
and parenDepth == 0 and bracketDepth == 0:
# found it
fragments.append(argSrcStr[:-1].strip())
argSrcStr = ""
argSrcStr = argSrcStr.strip()
if len(argSrcStr) > 0:
fragments.append(argSrcStr)
return fragments
def processScalaLog(logStatement, format_index):
formatArgument = logStatement['arguments'][format_index].source
totalStaticChars = 0
totalDynaVars = 0
completeLog = ""
fmtOutput = "\t#%-4d %-4d %-15s %s" # static chars, dynaArgs, type, fragment
inlineVarRegex = r'\$(\{[^\}]+}|[a-zA-Z0-9]+)'
formatSpecifierRegex = "%" \
"(?P<flags>[-+ #0]+)?" \
"(?P<width>[\\d]+|\\*)?" \
"(\\.(?P<precision>\\d+|\\*))?" \
"(?P<length>hh|h|l|ll|j|z|Z|t|L)?" \
"(?P<specifier>[diuoxXfFeEgGaAcspn])"
for fragment in separateLogFragments(formatArgument):
isSubstitution = fragment[0] == 's'
# Handle the triple quote case since it's the easiest.
if fragment.startswith("s\"\"\"") \
or fragment.startswith("\"\"\"") \
or fragment.startswith("(\"\"\""):
# This is currently an unhandled case, so we just error
assert(fragment[0] != '(')
# Rip out the googy center of the triple quotes """
assert(len(re.findall("\"\"\"", fragment)) == 2)
begin = fragment.index("\"\"\"") + 3
end = fragment.index("\"\"\"", begin)
fragment = fragment[begin:end].replace("\n", "")
# Count the types
if(isSubstitution):
numDynaVars = len(re.findall(inlineVarRegex, fragment))
numStaticChars = len(re.sub(inlineVarRegex, '', fragment))
else:
numDynaVars = len(re.findall(formatSpecifierRegex, fragment))
numStaticChars = len(re.sub(formatSpecifierRegex, '', fragment))
# Detect substitions (i.e. s"Hello $user")
elif isSubstitution:
fragment = fragment[2:-1] # remove the s""
numDynaVars = len(re.findall(inlineVarRegex, fragment))
numStaticChars = len(re.sub(inlineVarRegex, '', fragment))
# print fmtOutput % (numStaticChars, numDynaVars, "Substitution", fragment)
# Detect "static string" or "Format".format(..) or ("" + "").format()
elif fragment[0] == '\"' or fragment[0] == '(':
fragment = extractJavaFmtString(fragment)
numDynaVars = len(re.findall(formatSpecifierRegex, fragment))
numStaticChars = len(re.sub(formatSpecifierRegex, '', fragment))
# print fmtOutput % (numStaticChars, numDynaVars, "Format", fragment)
# They are just variables, i.e. logInfo(variable)
else:
numDynaVars = 1
numStaticChars = 0
fragment = "v:{" + fragment.replace("\n", "") + "}"
# print fmtOutput %(numStaticChars, numDynaVars, "Variable", fragment)
completeLog += fragment
totalStaticChars += numStaticChars
totalDynaVars += numDynaVars
return "%-4d %-4d %-4d %-4d %-4d %-4d %s" % (totalStaticChars, totalDynaVars,
0,0,0,0,
completeLog)
if __name__ == "__main__":
arguments = docopt(__doc__, version='Scala/Java Preprocesor v0.1')
sourceFiles = []
for dirpath, dirs, files in os.walk(arguments['ROOT_DIR']):
for file in files:
if file.endswith("scala") or file.endswith("java"):
sourceFiles.append(os.path.join(dirpath, file))
print "# Static Dynamic Ints Floats String Special Format"
print "# Note Int/FLoats/String/Special are always 0"
for sourceFile in sourceFiles:
processFile(sourceFile, arguments["LOG_FN"], 0, processScalaLog)