Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp296797pxx; Thu, 29 Oct 2020 02:56:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwodY8ENBITx18x7TYTu9ZTO2COuYDj2SYUfADFGFrMpAnDBsYrc3YmYFbLEze+Bthio3oB X-Received: by 2002:a17:906:c193:: with SMTP id g19mr3292363ejz.393.1603965364364; Thu, 29 Oct 2020 02:56:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603965364; cv=none; d=google.com; s=arc-20160816; b=VOEIPxckSyrjJoLiDNv1Fz62PXHvj/MNx/HZpx7EfekQaXuPAp7xf/NUHuoOJ5MBNK GYUC0Fyb1RyvfPy8SuCNnklGKGuJp40UBAo2VaQBrmhnXeM4astkyY3mfAQ3gRfsdcwH ZHqmYr5OGFfOElfJSYA8ATgzPI5962wn1gUXLSGDXWq7shjdnUex5/f0vtrj14ixmBjL JfrXHOB6L6zFi7PaDgKjKcYO0EqQN9eWXvx6FChQeLn+D1zBDlpwjv5wbJNmPqrbQcWR wNug6Okaamc6ca6cXgijXBiSZOZtYzZMY0FtMYz78SuB/NELM+9O4nHhqBXFb9w4BlKu MZVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Oo8sZEsAupih5oGInwCSLHh02RguvPMKfKDVkOXiav0=; b=L9Ucq6QpYuamq0+cCAZIF8nv5oBY5cwqrBtl1mBFm/0K/trYbBlWrslDUzgBmhAaWL hSLigdtiMmYmlf4FuCMWRZfWPWHL7j+TWcPi/MmnB/H4/M5/7hlVgWT2MtiNhOabPw04 2kTzztCCv9ootjuC5oXu9pbuQ9q8a2P+VdvOmrDND3pTSfabtK9v0Ao3EfBPMHKmqwL0 yqETB+ubDAGZkl2Ti+sb5mYrT5Ul0QDIHXJ8H54sPQbNd5tiML16/n2YxNKoL7bAlpOR uJR7gBv3QU0NfoA3I4kOnQVOz8ORfFCm47bPGHXmWSvhottuHfnO715xr4pCUAEPZFyX +QsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=yXmPCZDm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b6si1805463ejb.204.2020.10.29.02.55.42; Thu, 29 Oct 2020 02:56:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=yXmPCZDm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727375AbgJ1WSK (ORCPT + 99 others); Wed, 28 Oct 2020 18:18:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:60502 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731711AbgJ1WRm (ORCPT ); Wed, 28 Oct 2020 18:17:42 -0400 Received: from mail.kernel.org (ip5f5ad5b2.dynamic.kabel-deutschland.de [95.90.213.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8C8692476B; Wed, 28 Oct 2020 14:23:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603895014; bh=qhW1GvuIVCQ5vbQL0lfUWFV1EZgXyvxR2SiRM2TYiNo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yXmPCZDmwjT/81D0RBjxomLFFAINyAa/wbuvESwY2qhuer2e6xovStpgEfX5Lporl FVFUWEU0nzAjRqcnMI7lQuSTGKiL46DXYpqTQOuuYcGiGGguT1ysLThCK4oJWA8ndE kunekbkm/IWnEGEYdKttxe+rnwssT81PwJBBcHak= Received: from mchehab by mail.kernel.org with local (Exim 4.94) (envelope-from ) id 1kXmMO-003hlG-Iv; Wed, 28 Oct 2020 15:23:32 +0100 From: Mauro Carvalho Chehab To: Linux Doc Mailing List , Greg Kroah-Hartman Cc: Mauro Carvalho Chehab , "Jonathan Corbet" , "Mauro Carvalho Chehab" , linux-kernel@vger.kernel.org Subject: [PATCH 12/33] docs: kernel_abi.py: add a script to parse ABI documentation Date: Wed, 28 Oct 2020 15:23:10 +0100 Message-Id: X-Mailer: git-send-email 2.26.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: Mauro Carvalho Chehab Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mauro Carvalho Chehab The ABI documentation is special: it is not plain text files, but, instead, files with an strict format, as specified by Documentation/ABI/README. Add a parser for it. Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Mauro Carvalho Chehab --- Documentation/sphinx/kernel_abi.py | 157 +++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 Documentation/sphinx/kernel_abi.py diff --git a/Documentation/sphinx/kernel_abi.py b/Documentation/sphinx/kernel_abi.py new file mode 100644 index 000000000000..f8e7a02066c2 --- /dev/null +++ b/Documentation/sphinx/kernel_abi.py @@ -0,0 +1,157 @@ +# -*- coding: utf-8; mode: python -*- +# SPDX-License-Identifier: GPL-2.0 +u""" + kernel-abi + ~~~~~~~~~~ + + Implementation of the ``kernel-abi`` reST-directive. + + :copyright: Copyright (C) 2016 Markus Heiser + :copyright: Copyright (C) 2016-2020 Mauro Carvalho Chehab + :maintained-by: Mauro Carvalho Chehab + :license: GPL Version 2, June 1991 see Linux/COPYING for details. + + The ``kernel-abi`` (:py:class:`KernelCmd`) directive calls the + scripts/get_abi.pl script to parse the Kernel ABI files. + + Overview of directive's argument and options. + + .. code-block:: rst + + .. kernel-abi:: + :debug: + + The argument ```` 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 sys +import os +from os import path +import subprocess + +from sphinx.ext.autodoc import AutodocReporter + +from docutils import nodes +from docutils.parsers.rst import Directive, directives +from docutils.statemachine import ViewList +from docutils.utils.error_reporting import ErrorString + + +__version__ = '1.0' + +# We can't assume that six is installed +PY3 = sys.version_info[0] == 3 +PY2 = sys.version_info[0] == 2 +if PY3: + # pylint: disable=C0103, W0622 + unicode = str + basestring = str + +def setup(app): + + app.add_directive("kernel-abi", KernelCmd) + return dict( + version = __version__ + , parallel_read_safe = True + , parallel_write_safe = True + ) + +class KernelCmd(Directive): + + u"""KernelABI (``kernel-abi``) directive""" + + required_arguments = 1 + optional_arguments = 0 + 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-abi 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_abi.pl rest --dir " + cmd += self.arguments[0] + + 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 + , universal_newlines = True + , **kwargs + ) + out, err = proc.communicate() + if err: + self.warn(err) + 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 unicode(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 + 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.26.2