pep8.py 3.4 KB

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