2010-09-02 16:46:46 +08:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import string
|
|
|
|
|
|
|
|
basename = ''
|
2010-10-31 21:52:58 +08:00
|
|
|
output = ''
|
2010-09-02 16:46:46 +08:00
|
|
|
|
|
|
|
def mkromfs_output(out):
|
2010-10-31 21:52:58 +08:00
|
|
|
# print '%s' % out,
|
|
|
|
output.write(out)
|
2010-09-02 16:46:46 +08:00
|
|
|
|
|
|
|
def mkromfs_file(filename, arrayname):
|
2010-10-31 21:52:58 +08:00
|
|
|
f = file(filename, "rb")
|
2010-09-02 16:46:46 +08:00
|
|
|
arrayname = arrayname.replace('.', '_')
|
2010-12-13 07:37:56 +08:00
|
|
|
arrayname = arrayname.replace('-', '_')
|
2010-09-02 16:46:46 +08:00
|
|
|
mkromfs_output('const static unsigned char %s[] = {\n' % arrayname)
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
while True:
|
|
|
|
byte = f.read(1)
|
2010-10-31 21:52:58 +08:00
|
|
|
|
|
|
|
if len(byte) != 1:
|
2010-09-02 16:46:46 +08:00
|
|
|
break
|
2010-10-31 21:52:58 +08:00
|
|
|
|
2010-09-02 16:46:46 +08:00
|
|
|
mkromfs_output('0x%02x,' % ord(byte))
|
|
|
|
|
|
|
|
count = count + 1
|
|
|
|
if count == 16:
|
|
|
|
count = 0
|
|
|
|
mkromfs_output('\n')
|
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
if count == 0:
|
|
|
|
mkromfs_output('};\n\n')
|
|
|
|
else:
|
|
|
|
mkromfs_output('\n};\n\n')
|
|
|
|
|
2010-09-02 16:46:46 +08:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
def mkromfs_dir(dirname, is_root = False):
|
|
|
|
list = os.listdir(dirname)
|
|
|
|
path = os.path.abspath(dirname)
|
|
|
|
|
|
|
|
# make for directory
|
|
|
|
for item in list:
|
|
|
|
fullpath = os.path.join(path, item)
|
|
|
|
if os.path.isdir(fullpath):
|
2010-10-31 21:52:58 +08:00
|
|
|
# if it is an empty directory, ignore it
|
|
|
|
l = os.listdir(fullpath)
|
|
|
|
if len(l):
|
|
|
|
mkromfs_dir(fullpath)
|
2010-09-02 16:46:46 +08:00
|
|
|
|
|
|
|
# make for files
|
|
|
|
for item in list:
|
|
|
|
fullpath = os.path.join(path, item)
|
|
|
|
if os.path.isfile(fullpath):
|
|
|
|
subpath = fullpath[len(basename):]
|
|
|
|
array = subpath.split('\\')
|
|
|
|
arrayname = string.join(array, '_')
|
|
|
|
mkromfs_file(fullpath, arrayname)
|
|
|
|
|
|
|
|
subpath = path[len(basename):]
|
|
|
|
dir = subpath.split('\\')
|
|
|
|
direntname = string.join(dir, '_')
|
|
|
|
if is_root:
|
2010-10-31 21:52:58 +08:00
|
|
|
mkromfs_output('const struct romfs_dirent _root_dirent[] = {\n')
|
2010-09-02 16:46:46 +08:00
|
|
|
else:
|
|
|
|
mkromfs_output(('const static struct romfs_dirent %s[] = {\n' % direntname))
|
|
|
|
|
|
|
|
for item in list:
|
|
|
|
fullpath = os.path.join(path, item)
|
2010-10-31 21:52:58 +08:00
|
|
|
fn = fullpath[len(dirname):]
|
|
|
|
if fn[0] == '\\':
|
|
|
|
fn = fn[1:]
|
|
|
|
fn = fn.replace('\\', '/')
|
2010-09-02 16:46:46 +08:00
|
|
|
|
|
|
|
subpath = fullpath[len(basename):]
|
|
|
|
items = subpath.split('\\')
|
|
|
|
item_name = string.join(items, '_')
|
|
|
|
item_name = item_name.replace('.', '_')
|
2010-12-13 07:37:56 +08:00
|
|
|
item_name = item_name.replace('-', '_')
|
2010-09-02 16:46:46 +08:00
|
|
|
subpath = subpath.replace('\\', '/')
|
|
|
|
if subpath[0] == '/':
|
|
|
|
subpath = subpath[1:]
|
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
if not os.path.isfile(fullpath):
|
|
|
|
l = os.listdir(fullpath)
|
|
|
|
if len(l):
|
2011-02-22 08:14:23 +08:00
|
|
|
mkromfs_output(('\t{ROMFS_DIRENT_DIR, "%s", (rt_uint8_t*) %s, sizeof(%s)/sizeof(%s[0])},\n' % (fn, item_name, item_name, item_name)))
|
2010-10-31 21:52:58 +08:00
|
|
|
else:
|
|
|
|
mkromfs_output(('\t{ROMFS_DIRENT_DIR, "%s", RT_NULL, 0},\n' % fn))
|
|
|
|
|
|
|
|
for item in list:
|
|
|
|
fullpath = os.path.join(path, item)
|
|
|
|
fn = fullpath[len(dirname):]
|
|
|
|
if fn[0] == '\\':
|
|
|
|
fn = fn[1:]
|
|
|
|
fn = fn.replace('\\', '/')
|
|
|
|
|
|
|
|
subpath = fullpath[len(basename):]
|
|
|
|
items = subpath.split('\\')
|
|
|
|
item_name = string.join(items, '_')
|
|
|
|
item_name = item_name.replace('.', '_')
|
2010-12-13 07:37:56 +08:00
|
|
|
item_name = item_name.replace('-', '_')
|
2010-10-31 21:52:58 +08:00
|
|
|
subpath = subpath.replace('\\', '/')
|
|
|
|
if subpath[0] == '/':
|
|
|
|
subpath = subpath[1:]
|
|
|
|
|
2010-09-02 16:46:46 +08:00
|
|
|
if os.path.isfile(fullpath):
|
|
|
|
mkromfs_output(('\t{ROMFS_DIRENT_FILE, "%s", %s, sizeof(%s)},\n' % (fn, item_name, item_name)))
|
|
|
|
|
|
|
|
mkromfs_output('};\n\n')
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
try:
|
|
|
|
basename = os.path.abspath(sys.argv[1])
|
|
|
|
filename = os.path.abspath(sys.argv[2])
|
|
|
|
except IndexError:
|
|
|
|
print "Usage: %s <dirname> <filename>" % sys.argv[0]
|
|
|
|
raise SystemExit
|
|
|
|
|
2010-10-31 21:52:58 +08:00
|
|
|
output = file(filename, 'wt')
|
2010-09-02 16:46:46 +08:00
|
|
|
mkromfs_output("#include <dfs_romfs.h>\n\n")
|
|
|
|
mkromfs_dir(basename, is_root = True)
|
2010-10-31 21:52:58 +08:00
|
|
|
|
2011-02-22 08:14:23 +08:00
|
|
|
mkromfs_output("const struct romfs_dirent romfs_root = {ROMFS_DIRENT_DIR, \"/\", (rt_uint8_t*) _root_dirent, sizeof(_root_dirent)/sizeof(_root_dirent[0])};\n\n")
|