summaryrefslogtreecommitdiff
path: root/components/dfs/filesystems/romfs/mkromfs.py
blob: a84458e892fdf33c6619c31e0b2bf45e5847edcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import sys
import os
import string

basename = ''
output = ''

def mkromfs_output(out):
    # print '%s' % out,
    output.write(out)

def mkromfs_file(filename, arrayname):
    f = file(filename, "rb")
    arrayname = arrayname.replace('.', '_')
    arrayname = arrayname.replace('-', '_')
    mkromfs_output('const static unsigned char %s[] = {\n' % arrayname)

    count = 0
    while True:
        byte = f.read(1)

        if len(byte) != 1:
            break

        mkromfs_output('0x%02x,' % ord(byte))
        
        count = count + 1
        if count == 16:
            count = 0
            mkromfs_output('\n')
    
    if count == 0:
        mkromfs_output('};\n\n')
    else:
        mkromfs_output('\n};\n\n')

    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):
            # if it is an empty directory, ignore it
            l = os.listdir(fullpath)
            if len(l):
                mkromfs_dir(fullpath)
    
    # 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:
        mkromfs_output('const struct romfs_dirent _root_dirent[] = {\n')
    else:
        mkromfs_output(('const static struct romfs_dirent %s[] = {\n' % direntname))
    
    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('.', '_')
        item_name = item_name.replace('-', '_')
        subpath = subpath.replace('\\', '/')
        if subpath[0] == '/':
            subpath = subpath[1:]

        if not os.path.isfile(fullpath):
            l = os.listdir(fullpath)
            if len(l):
                mkromfs_output(('\t{ROMFS_DIRENT_DIR, "%s", (rt_uint8_t*) %s, sizeof(%s)/sizeof(%s[0])},\n' % (fn, item_name, item_name, item_name)))
            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('.', '_')
        item_name = item_name.replace('-', '_')
        subpath = subpath.replace('\\', '/')
        if subpath[0] == '/':
            subpath = subpath[1:]
    
        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

    output = file(filename, 'wt')
    mkromfs_output("#include <dfs_romfs.h>\n\n")
    mkromfs_dir(basename, is_root = True)
    
    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")