|
| 1 | +import os |
| 2 | +import json |
| 3 | +import argparse |
| 4 | +import html_utl |
| 5 | + |
| 6 | + |
| 7 | +def get_sort_keys(): |
| 8 | + return [ |
| 9 | + "total_time", |
| 10 | + "max_time", |
| 11 | + "hit_count", |
| 12 | + "max_runs", |
| 13 | + "run_count", |
| 14 | + ] |
| 15 | + |
| 16 | + |
| 17 | +def get_integer_keys(): |
| 18 | + return [ |
| 19 | + "hit_count", |
| 20 | + "max_runs", |
| 21 | + "run_count", |
| 22 | + ] |
| 23 | + |
| 24 | + |
| 25 | +def get_column_names(): |
| 26 | + return [ |
| 27 | + "Time", |
| 28 | + "TimePeek", |
| 29 | + "#Hits", |
| 30 | + "RunPeek", |
| 31 | + "#Runs", |
| 32 | + ] |
| 33 | + |
| 34 | + |
| 35 | +def as_html(stats, keys_order=None): |
| 36 | + if keys_order is None: |
| 37 | + keys_order = get_sort_keys() |
| 38 | + result = html_utl.get_html_prefix("TMPROF") |
| 39 | + sorted_stats = stats[:] |
| 40 | + reversed_keys_order = keys_order[:] |
| 41 | + reversed_keys_order.reverse() |
| 42 | + for key in reversed_keys_order: |
| 43 | + sorted_stats = sorted(sorted_stats, key=lambda x: x[key]) |
| 44 | + sorted_stats.reverse() |
| 45 | + total_time = sum([record["total_time"] for record in sorted_stats]) |
| 46 | + result += "<h2>Time profile</h2>\n" |
| 47 | + result += "<table>\n" |
| 48 | + result += " <caption>All times are in seconds.</caption>\n" |
| 49 | + result += " <tr>\n" |
| 50 | + result += " <th>#</th>\n" |
| 51 | + result += " <th>Function</th>\n" |
| 52 | + result += " <th>%</th>\n" |
| 53 | + for key in keys_order: |
| 54 | + result += " <th>" + get_column_names()[get_sort_keys().index(key)] +"</th>\n" |
| 55 | + result += " <th>File</th>\n" |
| 56 | + result += " <th>Line</th>\n" |
| 57 | + result += " </tr>\n" |
| 58 | + for idx, record in enumerate(sorted_stats): |
| 59 | + result += " <tr>\n" |
| 60 | + result += " <td>" + str(idx + 1) + "</td>\n" |
| 61 | + result += " <td>" + html_utl.escape_text_to_HTML(record["function"]) + "</td>\n" |
| 62 | + result += " <td>" + format(100.0 * record["total_time"] / (total_time + 0.0001), ".1f") + "</td>\n" |
| 63 | + for key in keys_order: |
| 64 | + if key in get_integer_keys(): |
| 65 | + result += " <td>" + str(record[key]) + "</td>\n" |
| 66 | + else: |
| 67 | + result += " <td>" + format(record[key], ".3f") + "</td>\n" |
| 68 | + result += " <td>" + record["file"] + "</td>\n" |
| 69 | + result += " <td>" + str(record["line"]) + "</td>\n" |
| 70 | + result += " </tr>\n" |
| 71 | + return result + html_utl.get_html_suffix() |
| 72 | + |
| 73 | + |
| 74 | +def generate_html_from_stats(stats, out_html_file, keys_order=None): |
| 75 | + with open(out_html_file, "w") as ofile: |
| 76 | + ofile.write(as_html(stats, keys_order)) |
| 77 | + |
| 78 | + |
| 79 | +def generate_html(in_json_file, out_html_file, keys_order=None): |
| 80 | + with open(in_json_file, "r") as ifile: |
| 81 | + stats = json.load(ifile) |
| 82 | + generate_html_from_stats(stats, out_html_file, keys_order) |
| 83 | + |
| 84 | + |
| 85 | +def _main(cmdline): |
| 86 | + if cmdline.version: |
| 87 | + print("v.1.0") |
| 88 | + return 0 |
| 89 | + |
| 90 | + cmdline.input = os.path.abspath(cmdline.input) |
| 91 | + if not os.path.isfile(cmdline.input): |
| 92 | + print("ERROR: Cannot access the input JSON file: " + cmdline.input) |
| 93 | + return -1 |
| 94 | + if os.path.splitext(cmdline.input)[1].lower() != ".json": |
| 95 | + print("ERROR: Ihe input file does not have '.json' extension: " + cmdline.input) |
| 96 | + return -2 |
| 97 | + |
| 98 | + if cmdline.output is None: |
| 99 | + cmdline.output = os.path.splitext(cmdline.input)[0] + ".html" |
| 100 | + else: |
| 101 | + cmdline.output = os.path.abspath(cmdline.output) |
| 102 | + if os.path.splitext(cmdline.output)[1].lower() != ".html": |
| 103 | + print("ERROR: Ihe output file does not have '.html' extension: " + cmdline.output) |
| 104 | + return -3 |
| 105 | + os.makedirs(os.path.dirname(cmdline.output), exist_ok=True) |
| 106 | + |
| 107 | + if cmdline.keys is None: |
| 108 | + cmdline.keys = get_sort_keys() |
| 109 | + else: |
| 110 | + order = [] |
| 111 | + if len(cmdline.keys) != len(set(cmdline.keys)): |
| 112 | + print("ERROR: The option --keys was passed duplicate keys.") |
| 113 | + return -4 |
| 114 | + for key in cmdline.keys: |
| 115 | + if key not in get_sort_keys(): |
| 116 | + print("ERROR: The key '" + key + "' is not valid. Use --help fro more details.") |
| 117 | + return -5 |
| 118 | + order.append(key) |
| 119 | + for key in get_sort_keys(): |
| 120 | + if key not in order: |
| 121 | + order.append(key) |
| 122 | + cmdline.keys = order |
| 123 | + |
| 124 | + generate_html(cmdline.input, cmdline.output, cmdline.keys) |
| 125 | + # try: |
| 126 | + # generate_html(cmdline.input, cmdline.output, cmdline.keys) |
| 127 | + # except: |
| 128 | + # print("ERROR: The conversion to HTML has FAILED.") |
| 129 | + # return -6 |
| 130 | + |
| 131 | + return 0 |
| 132 | + |
| 133 | + |
| 134 | +def _parse_cmd_line(): |
| 135 | + parser = argparse.ArgumentParser( |
| 136 | + description="This is the security analyser of Java web applications.") |
| 137 | + parser.add_argument("-V", "--version", action="store_true", |
| 138 | + help="Prints a version string.") |
| 139 | + parser.add_argument("input", type=str, |
| 140 | + help="A path-name of a JSON file containing time profiling data.") |
| 141 | + parser.add_argument("-o", "--output", type=str, |
| 142 | + help="A path-name of an HTML file to be filled in by the time profiling data in the JSON file. " |
| 143 | + "If not specified, the HTML file (with the same name as the JSON file) will be created " |
| 144 | + "in the directory of the JSON file. In the HTML the keys corresponds to these columns " |
| 145 | + "respectively: " + ",".join(get_column_names()) + ".") |
| 146 | + parser.add_argument("-k", "--keys", nargs='+', |
| 147 | + help="An ordered list of keys defining the lexicographical order of columns in the " |
| 148 | + "HTML table. Here are the keys, listed in the default order: " + |
| 149 | + ", ".join(get_sort_keys()) + ". Not specifies some keys will be appended to " |
| 150 | + "from the default order.") |
| 151 | + return parser.parse_args() |
| 152 | + |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + exit(_main(_parse_cmd_line())) |
0 commit comments