blob: 2bb967cf29da025c34c55db61967b3fafe2f2999 [file] [log] [blame]
Jukka Rissanen0ff4c252017-09-13 10:43:30 +03001#!/usr/bin/env python3
2#
3# Copyright (c) 2017 Intel Corporation
4#
5# SPDX-License-Identifier: Apache-2.0
6
Ruslan Mstoi1bae7672020-06-01 12:30:19 +03007
8"""Convert a file to a list of hex characters
9
10The list of hex characters can then be included to a source file. Optionally,
11the output can be compressed.
12
13"""
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030014
15import argparse
16import codecs
17import gzip
18import io
19
Anas Nashif72565532017-12-12 08:19:25 -050020
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030021def parse_args():
22 global args
23
Anas Nashif72565532017-12-12 08:19:25 -050024 parser = argparse.ArgumentParser(
25 description=__doc__,
Jamie McCraeec704442023-01-04 16:08:36 +000026 formatter_class=argparse.RawDescriptionHelpFormatter, allow_abbrev=False)
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030027
28 parser.add_argument("-f", "--file", required=True, help="Input file")
Pieter De Gendtae8c7242023-09-13 16:22:42 +020029 parser.add_argument("-o", "--offset", type=lambda x: int(x, 0), default=0,
30 help="Byte offset in the input file")
31 parser.add_argument("-l", "--length", type=lambda x: int(x, 0), default=-1,
32 help="""Length in bytes to read from the input file.
33 Defaults to reading till the end of the input file.""")
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030034 parser.add_argument("-g", "--gzip", action="store_true",
Anas Nashif72565532017-12-12 08:19:25 -050035 help="Compress the file using gzip before output")
Marc Herbert3061c922019-03-21 14:40:45 -070036 parser.add_argument("-t", "--gzip-mtime", type=int, default=0,
Ruslan Mstoia8e06552020-06-01 14:29:46 +030037 nargs='?', const=None,
Marc Herbert3061c922019-03-21 14:40:45 -070038 help="""mtime seconds in the gzip header.
39 Defaults to zero to keep builds deterministic. For
40 current date and time (= "now") use this option
41 without any value.""")
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030042 args = parser.parse_args()
43
Anas Nashif72565532017-12-12 08:19:25 -050044
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030045def get_nice_string(list_or_iterator):
Anas Nashif72565532017-12-12 08:19:25 -050046 return ", ".join("0x" + str(x) for x in list_or_iterator)
47
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030048
49def make_hex(chunk):
50 hexdata = codecs.encode(chunk, 'hex').decode("utf-8")
Anas Nashif72565532017-12-12 08:19:25 -050051 hexlist = map(''.join, zip(*[iter(hexdata)] * 2))
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030052 print(get_nice_string(hexlist) + ',')
53
Anas Nashif72565532017-12-12 08:19:25 -050054
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030055def main():
56 parse_args()
57
58 if args.gzip:
Marc Herbert195195a2019-03-21 14:24:00 -070059 with io.BytesIO() as content:
60 with open(args.file, 'rb') as fg:
Pieter De Gendtae8c7242023-09-13 16:22:42 +020061 fg.seek(args.offset)
Marc Herbert195195a2019-03-21 14:24:00 -070062 with gzip.GzipFile(fileobj=content, mode='w',
Marc Herbert3061c922019-03-21 14:40:45 -070063 mtime=args.gzip_mtime,
Marc Herbert195195a2019-03-21 14:24:00 -070064 compresslevel=9) as gz_obj:
Pieter De Gendtae8c7242023-09-13 16:22:42 +020065 gz_obj.write(fg.read(args.length))
Marc Herbert195195a2019-03-21 14:24:00 -070066
67 content.seek(0)
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030068 for chunk in iter(lambda: content.read(8), b''):
69 make_hex(chunk)
70 else:
71 with open(args.file, "rb") as fp:
Pieter De Gendtae8c7242023-09-13 16:22:42 +020072 fp.seek(args.offset)
73 if args.length < 0:
74 for chunk in iter(lambda: fp.read(8), b''):
75 make_hex(chunk)
76 else:
77 remainder = args.length
78 for chunk in iter(lambda: fp.read(min(8, remainder)), b''):
79 make_hex(chunk)
80 remainder = remainder - len(chunk)
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030081
Anas Nashif72565532017-12-12 08:19:25 -050082
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030083if __name__ == "__main__":
84 main()