2024-07-11 20:27:19 +10:00
|
|
|
#!/usr/bin/env python3.11
|
2018-05-13 21:23:57 +10:00
|
|
|
|
|
|
|
|
|
2021-11-16 11:25:40 +10:00
|
|
|
import asyncio
|
2021-11-18 13:11:14 +10:00
|
|
|
import functools
|
2018-05-13 21:23:57 +10:00
|
|
|
import gzip
|
|
|
|
|
import http.server
|
|
|
|
|
import os
|
|
|
|
|
import pickle
|
2021-11-18 21:35:59 +10:00
|
|
|
import sys
|
|
|
|
|
import urllib.parse
|
2018-05-13 21:23:57 +10:00
|
|
|
|
2021-11-25 23:26:04 +10:00
|
|
|
import fill3
|
2021-11-21 00:33:52 +10:00
|
|
|
import termstr
|
|
|
|
|
|
2018-09-17 23:59:38 +10:00
|
|
|
import eris.tools as tools
|
2018-05-13 21:23:57 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
USAGE = """Usage:
|
2018-09-17 23:59:38 +10:00
|
|
|
eris-webserver <directory>
|
2018-05-13 21:23:57 +10:00
|
|
|
|
|
|
|
|
Example:
|
2018-09-17 23:59:38 +10:00
|
|
|
eris-webserver my_project
|
2018-05-13 21:23:57 +10:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_page(body_html, title):
|
2021-11-21 22:53:12 +10:00
|
|
|
text = f"""
|
2021-11-17 10:05:25 +10:00
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<title>{title}</title>
|
|
|
|
|
<style type="text/css">
|
|
|
|
|
body {{ margin:2; background-color:black; }}
|
|
|
|
|
iframe {{ height:100%;width:100%;border:1px solid white; }}
|
2021-11-18 08:07:39 +10:00
|
|
|
a:focus, a:hover {{ filter:brightness(180%); }}
|
2021-11-17 10:05:25 +10:00
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>{body_html}</body>
|
|
|
|
|
</html>"""
|
2021-11-21 22:53:12 +10:00
|
|
|
return gzip.compress(text.encode("utf-8"))
|
2021-11-17 10:05:25 +10:00
|
|
|
|
|
|
|
|
|
2021-11-18 13:11:14 +10:00
|
|
|
@functools.lru_cache(maxsize=100)
|
|
|
|
|
def make_listing_page(url_path):
|
2021-11-18 21:35:59 +10:00
|
|
|
unquoted_path = urllib.parse.unquote(url_path)
|
2021-11-21 00:33:52 +10:00
|
|
|
path, tool_name = os.path.split(unquoted_path)
|
|
|
|
|
result = index[(path, tool_name)]
|
|
|
|
|
tool = getattr(tools, tool_name)
|
|
|
|
|
tool_name_colored = tools.tool_name_colored(tool, path)
|
2022-05-11 11:11:37 +10:00
|
|
|
header = fill3.appearance_as_html([lscolors.path_colored(path) + " - " + tool_name_colored,
|
2022-10-20 16:26:09 +10:00
|
|
|
termstr.TermStr(" " * 100, is_underlined=True)])
|
2022-01-18 14:37:12 +10:00
|
|
|
body = fill3.appearance_as_html(result.appearance())
|
2021-11-21 00:33:52 +10:00
|
|
|
return make_page(header + body, f"{path} - {tool_name}")
|
2021-11-18 13:11:14 +10:00
|
|
|
|
|
|
|
|
|
2018-05-13 21:23:57 +10:00
|
|
|
class Webserver(http.server.BaseHTTPRequestHandler):
|
|
|
|
|
|
|
|
|
|
def _set_headers(self):
|
|
|
|
|
self.send_response(200)
|
|
|
|
|
self.send_header("Content-type", "text/html")
|
2021-11-21 22:53:12 +10:00
|
|
|
self.send_header("Content-encoding", "gzip")
|
2018-05-13 21:23:57 +10:00
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
|
self._set_headers()
|
|
|
|
|
if self.path == "/":
|
2021-11-17 10:05:25 +10:00
|
|
|
page = main_page
|
|
|
|
|
elif self.path == "/summary":
|
2018-05-13 21:23:57 +10:00
|
|
|
page = summary_page
|
|
|
|
|
elif "/" in self.path[1:]:
|
2021-11-18 13:11:14 +10:00
|
|
|
page = make_listing_page(self.path[1:])
|
2018-05-13 21:23:57 +10:00
|
|
|
else:
|
|
|
|
|
return
|
2021-11-21 22:53:12 +10:00
|
|
|
self.wfile.write(page)
|
2018-05-13 21:23:57 +10:00
|
|
|
|
|
|
|
|
|
2021-11-17 10:05:25 +10:00
|
|
|
def make_main_page(project_name):
|
2021-11-25 23:26:04 +10:00
|
|
|
body_html = """
|
|
|
|
|
<table style="height:100%;width:100%;">
|
|
|
|
|
<tr>
|
|
|
|
|
<td style="height:100%;width:38.198%;">
|
|
|
|
|
<iframe name=summary src="summary"></iframe></td>
|
|
|
|
|
<td style="height:100%;">
|
|
|
|
|
<iframe name=listing></iframe></td>
|
|
|
|
|
</tr>
|
|
|
|
|
</table>"""
|
2021-11-17 10:05:25 +10:00
|
|
|
return make_page(body_html, "Summary of " + project_name)
|
|
|
|
|
|
|
|
|
|
|
2018-05-13 21:23:57 +10:00
|
|
|
def make_summary_page(project_name, summary):
|
|
|
|
|
summary_html, summary_styles = summary.as_html()
|
2021-11-29 12:51:34 +10:00
|
|
|
body_html = "\n".join(style.as_html() for style in summary_styles) + "\n" + summary_html
|
2018-05-13 21:23:57 +10:00
|
|
|
return make_page(body_html, "Summary of " + project_name)
|
|
|
|
|
|
|
|
|
|
|
2021-11-29 12:51:34 +10:00
|
|
|
def run(server_class=http.server.HTTPServer, handler_class=Webserver, port=8080):
|
2018-05-13 21:23:57 +10:00
|
|
|
server_address = ("", port)
|
|
|
|
|
httpd = server_class(server_address, handler_class)
|
2021-12-05 19:46:45 +10:00
|
|
|
print(f"Starting httpd… See http://localhost:{port}/")
|
2018-05-13 21:23:57 +10:00
|
|
|
httpd.serve_forever()
|
|
|
|
|
|
|
|
|
|
|
2021-11-21 01:38:16 +10:00
|
|
|
def get_summary(project_path):
|
2021-11-29 12:51:34 +10:00
|
|
|
pickle_path = os.path.join(project_path, tools.CACHE_PATH, "summary.pickle")
|
2018-05-13 21:23:57 +10:00
|
|
|
with gzip.open(pickle_path, "rb") as file_:
|
|
|
|
|
screen = pickle.load(file_)
|
2021-11-16 11:25:40 +10:00
|
|
|
summary = screen._summary
|
|
|
|
|
summary._jobs_added_event = asyncio.Event()
|
|
|
|
|
for entry in summary._old_entries:
|
|
|
|
|
summary.add_entry(entry)
|
2021-11-16 16:11:32 +10:00
|
|
|
if summary.is_directory_sort:
|
|
|
|
|
summary.is_directory_sort = False
|
|
|
|
|
summary.sort_entries()
|
2021-11-21 01:38:16 +10:00
|
|
|
return summary
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
global main_page, summary_page, index
|
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
|
print(USAGE)
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
project_path = os.path.abspath(sys.argv[1])
|
|
|
|
|
os.chdir(project_path)
|
|
|
|
|
summary = get_summary(project_path)
|
|
|
|
|
project_name = os.path.basename(project_path)
|
2021-11-16 11:25:40 +10:00
|
|
|
summary_page = make_summary_page(project_name, summary)
|
2018-05-13 21:23:57 +10:00
|
|
|
index = {}
|
2021-11-16 11:25:40 +10:00
|
|
|
for row in summary._entries:
|
2018-05-13 21:23:57 +10:00
|
|
|
for result in row:
|
|
|
|
|
index[(result.path[2:], result.tool.__name__)] = result.result
|
2021-11-17 10:05:25 +10:00
|
|
|
main_page = make_main_page(project_name)
|
2018-05-13 21:23:57 +10:00
|
|
|
run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|