blob: 0fcb9eb6a33f4bc73690c7c88bf7f22ce7288280 [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__,
26 formatter_class=argparse.RawDescriptionHelpFormatter)
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030027
28 parser.add_argument("-f", "--file", required=True, help="Input file")
29 parser.add_argument("-g", "--gzip", action="store_true",
Anas Nashif72565532017-12-12 08:19:25 -050030 help="Compress the file using gzip before output")
Marc Herbert3061c922019-03-21 14:40:45 -070031 parser.add_argument("-t", "--gzip-mtime", type=int, default=0,
Ruslan Mstoia8e06552020-06-01 14:29:46 +030032 nargs='?', const=None,
Marc Herbert3061c922019-03-21 14:40:45 -070033 help="""mtime seconds in the gzip header.
34 Defaults to zero to keep builds deterministic. For
35 current date and time (= "now") use this option
36 without any value.""")
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030037 args = parser.parse_args()
38
Anas Nashif72565532017-12-12 08:19:25 -050039
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030040def get_nice_string(list_or_iterator):
Anas Nashif72565532017-12-12 08:19:25 -050041 return ", ".join("0x" + str(x) for x in list_or_iterator)
42
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030043
44def make_hex(chunk):
45 hexdata = codecs.encode(chunk, 'hex').decode("utf-8")
Anas Nashif72565532017-12-12 08:19:25 -050046 hexlist = map(''.join, zip(*[iter(hexdata)] * 2))
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030047 print(get_nice_string(hexlist) + ',')
48
Anas Nashif72565532017-12-12 08:19:25 -050049
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030050def main():
51 parse_args()
52
53 if args.gzip:
Marc Herbert195195a2019-03-21 14:24:00 -070054 with io.BytesIO() as content:
55 with open(args.file, 'rb') as fg:
56 with gzip.GzipFile(fileobj=content, mode='w',
Marc Herbert3061c922019-03-21 14:40:45 -070057 mtime=args.gzip_mtime,
Marc Herbert195195a2019-03-21 14:24:00 -070058 compresslevel=9) as gz_obj:
59 gz_obj.write(fg.read())
60
61 content.seek(0)
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030062 for chunk in iter(lambda: content.read(8), b''):
63 make_hex(chunk)
64 else:
65 with open(args.file, "rb") as fp:
66 for chunk in iter(lambda: fp.read(8), b''):
67 make_hex(chunk)
68
Anas Nashif72565532017-12-12 08:19:25 -050069
Jukka Rissanen0ff4c252017-09-13 10:43:30 +030070if __name__ == "__main__":
71 main()