Index: third_party/pylint/epylint.py |
=================================================================== |
--- third_party/pylint/epylint.py (revision 292986) |
+++ third_party/pylint/epylint.py (working copy) |
@@ -1,5 +1,19 @@ |
-#!/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. |
@@ -15,7 +29,8 @@ |
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 |
@@ -27,75 +42,75 @@ |
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, re |
+import sys, os |
+import os.path as osp |
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): |
+def lint(filename, options=None): |
"""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 |
- fullPath = os.path.abspath(filename) |
- parentPath, childPath = os.path.dirname(fullPath), os.path.basename(fullPath) |
+ full_path = osp.abspath(filename) |
+ parent_path = osp.dirname(full_path) |
+ child_path = osp.basename(full_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) |
+ 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) |
# Start pylint |
- process = Popen('pylint -f parseable -r n --disable=C,R,I "%s"' % |
- childPath, shell=True, stdout=PIPE, stderr=PIPE, |
- cwd=parentPath) |
- p = process.stdout |
+ # 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) |
- # 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: |
+ for line in process.stdout: |
# 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] == childPath: |
+ if parts and parts[0] == child_path: |
line = ":".join([filename] + parts[1:]) |
print line, |
- p.close() |
+ process.wait() |
+ return process.returncode |
-def Run(): |
- lint(sys.argv[1]) |
- |
def py_run(command_options='', return_std=False, stdout=None, stderr=None, |
script='epylint'): |
- """Run pylint from python (needs Python >= 2.4). |
+ """Run pylint from python |
``command_options`` is a string containing ``pylint`` command line options; |
``return_std`` (boolean) indicates return of created standart output |
@@ -137,7 +152,8 @@ |
else: |
stderr = sys.stderr |
# Call pylint in a subprocess |
- p = Popen(command_line, shell=True, stdout=stdout, stderr=stderr) |
+ p = Popen(command_line, shell=True, stdout=stdout, stderr=stderr, |
+ env=_get_env(), universal_newlines=True) |
p.wait() |
# Return standart output and error |
if return_std: |
@@ -144,6 +160,16 @@ |
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__': |
- lint(sys.argv[1]) |
- |
+ Run() |