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__, |
| 26 | formatter_class=argparse.RawDescriptionHelpFormatter) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 27 | |
| 28 | parser.add_argument("-f", "--file", required=True, help="Input file") |
| 29 | parser.add_argument("-g", "--gzip", action="store_true", |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 30 | help="Compress the file using gzip before output") |
Marc Herbert | 3061c92 | 2019-03-21 14:40:45 -0700 | [diff] [blame] | 31 | parser.add_argument("-t", "--gzip-mtime", type=int, default=0, |
Ruslan Mstoi | a8e0655 | 2020-06-01 14:29:46 +0300 | [diff] [blame] | 32 | nargs='?', const=None, |
Marc Herbert | 3061c92 | 2019-03-21 14:40:45 -0700 | [diff] [blame] | 33 | 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 Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 37 | args = parser.parse_args() |
| 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 get_nice_string(list_or_iterator): |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 41 | return ", ".join("0x" + str(x) for x in list_or_iterator) |
| 42 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 43 | |
| 44 | def make_hex(chunk): |
| 45 | hexdata = codecs.encode(chunk, 'hex').decode("utf-8") |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 46 | hexlist = map(''.join, zip(*[iter(hexdata)] * 2)) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 47 | print(get_nice_string(hexlist) + ',') |
| 48 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 49 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 50 | def main(): |
| 51 | parse_args() |
| 52 | |
| 53 | if args.gzip: |
Marc Herbert | 195195a | 2019-03-21 14:24:00 -0700 | [diff] [blame] | 54 | with io.BytesIO() as content: |
| 55 | with open(args.file, 'rb') as fg: |
| 56 | with gzip.GzipFile(fileobj=content, mode='w', |
Marc Herbert | 3061c92 | 2019-03-21 14:40:45 -0700 | [diff] [blame] | 57 | mtime=args.gzip_mtime, |
Marc Herbert | 195195a | 2019-03-21 14:24:00 -0700 | [diff] [blame] | 58 | compresslevel=9) as gz_obj: |
| 59 | gz_obj.write(fg.read()) |
| 60 | |
| 61 | content.seek(0) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 62 | 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 Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 69 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 70 | if __name__ == "__main__": |
| 71 | main() |