blob: 035a7dae45ffb598d904fbc7ee56b327a54b65f5 [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:])
56 relative_path = "/" + relative_path
57
58 # make sure none of the files are hidden
59 with open(full_filepath, 'rb') as input_file:
60 file_content = input_file.read()
61
62 print("Including {:<40} size {:>7}".format(relative_path, len(file_content)))
63
64 sha = hashlib.sha256()
65 sha.update(file_content)
66
67 sha_text = "".join("{:02x}".format(x) for x in sha.digest())
68
69 print(sha_text)
70 file_content = gzip.compress(file_content)
71 #file_content = file_content[:10]
72
73 array_binary_text = ', '.join('0x{:02x}'.format(x) for x in file_content)
74
75 output_handle.write(MIDDLE_BUFFER.format(relative_path, array_binary_text))
76
77 output_handle.write("};\n")
78 output_handle.write(END_BUFFER)
79
80if __name__ == "__main__":
81 main()