~ubuntu-branches/ubuntu/natty/python-distutils-extra/natty-updates

« back to all changes in this revision

Viewing changes to DistUtilsExtra/command/pylint.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-02-11 10:39:50 UTC
  • mfrom: (18.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110211103950-kseoijf2ilwdr3rr
Tags: 2.24-1
* auto.py: Fix the string comparison to not install *.notifyrc.in files
  twice. Thanks Éric Araujo for spotting this!
* DistUtilsExtra/auto.py, __provides(): Do not accidentally clobber
  "py_modules" with "packages", by using a proper copy of the list. Remove
  error filter workaround from test/auto.py.
* Rename "check" command to "pylint", to avoid clobbering distutils' own
  check command in 2.7/3.x. (LP: #714655)
* DistUtilsExtra/auto.py, DistUtilsExtra/command/__init__.py: Actually
  expose the pylint command as a setup.py command.
* Remove pykdeuic4 integration, it's been a continuous source of build
  failures and bugs. Using uic.loadUi() is much more robust.
  Instead, install Qt *.ui files into /usr/share/projectname/ just like the
  GtkBuilder *.ui files.
* debian/compat, debian/control: Bump to dh 7 compatibility, as we are using
  dh_auto_* magic.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# DistUtilsExtra.command.pylint - DistUtilsExtra command to call pylint
 
2
#
 
3
# Author: Rodney Dawes <rodney.dawes@canonical.com>
 
4
# Copyright 2009 Canonical Ltd.
 
5
 
 
6
"""DistUtilsExtra.command.pylint
 
7
 
 
8
Implements the DistUtilsExtra 'pylint' command.
 
9
"""
 
10
 
 
11
import os
 
12
import subprocess
 
13
 
 
14
from distutils.core import Command
 
15
 
 
16
 
 
17
class pylint (Command):
 
18
    """Command to run pylint and tests on a module."""
 
19
 
 
20
    description = "integrate pylint checks"
 
21
 
 
22
    user_options = [("config-file=", None,
 
23
                     "pylint config file to use"),
 
24
                    ("exclude-files=", None,
 
25
                     "list of files to exclude from lint checks"),
 
26
                    ("lint-files=", None,
 
27
                     "list of modules or packages to run lint checks on")
 
28
                   ]
 
29
 
 
30
    def initialize_options (self):
 
31
        self.config_file = None
 
32
        self.exclude_files = None
 
33
        self.lint_files = None
 
34
 
 
35
    def finalize_options (self):
 
36
        if self.config_file is None:
 
37
            self.config_file = ""
 
38
        if self.exclude_files is None:
 
39
            self.exclude_files = "[]"
 
40
        if self.lint_files is None:
 
41
            self.lint_files = "[" + self.__find_files() + "]"
 
42
 
 
43
    def run (self):
 
44
        pylint_args = ["--output-format=parseable",
 
45
                       "--include-ids=yes"]
 
46
 
 
47
        if self.config_file:
 
48
            pylint_args.append("--rcfile=" + self.config_file)
 
49
 
 
50
        for file in eval(self.lint_files):
 
51
            pylint_args.append(file)
 
52
 
 
53
        p = subprocess.Popen(["pylint"] + pylint_args,
 
54
                             bufsize=4096, stdout=subprocess.PIPE)
 
55
        notices = p.stdout
 
56
 
 
57
        output = "".join(notices.readlines())
 
58
        if output != "":
 
59
            print "== Pylint notices =="
 
60
            print self.__group_lines_by_file(output)
 
61
 
 
62
    def __group_lines_by_file(self, input):
 
63
        """Format file:line:message output as lines grouped by file."""
 
64
        outputs = []
 
65
        filename = ""
 
66
        excludes = eval(self.exclude_files)
 
67
        for line in input.splitlines():
 
68
            current = line.split(":", 3)
 
69
            if line.startswith("    "):
 
70
                outputs.append("    " + current[0] + "")
 
71
            elif line.startswith("build/") or current[0] in excludes or \
 
72
                    len(current) < 3:
 
73
                pass
 
74
            elif filename == current[0]:
 
75
                outputs.append("    " + current[1] + ": " + current[2])
 
76
            elif filename != current[0]:
 
77
                filename = current[0]
 
78
                outputs.append("")
 
79
                outputs.append(filename + ":")
 
80
                outputs.append("    " + current[1] + ": " + current[2])
 
81
 
 
82
        return "\n".join(outputs)
 
83
 
 
84
    def __find_files(self):
 
85
        """Find all Python files under the current tree."""
 
86
        pyfiles = []
 
87
        for root, dirs, files in os.walk(os.getcwd(), topdown=False):
 
88
            for file in files:
 
89
                if file.endswith(".py"):
 
90
                    pyfiles.append("'" + os.path.join(root, file) + "'")
 
91
        pyfiles.sort()
 
92
        return ",".join(pyfiles)