Skip to content

Commit

Permalink
Add review bot tool for doc8
Browse files Browse the repository at this point in the history
doc8 is a style checker for Sphinx (or other) RST documentation. doc8 is
like flake8 for reStructuredText. See https://pypi.python.org/pypi/doc8
for more information.

Testing Done:
- Uploaded a diff an .rst file as review request as saw that it added some
  "errors" of this reStructuredText file.
- Uploaded a "clean" .rst file and saw that it did no add any errors.

Reviewed at https://reviews.reviewboard.org/r/9489/
  • Loading branch information
misery authored and davidt committed Nov 27, 2018
1 parent 5a360cf commit ff05573
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
88 changes: 88 additions & 0 deletions bot/reviewbot/tools/doc8.py
@@ -0,0 +1,88 @@
"""Review Bot tool to run doc8."""

from __future__ import unicode_literals

import logging

from reviewbot.tools import Tool
from reviewbot.utils.process import execute, is_exe_in_path


class Doc8Tool(Tool):
"""Review Bot tool to run doc8."""

name = 'doc8'
version = '1.0'
description = 'Checks reStructuredText for style.'
timeout = 30
options = [
{
'name': 'max_line_length',
'field_type': 'django.forms.IntegerField',
'default': 79,
'field_options': {
'label': 'Maximum Line Length',
'help_text': 'The maximum line length to allow.',
'required': True,
},
},
{
'name': 'encoding',
'field_type': 'django.forms.CharField',
'default': 'utf-8',
'field_options': {
'label': 'Encoding',
'help_text': 'Encoding used for rst files.',
'required': True,
},
},
]

def check_dependencies(self):
"""Verify that the tool's dependencies are installed.
Returns:
bool:
True if all dependencies for the tool are satisfied. If this
returns False, the worker will not be listed for this Tool's queue,
and a warning will be logged.
"""
return is_exe_in_path('doc8')

def handle_file(self, f, settings):
"""Perform a review of a single file.
Args:
f (reviewbot.processing.review.File):
The file to process.
settings (dict):
Tool-specific settings.
"""
if not f.dest_file.lower().endswith('.rst'):
# Ignore the file.
return

path = f.get_patched_file_path()

if not path:
return

output = execute(
[
'doc8', '-q',
'--max-line-length=%s' % settings['max_line_length'],
'--file-encoding=%s' % settings['encoding'],
path,
],
split_lines=True,
ignore_errors=True)

for line in output:
try:
# Strip off the filename, since it might have colons in it.
line = line[len(path) + 1:]
line_num, message = line.split(':', 1)
f.comment(message.strip(), int(line_num))
except Exception:
logging.error('Cannot parse line with doc8: %s', line)
2 changes: 2 additions & 0 deletions bot/setup.py
Expand Up @@ -30,6 +30,7 @@
'clang = reviewbot.tools.clang:ClangTool',
'cppcheck = reviewbot.tools.cppcheck:CPPCheckTool',
'cpplint = reviewbot.tools.cpplint:CPPLintTool',
'doc8 = reviewbot.tools.doc8:Doc8Tool',
'flake8 = reviewbot.tools.flake8:Flake8Tool',
'jshint = reviewbot.tools.jshint:JSHintTool',
'pmd = reviewbot.tools.pmd:PMDTool',
Expand All @@ -47,6 +48,7 @@
'all': [
'buildbot>=0.8.7',
'cpplint>=0.0.3',
'doc8>=0.8.0',
'flake8>=3.3.0',
'pycodestyle',
'pydocstyle',
Expand Down
41 changes: 41 additions & 0 deletions docs/reviewbot/tools/doc8.rst
@@ -0,0 +1,41 @@
.. _tool-doc8:

====
doc8
====

DOC8_ is a style checker for Sphinx (or other) reStructuredText
documentation. Also it has basic support for plain text.
:command:`doc8` uses RST_LINT_ as a backend for the verification.

.. _DOC8: https://pypi.python.org/pypi/doc8
.. _RST_LINT: https://pypi.python.org/pypi/restructuredtext_lint


Checks
======

doc8 verifies the following styles in ``.rst`` files.

* Invalid rst format - D000
* Lines should not be longer than 79 characters - D001

* RST exception: line with no whitespace except in the beginning
* RST exception: lines with http or https urls
* RST exception: literal blocks
* RST exception: rst target directives

* No trailing whitespace - D002
* No tabulation for indentation - D003
* No carriage returns (use unix newlines) - D004
* No newline at end of file - D005


Installation
============

:command:`pydocstyle` can be installed on most systems by running::

$ pip install doc8

It may also be available in your system's package manager.
1 change: 1 addition & 0 deletions docs/reviewbot/tools/index.rst
Expand Up @@ -12,6 +12,7 @@ Review Bot Tools
clang
cppcheck
cpplint
doc8
flake8
jshint
pmd
Expand Down

0 comments on commit ff05573

Please sign in to comment.