[PATCH] kernel-doc-rst-lint: add tool to check kernel-doc and rst correctness
Jani Nikula
jani.nikula at intel.com
Tue May 31 17:11:33 CEST 2016
oo
Cc: Jani Nikula <jani.nikula at intel.com>
Simple kernel-doc and reStructuredText lint tool that can be used
independently and as a kernel build CHECK tool to validate kernel-doc
comments.
Independent usage:
$ kernel-doc-rst-lint FILE
Kernel CHECK usage:
$ make CHECK=3Dscripts/kernel-doc-rst-lint C=3D1 # (or C=3D2)
Depends on docutils and the rst-lint package
https://pypi.python.org/pypi/restructuredtext_lint
Signed-off-by: Jani Nikula <jani.nikula at intel.com>
---
scripts/kernel-doc-rst-lint | 106 ++++++++++++++++++++++++++++++++++++++++=
++++
1 file changed, 106 insertions(+)
create mode 100755 scripts/kernel-doc-rst-lint
diff --git a/scripts/kernel-doc-rst-lint b/scripts/kernel-doc-rst-lint
new file mode 100755
index 000000000000..7e0157679f83
--- /dev/null
+++ b/scripts/kernel-doc-rst-lint
@@ -0,0 +1,106 @@
+#!/usr/bin/env python
+# coding=3Dutf-8
+#
+# Copyright =C2=A9 2016 Intel Corporation
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"=
),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS =
OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTH=
ER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEAL=
INGS
+# IN THE SOFTWARE.
+#
+# Authors:
+# Jani Nikula <jani.nikula at intel.com>
+#
+# Simple kernel-doc and reStructuredText lint tool that can be used
+# independently and as a kernel build CHECK tool to validate kernel-doc
+# comments.
+#
+# Independent usage:
+# $ kernel-doc-rst-lint FILE
+#
+# Kernel CHECK usage:
+# $ make CHECK=3Dscripts/kernel-doc-rst-lint C=3D1 # (or C=3D2)
+#
+# Depends on docutils and the rst-lint package
+# https://pypi.python.org/pypi/restructuredtext_lint
+#
+
+import os
+import subprocess
+import sys
+
+from docutils.parsers.rst import directives
+from docutils.parsers.rst import Directive
+from docutils.parsers.rst import roles
+from docutils import nodes, statemachine
+import restructuredtext_lint
+
+class DummyDirective(Directive):
+ required_argument =3D 1
+ optional_arguments =3D 0
+ option_spec =3D { }
+ has_content =3D True
+
+ def run(self):
+ return []
+
+# Fake the Sphinx C Domain directives and roles
+directives.register_directive('c:function', DummyDirective)
+directives.register_directive('c:type', DummyDirective)
+roles.register_generic_role('c:func', nodes.emphasis)
+roles.register_generic_role('c:type', nodes.emphasis)
+
+# We accept but ignore parameters to be compatible with how the kernel bui=
ld
+# invokes CHECK.
+if len(sys.argv) < 2:
+ sys.stderr.write('usage: kernel-doc-rst-lint [IGNORED OPTIONS] FILE\n'=
);
+ sys.exit(1)
+
+infile =3D sys.argv[len(sys.argv) - 1]
+cmd =3D ['scripts/kernel-doc', '-rst', infile]
+
+try:
+ p =3D subprocess.Popen(cmd, stdout=3Dsubprocess.PIPE, stderr=3Dsubproc=
ess.PIPE, universal_newlines=3DTrue)
+ out, err =3D p.communicate()
+
+ # python2 needs conversion to unicode.
+ # python3 with universal_newlines=3DTrue returns strings.
+ if sys.version_info.major < 3:
+ out, err =3D unicode(out, 'utf-8'), unicode(err, 'utf-8')
+
+ # kernel-doc errors
+ sys.stderr.write(err)
+ if p.returncode !=3D 0:
+ sys.exit(p.returncode)
+
+ # restructured text errors
+ lines =3D statemachine.string2lines(out, 8, convert_whitespace=3DTrue)
+ lint_errors =3D restructuredtext_lint.lint(out, infile)
+ for error in lint_errors:
+ # Ignore INFO
+ if error.level <=3D 1:
+ continue
+
+ print(error.source + ': ' + error.type + ': ' + error.full_message)
+ if error.line is not None:
+ print('Context:')
+ print('\t' + lines[error.line - 1])
+ print('\t' + lines[error.line])
+
+except Exception as e:
+ sys.stderr.write(str(e) + '\n')
+ sys.exit(1)
--=20
2.1.4
--=20
Jani Nikula, Intel Open Source Technology Center
More information about the drbd-dev
mailing list