1
2
3 """ Paver Code Quality Tasks.
4
5 Copyright (c) 2011 The PyroScope Project <pyroscope.project@gmail.com>
6 """
7
8
9
10
11
12
13
14
15
16
17
18
19
20 from __future__ import with_statement
21
22 import os
23 import sys
24 import subprocess
25
26 from paver import easy
27 from pyrobase.paver import support
28
29
30 @easy.task
31 @easy.cmdopts([
32 ('output=', 'o', 'Create report file (.html, .log, or .txt) [stdout]'),
33 ('rcfile=', 'r', 'Configuration file [./pylint.cfg]'),
34 ('msg-only', 'm', 'Only generate messages (no reports)'),
35 ])
38 "report pylint results"
39 from pylint import lint as linter
40
41
42 report_formats = {
43 ".html": "html",
44 ".log": "parseable",
45 ".txt": "text",
46 }
47
48 lint_build_dir = easy.path("build/lint")
49 lint_build_dir.exists() or lint_build_dir.makedirs()
50
51 argv = []
52 rcfile = easy.options.lint.get("rcfile")
53 if not rcfile and easy.path("pylint.cfg").exists():
54 rcfile = "pylint.cfg"
55 if rcfile:
56 argv += ["--rcfile", os.path.abspath(rcfile)]
57 if easy.options.lint.get("msg_only", False):
58 argv += ["-rn"]
59 argv += [
60 "--import-graph", (lint_build_dir / "imports.dot").abspath(),
61 ]
62 argv += support.toplevel_packages()
63
64 sys.stderr.write("Running %s::pylint '%s'\n" % (sys.argv[0], "' '".join(argv)))
65 outfile = easy.options.lint.get("output", None)
66 if outfile:
67 outfile = os.path.abspath(outfile)
68
69 try:
70 with easy.pushd("src" if easy.path("src").exists() else "."):
71 if outfile:
72 argv.extend(["-f", report_formats.get(easy.path(outfile).ext, "text")])
73 sys.stderr.write("Writing output to %r\n" % (str(outfile),))
74 outhandle = open(outfile, "w")
75 try:
76 subprocess.check_call(["pylint"] + argv, stdout=outhandle)
77 finally:
78 outhandle.close()
79 else:
80 subprocess.check_call(["pylint"] + argv, )
81 sys.stderr.write("paver::lint - No problems found.\n")
82 except subprocess.CalledProcessError, exc:
83 if exc.returncode & 32:
84
85 sys.stderr.write("paver::lint - Usage error, bad arguments %r?!\n" % (argv,))
86 sys.exit(exc.returncode)
87 else:
88 bits = {
89 1: "fatal",
90 2: "error",
91 4: "warning",
92 8: "refactor",
93 16: "convention",
94 }
95 sys.stderr.write("paver::lint - Some %s message(s) issued.\n" % (
96 ", ".join([text for bit, text in bits.items() if exc.returncode & bit])
97 ))
98 if exc.returncode & 3:
99 sys.stderr.write("paver::lint - Exiting due to fatal / error message.\n")
100 sys.exit(exc.returncode)
101