Commit e84884ae authored by Michael Murtaugh's avatar Michael Murtaugh

really basic directory listing

parent 5d1b0873
......@@ -12,3 +12,7 @@ Install
pip install aiohttp
python setup.py install
To do
--------------------------
* (websockets-based) view to monitor output of make command
#!/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 = "<h1>{}</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(
'<li><a href="{url}">{name}</a></li>'.format(url=file_url,
name=file_name)
)
ul = "<ul>\n{}\n</ul>".format('\n'.join(index_list))
body = "<body>\n{}\n{}\n</body>".format(h1, ul)
head_str = "<head>\n<title>{}</title>\n</head>".format(index_of)
html = "<html>\n{}\n{}\n</html>".format(head_str, body)
return html
def main ():
if sys.platform == 'win32':
# Folowing: https://docs.python.org/3/library/asyncio-subprocess.html
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment