2019-10-04 17:16:12 -07:00
|
|
|
#!/usr/bin/env python3
|
2021-12-30 15:06:42 -08:00
|
|
|
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2019-10-04 17:16:12 -07:00
|
|
|
#
|
2019-10-15 12:33:16 -07:00
|
|
|
# This source code is licensed under the MIT license found in the
|
|
|
|
|
# LICENSE file in the root directory of this source tree.
|
2019-10-04 17:16:12 -07:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
This module exists so that a Windows build can have access to functionality
|
2020-10-14 13:18:35 -07:00
|
|
|
like xxd -i.
|
|
|
|
|
This script is intended to be compliant with both py2 and py3.
|
2019-10-04 17:16:12 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import argparse
|
2020-10-14 13:18:35 -07:00
|
|
|
import sys
|
2019-10-04 17:16:12 -07:00
|
|
|
from os import path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# This is about 80 characters long (4 characters per byte + comma + space)
|
|
|
|
|
BYTES_PER_LINE = 12
|
2020-10-14 13:18:35 -07:00
|
|
|
IS_PY3 = sys.version_info > (3, 0)
|
2019-10-04 17:16:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
parser.add_argument("file")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
# Ensure the file exists before writing out anything
|
|
|
|
|
if not path.exists(args.file):
|
2020-10-14 13:18:35 -07:00
|
|
|
raise Exception('File "{}" doesn\'t exist'.format(args.file))
|
2019-10-04 17:16:12 -07:00
|
|
|
|
|
|
|
|
with open(args.file, "rb") as f:
|
|
|
|
|
# Could read in chunks instead for extra performance, but this script
|
|
|
|
|
# isn't meant to be used on gigantic files.
|
|
|
|
|
file_as_bytes = f.read()
|
|
|
|
|
|
|
|
|
|
# Make groups of bytes that fit in a single line
|
|
|
|
|
lines = [
|
|
|
|
|
file_as_bytes[i : i + BYTES_PER_LINE]
|
|
|
|
|
for i in range(0, len(file_as_bytes), BYTES_PER_LINE)
|
|
|
|
|
]
|
2020-10-14 13:18:35 -07:00
|
|
|
# in python2, byte is same as str and interating it yield strs.
|
|
|
|
|
# in python3, byte is distinct and interating it yield ints.
|
|
|
|
|
print(
|
|
|
|
|
",\n".join(
|
|
|
|
|
", ".join("0x{:02x}".format(b if IS_PY3 else ord(b)) for b in l)
|
|
|
|
|
for l in lines
|
|
|
|
|
)
|
|
|
|
|
)
|
2019-10-04 17:16:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|