Skip to content

Support Python 2.7+ #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"caption": "Preferences",
"children": [
{
"caption": "Package Settings",
"children": [
{
"caption": "Python Coverage",
"children": [
{
"args": {
"file": "${packages}/Python Coverage/Preferences.sublime-settings"
},
"caption": "Settings \u2013 Default",
"command": "open_file"
},
{
"args": {
"file": "${packages}/User/Python Coverage.sublime-settings"
},
"caption": "Settings \u2013 User",
"command": "open_file"
}
]
}
],
"id": "package-settings",
"mnemonic": "P"
}
],
"id": "preferences",
"mnemonic": "n"
}
]
4 changes: 4 additions & 0 deletions Preferences.sublime-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"use_shell": false, // If true, use system Python rather than Sublime's built-in Python.
"python_path" : "" // Determines Python path if `use_shell` is true. If empty, defaults to best guess.
}
64 changes: 18 additions & 46 deletions SublimePythonCoverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,10 @@

import sublime
import sublime_plugin
from coverage import coverage
from coverage.files import FnmatchMatcher
from utils import get_missing_coverage_lines, find, find_cmd, find_tests
PLUGIN_FILE = os.path.abspath(__file__)


def find(base, rel, access=os.R_OK):
if not isinstance(rel, basestring):
rel = os.path.join(*rel)
while 1:
path = os.path.join(base, rel)
if os.access(path, access):
return path
baseprev = base
base = os.path.dirname(base)
if not base or base == baseprev:
return


def find_cmd(base, cmd):
return find(base, ('bin', cmd), os.X_OK)


def find_tests(fname):
dirname = os.path.dirname(fname)
init = os.path.join(dirname, '__init__.py')
if not os.path.exists(init):
# not a package; run tests for the file
return fname

setup = find(dirname, 'setup.py')
if setup:
# run tests for the whole distribution
return os.path.dirname(setup)

# run tests for the package
return os.path.dirname(fname)


class SublimePythonCoverageListener(sublime_plugin.EventListener):
"""Event listener to highlight uncovered lines when a Python file is loaded."""

Expand All @@ -82,6 +48,15 @@ class ShowPythonCoverageCommand(sublime_plugin.TextCommand):
"""Highlight uncovered lines in the current file based on a previous coverage run."""

def run(self, edit):
def get_setting(key, default):
return user_settings.get(key, settings.get(key, default))

user_settings = sublime.load_settings('Python Coverage.sublime-settings')
settings = sublime.load_settings('Preferences.sublime-settings')

use_shell = get_setting('use_shell', False)
python_path = get_setting('python_path', '')

view = self.view
fname = view.file_name()
if not fname:
Expand All @@ -92,25 +67,22 @@ def run(self, edit):
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)
if not omit_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)
for line in missing:
outlines.append(view.full_line(view.text_point(line - 1, 0)))
missing_coverage_lines = get_missing_coverage_lines(
fname, cov_file, PLUGIN_FILE,
quiet=False, use_shell=use_shell, python_path=python_path
)
for line in missing_coverage_lines:
outlines.append(view.full_line(view.text_point(line - 1, 0)))

# update highlighted regions
view.erase_regions('SublimePythonCoverage')
if outlines:
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
Expand Down
19 changes: 19 additions & 0 deletions run_gmcl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Print the result of get_missing_coverage_lines()
#
# Usage:
# python run_gmcl.py FILENAME COVERAGEFILENAME


from utils import get_missing_coverage_lines
import os.path
import sys


def main():
print "\n".join(map(str, get_missing_coverage_lines(
*sys.argv[1:] + [os.path.abspath(__file__)]
)))


if __name__ == '__main__':
main()
79 changes: 79 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from coverage import coverage
from coverage.files import FnmatchMatcher
import os
import os.path
import subprocess


def get_missing_coverage_lines(fname, cov_file, plugin_file, quiet=True, use_shell=False, python_path=None):
def find_python_path():
for path in os.environ['PATH'].split(os.pathsep):
filename = os.path.join(path, 'python')
if os.path.isfile(filename):
return filename

if use_shell:
if not python_path:
python_path = find_python_path()

if not quiet:
print 'Running coverage via shell (Python=%r)' % python_path

cmd = [
python_path, os.path.abspath(os.path.join(os.path.dirname(plugin_file), 'run_gmcl.py')),
fname, cov_file
]

proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
if proc.returncode != 0:
raise Exception('%r failed with returncode %s. Stderr:\n%s' % (cmd, proc.returncode, stderr))

return map(int, stdout.split())
else:
if not quiet:
print 'Running coverage via Sublime Python'

cov = coverage(data_file=cov_file)
omit_matcher = FnmatchMatcher(cov.omit)
if not omit_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)
return missing
return []


def find(base, rel, access=os.R_OK):
if not isinstance(rel, basestring):
rel = os.path.join(*rel)
while 1:
path = os.path.join(base, rel)
if os.access(path, access):
return path
baseprev = base
base = os.path.dirname(base)
if not base or base == baseprev:
return


def find_cmd(base, cmd):
return find(base, ('bin', cmd), os.X_OK)


def find_tests(fname):
dirname = os.path.dirname(fname)
init = os.path.join(dirname, '__init__.py')
if not os.path.exists(init):
# not a package; run tests for the file
return fname

setup = find(dirname, 'setup.py')
if setup:
# run tests for the whole distribution
return os.path.dirname(setup)

# run tests for the package
return os.path.dirname(fname)