-
Notifications
You must be signed in to change notification settings - Fork 1
/
flaky.py
194 lines (149 loc) · 5.19 KB
/
flaky.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
"""
Uses pyflakes to process files and look for pyflakes validation errors.
"""
import _ast
import atexit
import json
import os
import sys
from pyflakes.scripts import pyflakes
from pyflakes import checker
MEMORY = {}
def save_memory():
if MEMORY:
with open("memory.json", "w") as memfile:
json.dump(MEMORY, memfile)
atexit.register(save_memory)
def load_memory():
global MEMORY
if os.path.exists("memory.json") and os.stat("memory.json").st_size > 0:
with open("memory.json", "r") as memfile:
MEMORY = json.load(memfile)
def check(codeString, filename):
try:
tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
except SyntaxError as value:
msg = value.args[0]
(lineno, offset, text) = value.lineno, value.offset, value.text
if text is None:
print >>sys.stderr, "%s: problem decoding source"%(filename,)
else:
line = text.splintlines()[-1]
if offset is not None:
offset = offset - (len(text) - len(line))
print >>sys.stderr, "%s:%d: %s"%(filename, lineno, msg)
print >>sys.stderr, line
if offset is not None:
print >>sys.stderr, " " * offset, "^"
raise
else:
w = checker.Checker(tree, filename)
w.messages.sort(key=lambda msg: msg.lineno)
return w.messages
def checkPath(filename):
try:
return check(file(filename, "U").read() + "\n", filename)
except IOError, msg:
print >>sys.stderr, "%s: %s"%(filename, msg.args[1])
return []
IGNORE, DELETE, REPLACE, CONTINUE, ADD_IMPORT = "i", "d", "r", "c", "a"
def prompt():
input = raw_input("action (ignore/delete/replace/continue/add_import): ")
if input:
action = input[-1]
action = action.lower()
if action in (IGNORE, DELETE, REPLACE, CONTINUE, ADD_IMPORT):
return action
else:
return None
else:
return None
def updatePath(filename, warnings, warning_db, safe=True):
with open(filename, "r") as infile:
contents = infile.readlines()
name_format = "{}"
if safe:
name_format = "{}.clean"
for index, line in enumerate(contents):
if 'import' in line:
last_import_index = index
with open(name_format.format(filename), "w") as outfile:
print "updating file: {}".format(filename)
for warning in warnings:
print "{}".format(warning)
print "source: {}".format(contents[warning.lineno - 1])
message = str(warning).split(":")[-1]
replacement = ""
if message in warning_db:
# get the action from memory
action, replacement = warning_db[message]
else:
# prompt for the action...
action = prompt()
while not action:
action = prompt()
if action == REPLACE:
replacement = raw_input("replacement: ")
if action == ADD_IMPORT:
replacement = raw_input("import: ")
# and memorize the action:
warning_db[message] = (action, replacement)
# perform the action:
if action == REPLACE:
line = contents[warning.lineno - 1]
indent = len(line) - len(line.lstrip())
contents[warning.lineno - 1] = "{}{}\n".format(
" " * indent,
replacement
)
elif action == ADD_IMPORT:
contents.insert(last_import_index, replacement)
elif action == IGNORE:
warning_db[message] = (action, '')
elif action == DELETE:
contents[warning.lineno - 1] = "\n"
elif action == CONTINUE:
pass
# write the modified file out:
# TODO: overwrite original file
for line in contents:
outfile.write(line)
print warning_db
#TODO: when we're done debugging
# check the original file for any further warnings
#return checkPath(filename)
return []
def main():
"""
process a file, directory, or stdin with pyflakes
Given a file:
- check with pyflakes
- run update path if there are warnings until that file is clean
"""
load_memory()
warnings = []
args = sys.argv[1:]
if args:
for arg in args:
if os.path.isdir(arg):
for dirpath, dirnames, filenames in os.walk(arg):
for filename in filenames:
if filename.endswith('.py'):
ws = checkPath(os.path.join(dirpath, filename))
warnings.extend(ws)
else:
ws = checkPath(arg)
while ws:
ws = updatePath(arg, ws, MEMORY)
else:
# if it's stdin all we can do is print out warnings:
warnings.extend(check(sys.stdin.read(), '<stdin>'))
for warning in warnings:
print warning
raise SystemExit(warnings > 0)
def g():
x = 0
return
def f():
x = 0
return