mkromfs.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import sys
  2. import os
  3. import string
  4. basename = ''
  5. output = ''
  6. sep = os.sep
  7. def mkromfs_output(out):
  8. # print '%s' % out,
  9. output.write(out)
  10. def mkromfs_file(filename, arrayname):
  11. f = file(filename, "rb")
  12. arrayname = arrayname.replace('.', '_')
  13. arrayname = arrayname.replace('-', '_')
  14. mkromfs_output('const static unsigned char %s[] = {\n' % arrayname)
  15. count = 0
  16. while True:
  17. byte = f.read(1)
  18. if len(byte) != 1:
  19. break
  20. mkromfs_output('0x%02x,' % ord(byte))
  21. count = count + 1
  22. if count == 16:
  23. count = 0
  24. mkromfs_output('\n')
  25. if count == 0:
  26. mkromfs_output('};\n\n')
  27. else:
  28. mkromfs_output('\n};\n\n')
  29. f.close()
  30. def mkromfs_dir(dirname, is_root = False):
  31. list = os.listdir(dirname)
  32. path = os.path.abspath(dirname)
  33. # make for directory
  34. for item in list:
  35. fullpath = os.path.join(path, item)
  36. if os.path.isdir(fullpath):
  37. # if it is an empty directory, ignore it
  38. l = os.listdir(fullpath)
  39. if len(l):
  40. mkromfs_dir(fullpath)
  41. # make for files
  42. for item in list:
  43. fullpath = os.path.join(path, item)
  44. if os.path.isfile(fullpath):
  45. subpath = fullpath[len(basename):]
  46. array = subpath.split(sep)
  47. arrayname = string.join(array, '_')
  48. mkromfs_file(fullpath, arrayname)
  49. subpath = path[len(basename):]
  50. dir = subpath.split(sep)
  51. direntname = string.join(dir, '_')
  52. if is_root:
  53. mkromfs_output('const struct romfs_dirent _root_dirent[] = {\n')
  54. else:
  55. mkromfs_output(('const static struct romfs_dirent %s[] = {\n' % direntname))
  56. for item in list:
  57. fullpath = os.path.join(path, item)
  58. fn = fullpath[len(dirname):]
  59. if fn[0] == sep:
  60. fn = fn[1:]
  61. fn = fn.replace('\\', '/')
  62. subpath = fullpath[len(basename):]
  63. items = subpath.split(sep)
  64. item_name = string.join(items, '_')
  65. item_name = item_name.replace('.', '_')
  66. item_name = item_name.replace('-', '_')
  67. subpath = subpath.replace('\\', '/')
  68. if subpath[0] == '/':
  69. subpath = subpath[1:]
  70. if not os.path.isfile(fullpath):
  71. l = os.listdir(fullpath)
  72. if len(l):
  73. mkromfs_output(('\t{ROMFS_DIRENT_DIR, "%s", (rt_uint8_t*) %s, sizeof(%s)/sizeof(%s[0])},\n' % (fn, item_name, item_name, item_name)))
  74. else:
  75. mkromfs_output(('\t{ROMFS_DIRENT_DIR, "%s", RT_NULL, 0},\n' % fn))
  76. for item in list:
  77. fullpath = os.path.join(path, item)
  78. fn = fullpath[len(dirname):]
  79. if fn[0] == sep:
  80. fn = fn[1:]
  81. fn = fn.replace('\\', '/')
  82. subpath = fullpath[len(basename):]
  83. items = subpath.split(sep)
  84. item_name = string.join(items, '_')
  85. item_name = item_name.replace('.', '_')
  86. item_name = item_name.replace('-', '_')
  87. subpath = subpath.replace('\\', '/')
  88. if subpath[0] == '/':
  89. subpath = subpath[1:]
  90. if os.path.isfile(fullpath):
  91. mkromfs_output(('\t{ROMFS_DIRENT_FILE, "%s", %s, sizeof(%s)},\n' % (fn, item_name, item_name)))
  92. mkromfs_output('};\n\n')
  93. if __name__ == "__main__":
  94. try:
  95. basename = os.path.abspath(sys.argv[1])
  96. filename = os.path.abspath(sys.argv[2])
  97. except IndexError:
  98. print "Usage: %s <dirname> <filename>" % sys.argv[0]
  99. raise SystemExit
  100. output = file(filename, 'wt')
  101. mkromfs_output("#include <dfs_romfs.h>\n\n")
  102. mkromfs_dir(basename, is_root = True)
  103. 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")