Sometimes, it is desired to run Sphinx from a virtual environment. Add a command line parameter to automatically build Sphinx from such environment. Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@xxxxxxxxxx> --- tools/docs/sphinx-build-wrapper | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/tools/docs/sphinx-build-wrapper b/tools/docs/sphinx-build-wrapper index e9c522794fbe..9c5df50fb99d 100755 --- a/tools/docs/sphinx-build-wrapper +++ b/tools/docs/sphinx-build-wrapper @@ -63,6 +63,7 @@ from jobserver import JobserverExec # pylint: disable=C0413,C0411,E0401 # # Some constants # +VENV_DEFAULT = "sphinx_latest" MIN_PYTHON_VERSION = PythonVersion("3.7").version PAPER = ["", "a4", "letter"] @@ -114,8 +115,9 @@ class SphinxBuilder: return path - def __init__(self, verbose=False, n_jobs=None): + def __init__(self, venv=None, verbose=False, n_jobs=None): """Initialize internal variables""" + self.venv = venv self.verbose = None # @@ -192,6 +194,21 @@ class SphinxBuilder: self.env = os.environ.copy() + # + # If venv command line argument is specified, run Sphinx from venv + # + if venv: + bin_dir = os.path.join(venv, "bin") + if not os.path.isfile(os.path.join(bin_dir, "activate")): + sys.exit(f"Venv {venv} not found.") + + # "activate" virtual env + self.env["PATH"] = bin_dir + ":" + self.env["PATH"] + self.env["VIRTUAL_ENV"] = venv + if "PYTHONHOME" in self.env: + del self.env["PYTHONHOME"] + print(f"Setting venv to {venv}") + def run_sphinx(self, sphinx_build, build_args, *args, **pwargs): """ Executes sphinx-build using current python3 command and setting @@ -206,7 +223,10 @@ class SphinxBuilder: cmd = [] - cmd.append(sys.executable) + if self.venv: + cmd.append("python") + else: + cmd.append(sys.executable) cmd.append(sphinx_build) @@ -528,11 +548,16 @@ def main(): parser.add_argument('-j', '--jobs', type=jobs_type, help="Sets number of jobs to use with sphinx-build") + parser.add_argument("-V", "--venv", nargs='?', const=f'{VENV_DEFAULT}', + default=None, + help=f'If used, run Sphinx from a venv dir (default dir: {VENV_DEFAULT})') + args = parser.parse_args() PythonVersion.check_python(MIN_PYTHON_VERSION) - builder = SphinxBuilder(verbose=args.verbose, n_jobs=args.jobs) + builder = SphinxBuilder(venv=args.venv, verbose=args.verbose, + n_jobs=args.jobs) builder.build(args.target, sphinxdirs=args.sphinxdirs, conf=args.conf, theme=args.theme, css=args.css, paper=args.paper) -- 2.51.0