- Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSublimePythonCoverage.py
More file actions
Latest commit
170 lines (126 loc) · 5.08 KB
/
Copy pathSublimePythonCoverage.py
File metadata and controls
170 lines (126 loc) · 5.08 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
169
170
# bootstrap
importos
plugin_path=os.path.dirname(__file__)
ifnotos.path.exists(os.path.join(plugin_path, 'coverage')):
# Fetch coverage.py
print'SublimePythonCoverage installing coverage.py.'
fromStringIOimportStringIO
importtarfile
importurllib
fromhashlibimportmd5
SOURCE='http://pypi.python.org/packages/source/c/coverage/coverage-3.5.2.tar.gz'
MD5SUM='28c43d41b13f8987ea14d7b1d4a4e3ec'
payload=urllib.urlopen(SOURCE).read()
ifmd5(payload).hexdigest() !=MD5SUM:
raiseImportError('Invalid checksum.')
tar=tarfile.open(mode='r:gz', fileobj=StringIO(payload))
formintar.getmembers():
ifnotm.name.startswith('coverage-3.5.2/coverage/'):
continue
m.name='/'.join(m.name.split('/')[2:])
tar.extract(m, os.path.join(plugin_path, 'coverage'))
print'SublimePythonCoverage successfully installed coverage.py.'
# end bootstrap
importsublime
importsublime_plugin
fromcoverageimportcoverage
fromcoverage.filesimportFnmatchMatcher
PLUGIN_FILE=os.path.abspath(__file__)
deffind(base, rel, access=os.R_OK):
ifnotisinstance(rel, basestring):
rel=os.path.join(*rel)
while1:
path=os.path.join(base, rel)
ifos.access(path, access):
returnpath
baseprev=base
base=os.path.dirname(base)
ifnotbaseorbase==baseprev:
return
deffind_cmd(base, cmd):
returnfind(base, ('bin', cmd), os.X_OK)
deffind_tests(fname):
dirname=os.path.dirname(fname)
init=os.path.join(dirname, '__init__.py')
ifnotos.path.exists(init):
# not a package; run tests for the file
returnfname
setup=find(dirname, 'setup.py')
ifsetup:
# run tests for the whole distribution
returnos.path.dirname(setup)
# run tests for the package
returnos.path.dirname(fname)
classSublimePythonCoverageListener(sublime_plugin.EventListener):
"""Event listener to highlight uncovered lines when a Python file is loaded."""
defon_load(self, view):
if'source.python'notinview.scope_name(0):
return
view.run_command('show_python_coverage')
classShowPythonCoverageCommand(sublime_plugin.TextCommand):
"""Highlight uncovered lines in the current file based on a previous coverage run."""
defrun(self, edit):
view=self.view
fname=view.file_name()
ifnotfname:
return
cov_file=find(fname, '.coverage')
ifnotcov_file:
print'Could not find .coverage file.'
return
# run analysis and find uncovered lines
cov=coverage(data_file=cov_file)
outlines= []
omit_matcher=FnmatchMatcher(cov.omit)
ifnotomit_matcher.match(fname):
cov_dir=os.path.dirname(cov_file)
os.chdir(cov_dir)
relpath=os.path.relpath(fname, cov_dir)
cov.load()
f, s, excluded, missing, m=cov.analysis2(relpath)
forlineinmissing:
outlines.append(view.full_line(view.text_point(line-1, 0)))
# update highlighted regions
view.erase_regions('SublimePythonCoverage')
ifoutlines:
view.add_regions('SublimePythonCoverage', outlines,
'markup.inserted', 'bookmark', sublime.HIDDEN)
# manually import the module containing ST2's default build command,
# since it's in a module whose name is a Python keyword :-s
ExecCommand=__import__('exec').ExecCommand
classTestExecCommand(ExecCommand):
"""An generic extension of the default build system which shows coverage at the end."""
runner=None
defcmd(self, runner, testpath):
NotImplemented
defrun(self, **kw):
if'cmd'notinkw:
fname=self.window.active_view().file_name()
# look for a virtualenv with nosetests, py.test etc
runner=find_cmd(fname, self.runner)
ifrunnerisNone:
# no virtualenv; maybe there's a global one
runner=self.runner
testpath=find_tests(fname)
ifos.path.isdir(testpath):
kw['working_dir'] =testpath
else:
kw['working_dir'] =os.path.dirname(testpath)
kw['cmd'] =self.cmd(runner, testpath)
super(TestExecCommand, self).run(**kw)
deffinish(self, proc):
super(TestExecCommand, self).finish(proc)
forviewinself.window.views():
view.run_command('show_python_coverage')
classNoseExecCommand(TestExecCommand):
"""An extension of the default build system using the Python Nose test
runner to generate coverage information."""
runner='nosetests'
defcmd(self, runner, testpath):
return [runner, '--with-coverage', testpath]
classPytestExecCommand(TestExecCommand):
"""An extension of the default build system using the py.test test
runner to generate coverage information."""
runner='py.test'
defcmd(self, runner, testpath):
return [runner]