Brian Silverman | 9be949c | 2015-05-14 00:15:21 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Comran Morshed | fe7f9ea | 2015-02-19 23:52:57 +0000 | [diff] [blame] | 2 | |
| 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 | |
| 11 | import os, os.path, sys |
| 12 | |
Brian Silverman | 9be949c | 2015-05-14 00:15:21 -0400 | [diff] [blame] | 13 | output = sys.argv[1] |
| 14 | assert output[0] == '"' |
| 15 | assert output[-1] == '"' |
| 16 | output = output[1:-1] |
| 17 | |
| 18 | if not os.path.exists(os.path.dirname(output)): |
| 19 | os.makedirs(os.path.dirname(output)) |
| 20 | o = open(output, 'w') |
Comran Morshed | fe7f9ea | 2015-02-19 23:52:57 +0000 | [diff] [blame] | 21 | |
| 22 | web = [] |
Comran Morshed | c4ce951 | 2015-03-08 11:51:09 +0000 | [diff] [blame] | 23 | for root, dirs, files in os.walk("./www_defaults", topdown=False): |
Comran Morshed | fe7f9ea | 2015-02-19 23:52:57 +0000 | [diff] [blame] | 24 | 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 | |
| 29 | o.write(""" |
| 30 | #include "internal/Embedded.h" |
| 31 | |
| 32 | #include <string> |
| 33 | #include <unordered_map> |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | std::unordered_map<std::string, EmbeddedContent> embedded = { |
| 38 | """) |
| 39 | |
| 40 | for 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 | |
| 46 | o.write(""" |
| 47 | }; |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | const 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 | |
| 60 | o.close() |