aputils/docs/build.py

50 lines
1.2 KiB
Python
Executable file

#!/usr/bin/env python3
import sys
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler
from pathlib import Path
from sphinx.cmd.build import main as sphinx
docpath = Path(__file__).resolve().parent
class Server(ThreadingHTTPServer):
def __init__(self, host, port, directory):
ThreadingHTTPServer.__init__(self, (host, port), SimpleHTTPRequestHandler)
self.directory = directory
def finish_request(self, request, client_address):
self.RequestHandlerClass(request, client_address, self, directory=self.directory)
def run(self):
with self as httpd:
host, port = httpd.socket.getsockname()[:2]
print(f'Serving HTTP on {host} port {port} (http://{host}:{port}/)')
try:
httpd.serve_forever()
except KeyboardInterrupt:
print('\nKeyboard interrupt received, exiting.')
return 0
def main():
if len(sys.argv) < 2:
print('Must include output type')
return 1
arg = sys.argv[1]
if arg == 'serve':
sphinx(['-M', 'html', str(docpath), docpath.joinpath('_build')])
s = Server('0.0.0.0', 8080, docpath.joinpath('_build/html'))
return s.run()
return sphinx(['-M', sys.argv[1], str(docpath), docpath.joinpath('_build')])
if __name__ == '__main__':
sys.exit(main())