~didrocks/ubuntuone-client/dont-suffer-zg-crash

« back to all changes in this revision

Viewing changes to contrib/pylint-wrapper

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-01-25 16:42:52 UTC
  • mto: This revision was merged to the branch mainline in revision 64.
  • Revision ID: james.westby@ubuntu.com-20110125164252-rl1pybasx1nsqgoy
Tags: upstream-1.5.3
ImportĀ upstreamĀ versionĀ 1.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
 
# pylint-wrapper: Script to wrap pylint output
4
 
#
5
 
# Author: Rodney Dawes <rodney.dawes@canonical.com>
6
 
#
7
 
# Copyright 2009 Canonical Ltd.
8
 
#
9
 
# This program is free software: you can redistribute it and/or modify it 
10
 
# under the terms of the GNU General Public License version 3, as published 
11
 
# by the Free Software Foundation.
12
 
#
13
 
# This program is distributed in the hope that it will be useful, but 
14
 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
15
 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
16
 
# PURPOSE.  See the GNU General Public License for more details.
17
 
#
18
 
# You should have received a copy of the GNU General Public License along 
19
 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
"""Wrapper script for pylint command"""
21
 
 
22
 
import ConfigParser
23
 
import os
24
 
import subprocess
25
 
 
26
 
try:
27
 
    SRCDIR = os.environ['SRCDIR']
28
 
except KeyError:
29
 
    SRCDIR = os.getcwd()
30
 
 
31
 
def _read_pylintrc_ignored():
32
 
    """Get the ignored files list from pylintrc"""
33
 
    config = ConfigParser.ConfigParser()
34
 
    config.read([os.path.join(SRCDIR, 'pylintrc')])
35
 
 
36
 
    try:
37
 
        return config.get("MASTER", "ignore").split(",")
38
 
    except ConfigParser.NoOptionError:
39
 
        return None
40
 
 
41
 
def _group_lines_by_file(data):
42
 
    """Format file:line:message output as lines grouped by file."""
43
 
    failed = False
44
 
    outputs = []
45
 
    filename = ""
46
 
    for line in data.splitlines():
47
 
        current = line.split(":", 3)
48
 
        if line.startswith("    "):
49
 
            outputs.append("    " + current[0] + "")
50
 
        elif line.startswith("build/") or len(current) < 3:
51
 
            pass
52
 
        elif filename == current[0]:
53
 
            # pylint warning W0511 is a custom note
54
 
            if not "[W0511]" in current[2]:
55
 
                failed = True
56
 
            outputs.append("    " + current[1] + ": " + current[2])
57
 
        elif filename != current[0]:
58
 
            filename = current[0]
59
 
            outputs.append("")
60
 
            outputs.append(filename + ":")
61
 
            # pylint warning W0511 is a custom note
62
 
            if not "[W0511]" in current[2]:
63
 
                failed = True
64
 
            outputs.append("    " + current[1] + ": " + current[2])
65
 
 
66
 
    return (failed, "\n".join(outputs))
67
 
 
68
 
def _find_files():
69
 
    """Find all Python files under the current tree."""
70
 
    pyfiles = []
71
 
    # pylint: disable-msg=W0612
72
 
    for root, dirs, files in os.walk(SRCDIR, topdown=False):
73
 
        for file in files:
74
 
            path = "%s/" % root
75
 
            if file.endswith(".py") or path.endswith("bin/"):
76
 
                pyfiles.append(os.path.join(root, file))
77
 
 
78
 
    pyfiles.sort()
79
 
    return pyfiles
80
 
 
81
 
 
82
 
failed = False
83
 
 
84
 
ignored = _read_pylintrc_ignored()
85
 
 
86
 
# So that we can match the path correctly
87
 
moreignores = [os.path.join(SRCDIR, item) for item in ignored]
88
 
ignored.extend(moreignores)
89
 
if os.environ.get('USE_PYFLAKES'):
90
 
    pylint_args = ["pyflakes"]
91
 
else:
92
 
    pylint_args = ["pylint",
93
 
                   "--output-format=parseable",
94
 
                   "--include-ids=yes",
95
 
                   "--rcfile=" + os.path.join(SRCDIR, "pylintrc"),]
96
 
    
97
 
 
98
 
for path in _find_files():
99
 
    if path not in ignored and not path.startswith(os.path.join(SRCDIR,
100
 
                                                                "_build")):
101
 
        pylint_args.append(path)
102
 
 
103
 
p = subprocess.Popen(pylint_args,
104
 
                     bufsize=4096, stdout=subprocess.PIPE)
105
 
notices = p.stdout
106
 
 
107
 
output = "".join(notices.readlines())
108
 
if output != "":
109
 
    print "== Python Lint Notices =="
110
 
    (failed, grouped) = _group_lines_by_file(output)
111
 
    print grouped
112
 
    print ""
113
 
 
114
 
returncode = p.wait()
115
 
if returncode != 0:
116
 
    exit(returncode)
117
 
 
118
 
if failed:
119
 
    exit(1)