tools: fix Python 3 issues in inspector_protocol
PR-URL: https://github.com/nodejs/node/pull/29296 Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
parent
b2cce432f3
commit
4662f67e38
@ -7,14 +7,16 @@ import zlib
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
xrange # Python 2
|
xrange # Python 2
|
||||||
|
PY2 = True
|
||||||
except NameError:
|
except NameError:
|
||||||
|
PY2 = False
|
||||||
xrange = range # Python 3
|
xrange = range # Python 3
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
fp = open(sys.argv[1])
|
with open(sys.argv[1]) as fp:
|
||||||
obj = json.load(fp)
|
obj = json.load(fp)
|
||||||
text = json.dumps(obj, separators=(',', ':'))
|
text = json.dumps(obj, separators=(',', ':')).encode('utf-8')
|
||||||
data = zlib.compress(text, zlib.Z_BEST_COMPRESSION)
|
data = zlib.compress(text, zlib.Z_BEST_COMPRESSION)
|
||||||
|
|
||||||
# To make decompression a little easier, we prepend the compressed data
|
# To make decompression a little easier, we prepend the compressed data
|
||||||
@ -24,8 +26,8 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
step = 20
|
step = 20
|
||||||
slices = (data[i:i+step] for i in xrange(0, len(data), step))
|
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)
|
text = ',\n'.join(slices)
|
||||||
|
|
||||||
fp = open(sys.argv[2], 'w')
|
with open(sys.argv[2], 'w') as fp:
|
||||||
fp.write(text)
|
fp.write(text)
|
||||||
|
@ -21,14 +21,12 @@ def main(argv):
|
|||||||
parser.add_argument("json_file", help="The .json output file write.")
|
parser.add_argument("json_file", help="The .json output file write.")
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
file_name = os.path.normpath(args.pdl_file)
|
file_name = os.path.normpath(args.pdl_file)
|
||||||
input_file = open(file_name, "r")
|
with open(file_name, "r") as input_file:
|
||||||
pdl_string = input_file.read()
|
pdl_string = input_file.read()
|
||||||
protocol = pdl.loads(pdl_string, file_name, args.map_binary_to_string)
|
protocol = pdl.loads(pdl_string, file_name, args.map_binary_to_string)
|
||||||
input_file.close()
|
|
||||||
|
|
||||||
output_file = open(os.path.normpath(args.json_file), 'wb')
|
with open(os.path.normpath(args.json_file), 'w') as output_file:
|
||||||
json.dump(protocol, output_file, indent=4, separators=(',', ': '))
|
json.dump(protocol, output_file, indent=4, separators=(',', ': '))
|
||||||
output_file.close()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user