blob: d0fd603980ceda977eeb270e8c271e29a7bfb02d [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 Silverman9be949c2015-05-14 00:15:21 -040013output = sys.argv[1]
14assert output[0] == '"'
15assert output[-1] == '"'
16output = output[1:-1]
17
18if not os.path.exists(os.path.dirname(output)):
19 os.makedirs(os.path.dirname(output))
20o = open(output, 'w')
Comran Morshedfe7f9ea2015-02-19 23:52:57 +000021
22web = []
Comran Morshedc4ce9512015-03-08 11:51:09 +000023for root, dirs, files in os.walk("./www_defaults", topdown=False):
Comran Morshedfe7f9ea2015-02-19 23:52:57 +000024 for name in files:
25 web.append(os.path.join(root, name))
26 for name in dirs:
27 web.append(os.path.join(root, name))
28
29o.write("""
30#include "internal/Embedded.h"
31
32#include <string>
33#include <unordered_map>
34
35namespace {
36
37std::unordered_map<std::string, EmbeddedContent> embedded = {
38""")
39
40for f in web:
41 bytes = open(f, 'rb').read()
42 o.write('{"/%s", {' % os.path.basename(f))
43 o.write('"' + "".join(['\\x%02x' % ord(x) for x in bytes]) + '"')
44 o.write(',%d }},' % len(bytes))
45
46o.write("""
47};
48
49} // namespace
50
51const EmbeddedContent* findEmbeddedContent(const std::string& name) {
52 auto found = embedded.find(name);
53 if (found == embedded.end()) {
54 return NULL;
55 }
56 return &found->second;
57}
58""")
59
60o.close()