-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpiglint.py
executable file
·180 lines (166 loc) · 3.76 KB
/
piglint.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
#!/usr/bin/env python
import re
import argparse
import sys
from os import path
REGISTER = re.compile("REGISTER\s'(.*)'.+")
ALIAS = re.compile("(\w+)\s+=.*(.*)")
STREAM = re.compile("(\w+)(\s|.+)=\s(STREAM|stream)\s(\w+)\s(THROUGH|through)\s(\w+);")
DEFINE = re.compile("^(DEFINE|define)\s(\w+)\s`(\w+.+)`")
GET_ALIAS_REGEX = re.compile("(\w+)(?=\s=)")
PARSE_ALIAS = []
SEEN_ALIAS = []
KEYWORDS = { "and":"",
"any":"",
"all":"",
"arrange":"",
"as":"",
"asc":"",
"AVG":"",
"bag":"",
"BinStorage":"",
"by":"",
"bytearray":"",
"cache":"",
"cat":"",
"cd":"",
"chararray":"",
"cogroup":"",
"CONCAT":"",
"copyFromLocal":"",
"copyToLocal":"",
"COUNT":"",
"cp":"",
"cross":"",
"%declare":"",
"%default":"",
"define":"",
"desc":"",
"describe":"",
"DIFF":"",
"distinct":"",
"double":"",
"du":"",
"dump":"",
"e":"",
"E":"",
"eval":"",
"exec":"",
"explain":"",
"f":"",
"F":"",
"filter":"",
"flatten":"",
"float":"",
"foreach":"",
"full":"",
"generate":"",
"group":"",
"help":"",
"if":"",
"illustrate":"",
"inner":"",
"input":"",
"int":"",
"into":"",
"is":"",
"join":"",
"kill":"",
"l":"",
"L":"",
"left":"",
"limit":"",
"load":"",
"long":"",
"ls":"",
"map":"",
"matches":"",
"MAX":"",
"MIN":"",
"mkdir":"",
"mv":"",
"not":"",
"null":"",
"or":"",
"order":"",
"outer":"",
"output":"",
"parallel":"",
"pig":"",
"PigDump":"",
"PigStorage":"",
"pwd":"",
"quit":"",
"register":"",
"right":"",
"rm":"",
"rmf":"",
"run":"",
"sample":"",
"set":"",
"ship":"",
"SIZE":"",
"split":"",
"stderr":"",
"stdin":"",
"stdout":"",
"store":"",
"stream":"",
"SUM":"",
"TextLoader":"",
"TOKENIZE":"",
"through":"",
"tuple":"",
"union":"",
"using":""}
def parse(f):
"""docstring for parse"""
STREAM_DEFS = []
for line_num, line in enumerate(f):
if ('DEFINE' or 'define') in line:
matched = DEFINE.match(line)
STREAM_FUNC = matched.group(2)
STREAM_DEFS.append(STREAM_FUNC)
if ('REGISTER' or 'register') in line:
matched = REGISTER.match(line)
UDF_PATH = matched.group(1)
if path.exists(UDF_PATH):
pass
else:
print '%s:%i: %s does not exist' % (f.name, line_num+1, UDF_PATH)
if ALIAS.match(line):
matched = ALIAS.match(line)
_ALIAS = matched.group(1)
if _ALIAS in PARSE_ALIAS:
print '%s:%i: Redefining used alias %s' % (f.name, line_num+1, _ALIAS)
else:
PARSE_ALIAS.append(_ALIAS)
if ('STREAM' or 'stream') in line:
matched = STREAM.match(line)
DEFINED_STREAM_FUNCTION = matched.group(6)
if DEFINED_STREAM_FUNCTION in STREAM_DEFS:
pass
else:
print '%s:%i: Undefined STREAM function %s' % (f.name, line_num+1, DEFINED_STREAM_FUNCTION)
if matched.group(4) not in PARSE_ALIAS:
print '%s:%i: Undefined alias %s' % (f.name, line_num+1, matched.group(4))
def alias(f):
"""docstring for alias"""
for line in f:
if GET_ALIAS_REGEX.match(line):
x = GET_ALIAS_REGEX.match(line)
SEEN_ALIAS.append(x.group(1))
if __name__ == '__main__':
ap = argparse.ArgumentParser(description='PigLint')
ap.add_argument('--file', action='store', dest='FILENAME', help='Pig script to be linted', required=True)
ap.add_argument('--version', action='version', version='%(prog)s 0.1')
args = ap.parse_args()
try:
f = open(args.FILENAME)
alias(f)
f = open(args.FILENAME)
parse(f)
f.close()
except IOError, e:
print(e)
sys.exit(-1)