diff options
| author | Ed Tanous <ed.tanous@intel.com> | 2017-03-02 16:48:24 -0800 |
|---|---|---|
| committer | Ed Tanous <ed.tanous@intel.com> | 2017-03-02 16:48:24 -0800 |
| commit | 904063f6e5eccdd15d7a9d2a21200f99abc679ae (patch) | |
| tree | 1ef33196bf604ac511f9a2596b449666dd0ab127 /scripts | |
| parent | f927347406ef6ea49edf710897b213ccd8c8e284 (diff) | |
| download | bmcweb-904063f6e5eccdd15d7a9d2a21200f99abc679ae.tar.gz bmcweb-904063f6e5eccdd15d7a9d2a21200f99abc679ae.zip | |
incremental
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/build_web_assets.py | 81 | ||||
| -rw-r--r-- | scripts/file_to_string_array.py | 29 |
2 files changed, 81 insertions, 29 deletions
diff --git a/scripts/build_web_assets.py b/scripts/build_web_assets.py new file mode 100755 index 0000000..035a7da --- /dev/null +++ b/scripts/build_web_assets.py @@ -0,0 +1,81 @@ +#! /usr/bin/python3 + +import argparse +import os +import gzip +import hashlib +from subprocess import Popen, PIPE + +THIS_DIR = os.path.dirname(os.path.realpath(__file__)) + +BEGIN_BUFFER = """ + + +#include <webassets.hpp> + +void crow::webassets::request_routes(crow::App<crow::TokenAuthorizationMiddleware>& app){ +""" + +MIDDLE_BUFFER = """ + CROW_ROUTE(app, "{}")([](const crow::request& req, crow::response& res) {{ + res.code = 200; + // TODO, if you have a browser from the dark ages that doesn't support gzip, + // unzip it before sending based on Accept-Encoding header + res.add_header("Content-Encoding", "gzip"); + + res.write({{{}}}); + res.end(); + + }}); +""" + + +END_BUFFER = """ + +""" + +def main(): + """ Main Function """ + file_list = [] + + parser = argparse.ArgumentParser() + parser.add_argument('-i', '--input', nargs='+', type=str) + parser.add_argument('-o', '--output', type=str) + args = parser.parse_args() + + file_list = args.input + + with open(args.output, 'w') as output_handle: + + output_handle.write(BEGIN_BUFFER) + + for full_filepath in file_list: + + pathsplit = full_filepath.split(os.path.sep) + relative_path = os.path.sep.join(pathsplit[pathsplit.index("static") + 1:]) + relative_path = "/" + relative_path + + # make sure none of the files are hidden + with open(full_filepath, 'rb') as input_file: + file_content = input_file.read() + + print("Including {:<40} size {:>7}".format(relative_path, len(file_content))) + + sha = hashlib.sha256() + sha.update(file_content) + + sha_text = "".join("{:02x}".format(x) for x in sha.digest()) + + print(sha_text) + file_content = gzip.compress(file_content) + #file_content = file_content[:10] + + array_binary_text = ', '.join('0x{:02x}'.format(x) for x in file_content) + + output_handle.write(MIDDLE_BUFFER.format(relative_path, array_binary_text)) + + output_handle.write("};\n") + output_handle.write(END_BUFFER) + +if __name__ == "__main__": + main() diff --git a/scripts/file_to_string_array.py b/scripts/file_to_string_array.py deleted file mode 100644 index b81e854..0000000 --- a/scripts/file_to_string_array.py +++ /dev/null @@ -1,29 +0,0 @@ -# -*- coding: utf-8 -*- -import os.path -import string -import sys - - -def print_buf(counter, buf): - buf2 = [('%02x' % ord(i)) for i in buf] - print '{0}: {1:<39} {2}'.format(('%07x' % (counter * 16)), - ' '.join([''.join(buf2[i:i + 2]) for i in range(0, len(buf2), 2)]), - ''.join([c if c in string.printable[:-5] else '.' for c in buf])) - - -def process_xxd(file_path): - with open(file_path, 'r') as f: - counter = 0 - while True: - buf = f.read(16) - if not buf: - break - print_buf(counter, buf) - counter += 1 - - -if __name__ == '__main__': - if not os.path.exists(sys.argv[1]): - print >> (sys.stderr, "The file doesn't exist.") - sys.exit(1) - process_xxd(sys.argv[1])
\ No newline at end of file |

