-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
87 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/python/pybuilder/plugins/python/jedi_linter_plugin.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of PyBuilder | ||
# | ||
# Copyright 2011-2014 PyBuilder Team | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Plugin for Jedi linting support. | ||
https://jedi.jedidjah.ch/ | ||
""" | ||
|
||
__author__ = 'Maximilien Riehl' | ||
|
||
try: | ||
import jedi | ||
except ImportError: | ||
jedi = None | ||
|
||
from pybuilder.core import after, task, init, use_plugin, depends | ||
from pybuilder.errors import BuildFailedException, MissingPrerequisiteException, InternalException | ||
from pybuilder.utils import discover_files_matching | ||
|
||
|
||
use_plugin("python.core") | ||
|
||
|
||
@init | ||
def initialize_jedi_linter_plugin(project, logger): | ||
project.build_depends_on("jedi") | ||
project.set_property_if_unset("jedi_linter_break_build", False) | ||
project.set_property_if_unset("jedi_linter_verbose", False) | ||
|
||
logger.warn("The jedi plugin is unstable since the linter API will probably change.") | ||
|
||
|
||
@after("prepare") | ||
def assert_jedi_is_installed(logger): | ||
if not jedi: | ||
raise MissingPrerequisiteException("Missing build dependency `jedi`, please install_dependencies first.") | ||
if "_analysis" not in dir(jedi.Script): | ||
raise InternalException("The jedi linter API changed, please contact the authors") | ||
|
||
|
||
@task | ||
@depends("prepare") | ||
def analyze(project, logger): | ||
source_modules = discover_files_matching(project.get_property("dir_source_main_python"), | ||
"*.py") | ||
errors = [] | ||
|
||
logger.info("Executing jedi linter on project sources.") | ||
|
||
try: | ||
for path in source_modules: | ||
for error in jedi.Script(path=path)._analysis(): | ||
errors.append(error) | ||
except Exception as e: | ||
logger.error("Jedi crashed: {0}".format(e)) | ||
|
||
number_of_errors = len(errors) | ||
output = logger.info if number_of_errors == 0 else logger.warn | ||
output("Jedi linter found {0} errors.".format(number_of_errors)) | ||
if project.get_property("jedi_linter_verbose") or project.get_property("verbose"): | ||
for error in errors: | ||
logger.warn(error) | ||
|
||
if project.get_property("jedi_linter_break_build") and number_of_errors > 0: | ||
raise BuildFailedException("Jedi linter found errors") |