11cb0ef41Sopenharmony_ci#!/usr/bin/env python
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciimport json
41cb0ef41Sopenharmony_ciimport struct
51cb0ef41Sopenharmony_ciimport sys
61cb0ef41Sopenharmony_ciimport zlib
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_citry:
91cb0ef41Sopenharmony_ci    xrange          # Python 2
101cb0ef41Sopenharmony_ci    PY2 = True
111cb0ef41Sopenharmony_ciexcept NameError:
121cb0ef41Sopenharmony_ci    PY2 = False
131cb0ef41Sopenharmony_ci    xrange = range  # Python 3
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciif __name__ == '__main__':
171cb0ef41Sopenharmony_ci  with open(sys.argv[1]) as fp:
181cb0ef41Sopenharmony_ci    obj = json.load(fp)
191cb0ef41Sopenharmony_ci  text = json.dumps(obj, separators=(',', ':')).encode('utf-8')
201cb0ef41Sopenharmony_ci  data = zlib.compress(text, zlib.Z_BEST_COMPRESSION)
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  # To make decompression a little easier, we prepend the compressed data
231cb0ef41Sopenharmony_ci  # with the size of the uncompressed data as a 24 bits BE unsigned integer.
241cb0ef41Sopenharmony_ci  assert len(text) < 1 << 24, 'Uncompressed JSON must be < 16 MiB.'
251cb0ef41Sopenharmony_ci  data = struct.pack('>I', len(text))[1:4] + data
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  step = 20
281cb0ef41Sopenharmony_ci  slices = (data[i:i+step] for i in xrange(0, len(data), step))
291cb0ef41Sopenharmony_ci  slices = [','.join(str(ord(c) if PY2 else c) for c in s) for s in slices]
301cb0ef41Sopenharmony_ci  text = ',\n'.join(slices)
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  with open(sys.argv[2], 'w') as fp:
331cb0ef41Sopenharmony_ci    fp.write(text)
34