#!/usr/bin/env python3 # # A small web server that can serve the `${root}/microzig-deploy` folder for testing the package infrastructure. # # Basically `python -m http.server 8080`, but also hides folders starting with `.data` so the "internals" aren't shown # to the user in the file listing. # from pathlib import Path from http.server import HTTPServer,SimpleHTTPRequestHandler from http import HTTPStatus import sys, os, io, urllib.parse, html SELF_DIR = Path(__file__).parent assert SELF_DIR.is_dir() ROOT_DIR = SELF_DIR.parent assert SELF_DIR.is_dir() DEPLOYMENT_DIR = ROOT_DIR / "microzig-deploy" if not DEPLOYMENT_DIR.is_dir(): print(f"{DEPLOYMENT_DIR} isn't a directory. Please create a directory first with ./tools/bundle.sh!") exit(1) class Handler(SimpleHTTPRequestHandler): def __init__(self, *args, **kwargs): super().__init__(*args, directory=str(DEPLOYMENT_DIR), **kwargs) def list_directory(self, path): """Helper to produce a directory listing (absent index.html). Return value is either a file object, or None (indicating an error). In either case, the headers are sent, making the interface the same as for send_head(). """ try: list = os.listdir(path) except OSError: self.send_error( HTTPStatus.NOT_FOUND, "No permission to list directory") return None list.sort(key=lambda a: a.lower()) r = [] try: displaypath = urllib.parse.unquote(self.path, errors='surrogatepass') except UnicodeDecodeError: displaypath = urllib.parse.unquote(self.path) displaypath = html.escape(displaypath, quote=False) enc = sys.getfilesystemencoding() title = 'Directory listing for %s' % displaypath r.append('') r.append('\n') r.append('' % enc) r.append('%s\n' % title) r.append('\n

%s

' % title) r.append('
\n\n
\n\n\n') encoded = '\n'.join(r).encode(enc, 'surrogateescape') f = io.BytesIO() f.write(encoded) f.seek(0) self.send_response(HTTPStatus.OK) self.send_header("Content-type", "text/html; charset=%s" % enc) self.send_header("Content-Length", str(len(encoded))) self.end_headers() return f if __name__ == "__main__": httpd = HTTPServer(('', 8080), Handler) httpd.serve_forever()