blob: e4d8cabcb5c807a4153b9899a435f926d7380ccd [file] [log] [blame]
Ed Tanous904063f2017-03-02 16:48:24 -08001#! /usr/bin/python3
2
3import argparse
4import os
5import gzip
6import hashlib
7from subprocess import Popen, PIPE
8
9THIS_DIR = os.path.dirname(os.path.realpath(__file__))
10
11BEGIN_BUFFER = """
12
13
14#include <webassets.hpp>
15
16void crow::webassets::request_routes(crow::App<crow::TokenAuthorizationMiddleware>& app){
17"""
18
19MIDDLE_BUFFER = """
20 CROW_ROUTE(app, "{}")([](const crow::request& req, crow::response& res) {{
21 res.code = 200;
22 // TODO, if you have a browser from the dark ages that doesn't support gzip,
23 // unzip it before sending based on Accept-Encoding header
24 res.add_header("Content-Encoding", "gzip");
25
26 res.write({{{}}});
27 res.end();
28
29 }});
30"""
31
32
33END_BUFFER = """
34
35"""
36
37def main():
38 """ Main Function """
39 file_list = []
40
41 parser = argparse.ArgumentParser()
42 parser.add_argument('-i', '--input', nargs='+', type=str)
43 parser.add_argument('-o', '--output', type=str)
44 args = parser.parse_args()
45
46 file_list = args.input
47
48 with open(args.output, 'w') as output_handle:
49
50 output_handle.write(BEGIN_BUFFER)
51
52 for full_filepath in file_list:
53
54 pathsplit = full_filepath.split(os.path.sep)
55 relative_path = os.path.sep.join(pathsplit[pathsplit.index("static") + 1:])
Ed Tanous9b65f1f2017-03-07 15:17:13 -080056
57 relative_path = "/static/" + relative_path
58
Ed Tanous38bdb982017-03-03 14:19:33 -080059 # handle the default routes
Ed Tanous9b65f1f2017-03-07 15:17:13 -080060 if relative_path == "/static/index.html":
61 relative_path = "/"
Ed Tanous38bdb982017-03-03 14:19:33 -080062
Ed Tanous904063f2017-03-02 16:48:24 -080063 # make sure none of the files are hidden
64 with open(full_filepath, 'rb') as input_file:
65 file_content = input_file.read()
66
67 print("Including {:<40} size {:>7}".format(relative_path, len(file_content)))
68
69 sha = hashlib.sha256()
70 sha.update(file_content)
71
72 sha_text = "".join("{:02x}".format(x) for x in sha.digest())
73
74 print(sha_text)
75 file_content = gzip.compress(file_content)
76 #file_content = file_content[:10]
77
78 array_binary_text = ', '.join('0x{:02x}'.format(x) for x in file_content)
79
80 output_handle.write(MIDDLE_BUFFER.format(relative_path, array_binary_text))
81
82 output_handle.write("};\n")
83 output_handle.write(END_BUFFER)
84
85if __name__ == "__main__":
86 main()