Module: check_mk
Branch: master
Commit: 86bf9e9374e400671aefc950466f7d630304970b
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=86bf9e9374e400…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Sat Apr 23 21:56:33 2016 +0200
pylint: Writing out original file and line of reported problems
---
pylint/pylint-web | 5 +++--
pylint/pylint_cmk.py | 48 +++++++++++++++++++++++++++++++++++++++++++++--
pylint/pylintrc.checks | 3 ++-
pylint/pylintrc.modules | 3 ++-
pylint/pylintrc.web | 4 ++--
5 files changed, 55 insertions(+), 8 deletions(-)
diff --git a/pylint/pylint-web b/pylint/pylint-web
index 29ff5e3..92e53cd 100755
--- a/pylint/pylint-web
+++ b/pylint/pylint-web
@@ -4,7 +4,6 @@
import os
import sys
import glob
-import shutil
import tempfile
import pylint_cmk
@@ -43,7 +42,9 @@ modules = glob.glob("../web/htdocs/*.py") \
for module in modules:
print("Copy %s to test directory" % module)
- shutil.copy(module, base_path + "/" + os.path.basename(module))
+ f = open(base_path + "/" + os.path.basename(module), "w")
+ pylint_cmk.add_file(f, module)
+ f.close()
# Move the whole plugins code to their modules, then
# run pylint only on the modules
diff --git a/pylint/pylint_cmk.py b/pylint/pylint_cmk.py
index 633e8e0..a2198e0 100644
--- a/pylint/pylint_cmk.py
+++ b/pylint/pylint_cmk.py
@@ -3,10 +3,14 @@
import os
import sys
+import glob
import shutil
import subprocess
import tempfile
+from pylint.reporters.text import ColorizedTextReporter
+from pylint.utils import Message
+
def ordered_module_files():
modules = [
@@ -65,9 +69,13 @@ def check_files():
def add_file(f, path):
+ # Change path to be relative to "workdir" /home/git or the workdir
+ # in the build system.
+ relpath = os.path.relpath(os.path.realpath(path),
+ os.path.dirname(os.path.dirname(os.getcwd())))
f.write("\n")
f.write("#\n")
- f.write("# " + path + "\n")
+ f.write("# ORIG-FILE: " + relpath + "\n")
f.write("#\n")
f.write("\n")
f.write(file(path).read())
@@ -81,7 +89,7 @@ def get_test_dir():
else:
base_path = tempfile.mkdtemp(prefix="cmk_pylint")
- print("Prepare check in %s..." % base_path)
+ print("Prepare check in %s ..." % base_path)
return base_path
@@ -93,6 +101,7 @@ def run_pylint(cfg_file, base_path):
pylint_cfg = os.getcwd() + "/" + cfg_file
+ os.putenv("PYLINT_PATH", os.getcwd())
cmd = "pylint --rcfile=\"%s\" %s*.py" % (pylint_cfg,
pylint_args)
print("Running pylint with: %s" % cmd)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
@@ -125,3 +134,38 @@ def ensure_equal_branches():
sys.stderr.write("ERROR: Different branches (%s != %s)\n" %
(cmk_branch, cmc_branch))
sys.exit(1)
+
+
+
+# Check_MK currently uses a packed version of it's files to
+# run the pylint tests because it's not well structured in
+# python modules. This custom reporter rewrites the found
+# messages to tell the users the original location in the
+# python sources
+class CMKColorizedTextReporter(ColorizedTextReporter):
+ def handle_message(self, msg):
+ lines = file(msg.abspath).readlines()
+
+ line_nr = msg.line
+ orig_file, went_back = None, -3
+ while line_nr > 0:
+ line_nr -= 1
+ went_back += 1
+ line = lines[line_nr]
+ if line.startswith("# ORIG-FILE: "):
+ orig_file = line.split(": ", 1)[1].strip()
+ break
+
+ if orig_file != None:
+ msg = msg._replace(line=went_back, path=orig_file)
+
+ ColorizedTextReporter.handle_message(self, msg)
+
+
+# Is called by pylint to load this plugin
+def register(linter):
+ sys.path = glob.glob("/omd/versions/default/lib/python/*.egg") \
+ + [ "/omd/versions/default/lib/python" ] \
+ + sys.path
+
+ linter.register_reporter(CMKColorizedTextReporter)
diff --git a/pylint/pylintrc.checks b/pylint/pylintrc.checks
index a4468df..27defb2 100644
--- a/pylint/pylintrc.checks
+++ b/pylint/pylintrc.checks
@@ -1,6 +1,7 @@
[MASTER]
# Use the OMD default version lib dir to add the mod_python modules
-init-hook='import sys, glob; sys.path =
glob.glob("/omd/versions/default/lib/python/*.egg") + [ "web/htdocs/",
"/omd/versions/default/lib/python" ] + sys.path'
+init-hook='import sys; sys.path.insert(0,
os.environ.get("PYLINT_PATH"))'
+load-plugins=pylint_cmk
persistent=yes
# We only want to see errors for the moment. Disable this one day. But prepare to cleanup
a lot of stuff.
errors-only=yes
diff --git a/pylint/pylintrc.modules b/pylint/pylintrc.modules
index 40a030b..7194bb7 100644
--- a/pylint/pylintrc.modules
+++ b/pylint/pylintrc.modules
@@ -1,6 +1,7 @@
[MASTER]
# Use the OMD default version lib dir to add the mod_python modules
-init-hook='import sys, glob; sys.path =
glob.glob("/omd/versions/default/lib/python/*.egg") + [ "web/htdocs/",
"/omd/versions/default/lib/python" ] + sys.path'
+init-hook='import sys; sys.path.insert(0,
os.environ.get("PYLINT_PATH"))'
+load-plugins=pylint_cmk
persistent=yes
# We only want to see errors for the moment. Disable this one day. But prepare to cleanup
a lot of stuff.
errors-only=yes
diff --git a/pylint/pylintrc.web b/pylint/pylintrc.web
index 97eb5dd..bc42d0c 100644
--- a/pylint/pylintrc.web
+++ b/pylint/pylintrc.web
@@ -1,8 +1,8 @@
[MASTER]
# Use the OMD default version lib dir to add the mod_python modules
-init-hook='import sys; sys.path = [ "web/htdocs/",
"/omd/versions/default/lib/python" ] + sys.path'
+init-hook='import sys; sys.path.insert(0,
os.environ.get("PYLINT_PATH"))'
+load-plugins=pylint_cmk
persistent=yes
-
# We only want to see errors for the moment. Disable this one day. But prepare to cleanup
a lot of stuff.
errors-only=yes