Skip to content

Commit

Permalink
tools: fix Python 3 issues in inspector_protocol
Browse files Browse the repository at this point in the history
PR-URL: nodejs#29296
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
(cherry picked from commit 4662f67)
  • Loading branch information
cclauss authored and BaochengSu committed Oct 9, 2020
1 parent c9586ad commit 51e3fb5
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions tools/compress_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

try:
xrange # Python 2
PY2 = True
except NameError:
PY2 = False
xrange = range # Python 3


if __name__ == '__main__':
fp = open(sys.argv[1])
obj = json.load(fp)
text = json.dumps(obj, separators=(',', ':'))
with open(sys.argv[1]) as fp:
obj = json.load(fp)
text = json.dumps(obj, separators=(',', ':')).encode('utf-8')
data = zlib.compress(text, zlib.Z_BEST_COMPRESSION)

# To make decompression a little easier, we prepend the compressed data
Expand All @@ -24,8 +26,8 @@

step = 20
slices = (data[i:i+step] for i in xrange(0, len(data), step))
slices = [','.join(str(ord(c)) for c in s) for s in slices]
slices = [','.join(str(ord(c) if PY2 else c) for c in s) for s in slices]
text = ',\n'.join(slices)

fp = open(sys.argv[2], 'w')
fp.write(text)
with open(sys.argv[2], 'w') as fp:
fp.write(text)

0 comments on commit 51e3fb5

Please sign in to comment.