All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
To: Linux Doc Mailing List <linux-doc@vger.kernel.org>
Cc: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
	"Jonathan Corbet" <corbet@lwn.net>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-kernel@vger.kernel.org
Subject: [PATCH 4/6] sphinx: kernel_feat.py: add a script to parse feature files
Date: Mon, 30 Nov 2020 16:36:33 +0100	[thread overview]
Message-ID: <bb2e51e5aa883e2583a4a6280f1c1b391bd8ef4c.1606748711.git.mchehab+huawei@kernel.org> (raw)
In-Reply-To: <cover.1606748711.git.mchehab+huawei@kernel.org>

The feature files have a special well-defined format. Add
a script that parses them, allowing to search for a feature
and/or by an architecture and to produce ReST-compatible
outputs.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 Documentation/sphinx/kernel_feat.py | 169 ++++++++++++++++++++++++++++
 1 file changed, 169 insertions(+)
 create mode 100644 Documentation/sphinx/kernel_feat.py

diff --git a/Documentation/sphinx/kernel_feat.py b/Documentation/sphinx/kernel_feat.py
new file mode 100644
index 000000000000..2fee04f1dedd
--- /dev/null
+++ b/Documentation/sphinx/kernel_feat.py
@@ -0,0 +1,169 @@
+# coding=utf-8
+# SPDX-License-Identifier: GPL-2.0
+#
+u"""
+    kernel-feat
+    ~~~~~~~~~~~
+
+    Implementation of the ``kernel-feat`` reST-directive.
+
+    :copyright:  Copyright (C) 2016  Markus Heiser
+    :copyright:  Copyright (C) 2016-2019  Mauro Carvalho Chehab
+    :maintained-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
+    :license:    GPL Version 2, June 1991 see Linux/COPYING for details.
+
+    The ``kernel-feat`` (:py:class:`KernelFeat`) directive calls the
+    scripts/get_feat.pl script to parse the Kernel ABI files.
+
+    Overview of directive's argument and options.
+
+    .. code-block:: rst
+
+        .. kernel-feat:: <ABI directory location>
+            :debug:
+
+    The argument ``<ABI directory location>`` is required. It contains the
+    location of the ABI files to be parsed.
+
+    ``debug``
+      Inserts a code-block with the *raw* reST. Sometimes it is helpful to see
+      what reST is generated.
+
+"""
+
+import codecs
+import os
+import subprocess
+import sys
+
+from os import path
+
+from docutils import nodes, statemachine
+from docutils.statemachine import ViewList
+from docutils.parsers.rst import directives, Directive
+from docutils.utils.error_reporting import ErrorString
+
+#
+# AutodocReporter is only good up to Sphinx 1.7
+#
+import sphinx
+
+Use_SSI = sphinx.__version__[:3] >= '1.7'
+if Use_SSI:
+    from sphinx.util.docutils import switch_source_input
+else:
+    from sphinx.ext.autodoc import AutodocReporter
+
+__version__  = '1.0'
+
+def setup(app):
+
+    app.add_directive("kernel-feat", KernelFeat)
+    return dict(
+        version = __version__
+        , parallel_read_safe = True
+        , parallel_write_safe = True
+    )
+
+class KernelFeat(Directive):
+
+    u"""KernelFeat (``kernel-feat``) directive"""
+
+    required_arguments = 1
+    optional_arguments = 2
+    has_content = False
+    final_argument_whitespace = True
+
+    option_spec = {
+        "debug"     : directives.flag
+    }
+
+    def warn(self, message, **replace):
+        replace["fname"]   = self.state.document.current_source
+        replace["line_no"] = replace.get("line_no", self.lineno)
+        message = ("%(fname)s:%(line_no)s: [kernel-feat WARN] : " + message) % replace
+        self.state.document.settings.env.app.warn(message, prefix="")
+
+    def run(self):
+
+        doc = self.state.document
+        if not doc.settings.file_insertion_enabled:
+            raise self.warning("docutils: file insertion disabled")
+
+        env = doc.settings.env
+        cwd = path.dirname(doc.current_source)
+        cmd = "get_feat.pl rest --dir "
+        cmd += self.arguments[0]
+
+        if len(self.arguments) > 1:
+            cmd += " --arch " + self.arguments[1]
+
+        srctree = path.abspath(os.environ["srctree"])
+
+        fname = cmd
+
+        # extend PATH with $(srctree)/scripts
+        path_env = os.pathsep.join([
+            srctree + os.sep + "scripts",
+            os.environ["PATH"]
+        ])
+        shell_env = os.environ.copy()
+        shell_env["PATH"]    = path_env
+        shell_env["srctree"] = srctree
+
+        lines = self.runCmd(cmd, shell=True, cwd=cwd, env=shell_env)
+        nodeList = self.nestedParse(lines, fname)
+        return nodeList
+
+    def runCmd(self, cmd, **kwargs):
+        u"""Run command ``cmd`` and return it's stdout as unicode."""
+
+        try:
+            proc = subprocess.Popen(
+                cmd
+                , stdout = subprocess.PIPE
+                , stderr = subprocess.PIPE
+                , **kwargs
+            )
+            out, err = proc.communicate()
+
+            out, err = codecs.decode(out, 'utf-8'), codecs.decode(err, 'utf-8')
+
+            if proc.returncode != 0:
+                raise self.severe(
+                    u"command '%s' failed with return code %d"
+                    % (cmd, proc.returncode)
+                )
+        except OSError as exc:
+            raise self.severe(u"problems with '%s' directive: %s."
+                              % (self.name, ErrorString(exc)))
+        return out
+
+    def nestedParse(self, lines, fname):
+        content = ViewList()
+        node    = nodes.section()
+
+        if "debug" in self.options:
+            code_block = "\n\n.. code-block:: rst\n    :linenos:\n"
+            for l in lines.split("\n"):
+                code_block += "\n    " + l
+            lines = code_block + "\n\n"
+
+        for c, l in enumerate(lines.split("\n")):
+            content.append(l, fname, c)
+
+        buf  = self.state.memo.title_styles, self.state.memo.section_level, self.state.memo.reporter
+
+        if Use_SSI:
+            with switch_source_input(self.state, content):
+                self.state.nested_parse(content, 0, node, match_titles=1)
+        else:
+            self.state.memo.title_styles  = []
+            self.state.memo.section_level = 0
+            self.state.memo.reporter      = AutodocReporter(content, self.state.memo.reporter)
+            try:
+                self.state.nested_parse(content, 0, node, match_titles=1)
+            finally:
+                self.state.memo.title_styles, self.state.memo.section_level, self.state.memo.reporter = buf
+
+        return node.children
-- 
2.28.0


  parent reply	other threads:[~2020-11-30 15:38 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-30 15:36 [PATCH 0/6] Add documentation for Documentation/features at the built docs Mauro Carvalho Chehab
2020-11-30 15:36 ` [OpenRISC] " Mauro Carvalho Chehab
2020-11-30 15:36 ` Mauro Carvalho Chehab
2020-11-30 15:36 ` Mauro Carvalho Chehab
2020-11-30 15:36 ` Mauro Carvalho Chehab
2020-11-30 15:36 ` Mauro Carvalho Chehab
2020-11-30 15:36 ` [PATCH 1/6] scripts: get_feat.pl: add a script to handle Documentation/features Mauro Carvalho Chehab
2020-11-30 15:36 ` [PATCH 2/6] scripts: get_feat.pl: improve matrix output Mauro Carvalho Chehab
2020-11-30 15:36 ` [PATCH 3/6] scripts: get_feat.pl: use its implementation for list-arch.sh Mauro Carvalho Chehab
2020-11-30 15:36 ` Mauro Carvalho Chehab [this message]
2020-11-30 15:36 ` [PATCH 5/6] docs: admin-guide: add a features list Mauro Carvalho Chehab
2020-11-30 15:36 ` [PATCH 6/6] docs: archis: add a per-architecture " Mauro Carvalho Chehab
2020-11-30 15:36   ` [OpenRISC] " Mauro Carvalho Chehab
2020-11-30 15:36   ` Mauro Carvalho Chehab
2020-11-30 15:36   ` Mauro Carvalho Chehab
2020-11-30 15:36   ` Mauro Carvalho Chehab
2020-11-30 15:36   ` Mauro Carvalho Chehab
2020-12-03 22:36 ` [PATCH 0/6] Add documentation for Documentation/features at the built docs Jonathan Corbet
2020-12-03 22:36   ` [OpenRISC] " Jonathan Corbet
2020-12-03 22:36   ` Jonathan Corbet
2020-12-03 22:36   ` Jonathan Corbet
2020-12-03 22:36   ` Jonathan Corbet
2020-12-03 22:36   ` Jonathan Corbet
2020-12-04  9:17   ` [PATCH] scripts: get_feat.pl: make complete table more coincise Mauro Carvalho Chehab
2020-12-04  9:26     ` [PATCH v2] " Mauro Carvalho Chehab
2020-12-04  9:35       ` [PATCH v3] " Mauro Carvalho Chehab
2020-12-04  9:52         ` Mauro Carvalho Chehab
2020-12-04 15:32   ` [PATCH v4 0/3] improve get_feat.pl output when all features are displayed Mauro Carvalho Chehab
2020-12-04 15:32     ` [PATCH v4 1/3] scripts: get_feat.pl: make complete table more coincise Mauro Carvalho Chehab
2020-12-04 15:32     ` [PATCH v4 2/3] scripts: get_feat.pl: change the group by order Mauro Carvalho Chehab
2020-12-04 15:32     ` [PATCH v4 3/3] scripts: get_feat.pl: reduce table width for all features output Mauro Carvalho Chehab
2020-12-04 21:48     ` [PATCH v4 0/3] improve get_feat.pl output when all features are displayed Jonathan Corbet
2020-12-05 16:03       ` Mauro Carvalho Chehab
2020-12-07  9:33         ` Mauro Carvalho Chehab
2020-12-07 11:34           ` Mauro Carvalho Chehab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bb2e51e5aa883e2583a4a6280f1c1b391bd8ef4c.1606748711.git.mchehab+huawei@kernel.org \
    --to=mchehab+huawei@kernel.org \
    --cc=corbet@lwn.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.