Index: third_party/pylint/epylint.py |
diff --git a/third_party/pylint/epylint.py b/third_party/pylint/epylint.py |
index beae481b818c50d123172f22be3ac2925bd42ea6..f6b16e7ac971a386eff2092396882e3320708f7a 100644 |
--- a/third_party/pylint/epylint.py |
+++ b/third_party/pylint/epylint.py |
@@ -1,19 +1,5 @@ |
+#!/usr/bin/env python |
# -*- coding: utf-8; mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=python:et:sw=4:ts=4:sts=4 |
-# Copyright (c) 2003-2013 LOGILAB S.A. (Paris, FRANCE). |
-# http://www.logilab.fr/ -- mailto:contact@logilab.fr |
-# |
-# This program is free software; you can redistribute it and/or modify it under |
-# the terms of the GNU General Public License as published by the Free Software |
-# Foundation; either version 2 of the License, or (at your option) any later |
-# version. |
-# |
-# This program is distributed in the hope that it will be useful, but WITHOUT |
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
-# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details |
-# |
-# You should have received a copy of the GNU General Public License along with |
-# this program; if not, write to the Free Software Foundation, Inc., |
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
"""Emacs and Flymake compatible Pylint. |
This script is for integration with emacs and is compatible with flymake mode. |
@@ -29,8 +15,7 @@ For example: |
a/b/x.py |
a/c/y.py |
- - Then if y.py imports x as "from a.b import x" the following produces pylint |
- errors |
+ - Then if y.py imports x as "from a.b import x" the following produces pylint errors |
cd a/c; pylint y.py |
@@ -42,75 +27,75 @@ For example: |
we are checking we need to go out of it to avoid these false positives. |
-You may also use py_run to run pylint with desired options and get back (or not) |
-its output. |
+You may also use py_run to run pylint with desired options and get back (or not) its output. |
""" |
-import sys, os |
-import os.path as osp |
+import sys, os, re |
from subprocess import Popen, PIPE |
-def _get_env(): |
- '''Extracts the environment PYTHONPATH and appends the current sys.path to |
- those.''' |
- env = dict(os.environ) |
- env['PYTHONPATH'] = os.pathsep.join(sys.path) |
- return env |
-def lint(filename, options=None): |
+def lint(filename): |
"""Pylint the given file. |
- When run from emacs we will be in the directory of a file, and passed its |
- filename. If this file is part of a package and is trying to import other |
- modules from within its own package or another package rooted in a directory |
- below it, pylint will classify it as a failed import. |
+ When run from emacs we will be in the directory of a file, and passed its filename. |
+ If this file is part of a package and is trying to import other modules from within |
+ its own package or another package rooted in a directory below it, pylint will classify |
+ it as a failed import. |
- To get around this, we traverse down the directory tree to find the root of |
- the package this module is in. We then invoke pylint from this directory. |
+ To get around this, we traverse down the directory tree to find the root of the package this |
+ module is in. We then invoke pylint from this directory. |
- Finally, we must correct the filenames in the output generated by pylint so |
- Emacs doesn't become confused (it will expect just the original filename, |
- while pylint may extend it with extra directories if we've traversed down |
- the tree) |
+ Finally, we must correct the filenames in the output generated by pylint so Emacs doesn't |
+ become confused (it will expect just the original filename, while pylint may extend it with |
+ extra directories if we've traversed down the tree) |
""" |
# traverse downwards until we are out of a python package |
- full_path = osp.abspath(filename) |
- parent_path = osp.dirname(full_path) |
- child_path = osp.basename(full_path) |
+ fullPath = os.path.abspath(filename) |
+ parentPath, childPath = os.path.dirname(fullPath), os.path.basename(fullPath) |
- while parent_path != "/" and osp.exists(osp.join(parent_path, '__init__.py')): |
- child_path = osp.join(osp.basename(parent_path), child_path) |
- parent_path = osp.dirname(parent_path) |
+ while parentPath != "/" and os.path.exists(os.path.join(parentPath, '__init__.py')): |
+ childPath = os.path.join(os.path.basename(parentPath), childPath) |
+ parentPath = os.path.dirname(parentPath) |
# Start pylint |
- # Ensure we use the python and pylint associated with the running epylint |
- from pylint import lint as lint_mod |
- lint_path = lint_mod.__file__ |
- options = options or ['--disable=C,R,I'] |
- cmd = [sys.executable, lint_path] + options + [ |
- '--msg-template', '{path}:{line}: {category} ({msg_id}, {symbol}, {obj}) {msg}', |
- '-r', 'n', child_path] |
- process = Popen(cmd, stdout=PIPE, cwd=parent_path, env=_get_env(), |
- universal_newlines=True) |
- |
- for line in process.stdout: |
+ process = Popen('pylint -f parseable -r n --disable=C,R,I "%s"' % |
+ childPath, shell=True, stdout=PIPE, stderr=PIPE, |
+ cwd=parentPath) |
+ p = process.stdout |
+ |
+ # The parseable line format is '%(path)s:%(line)s: [%(sigle)s%(obj)s] %(msg)s' |
+ # NOTE: This would be cleaner if we added an Emacs reporter to pylint.reporters.text .. |
+ regex = re.compile(r"\[(?P<type>[WE])(?P<remainder>.*?)\]") |
+ |
+ def _replacement(mObj): |
+ "Alter to include 'Error' or 'Warning'" |
+ if mObj.group("type") == "W": |
+ replacement = "Warning" |
+ else: |
+ replacement = "Error" |
+ # replace as "Warning (W0511, funcName): Warning Text" |
+ return "%s (%s%s):" % (replacement, mObj.group("type"), mObj.group("remainder")) |
+ |
+ for line in p: |
# remove pylintrc warning |
if line.startswith("No config file found"): |
continue |
- |
+ line = regex.sub(_replacement, line, 1) |
# modify the file name thats output to reverse the path traversal we made |
parts = line.split(":") |
- if parts and parts[0] == child_path: |
+ if parts and parts[0] == childPath: |
line = ":".join([filename] + parts[1:]) |
print line, |
- process.wait() |
- return process.returncode |
+ p.close() |
+ |
+def Run(): |
+ lint(sys.argv[1]) |
def py_run(command_options='', return_std=False, stdout=None, stderr=None, |
script='epylint'): |
- """Run pylint from python |
+ """Run pylint from python (needs Python >= 2.4). |
``command_options`` is a string containing ``pylint`` command line options; |
``return_std`` (boolean) indicates return of created standart output |
@@ -152,24 +137,13 @@ def py_run(command_options='', return_std=False, stdout=None, stderr=None, |
else: |
stderr = sys.stderr |
# Call pylint in a subprocess |
- p = Popen(command_line, shell=True, stdout=stdout, stderr=stderr, |
- env=_get_env(), universal_newlines=True) |
+ p = Popen(command_line, shell=True, stdout=stdout, stderr=stderr) |
p.wait() |
# Return standart output and error |
if return_std: |
return (p.stdout, p.stderr) |
-def Run(): |
- if len(sys.argv) == 1: |
- print "Usage: %s <filename> [options]" % sys.argv[0] |
- sys.exit(1) |
- elif not osp.exists(sys.argv[1]): |
- print "%s does not exist" % sys.argv[1] |
- sys.exit(1) |
- else: |
- sys.exit(lint(sys.argv[1], sys.argv[2:])) |
- |
- |
if __name__ == '__main__': |
- Run() |
+ lint(sys.argv[1]) |
+ |