Comran Morshed | fe7f9ea | 2015-02-19 23:52:57 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 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 | |
| 13 | o = open('embedded.h', 'w') |
| 14 | |
| 15 | web = [] |
| 16 | for root, dirs, files in os.walk("./web", topdown=False): |
| 17 | for name in files: |
| 18 | web.append(os.path.join(root, name)) |
| 19 | for name in dirs: |
| 20 | web.append(os.path.join(root, name)) |
| 21 | |
| 22 | o.write(""" |
| 23 | #include "internal/Embedded.h" |
| 24 | |
| 25 | #include <string> |
| 26 | #include <unordered_map> |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | std::unordered_map<std::string, EmbeddedContent> embedded = { |
| 31 | """) |
| 32 | |
| 33 | for f in web: |
| 34 | bytes = open(f, 'rb').read() |
| 35 | o.write('{"/%s", {' % os.path.basename(f)) |
| 36 | o.write('"' + "".join(['\\x%02x' % ord(x) for x in bytes]) + '"') |
| 37 | o.write(',%d }},' % len(bytes)) |
| 38 | |
| 39 | o.write(""" |
| 40 | }; |
| 41 | |
| 42 | } // namespace |
| 43 | |
| 44 | const EmbeddedContent* findEmbeddedContent(const std::string& name) { |
| 45 | auto found = embedded.find(name); |
| 46 | if (found == embedded.end()) { |
| 47 | return NULL; |
| 48 | } |
| 49 | return &found->second; |
| 50 | } |
| 51 | """) |
| 52 | |
| 53 | o.close() |