summaryrefslogtreecommitdiffstatshomepage
path: root/shared/memzip/make-memzip.py
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2021-07-09 12:38:25 +1000
committerDamien George <damien@micropython.org>2021-07-12 17:07:36 +1000
commit4d546713ec8858cbf908de45de11cbfc46a20971 (patch)
treefda10704c197bf3500f09297d29239c15f878fab /shared/memzip/make-memzip.py
parent925878b2f83e09987bd1688746b92898ee8bbcac (diff)
downloadmicropython-4d546713ec8858cbf908de45de11cbfc46a20971.tar.gz
micropython-4d546713ec8858cbf908de45de11cbfc46a20971.zip
shared: Introduce new top-level dir and move 1st party lib code there.
This commit moves all first-party code developed for this project from lib/ to shared/, so that lib/ now only contains third-party code. The following directories are moved as-is from lib to shared: lib/libc -> shared/libc lib/memzip -> shared/memzip lib/netutils -> shared/netutils lib/timeutils -> shared/timeutils lib/upytesthelper -> shared/upytesthelper All files in lib/embed/ have been moved to shared/libc/. lib/mp-readline has been moved to shared/readline. lib/utils has been moved to shared/runtime, with the exception of lib/utils/printf.c which has been moved to shared/libc/printf.c. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'shared/memzip/make-memzip.py')
-rwxr-xr-xshared/memzip/make-memzip.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/shared/memzip/make-memzip.py b/shared/memzip/make-memzip.py
new file mode 100755
index 0000000000..9730f5e008
--- /dev/null
+++ b/shared/memzip/make-memzip.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+#
+# Takes a directory of files and zips them up (as uncompressed files).
+# This then gets converted into a C data structure which can be read
+# like a filesystem at runtime.
+#
+# This is somewhat like frozen modules in python, but allows arbitrary files
+# to be used.
+
+from __future__ import print_function
+
+import argparse
+import os
+import subprocess
+import sys
+import types
+
+def create_zip(zip_filename, zip_dir):
+ abs_zip_filename = os.path.abspath(zip_filename)
+ save_cwd = os.getcwd()
+ os.chdir(zip_dir)
+ if os.path.exists(abs_zip_filename):
+ os.remove(abs_zip_filename)
+ subprocess.check_call(['zip', '-0', '-r', '-D', abs_zip_filename, '.'])
+ os.chdir(save_cwd)
+
+def create_c_from_file(c_filename, zip_filename):
+ with open(zip_filename, 'rb') as zip_file:
+ with open(c_filename, 'wb') as c_file:
+ print('#include <stdint.h>', file=c_file)
+ print('', file=c_file)
+ print('const uint8_t memzip_data[] = {', file=c_file)
+ while True:
+ buf = zip_file.read(16)
+ if not buf:
+ break
+ print(' ', end='', file=c_file)
+ for byte in buf:
+ if type(byte) is types.StringType:
+ print(' 0x{:02x},'.format(ord(byte)), end='', file=c_file)
+ else:
+ print(' 0x{:02x},'.format(byte), end='', file=c_file)
+ print('', file=c_file)
+ print('};', file=c_file)
+
+def main():
+ parser = argparse.ArgumentParser(
+ prog='make-memzip.py',
+ usage='%(prog)s [options] [command]',
+ description='Generates a C source memzip file.'
+ )
+ parser.add_argument(
+ '-z', '--zip-file',
+ dest='zip_filename',
+ help='Specifies the name of the created zip file.',
+ default='memzip_files.zip'
+ )
+ parser.add_argument(
+ '-c', '--c-file',
+ dest='c_filename',
+ help='Specifies the name of the created C source file.',
+ default='memzip_files.c'
+ )
+ parser.add_argument(
+ dest='source_dir',
+ default='memzip_files'
+ )
+ args = parser.parse_args(sys.argv[1:])
+
+ print('args.zip_filename =', args.zip_filename)
+ print('args.c_filename =', args.c_filename)
+ print('args.source_dir =', args.source_dir)
+
+ create_zip(args.zip_filename, args.source_dir)
+ create_c_from_file(args.c_filename, args.zip_filename)
+
+if __name__ == "__main__":
+ main()
+