mkbundle: another StringIO fix, python3 conversion, fixes #2469

This commit is contained in:
Jaroslav Kysela 2014-11-11 22:43:37 +01:00
parent e0f8ee4669
commit afdd605341
2 changed files with 45 additions and 42 deletions

View file

@ -83,7 +83,7 @@ BUNDLE_FLAGS = ${BUNDLE_FLAGS-yes}
# Binaries/Scripts
#
MKBUNDLE = $(PYTHON) $(ROOTDIR)/support/mkbundle
MKBUNDLE = $(PYTHON)3 $(ROOTDIR)/support/mkbundle
#
# Debug/Output

View file

@ -6,9 +6,9 @@
import os, sys, re
import gzip
try:
from io import StringIO
except ImportError:
from cStringIO import StringIO
from cStringIO import StringIO as BytesIO
except:
from io import BytesIO
from optparse import OptionParser
# Add reverse path split
@ -38,7 +38,7 @@ if opts.output:
depf = None
if opts.deps:
depf = open(opts.deps, 'w')
print >>depf, '%s: \\' % opts.output
depf.write('%s: \\\n' % opts.output)
# Build heirarchy
root = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..'))
@ -68,17 +68,18 @@ def output_file ( path, name, idx, next = -1 ):
p = os.path.join(root, path, name);
# Dep file
if depf: print >>depf, ' %s\\' % p
if depf:
depf.write(' %s\\\n' % p)
# First the data
print >>outf, '// FILE : %s %s %d %d' % (path, name, idx, next)
print >>outf, 'static const uint8_t filebundle_data_%06d[] = {' % idx,
outf.write('/* FILE : %s %s %d %d */\n' % (path, name, idx, next))
outf.write('static const uint8_t filebundle_data_%06d[] = {' % idx)
o = -1
d = open(p, 'rb').read()
if opts.gzip:
o = len(d)
l = opts.gzlevel
t = StringIO()
t = BytesIO()
if l < 0: l = 1
if l > 9: l = 9
z = gzip.GzipFile(filename=name, mode='w', compresslevel=l, fileobj=t)
@ -88,40 +89,42 @@ def output_file ( path, name, idx, next = -1 ):
t.close()
i = 0
for b in d:
if not (i % 12): print >>outf, '\n ',
print >>outf, '0x%02x,' % ord(b),
if not (i % 12): outf.write('\n ')
if type(b) == str:
b = ord(b)
outf.write('0x%02x,' % b)
i = i + 1
print >>outf, ''
print >>outf, '};'
outf.write('\n')
outf.write('};\n')
print >>outf, 'static filebundle_entry_t filebundle_entry_%06d = {' % idx
print >>outf, ' .type = FB_FILE,'
print >>outf, ' .name = "%s",' % name
print >>outf, ' .next = %s,' % n
print >>outf, ' {'
print >>outf, ' .f.size = %d,' % len(d)
print >>outf, ' .f.orig = %d,' % o
print >>outf, ' .f.data = filebundle_data_%06d' % idx
print >>outf, ' },'
print >>outf, '};'
print >>outf, ''
outf.write('static filebundle_entry_t filebundle_entry_%06d = {\n' % idx)
outf.write(' .type = FB_FILE,\n')
outf.write(' .name = "%s",\n' % name)
outf.write(' .next = %s,\n' % n)
outf.write(' {\n')
outf.write(' .f.size = %d,\n' % len(d))
outf.write(' .f.orig = %d,\n' % o)
outf.write(' .f.data = filebundle_data_%06d\n' % idx)
outf.write(' },\n')
outf.write('};\n')
outf.write('\n')
# Output a directory
def output_dir ( path, name, idx, child, count, next = -1 ):
n = 'NULL'
if next >= 0: n = '&filebundle_entry_%06d' % next
print >>outf, '// DIR: %s %s %d %d %d %d'\
% (path, name, idx, child, count, next)
print >>outf, 'static filebundle_entry_t filebundle_entry_%06d = {' % idx
print >>outf, ' .type = FB_DIR,'
print >>outf, ' .name = "%s",' % name
print >>outf, ' .next = %s,' % n
print >>outf, ' {'
print >>outf, ' .d.count = %d,' % count
print >>outf, ' .d.child = &filebundle_entry_%06d' % child
print >>outf, ' },'
print >>outf, '};'
print >>outf, ''
outf.write('/* DIR: %s %s %d %d %d %d */\n' \
% (path, name, idx, child, count, next))
outf.write('static filebundle_entry_t filebundle_entry_%06d = {\n' % idx)
outf.write(' .type = FB_DIR,\n')
outf.write(' .name = "%s",\n' % name)
outf.write(' .next = %s,\n' % n)
outf.write(' {\n')
outf.write(' .d.count = %d,\n' % count)
outf.write(' .d.child = &filebundle_entry_%06d\n' % child)
outf.write(' },\n')
outf.write('};\n')
outf.write('\n')
# Create output
def add_entry ( ents, path = "", name = "", idx = -1, next = -1 ):
@ -154,14 +157,14 @@ def add_entry ( ents, path = "", name = "", idx = -1, next = -1 ):
return idx
# Output header
print >>outf, '// Auto-generated - DO NOT EDIT'
print >>outf, '// COMMAND: [%s]' % (' '.join(sys.argv))
print >>outf, ''
print >>outf, '#include "filebundle.h"'
print >>outf, ''
outf.write('/* Auto-generated - DO NOT EDIT */\n')
outf.write('/* COMMAND: [%s] */\n' % (' '.join(sys.argv)))
outf.write('\n')
outf.write('#include "filebundle.h"\n')
outf.write('\n')
# Output entries
idx = add_entry(ents)
# Output top link
print >>outf, 'filebundle_entry_t *filebundle_root = &filebundle_entry_%06d;' % idx
outf.write('filebundle_entry_t *filebundle_root = &filebundle_entry_%06d;\n' % idx)