-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhawcEndpointHelper.awk
More file actions
135 lines (110 loc) · 3.1 KB
/
Copy pathhawcEndpointHelper.awk
File metadata and controls
135 lines (110 loc) · 3.1 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
# Helper script that works with hawcEndpointSummary.sh
#
# looks at a Python file and extracts some
# relevant info about its url mappings
#
# Works fine even for annotations that span multiple lines?
#
BEGIN {
# we are just using awk to process whole lines; we don't want to split
# so we set FS to some garbage string we won't actually ever encounter
FS = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
IGNORECASE=1
# lhsLen = 100
lhsLen = PAD_WIDTH
processingUrl = 0
currUrl = ""
basePath = ""
currControllerPath = ""
}
match($1, /\<url\(/) {
# print $0
processingUrl = 1
# currUrl = ""
# would need to start counting parentheses to properly do multiline ones...work on this later.
# capture the first argument to the url function; that's the pattern
parsedUrl = match($0, /url\(r'([^']*)'/, a)
valueAttribute = ""
if (parsedUrl > 0) {
# print a[1] "\t\t\t" FILENAME
pattern = a[1]
# strip the leading "^" and the trailing "$"; visual noise for this report
if (match(pattern, /^\^(.*)/, goodParts)) { pattern = goodParts[1] }
if (match(pattern, /(.*)\$$/, goodParts)) { pattern = goodParts[1] }
# replace things like (?P<pk>\d+) with {pk}
if (match(pattern, /(.*)\(.*<(.*)>.*\)(.*)/, goodParts)) {
print "!!!!!"
pattern = goodParts[1] "{" goodParts[2] "}" goodParts[3]
}
pattern = "/" URL_PREFIX pattern
output = pattern
while (length(output) < lhsLen) {
output = output " "
}
output = output FILENAME
output = output " [" NR "]"
print output
}
}
currControllerPath == "" {
# print "#### STARTING ON " FILENAME " ####"
if (match(FILENAME, /.*src\/main\/java\/com\/icfi\/dragon\/web(.*)/, a)) {
currControllerPath = a[1]
} else {
currControllerPath = FILENAME
}
}
match($1, /^public class/) {
pastClassDeclaration = 1
}
match($1, /@RequestMapping/) {
processingAnnotation = 1
currAnnotation = ""
}
processingAnnotation == 1 {
currAnnotation = currAnnotation $0
}
# doing it this way lets us catch annotations that span multiple lines
processingAnnotation == 1 && match($1, /)/) {
processingAnnotation = 0
# print (pastClassDeclaration == 1 ? "method: " : "class: ") currAnnotation
hasValueAttribute = match(currAnnotation, /value *= *"([{}a-zA-Z\/.]*)"/, a)
valueAttribute = ""
if (hasValueAttribute > 0) {
valueAttribute = a[1]
} else {
if (match(currAnnotation, /\( *"([{}a-zA-Z\/.]*)" *\)/, a)) {
valueAttribute = a[1]
}
}
path = ""
if (valueAttribute != "") {
if (pastClassDeclaration == 0) {
basePath = valueAttribute
# print ">>>>>>> basepath set to " basePath
} else {
path = basePath "/" valueAttribute
}
} else {
if (pastClassDeclaration == 1) {
path = basePath
} else {
path = "error - couldn't figure out a path"
}
}
if (path != "") {
path = gensub(/\/\//, "/", "g", path)
hasMethodAttribute = match(currAnnotation, /method *= *([a-zA-Z\/.]*)/, a)
method = "GET"
if (hasMethodAttribute > 0 && match(a[1], /.*POST$/)) {
method = "POST"
}
output = path " (" method ")"
while (length(output) < lhsLen) {
output = output " "
}
output = output currControllerPath
output = output " [" NR "]"
print output
}
}