1#! /usr/bin/env python
2# encoding: utf-8
3#
4# written by Sylvain Rouquette, 2011
5
6'''
7Install pep8 module:
8$ easy_install pep8
9	or
10$ pip install pep8
11
12To add the pep8 tool to the waf file:
13$ ./waf-light --tools=compat15,pep8
14	or, if you have waf >= 1.6.2
15$ ./waf update --files=pep8
16
17
18Then add this to your wscript:
19
20[at]extension('.py', 'wscript')
21def run_pep8(self, node):
22	self.create_task('Pep8', node)
23
24'''
25
26import threading
27from waflib import Task, Options
28
29pep8 = __import__('pep8')
30
31
32class Pep8(Task.Task):
33	color = 'PINK'
34	lock = threading.Lock()
35
36	def check_options(self):
37		if pep8.options:
38			return
39		pep8.options = Options.options
40		pep8.options.prog = 'pep8'
41		excl = pep8.options.exclude.split(',')
42		pep8.options.exclude = [s.rstrip('/') for s in excl]
43		if pep8.options.filename:
44			pep8.options.filename = pep8.options.filename.split(',')
45		if pep8.options.select:
46			pep8.options.select = pep8.options.select.split(',')
47		else:
48			pep8.options.select = []
49		if pep8.options.ignore:
50			pep8.options.ignore = pep8.options.ignore.split(',')
51		elif pep8.options.select:
52			# Ignore all checks which are not explicitly selected
53			pep8.options.ignore = ['']
54		elif pep8.options.testsuite or pep8.options.doctest:
55			# For doctest and testsuite, all checks are required
56			pep8.options.ignore = []
57		else:
58			# The default choice: ignore controversial checks
59			pep8.options.ignore = pep8.DEFAULT_IGNORE.split(',')
60		pep8.options.physical_checks = pep8.find_checks('physical_line')
61		pep8.options.logical_checks = pep8.find_checks('logical_line')
62		pep8.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0)
63		pep8.options.messages = {}
64
65	def run(self):
66		with Pep8.lock:
67			self.check_options()
68		pep8.input_file(self.inputs[0].abspath())
69		return 0 if not pep8.get_count() else -1
70
71
72def options(opt):
73	opt.add_option('-q', '--quiet', default=0, action='count',
74				   help="report only file names, or nothing with -qq")
75	opt.add_option('-r', '--repeat', action='store_true',
76				   help="show all occurrences of the same error")
77	opt.add_option('--exclude', metavar='patterns',
78				   default=pep8.DEFAULT_EXCLUDE,
79				   help="exclude files or directories which match these "
80				   "comma separated patterns (default: %s)" %
81				   pep8.DEFAULT_EXCLUDE,
82				   dest='exclude')
83	opt.add_option('--filename', metavar='patterns', default='*.py',
84				   help="when parsing directories, only check filenames "
85				   "matching these comma separated patterns (default: "
86				   "*.py)")
87	opt.add_option('--select', metavar='errors', default='',
88				   help="select errors and warnings (e.g. E,W6)")
89	opt.add_option('--ignore', metavar='errors', default='',
90				   help="skip errors and warnings (e.g. E4,W)")
91	opt.add_option('--show-source', action='store_true',
92				   help="show source code for each error")
93	opt.add_option('--show-pep8', action='store_true',
94				   help="show text of PEP 8 for each error")
95	opt.add_option('--statistics', action='store_true',
96				   help="count errors and warnings")
97	opt.add_option('--count', action='store_true',
98				   help="print total number of errors and warnings "
99				   "to standard error and set exit code to 1 if "
100				   "total is not null")
101	opt.add_option('--benchmark', action='store_true',
102				   help="measure processing speed")
103	opt.add_option('--testsuite', metavar='dir',
104				   help="run regression tests from dir")
105	opt.add_option('--doctest', action='store_true',
106				   help="run doctest on myself")
107