def run(self, results):
"""Writes report.
@param results: Cuckoo results dict.
@raise CuckooReportError: if fails to write report.
"""
if not HAVE_JINJA2:
raise CuckooReportError("Failed to generate HTML report: "
"Jinja2 Python library is not installed")
shots_path = os.path.join(self.analysis_path, "shots")
if os.path.exists(shots_path):
shots = []
counter = 1
for shot_name in os.listdir(shots_path):
if not shot_name.endswith(".jpg"):
continue
shot_path = os.path.join(shots_path, shot_name)
if os.path.getsize(shot_path) == 0:
continue
shot = {}
shot["id"] = os.path.splitext(File(shot_path).get_name())[0]
shot["data"] = base64.b64encode(open(shot_path, "rb").read())
shots.append(shot)
counter += 1
shots.sort(key=lambda shot: shot["id"])
results["shots"] = shots
else:
results["shots"] = []
env = Environment(autoescape=True)
env.loader = FileSystemLoader(os.path.join(CUCKOO_ROOT,
"data", "html"))
try:
tpl = env.get_template("report.html")
html = tpl.render({"results": results, "summary_report" : False})
except UndefinedError as e:
raise CuckooReportError("Failed to generate summary HTML report: {} ".format(e))
except TemplateNotFound as e:
raise CuckooReportError("Failed to generate summary HTML report: {} {} ".format(e, e.name))
except (TemplateSyntaxError, TemplateAssertionError) as e:
raise CuckooReportError("Failed to generate summary HTML report: {} on {}, line {} ".format(e, e.name,
e.lineno))
try:
with codecs.open(os.path.join(self.reports_path, "report.html"), "w", encoding="utf-8") as report:
report.write(html)
except (TypeError, IOError) as e:
raise CuckooReportError("Failed to write HTML report: %s" % e)
return True
评论列表
文章目录