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 | |
Ruslan Mstoi | 1bae767 | 2020-06-01 12:30:19 +0300 | [diff] [blame] | 7 | |
| 8 | """Convert a file to a list of hex characters |
| 9 | |
| 10 | The list of hex characters can then be included to a source file. Optionally, |
| 11 | the output can be compressed. |
| 12 | |
| 13 | """ |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 14 | |
| 15 | import argparse |
| 16 | import codecs |
| 17 | import gzip |
| 18 | import io |
| 19 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 20 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 21 | def parse_args(): |
| 22 | global args |
| 23 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 24 | parser = argparse.ArgumentParser( |
| 25 | description=__doc__, |
Jamie McCrae | ec70444 | 2023-01-04 16:08:36 +0000 | [diff] [blame] | 26 | formatter_class=argparse.RawDescriptionHelpFormatter, allow_abbrev=False) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 27 | |
| 28 | parser.add_argument("-f", "--file", required=True, help="Input file") |
Pieter De Gendt | ae8c724 | 2023-09-13 16:22:42 +0200 | [diff] [blame] | 29 | 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 Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 34 | parser.add_argument("-g", "--gzip", action="store_true", |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 35 | help="Compress the file using gzip before output") |
Marc Herbert | 3061c92 | 2019-03-21 14:40:45 -0700 | [diff] [blame] | 36 | parser.add_argument("-t", "--gzip-mtime", type=int, default=0, |
Ruslan Mstoi | a8e0655 | 2020-06-01 14:29:46 +0300 | [diff] [blame] | 37 | nargs='?', const=None, |
Marc Herbert | 3061c92 | 2019-03-21 14:40:45 -0700 | [diff] [blame] | 38 | 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 Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 42 | args = parser.parse_args() |
| 43 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 44 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 45 | def get_nice_string(list_or_iterator): |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 46 | return ", ".join("0x" + str(x) for x in list_or_iterator) |
| 47 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 48 | |
| 49 | def make_hex(chunk): |
| 50 | hexdata = codecs.encode(chunk, 'hex').decode("utf-8") |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 51 | hexlist = map(''.join, zip(*[iter(hexdata)] * 2)) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 52 | print(get_nice_string(hexlist) + ',') |
| 53 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 54 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 55 | def main(): |
| 56 | parse_args() |
| 57 | |
| 58 | if args.gzip: |
Marc Herbert | 195195a | 2019-03-21 14:24:00 -0700 | [diff] [blame] | 59 | with io.BytesIO() as content: |
| 60 | with open(args.file, 'rb') as fg: |
Pieter De Gendt | ae8c724 | 2023-09-13 16:22:42 +0200 | [diff] [blame] | 61 | fg.seek(args.offset) |
Marc Herbert | 195195a | 2019-03-21 14:24:00 -0700 | [diff] [blame] | 62 | with gzip.GzipFile(fileobj=content, mode='w', |
Marc Herbert | 3061c92 | 2019-03-21 14:40:45 -0700 | [diff] [blame] | 63 | mtime=args.gzip_mtime, |
Marc Herbert | 195195a | 2019-03-21 14:24:00 -0700 | [diff] [blame] | 64 | compresslevel=9) as gz_obj: |
Pieter De Gendt | ae8c724 | 2023-09-13 16:22:42 +0200 | [diff] [blame] | 65 | gz_obj.write(fg.read(args.length)) |
Marc Herbert | 195195a | 2019-03-21 14:24:00 -0700 | [diff] [blame] | 66 | |
| 67 | content.seek(0) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 68 | 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 Gendt | ae8c724 | 2023-09-13 16:22:42 +0200 | [diff] [blame] | 72 | 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 Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 81 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 82 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 83 | if __name__ == "__main__": |
| 84 | main() |