2015-07-06 07:46:48 -04:00
|
|
|
#!/usr/bin/env python
|
2015-01-20 04:52:12 -05:00
|
|
|
#
|
|
|
|
# Create frozen modules structure for MicroPython.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# Have a directory with modules to be frozen (only modules, not packages
|
|
|
|
# supported so far):
|
|
|
|
#
|
|
|
|
# frozen/foo.py
|
|
|
|
# frozen/bar.py
|
|
|
|
#
|
|
|
|
# Run script, passing path to the directory above:
|
|
|
|
#
|
|
|
|
# ./make-frozen.py frozen > frozen.c
|
|
|
|
#
|
2016-01-31 17:24:16 -05:00
|
|
|
# Include frozen.c in your build, having defined MICROPY_MODULE_FROZEN_STR in
|
2015-01-20 04:52:12 -05:00
|
|
|
# config.
|
|
|
|
#
|
2015-07-06 07:09:16 -04:00
|
|
|
from __future__ import print_function
|
2015-01-20 04:52:12 -05:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
def module_name(f):
|
|
|
|
return f[:-len(".py")]
|
|
|
|
|
|
|
|
modules = []
|
|
|
|
|
2015-06-03 12:24:57 -04:00
|
|
|
root = sys.argv[1].rstrip("/")
|
2015-05-29 17:23:32 -04:00
|
|
|
root_len = len(root)
|
|
|
|
|
|
|
|
for dirpath, dirnames, filenames in os.walk(root):
|
2015-01-20 04:52:12 -05:00
|
|
|
for f in filenames:
|
2015-05-29 17:23:32 -04:00
|
|
|
fullpath = dirpath + "/" + f
|
|
|
|
st = os.stat(fullpath)
|
2015-06-03 12:24:57 -04:00
|
|
|
modules.append((fullpath[root_len + 1:], st))
|
2015-01-20 04:52:12 -05:00
|
|
|
|
|
|
|
print("#include <stdint.h>")
|
2016-01-31 17:24:16 -05:00
|
|
|
print("const char mp_frozen_str_names[] = {")
|
2016-01-03 11:08:45 -05:00
|
|
|
for f, st in modules:
|
|
|
|
m = module_name(f)
|
|
|
|
print('"%s\\0"' % m)
|
|
|
|
print('"\\0"};')
|
|
|
|
|
2016-01-31 17:24:16 -05:00
|
|
|
print("const uint32_t mp_frozen_str_sizes[] = {")
|
2015-01-20 04:52:12 -05:00
|
|
|
|
|
|
|
for f, st in modules:
|
|
|
|
print("%d," % st.st_size)
|
|
|
|
|
2016-01-03 11:08:45 -05:00
|
|
|
print("};")
|
2015-01-20 04:52:12 -05:00
|
|
|
|
2016-01-31 17:24:16 -05:00
|
|
|
print("const char mp_frozen_str_content[] = {")
|
2015-01-20 04:52:12 -05:00
|
|
|
for f, st in modules:
|
2015-05-29 17:38:44 -04:00
|
|
|
data = open(sys.argv[1] + "/" + f, "rb").read()
|
2015-07-06 07:27:57 -04:00
|
|
|
# Python2 vs Python3 tricks
|
|
|
|
data = repr(data)
|
|
|
|
if data[0] == "b":
|
|
|
|
data = data[1:]
|
|
|
|
data = data[1:-1]
|
2015-01-20 04:52:12 -05:00
|
|
|
data = data.replace('"', '\\"')
|
2016-01-01 00:51:07 -05:00
|
|
|
print('"%s\\0"' % data)
|
2015-01-20 04:52:12 -05:00
|
|
|
print("};")
|