mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-23 10:36:26 +08:00
Add binpack coarse shuffle tool.
This commit is contained in:
69
script/shuffle_binpack.py
Normal file
69
script/shuffle_binpack.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import struct
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
def index_binpack(file):
|
||||||
|
print('Indexing...')
|
||||||
|
index = []
|
||||||
|
offset = 0
|
||||||
|
report_every = 100
|
||||||
|
prev_mib = -report_every
|
||||||
|
while file.peek():
|
||||||
|
chunk_header = file.read(8)
|
||||||
|
assert chunk_header[0:4] == b'BINP'
|
||||||
|
size = struct.unpack('<I', chunk_header[4:])[0]
|
||||||
|
file.seek(size, os.SEEK_CUR)
|
||||||
|
index.append((offset, size + 8))
|
||||||
|
offset += size + 8
|
||||||
|
|
||||||
|
mib = offset // 1024 // 1024
|
||||||
|
if mib // 100 != prev_mib // 100:
|
||||||
|
print('Indexed {} MiB'.format(mib))
|
||||||
|
prev_mib = mib
|
||||||
|
|
||||||
|
return index
|
||||||
|
|
||||||
|
def copy_binpack_indexed(in_file, index, out_file):
|
||||||
|
print('Copying...')
|
||||||
|
total_size = 0
|
||||||
|
report_every = 100
|
||||||
|
prev_mib = -report_every
|
||||||
|
for offset, size in index:
|
||||||
|
in_file.seek(offset, os.SEEK_SET)
|
||||||
|
data = in_file.read(size)
|
||||||
|
assert len(data) == size
|
||||||
|
out_file.write(data)
|
||||||
|
|
||||||
|
total_size += size
|
||||||
|
mib = total_size // 1024 // 1024
|
||||||
|
if mib // 100 != prev_mib // 100:
|
||||||
|
print('Copied {} MiB'.format(mib))
|
||||||
|
prev_mib = mib
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if len(sys.argv) < 3:
|
||||||
|
print('Usage: python shuffle_binpack.py infile outfile')
|
||||||
|
return
|
||||||
|
|
||||||
|
in_filename = sys.argv[1]
|
||||||
|
out_filename = sys.argv[2]
|
||||||
|
|
||||||
|
if (Path(out_filename).exists()):
|
||||||
|
print('Output path already exists. Please specify a path to a file that does not exist.')
|
||||||
|
return
|
||||||
|
|
||||||
|
in_file = open(in_filename, 'rb')
|
||||||
|
out_file = open(out_filename, 'wb')
|
||||||
|
|
||||||
|
index = index_binpack(in_file)
|
||||||
|
print('Shuffling...')
|
||||||
|
random.shuffle(index)
|
||||||
|
|
||||||
|
copy_binpack_indexed(in_file, index, out_file)
|
||||||
|
|
||||||
|
in_file.close()
|
||||||
|
out_file.close()
|
||||||
|
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user