blob: fdcdfd96d66002fdcf0624581a51f46626a09911 [file] [log] [blame]
Brian Silverman9be949c2015-05-14 00:15:21 -04001#!/usr/bin/env python3
Comran Morshedfe7f9ea2015-02-19 23:52:57 +00002
3# This file is a modified version of the gen_embedded script included in the
4# scripts directory of seasocks (version 1.1.2). It has been modified to
5# recursively find the web files itself, which was originally done by piping
6# in the results from a "find" shell command.
7
8# The embedded files includes only those that are required for the server to run
9# (including 404 files, a default index page, favicon, etc.)
10
11import os, os.path, sys
12
Brian Silverman049dcb62015-09-27 19:08:00 -040013output = sys.argv[1].replace('"', '')
Brian Silverman9be949c2015-05-14 00:15:21 -040014
15if not os.path.exists(os.path.dirname(output)):
16 os.makedirs(os.path.dirname(output))
Comran Morshedfe7f9ea2015-02-19 23:52:57 +000017
Brian Silverman049dcb62015-09-27 19:08:00 -040018if len(sys.argv) >= 3:
19 web = sys.argv[2:]
20else:
21 web = []
22 for root, dirs, files in os.walk("./www_defaults", topdown=False):
23 for name in files + dirs:
24 web.append(os.path.join(root, name))
Comran Morshedfe7f9ea2015-02-19 23:52:57 +000025
Brian Silverman049dcb62015-09-27 19:08:00 -040026with open(output, 'w') as o:
27 o.write("""
Comran Morshedfe7f9ea2015-02-19 23:52:57 +000028#include "internal/Embedded.h"
29
30#include <string>
31#include <unordered_map>
32
33namespace {
34
35std::unordered_map<std::string, EmbeddedContent> embedded = {
36""")
37
Brian Silverman049dcb62015-09-27 19:08:00 -040038 for f in web:
39 with open(f, 'rb') as file:
40 bytes = file.read()
41 o.write('{"/%s", {' % os.path.basename(f))
Brian Silverman6470f442018-08-05 12:08:16 -070042 o.write('"' + "".join(['\\x%02x' % x for x in bytes]) + '"')
Brian Silverman049dcb62015-09-27 19:08:00 -040043 o.write(',%d }},' % len(bytes))
Comran Morshedfe7f9ea2015-02-19 23:52:57 +000044
Brian Silverman049dcb62015-09-27 19:08:00 -040045 o.write("""
Comran Morshedfe7f9ea2015-02-19 23:52:57 +000046};
47
48} // namespace
49
50const EmbeddedContent* findEmbeddedContent(const std::string& name) {
51 auto found = embedded.find(name);
52 if (found == embedded.end()) {
53 return NULL;
54 }
55 return &found->second;
56}
57""")