Package pyrobase :: Package paver :: Module quality
[hide private]
[frames] | no frames]

Source Code for Module pyrobase.paver.quality

  1  # -*- coding: utf-8 -*- 
  2  # pylint: disable= 
  3  """ Paver Code Quality Tasks. 
  4   
  5      Copyright (c) 2011 The PyroScope Project <pyroscope.project@gmail.com> 
  6  """ 
  7  # This program is free software; you can redistribute it and/or modify 
  8  # it under the terms of the GNU General Public License as published by 
  9  # the Free Software Foundation; either version 2 of the License, or 
 10  # (at your option) any later version. 
 11  # 
 12  # This program is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 15  # GNU General Public License for more details. 
 16  # 
 17  # You should have received a copy of the GNU General Public License along 
 18  # with this program; if not, write to the Free Software Foundation, Inc., 
 19  # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 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  ]) 
36 @support.task_requires("pylint>=1.4") 37 -def lint():
38 "report pylint results" 39 from pylint import lint as linter # pylint: disable=W0404 40 41 # report according to file extension 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() # pylint: disable=expression-not-assigned 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 # usage error (internal error in this code) 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