#!/usr/bin/env python3
# flake8: noqa

"""Build the docs with https://www.sphinx-doc.org."""

import contextlib
import os
import pathlib
import sys

from sphinx.cmd import build

SOURCE = pathlib.Path('docs')

TARGET = pathlib.Path('_build')

RESULT = SOURCE / TARGET / 'index.html'

ARGS = ['.', str(TARGET)]


@contextlib.contextmanager
def chdir(path):
    cwd_before = os.getcwd()
    os.chdir(path)
    try:
        yield path
    finally:
        os.chdir(cwd_before)


args = sys.argv[1:]
if not args:
    args += ARGS

print(f'os.chdir({SOURCE})')
with chdir(SOURCE):
    print(f'sphinx.cmd.build.main({args})')
    result = build.main(args)

print('', RESULT, sep='\n')

try:
    assert RESULT.stat().st_size, f'should be non-empty: {RESULT}'
finally:
    sys.exit(result)

@@ -281,16 +281,36 @@
 l'%0A%0A
-ARGS = %5B
+DEFAULT_ARGS = %5B'-n', '-v', 
 '.',
@@ -454,24 +454,64 @@
    finally:%0A
+        print(f'os.chdir(%7Bcwd_before%7D')%0A
         os.c
@@ -575,11 +575,18 @@
 rgs 
-+
 = 
+DEFAULT_
 ARGS

increase docs build verbosity