From e5f05fa2b9f60503e121102ba94390e6974ced1e Mon Sep 17 00:00:00 2001 From: Tomasz Sobczyk Date: Mon, 7 Sep 2020 14:32:05 +0200 Subject: [PATCH] Add a script to extract a contiguous range of entries from a .bin file. --- script/extract_bin.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 script/extract_bin.py diff --git a/script/extract_bin.py b/script/extract_bin.py new file mode 100644 index 00000000..9574aa17 --- /dev/null +++ b/script/extract_bin.py @@ -0,0 +1,42 @@ +import sys + +ENTRY_SIZE = 40 +NUM_ENTRIES_IN_CHUNK = 1024*1024 + +def copy(infile, outfile, count, times): + if times > 1: + outfile.write(infile.read(count*ENTRY_SIZE)*times) + else: + offset = 0 + while offset < count: + to_read = NUM_ENTRIES_IN_CHUNK if offset + NUM_ENTRIES_IN_CHUNK <= count else count - offset + + outfile.write(infile.read(to_read*ENTRY_SIZE)) + + offset += NUM_ENTRIES_IN_CHUNK + +def work(): + filename = sys.argv[1] + offset = int(sys.argv[2]) + count = int(sys.argv[3]) + times = int(sys.argv[4]) if len(sys.argv) >= 5 else 1 + + with open(filename, 'rb') as infile: + infile.seek(offset * ENTRY_SIZE) + filename_parts = filename.split('.') + out_path = '.'.join(filename_parts[:-1]) + '_' + str(offset) + '_' + str(count) + '_' + str(times) + '.' + filename_parts[-1] + with open(out_path, 'wb') as outfile: + copy(infile, outfile, count, times) + +def show_help(): + print('Usage: python extract_bin.py filename offset count [times]') + print('filename - the path to the .bin file to process') + print('offset - the number of sfens to skip') + print('count - the number of sfens to extract') + print('times - the number of times to repeat the extracted sfens. Default = 1') + print('The result is saved in a new file named `filename.stem`_`offset`_`count`_`times`.bin') + +if len(sys.argv) < 4: + show_help() +else: + work()