Package modules :: Package reporting :: Module reporthtml
[hide private]
[frames] | no frames]

Source Code for Module modules.reporting.reporthtml

 1  # Copyright (C) 2010-2014 Cuckoo Foundation. 
 2  # This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org 
 3  # See the file 'docs/LICENSE' for copying permission. 
 4   
 5  import os 
 6  import codecs 
 7  import base64 
 8   
 9  from lib.cuckoo.common.abstracts import Report 
10  from lib.cuckoo.common.constants import CUCKOO_ROOT 
11  from lib.cuckoo.common.exceptions import CuckooReportError 
12  from lib.cuckoo.common.objects import File 
13   
14  try: 
15      from jinja2.environment import Environment 
16      from jinja2.loaders import FileSystemLoader 
17      HAVE_JINJA2 = True 
18  except ImportError: 
19      HAVE_JINJA2 = False 
20   
21 -class ReportHTML(Report):
22 """Stores report in HTML format.""" 23
24 - def run(self, results):
25 """Writes report. 26 @param results: Cuckoo results dict. 27 @raise CuckooReportError: if fails to write report. 28 """ 29 if not HAVE_JINJA2: 30 raise CuckooReportError("Failed to generate HTML report: " 31 "Jinja2 Python library is not installed") 32 33 shots_path = os.path.join(self.analysis_path, "shots") 34 if os.path.exists(shots_path): 35 shots = [] 36 counter = 1 37 for shot_name in os.listdir(shots_path): 38 if not shot_name.endswith(".jpg"): 39 continue 40 41 shot_path = os.path.join(shots_path, shot_name) 42 43 if os.path.getsize(shot_path) == 0: 44 continue 45 46 shot = {} 47 shot["id"] = os.path.splitext(File(shot_path).get_name())[0] 48 shot["data"] = base64.b64encode(open(shot_path, "rb").read()) 49 shots.append(shot) 50 51 counter += 1 52 53 shots.sort(key=lambda shot: shot["id"]) 54 results["screenshots"] = shots 55 else: 56 results["screenshots"] = [] 57 58 env = Environment(autoescape=True) 59 env.loader = FileSystemLoader(os.path.join(CUCKOO_ROOT, 60 "data", "html")) 61 62 try: 63 tpl = env.get_template("report.html") 64 html = tpl.render({"results": results}) 65 except Exception as e: 66 raise CuckooReportError("Failed to generate HTML report: %s" % e) 67 68 try: 69 with codecs.open(os.path.join(self.reports_path, "report.html"), "w", encoding="utf-8") as report: 70 report.write(html) 71 except (TypeError, IOError) as e: 72 raise CuckooReportError("Failed to write HTML report: %s" % e) 73 74 return True
75