diff --git a/README.md b/README.md index 3082a8c624453af5d72cc94941d2cbe5e1ff598a..9401b2729fdf9a29b4be1dc33e0df846f06ee314 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,7 @@ Install pip install aiohttp python setup.py install + +To do +-------------------------- +* (websockets-based) view to monitor output of make command diff --git a/makeserver3/__init__.py b/makeserver3/__init__.py index 5271f0344cde9177d6865c4917417bb966b3e045..2b649e38dc4c4b0711f4f819f823d07e41751e66 100755 --- a/makeserver3/__init__.py +++ b/makeserver3/__init__.py @@ -1,11 +1,8 @@ -#!/usr/bin/env python - from aiohttp import web -# from yarl import URL, unquote import sys, os, asyncio -# from pathlib import Path from asyncio import create_subprocess_exec -from urllib.parse import unquote as urlunquote +from urllib.parse import unquote as urlunquote, quote as urlquote + async def is_makeable (path, cwd=".", makefile="makefile"): # print ("make_question {0} {1}".format(path, docroot)) @@ -31,13 +28,64 @@ async def do_route(request): # return FileResponse(filepath, chunk_size=self._chunk_size) path = urlunquote(request.rel_url.raw_path.lstrip("/")) print ("do_route", path, file=sys.stderr) - if await is_makeable(path): + if path == '': + path = '.' + if path and await is_makeable(path): await make(path) if os.path.exists(path): - return web.FileResponse(path, chunk_size=256*1024) + if os.path.isdir(path): + return web.Response(text=directory_as_html(path, '', ''), content_type="text/html") + else: + return web.FileResponse(path, chunk_size=256*1024) else: return web.Response(text=f"404 on path {path}") +# Following web_urldispatcher.py#line=551 +# try: +# ret = Response(text=self._directory_as_html(filepath), +# content_type="text/html") +# except PermissionError: +# raise HTTPForbidden() + +def directory_as_html(filepath, directory, prefix): + # returns directory's index as html + + # sanity check + assert os.path.isdir(filepath) + + # relative_path_to_dir = filepath.relative_to(self._directory) + relative_path_to_dir = os.path.relpath(filepath, directory) + + index_of = "Index of /{}".format(relative_path_to_dir) + h1 = "

{}

".format(index_of) + + index_list = [] + dir_index = os.listdir(filepath) + for _file in sorted(dir_index): + # show file url as relative to static path + # rel_path = _file.relative_to(self._directory).as_posix() + # rel_path = os.path.relpath(_file, directory) + # file_url = prefix + '/' + rel_path + file_url = '/' + urlquote(os.path.join(filepath, _file)) + # if file is a directory, add '/' to the end of the name + if os.path.isdir(_file): + file_name = "{}/".format(_file) + else: + file_name = _file + + index_list.append( + '
  • {name}
  • '.format(url=file_url, + name=file_name) + ) + ul = "".format('\n'.join(index_list)) + body = "\n{}\n{}\n".format(h1, ul) + + head_str = "\n{}\n".format(index_of) + html = "\n{}\n{}\n".format(head_str, body) + + return html + + def main (): if sys.platform == 'win32': # Folowing: https://docs.python.org/3/library/asyncio-subprocess.html