|
| 1 | +import sphinx_autobuild |
| 2 | +import os |
| 3 | +import sys |
| 4 | +from contextlib import contextmanager |
| 5 | +import base64 |
| 6 | +from livereload import Server |
| 7 | +import http.server |
| 8 | +import http.server |
| 9 | +import socketserver |
| 10 | +import yaml |
| 11 | + |
| 12 | + |
| 13 | +class AuthHandler(http.server.SimpleHTTPRequestHandler): |
| 14 | + """ |
| 15 | + Authentication handler used to support HTTP authentication |
| 16 | + """ |
| 17 | + def do_HEAD(self): |
| 18 | + self.send_response(200) |
| 19 | + self.send_header('Content-type', 'text/html') |
| 20 | + self.end_headers() |
| 21 | + |
| 22 | + def do_AUTHHEAD(self): |
| 23 | + self.send_response(401) |
| 24 | + self.send_header('WWW-Authenticate', 'Basic realm=\"Restricted area\"') |
| 25 | + self.send_header('Content-type', 'text/html') |
| 26 | + self.end_headers() |
| 27 | + |
| 28 | + def do_GET(self): |
| 29 | + global key |
| 30 | + if self.headers.get('Authorization') is None: |
| 31 | + self.do_AUTHHEAD() |
| 32 | + self.wfile.write('Credentials required.'.encode('utf-8')) |
| 33 | + pass |
| 34 | + elif self.headers.get('Authorization') == 'Basic ' + key.decode('utf-8'): |
| 35 | + http.server.SimpleHTTPRequestHandler.do_GET(self) |
| 36 | + pass |
| 37 | + else: |
| 38 | + self.do_AUTHHEAD() |
| 39 | + self.wfile.write('Credentials required.'.encode('utf-8')) |
| 40 | + pass |
| 41 | + |
| 42 | +''' |
| 43 | +This function is used to simulate the manipulation of the stack (like pushd and popd in BASH) |
| 44 | +and change the folder with the usage of the context manager |
| 45 | +''' |
| 46 | +@contextmanager |
| 47 | +def pushd(new_dir): |
| 48 | + previous_dir = os.getcwd() |
| 49 | + os.chdir(new_dir) |
| 50 | + yield |
| 51 | + os.chdir(previous_dir) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == '__main__': |
| 55 | + |
| 56 | + key = '' |
| 57 | + config_file = '.sphinx-server.yml' |
| 58 | + install_folder = '/opt/sphinx-server/' |
| 59 | + build_folder = os.path.realpath('_build/html') |
| 60 | + source_folder = os.path.realpath('.') |
| 61 | + configuration = None |
| 62 | + |
| 63 | + with open(install_folder + config_file, 'r') as config_stream: |
| 64 | + configuration = yaml.safe_load(config_stream) |
| 65 | + |
| 66 | + if os.path.isfile(source_folder + '/' + config_file): |
| 67 | + with open(source_folder + '/' + config_file, "r") as custom_stream: |
| 68 | + configuration.update(yaml.safe_load(custom_stream)) |
| 69 | + |
| 70 | + if not os.path.exists(build_folder): |
| 71 | + os.makedirs(build_folder) |
| 72 | + |
| 73 | + if configuration.get('autobuild'): |
| 74 | + |
| 75 | + ignored_files = [] |
| 76 | + for path in configuration.get('ignore'): |
| 77 | + ignored_files.append(os.path.realpath(path)) |
| 78 | + |
| 79 | + builder = sphinx_autobuild.SphinxBuilder( |
| 80 | + outdir=build_folder, |
| 81 | + args=['-b', 'html', source_folder, build_folder]+sys.argv[1:], |
| 82 | + ignored=ignored_files |
| 83 | + ) |
| 84 | + |
| 85 | + server = Server(watcher=sphinx_autobuild.LivereloadWatchdogWatcher()) |
| 86 | + server.watch(source_folder, builder) |
| 87 | + server.watch(build_folder) |
| 88 | + |
| 89 | + builder.build() |
| 90 | + |
| 91 | + server.serve(port=8000, host='0.0.0.0', root=build_folder) |
| 92 | + else: |
| 93 | + # Building once when server starts |
| 94 | + builder = sphinx_autobuild.SphinxBuilder(outdir=build_folder, args=['-b', 'html', source_folder, build_folder]+sys.argv[1:]) |
| 95 | + builder.build() |
| 96 | + |
| 97 | + sys.argv = ['nouser', '8000'] |
| 98 | + |
| 99 | + if configuration.get('credentials')['username'] is not None: |
| 100 | + auth = configuration.get('credentials')['username'] + ':' + configuration.get('credentials')['password'] |
| 101 | + key = base64.b64encode(auth.encode('utf-8')) |
| 102 | + |
| 103 | + with pushd(build_folder): |
| 104 | + http.server.test(AuthHandler, http.server.HTTPServer) |
| 105 | + else: |
| 106 | + with pushd(build_folder): |
| 107 | + Handler = http.server.SimpleHTTPRequestHandler |
| 108 | + httpd = socketserver.TCPServer(('', 8000), Handler) |
| 109 | + httpd.serve_forever() |
0 commit comments