tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

spec_report.py (2565B)


      1 import os
      2 import sys
      3 import glob
      4 import html
      5 import fnmatch
      6 from os import path
      7 
      8 import coverage
      9 
     10 OUTPUT_TEMPLATE = """
     11 <!DOCTYPE html>
     12 <html>
     13 <head>
     14    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     15    <title>Spec Coverage</title>
     16    <link rel="stylesheet" href="style.css" type="text/css">
     17    <style>
     18        .covered {
     19        }
     20 
     21        .missed {
     22            background-color: lightcoral;
     23        }
     24        code {
     25            margin: 0;
     26            padding: 0;
     27            display:block;
     28            white-space:pre-wrap;
     29        }
     30    </style>
     31 </head>
     32 <body>
     33    %head
     34    <div><pre>
     35        %body
     36    </pre></div>
     37 </body>
     38 </html>
     39 """
     40 
     41 LINE_TEMPLATE = "<code class=\"%class\">%lineno| %source</code>"
     42 
     43 def write_report(data, source_file, output_file):
     44    module_name, executable_lines, excluded_lines, missing_lines, _ = data
     45    missing_lines = set(missing_lines)
     46 
     47    with open(output_file, "w") as output, open(source_file, "r") as source:
     48        lines = source.readlines()
     49 
     50        file_report = []
     51        padding = len(str(len(lines)))
     52 
     53        for index, line in enumerate(lines):
     54            line = line[0:-1]
     55            lineno = index + 1
     56            line_number = str(lineno).rjust(padding)
     57 
     58            covered = lineno not in missing_lines
     59            line_class = 'covered' if covered else 'missed'
     60 
     61            formatted_line = (LINE_TEMPLATE.replace('%class', line_class)
     62                                           .replace('%lineno', line_number)
     63                                           .replace('%source', html.escape(line)))
     64            file_report.append(formatted_line)
     65 
     66        report_body = ''.join(file_report)
     67 
     68        report_header = ''
     69 
     70        report = (OUTPUT_TEMPLATE.replace('%head', report_header)
     71                                 .replace('%body', report_body))
     72        output.write(report)
     73 
     74 def main(argv):
     75    parsing_path = path.normpath(path.join(path.dirname(__file__), ".."))
     76 
     77    files = argv[1:]
     78    if not files:
     79        files = [os.path.join(root, file) for root, _, files in os.walk(parsing_path)
     80                                          for file in fnmatch.filter(files, '*.vtt')]
     81 
     82    cov = coverage.Coverage()
     83    cov.start()
     84 
     85    for file_path in files:
     86        with open(file_path, "r") as file:
     87            source = file.read()
     88 
     89            import parser
     90            p = parser.VTTParser(source)
     91            p.parse()
     92 
     93    cov.stop()
     94 
     95    data = cov.analysis2(parser.__file__)
     96    write_report(data, parser.__file__, "report.html")
     97 
     98 if __name__ == '__main__':
     99    main(sys.argv)