blob: b6f43aad4f6e5d2ecb8fb0ce4477d3c73c1cbf9f [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)):
Ravago Jones5127ccc2022-07-31 16:32:45 -070016 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:
Ravago Jones5127ccc2022-07-31 16:32:45 -070019 web = sys.argv[2:]
Brian Silverman049dcb62015-09-27 19:08:00 -040020else:
Ravago Jones5127ccc2022-07-31 16:32:45 -070021 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:
Ravago Jones5127ccc2022-07-31 16:32:45 -070027 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
Ravago Jones5127ccc2022-07-31 16:32:45 -070038 for f in web:
39 with open(f, 'rb') as file:
40 bytes = file.read()
41 o.write('{"/%s", {' % os.path.basename(f))
42 o.write('"' + "".join(['\\x%02x' % x for x in bytes]) + '"')
43 o.write(',%d }},' % len(bytes))
Comran Morshedfe7f9ea2015-02-19 23:52:57 +000044
Ravago Jones5127ccc2022-07-31 16:32:45 -070045 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""")