diff options
author | chai <chaifix@163.com> | 2018-10-15 19:43:29 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-10-15 19:43:29 +0800 |
commit | aeeb0c08ac1cf1604bb1659b2c13c1a1ed500848 (patch) | |
tree | 14b87968588dea691057b5e86d1f945d0d4b9215 /tools/wrapy.py | |
parent | f3ee4be63e1c78c6f09fcb994b124533b1e5d3c1 (diff) |
*misc
Diffstat (limited to 'tools/wrapy.py')
-rw-r--r-- | tools/wrapy.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/tools/wrapy.py b/tools/wrapy.py index e69de29..a1975ea 100644 --- a/tools/wrapy.py +++ b/tools/wrapy.py @@ -0,0 +1,57 @@ +#!/usr/bin/python2.7 +import os, sys, random, re + +def fmt(fmt, dic): + for k in dic: + fmt = fmt.replace("{%s}" % k, str(dic[k])) + return fmt + + +def makeArray(data): + i = [0] + def fn(x): + x = str(ord(x)) + "," + if i[0] + len(x) > 78: + i[0] = len(x) + x = '\n' + x + else: + i[0] += len(x) + return x + return '{' + "".join(map(fn, data)).rstrip(",") + '}' + + +def safename(filename): + return re.sub("[^a-z0-9]", "_", os.path.basename(filename).lower()) + + +def process(filenames): + if type(filenames) is str: + filenames = [filenames] + + strings = [] + + for filename in filenames: + data = open(filename, "rb").read() + strings.append( + fmt("/* {filename} */\n" +\ + "static const char {name}[] = \n{array};", + { + "filename" : os.path.basename(filename), + "name" : safename(filename), + "array" : makeArray(data), + })) + + return "/* Automatically generated; do not edit */\n\n" +\ + "\n\n".join(strings) + + +def main(): + if len(sys.argv) < 2: + print "usage: embed FILENAMES" + sys.exit(1) + + print process(sys.argv[1:]) + + +if __name__ == "__main__": + main()
\ No newline at end of file |