from __future__ import print_function import os import sys import glob from struct import * def jp(*paths): return os.path.normcase(os.path.join(paths[0], *paths[1:])) def opencreate(path, mode): path = os.path.normcase(path) dir = os.path.dirname(path) if dir != '' and not os.path.exists(dir): os.makedirs(dir) return open(path, mode) def build(directory, name): img = opencreate(name+'.img', 'wb') dirf = opencreate(name+'.dir', 'wb') tablepos = img.tell() curpos = 0 dir = [] for root, dirs, files in os.walk(directory): for file in files: fname = os.path.join(root, file) print(fname, file) f = open(fname, 'rb') data = f.read() f.close() img.write(data) size = (len(data)+2047)//2048 dir.append((curpos, size, file)) curpos += size img.seek(curpos*2048, 0) for entry in dir: dirf.write(pack('IHH24s', entry[0], entry[1], 0, entry[2].encode())) img.close() dirf.close() if len(sys.argv) > 2: directory = sys.argv[2] imagepath = sys.argv[1] build(directory, imagepath)