Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: python@g.o, "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH v3 2/5] distutils-r1.eclass: Add distutils_enable_sphinx helper function
Date: Fri, 22 Nov 2019 06:47:38
Message-Id: 20191122064649.1578-2-mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH v3 1/5] python-utils-r1.eclass: Introduce build_sphins() helper by "Michał Górny"
1 Add a helper function to easily take care of the most common
2 dev-python/sphinx usage for HTML doc building.
3
4 Signed-off-by: Michał Górny <mgorny@g.o>
5 ---
6 eclass/distutils-r1.eclass | 96 ++++++++++++++++++++++++++++++++++++++
7 1 file changed, 96 insertions(+)
8
9 Changed in v3:
10 - move the impl from python_compile_all to sphinx_compile_all, to make
11 it possible to override it easily.
12
13 diff --git a/eclass/distutils-r1.eclass b/eclass/distutils-r1.eclass
14 index 63e77bf014c1..6e75a3a79914 100644
15 --- a/eclass/distutils-r1.eclass
16 +++ b/eclass/distutils-r1.eclass
17 @@ -232,6 +232,102 @@ fi
18 # }
19 # @CODE
20
21 +# @FUNCTION: distutils_enable_sphinx
22 +# @USAGE: <subdir> [--no-autodoc | <plugin-pkgs>...]
23 +# @DESCRIPTION:
24 +# Set up IUSE, BDEPEND, python_check_deps() and python_compile_all() for
25 +# building HTML docs via dev-python/sphinx. python_compile_all() will
26 +# append to HTML_DOCS if docs are enabled.
27 +#
28 +# This helper is meant for the most common case, that is a single Sphinx
29 +# subdirectory with standard layout, building and installing HTML docs
30 +# behind USE=doc. It assumes it's the only consumer of the three
31 +# aforementioned functions. If you need to use a custom implemention,
32 +# you can't use it.
33 +#
34 +# If your package uses additional Sphinx plugins, they should be passed
35 +# (without PYTHON_USEDEP) as <plugin-pkgs>. The function will take care
36 +# of setting appropriate any-of dep and python_check_deps().
37 +#
38 +# If no plugin packages are specified, the eclass will still utilize
39 +# any-r1 API to support autodoc (documenting source code).
40 +# If the package uses neither autodoc nor additional plugins, you should
41 +# pass --no-autodoc to disable this API and simplify the resulting code.
42 +#
43 +# This function must be called in global scope. Take care not to
44 +# overwrite the variables set by it. If you need to extend
45 +# python_compile_all(), you can call the original implementation
46 +# as sphinx_compile_all.
47 +distutils_enable_sphinx() {
48 + debug-print-function ${FUNCNAME} "${@}"
49 + [[ ${#} -ge 1 ]] || die "${FUNCNAME} takes at least one arg: <subdir>"
50 +
51 + _DISTUTILS_SPHINX_SUBDIR=${1}
52 + shift
53 + _DISTUTILS_SPHINX_PLUGINS=( "${@}" )
54 +
55 + local deps autodoc=1 d
56 + for d; do
57 + if [[ ${d} == --no-autodoc ]]; then
58 + autodoc=
59 + else
60 + deps+="
61 + ${d}[\${PYTHON_USEDEP}]"
62 + fi
63 + done
64 +
65 + if [[ ! ${autodoc} && -n ${deps} ]]; then
66 + die "${FUNCNAME}: do not pass --no-autodoc if external plugins are used"
67 + fi
68 + if [[ ${autodoc} ]]; then
69 + deps="$(python_gen_any_dep "
70 + dev-python/sphinx[\${PYTHON_USEDEP}]
71 + ${deps}")"
72 +
73 + python_check_deps() {
74 + use doc || return 0
75 + local p
76 + for p in "${_DISTUTILS_SPHINX_PLUGINS[@]}"; do
77 + has_version "${p}[${PYTHON_USEDEP}]" || return 1
78 + done
79 + }
80 + else
81 + deps="dev-python/sphinx"
82 + fi
83 +
84 + sphinx_compile_all() {
85 + use doc || return
86 +
87 + local confpy=${_DISTUTILS_SPHINX_SUBDIR}/conf.py
88 + [[ -f ${confpy} ]] ||
89 + die "${confpy} not found, distutils_enable_sphinx call wrong"
90 +
91 + if [[ ${_DISTUTILS_SPHINX_PLUGINS[0]} == --no-autodoc ]]; then
92 + if grep -F -q 'sphinx.ext.autodoc' "${confpy}"; then
93 + die "distutils_enable_sphinx: --no-autodoc passed but sphinx.ext.autodoc found in ${confpy}"
94 + fi
95 + else
96 + if ! grep -F -q 'sphinx.ext.autodoc' "${confpy}"; then
97 + die "distutils_enable_sphinx: sphinx.ext.autodoc not found in ${confpy}, pass --no-autodoc"
98 + fi
99 + fi
100 +
101 + build_sphinx "${_DISTUTILS_SPHINX_SUBDIR}"
102 + }
103 + python_compile_all() { sphinx_compile_all; }
104 +
105 + IUSE+=" doc"
106 + if [[ ${EAPI} == [56] ]]; then
107 + DEPEND+=" doc? ( ${deps} )"
108 + else
109 + BDEPEND+=" doc? ( ${deps} )"
110 + fi
111 +
112 + # we need to ensure successful return in case we're called last,
113 + # otherwise Portage may wrongly assume sourcing failed
114 + return 0
115 +}
116 +
117 # @FUNCTION: distutils_enable_tests
118 # @USAGE: <test-runner>
119 # @DESCRIPTION:
120 --
121 2.24.0