-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
168 lines (122 loc) · 4.33 KB
/
main.py
File metadata and controls
168 lines (122 loc) · 4.33 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
## -*- coding: utf-8 -*-
import ast, random, string, codecs
from StringIO import StringIO
from mako.runtime import Context
from mako.template import Template
from rope.base.project import Project
from rope.base import (change, pyobjects, exceptions, pynames, worder,
codeanalyze)
from rope.refactor import patchedast
from rope.refactor.restructure import Restructure
from rope.refactor.importutils import module_imports
from rope.refactor.importutils.importinfo import FromImport
from rope.base.codeanalyze import SourceLinesAdapter, ChangeCollector
from mako.exceptions import RichTraceback
import markupsafe
class Highlight(object):
lines = None
node = None
resource = None
def print_code_exact(self):
return self.lines.code[self.node.region[0]:self.node.region[1]]
def print_code(self):
region = self.node.region
lines_min, lines_max = self.lines.get_line_number(region[0]), self.lines.get_line_number(region[1])
lines_min -= 1
if lines_min < 0:
lines_min = 0
lines_max += 2
if lines_max > self.lines.length():
lines_max = self.lines.length()
min_offset = self.lines.get_line_start(lines_min)
max_offset = self.lines.get_line_end(lines_max)
code = self.lines.code[min_offset: max_offset]
region_min = region[0] - min_offset
region_max = region[1] - min_offset
orig_offset = self.lines.get_line_start(self.node.lineno) + self.node.col_offset
orig_offset = orig_offset - min_offset
orig_f_offset = None
if hasattr(self.node, 'f_lineno') and hasattr(self.node, 'f_offset'):
try:
orig_f_offset = self.lines.get_line_start(self.node.f_lineno) + self.node.f_offset
orig_f_offset = orig_f_offset - min_offset
except:
pass
rnd = lambda : ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(20))
a1, a2, a3, a4, a5, a6 = tuple(rnd() for _ in range(6))
t = {
a1: u'<span style="background-color:#339999;">',
a2: u'</span>',
a3: u'<span style="background-color:#660000;">',
a4: u'</span>',
a5: u'<span style="background-color:#060f0f;">',
a6: u'</span>',
}
c = ChangeCollector(code)
c.add_change(region_min, region_min, a1)
c.add_change(region_max, region_max, a2)
c.add_change(orig_offset, orig_offset, a3)
c.add_change(orig_offset+1, orig_offset+1, a4)
if orig_f_offset:
c.add_change(orig_f_offset, orig_f_offset, a5)
c.add_change(orig_f_offset+1, orig_f_offset+1, a6)
code = markupsafe.escape(code)
rend_code = c.get_changed()
for w, h in t.items():
rend_code = rend_code.replace(w,h)
return rend_code
def translate_to_offset(lines, q):
offset = lines.get_line_start(q.lineno) + q.col_offset
return offset
def run(project_directory):
failures = {}
count_errors = 0
project = Project(project_directory)
files = project.pycore.get_python_files()
for file_num, resource in enumerate(files):
if count_errors > 500:
break
pymodule = project.pycore.resource_to_pyobject(resource)
source, node = pymodule.source_code, pymodule.get_ast()
lines = SourceLinesAdapter(source)
try:
patchedast.patch_ast(node, source)
except Exception as ex:
print 'warning: can not read {}'.format(resource)
continue
for n in ast.walk(node):
if hasattr(n, 'region') and hasattr(n, 'lineno') and n.region[0]:
if abs(translate_to_offset(lines, n) - n.region[0]) > 1 :
count_errors += 1
fail = Highlight()
fail.node = n
fail.lines = lines
fail.resource = resource
failures.setdefault(n.__class__, [])
failures[n.__class__].append(fail)
return failures
def render(failures):
template = Template(filename='tmpl.mako', output_encoding='utf-8', input_encoding='utf-8')
instances = []
for t, vals in failures.iteritems():
for i, val in enumerate(vals):
if i > 20:
break
instances.append(val)
buf = codecs.open("results.html", "w", encoding="utf-8")
ctx = Context(buf, instances = instances)
try:
res = template.render_context(ctx)
except:
raise
traceback = RichTraceback()
for (filename, lineno, function, line) in traceback.traceback:
pass
print("File %s, line %s, in %s" % (filename, lineno, function))
print(line, "\n")
print("%s: %s" % (str(traceback.error.__class__.__name__), traceback.error))
def main():
failures = run('/home/sc/t/turnik/.env/lib/python2.7/site-packages')
render(failures)
if __name__ == "__main__":
main()