Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2017 Intel Corporation |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | |
| 7 | # This converts a file to a list of hex characters which can then |
| 8 | # be included to a source file. |
| 9 | # Optionally, the output can be compressed if needed. |
| 10 | |
| 11 | import argparse |
| 12 | import codecs |
| 13 | import gzip |
| 14 | import io |
| 15 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 16 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 17 | def parse_args(): |
| 18 | global args |
| 19 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 20 | parser = argparse.ArgumentParser( |
| 21 | description=__doc__, |
| 22 | formatter_class=argparse.RawDescriptionHelpFormatter) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 23 | |
| 24 | parser.add_argument("-f", "--file", required=True, help="Input file") |
| 25 | parser.add_argument("-g", "--gzip", action="store_true", |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 26 | help="Compress the file using gzip before output") |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 27 | args = parser.parse_args() |
| 28 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 29 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 30 | def get_nice_string(list_or_iterator): |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 31 | return ", ".join("0x" + str(x) for x in list_or_iterator) |
| 32 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 33 | |
| 34 | def make_hex(chunk): |
| 35 | hexdata = codecs.encode(chunk, 'hex').decode("utf-8") |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 36 | hexlist = map(''.join, zip(*[iter(hexdata)] * 2)) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 37 | print(get_nice_string(hexlist) + ',') |
| 38 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 39 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 40 | def main(): |
| 41 | parse_args() |
| 42 | |
| 43 | if args.gzip: |
Marc Herbert | 195195a | 2019-03-21 14:24:00 -0700 | [diff] [blame] | 44 | with io.BytesIO() as content: |
| 45 | with open(args.file, 'rb') as fg: |
| 46 | with gzip.GzipFile(fileobj=content, mode='w', |
| 47 | compresslevel=9) as gz_obj: |
| 48 | gz_obj.write(fg.read()) |
| 49 | |
| 50 | content.seek(0) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 51 | for chunk in iter(lambda: content.read(8), b''): |
| 52 | make_hex(chunk) |
| 53 | else: |
| 54 | with open(args.file, "rb") as fp: |
| 55 | for chunk in iter(lambda: fp.read(8), b''): |
| 56 | make_hex(chunk) |
| 57 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 58 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 59 | if __name__ == "__main__": |
| 60 | main() |