dosage/scripts/mktestpage.py
2013-01-09 22:26:00 +01:00

125 lines
3.5 KiB
Python
Executable file

#!/usr/bin/env python
# Copyright (C) 2012-2013 Bastian Kleineidam
from __future__ import print_function
import sys
import os
import time
import cgi
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from dosagelib.scraper import get_scrapers
htmltemplate = """
---
extends: base.j2
title: Dosage by Bastian Kleineidam
description: a commandline webcomic downloader and archiver
---
{%% block js %%}
<script src="media/js/masonry.min.js"></script>
{%% endblock js %%}
{%% block content %%}
<div class="inner clearfix">
<section id="main-content">
<h2>Dosage test results from %(date)s</h2>
<p>Note that it is almost impossible to get a 100%% OK test run
due to temporary site failures.</p>
<div id="testresults">
%(content)s
</div>
<script>
window.onload = function() {
var wall = new Masonry(document.getElementById('testresults'), {
columnWidth: 240
});
};
</script>
{%% endblock content %%}
"""
def get_mtime (filename):
"""Return modification time of filename."""
return os.path.getmtime(filename)
def strdate(t):
"""Get formatted date string."""
return time.strftime("%d.%m.%Y", time.localtime(t))
def get_test_name(line):
"""Get scraper name from test output line."""
classname = line.split('::')[1][4:]
for scraper in get_scrapers():
if scraper.__name__ == classname:
try:
url = scraper.starter()
except Exception:
url = None
return scraper.get_name(), url
raise ValueError("Scraper %r not found" % classname)
def get_test(line):
"""Get test name from test output line."""
name, url = get_test_name(line)
result = "OK" if line.startswith(". ") else "FAILED"
return [name, url, result, ""]
def get_content(filename):
"""Get HTML content for test output."""
tests = []
with open(filename, "r") as f:
print("Tests parsed: 0", end=" ", file=sys.stderr)
num_tests = 0
add_reason = False
for line in f:
if line.startswith((". ", "F ")) and "test_comics" in line:
add_reason = line.startswith("F ")
num_tests += 1
try:
tests.append(get_test(line))
except Exception as msg:
print("WARNING:", msg, file=sys.stderr)
continue
elif add_reason and line.startswith(" E "):
reason = line[3:].strip()
tests[-1][-1] = reason
if num_tests % 5 == 0:
print(num_tests, end=" ", file=sys.stderr)
tests.sort()
res = []
for name, url, result, reason in tests:
css = result.lower()
if len(name) > 40:
name = name[:37] + "..."
if url:
args = quote_all(url, reason, css, name)
inner = '<a href="%s" title="%s" class="%s">%s</a>' % args
else:
args = quote_all(reason, css, name)
inner = '<span title="%s" class="%s">%s</span>' % args
res.append('<div class="item">%s</div>' % inner)
return os.linesep.join(res)
def quote_all(*args):
"""CGI-escape all arguments for."""
return tuple(cgi.escape(x, quote=True) for x in args)
def main(args):
"""Generate HTML output for test result."""
filename = args[0]
modified = get_mtime(filename)
content = get_content(filename)
attrs = {"date": strdate(modified), "content": content}
print(htmltemplate % attrs)
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))