documentation: add autoreload and build for documentation server

This commit is contained in:
Antoine Bartuccio 2019-10-13 03:15:49 +02:00
parent 77dddbc581
commit 1256744f1b
Signed by: klmp200
GPG Key ID: E7245548C53F904B
2 changed files with 34 additions and 8 deletions

View File

@ -25,13 +25,12 @@
import os
import sys
import signal
from http.server import test, CGIHTTPRequestHandler
from django.core.management.base import BaseCommand
# TODO Django 2.2 : implement autoreload following
# https://stackoverflow.com/questions/42907285/django-autoreload-add-watched-file
from django.utils import autoreload
class Command(BaseCommand):
@ -45,15 +44,15 @@ class Command(BaseCommand):
"addrport", nargs="?", help="Optional port number, or ipaddr:port"
)
def handle(self, *args, **kwargs):
os.chdir("doc")
def build_documentation(self):
os.chdir(os.path.join(self.project_dir, "doc"))
err = os.system("make html")
if err != 0:
self.stdout.write("A build error occured, exiting")
sys.exit(err)
self.stdout.write("A build error occured")
os.chdir("_build/html")
def start_server(self, **kwargs):
os.chdir(os.path.join(self.project_dir, "doc", "_build/html"))
addr = self.default_addr
port = self.default_port
if kwargs["addrport"]:
@ -69,3 +68,25 @@ class Command(BaseCommand):
sys.exit(0)
test(HandlerClass=CGIHTTPRequestHandler, port=int(port), bind=addr)
def build_and_start_server(self, **kwargs):
self.build_documentation()
self.start_server(**kwargs)
def handle(self, *args, **kwargs):
self.project_dir = os.getcwd()
signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
try:
if os.environ.get(autoreload.DJANGO_AUTORELOAD_ENV) == "true":
reloader = autoreload.get_reloader()
reloader.watch_dir(os.path.join(self.project_dir, "doc"), "**/*.rst")
autoreload.logger.info(
"Watching for file changes with %s", reloader.__class__.__name__
)
autoreload.start_django(reloader, self.build_and_start_server, **kwargs)
else:
exit_code = autoreload.restart_with_reloader()
sys.exit(exit_code)
except KeyboardInterrupt:
pass

View File

@ -106,10 +106,15 @@ La documentation est automatiquement mise en ligne sur readthedocs à chaque env
Pour l'utiliser en local ou globalement pour la modifier, il existe une commande du site qui génère la documentation et lance un serveur la rendant accessible à l'adresse http://localhost:8080.
Cette commande génère la documentation à chacune de ses modifications, inutile de relancer le serveur à chaque fois.
.. sourcecode:: bash
./manage.py documentation
# Il est possible de spécifier un port et une adresse d'écoute différente
./manage.py documentation adresse:port
Lancer les tests
----------------