gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 1 | from flask import Flask, render_template, url_for, request, g |
| 2 | from flask_bootstrap import Bootstrap |
| 3 | import json |
| 4 | import os.path |
gstepanov | c3ffac7 | 2015-02-05 15:39:45 +0200 | [diff] [blame] | 5 | from storage_api import create_storage, TEST_PATH |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 6 | |
| 7 | app = Flask(__name__) |
| 8 | Bootstrap(app) |
| 9 | |
| 10 | |
| 11 | def collect_tests(): |
| 12 | result = [] |
| 13 | |
gstepanov | c3ffac7 | 2015-02-05 15:39:45 +0200 | [diff] [blame] | 14 | for file in os.listdir(TEST_PATH): |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 15 | if file.endswith(".json"): |
| 16 | result.append(file.split('.')[0]) |
| 17 | |
| 18 | return result |
| 19 | |
| 20 | |
| 21 | def load_test(test_name): |
| 22 | test_name += '.json' |
| 23 | |
gstepanov | 3501ad4 | 2015-02-05 15:41:39 +0200 | [diff] [blame] | 24 | with open(TEST_PATH + "/" + test_name, 'rt') as f: |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 25 | raw = f.read() |
| 26 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame^] | 27 | if raw != "": |
| 28 | test = json.loads(raw) |
| 29 | else: |
| 30 | test = [] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 31 | |
| 32 | return test |
| 33 | |
| 34 | |
| 35 | @app.route("/", methods=['GET', 'POST']) |
| 36 | def index(): |
| 37 | data = [] |
| 38 | for test in collect_tests(): |
| 39 | d = {} |
| 40 | d["name"] = test |
| 41 | d["url"] = url_for("render_test", test_name=test) |
| 42 | data.append(d) |
| 43 | |
| 44 | return render_template("index.html", tests=data) |
| 45 | |
| 46 | |
| 47 | @app.route("/tests/<test_name>", methods=['GET']) |
| 48 | def render_test(test_name): |
| 49 | tests = load_test(test_name) |
| 50 | header_keys = ['build_id', 'iso_md5', 'type'] |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame^] | 51 | table = [[]] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 52 | |
| 53 | if len(tests) > 0: |
| 54 | sorted_keys = sorted(tests[0].keys()) |
| 55 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame^] | 56 | for key in sorted_keys: |
| 57 | if key not in header_keys: |
| 58 | header_keys.append(key) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 59 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame^] | 60 | for test in tests: |
| 61 | row = [] |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 62 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame^] | 63 | for header in header_keys: |
| 64 | if isinstance(test[header], list): |
| 65 | row.append(str(test[header][0]) + unichr(0x00B1) + str(test[header][1])) |
| 66 | else: |
| 67 | row.append(test[header]) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 68 | |
gstepanov | 0a8fdd3 | 2015-02-05 15:54:11 +0200 | [diff] [blame^] | 69 | table.append(row) |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 70 | |
| 71 | return render_template("table.html", headers=header_keys, table=table) |
| 72 | |
| 73 | |
| 74 | @app.route("/tests/<test_name>", methods=['POST']) |
| 75 | def add_test(test_name): |
| 76 | tests = json.loads(request.data) |
| 77 | |
| 78 | if not hasattr(g, "storage"): |
gstepanov | 993c74b | 2015-02-05 15:20:38 +0200 | [diff] [blame] | 79 | g.storage = create_storage("file://" + os.path.dirname(__file__) + "/test_results/sample.json", "", "") |
gstepanov | 2164d82 | 2015-02-04 19:49:40 +0200 | [diff] [blame] | 80 | |
| 81 | for test in tests: |
| 82 | g.storage.store(test) |
| 83 | return "Created", 201 |
| 84 | |
| 85 | |
| 86 | |
| 87 | if __name__ == "__main__": |
| 88 | app.run(debug=True) |