summaryrefslogtreecommitdiffstatshomepage
path: root/scripts/build/complay.py
blob: 68681c72fbf303cd3c41882acde3a3e2415ba9bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
##
## license:BSD-3-Clause
## copyright-holders:Aaron Giles, Andrew Gardner

from __future__ import with_statement

import sys
import os
import zlib

if len(sys.argv) < 4:
    print('Usage:')
    print('  complay <source.lay> <output.h> <varname>')
    print('')
    sys.exit(0)

srcfile = sys.argv[1]
dstfile = sys.argv[2]
varname = sys.argv[3]
type = 'UINT8'

try:
    myfile = open(srcfile, 'rb')
except IOError:
    sys.stderr.write("Unable to open source file '%s'\n" % srcfile)
    sys.exit(-1)

byteCount = os.path.getsize(srcfile)
compsize = 0
compressiontype = 1

try:
    dst = open(dstfile,'w')
    dst.write('const %s %s_data[] =\n{\n\t' % ( type, varname))
    offs = 0
    with open(srcfile, "rb") as src:
        while True:
            chunk = src.read(byteCount)	
            if chunk:
                compchunk = zlib.compress(chunk, 9)
                compsize = len(compchunk)
                for b in compchunk:
                    # For Python 2.x compatibility.
                    b = ord(b)
                    dst.write('%d' % b)
                    offs += 1
                    if offs != compsize:
                        dst.write(',')
            else:
                break
            dst.write('\n\t')

    dst.write('\n};\n')

except IOError:
    sys.stderr.write("Unable to open output file '%s'\n" % dstfile)
    sys.exit(-1)

try:
    dst.write('extern const internal_layout %s;\n' % ( varname ))
    dst.write('const internal_layout %s = { \n\t' % ( varname ))
    dst.write('%d,%d,%d,%s_data\n' % ( byteCount, compsize, compressiontype, varname ))
    dst.write('\n};\n')


    dst.close()
except IOError:
    sys.stderr.write("Unable to open output file '%s'\n" % dstfile)
    sys.exit(-1)