source: mainline/tools/mktmpfs.py@ a2a00e8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a2a00e8 was 3c80f2b, checked in by Martin Decky <martin@…>, 15 years ago

Python coding style cleanup (no change in functionality)

  • Property mode set to 100755
File size: 3.5 KB
Line 
1#!/usr/bin/env python
2#
3# Copyright (c) 2008 Martin Decky
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# - Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15# - The name of the author may not be used to endorse or promote products
16# derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29
30"""
31TMPFS creator
32"""
33
34import sys
35import os
36import xstruct
37
38exclude_names = set(['.svn', '.bzr'])
39
40HEADER = """little:
41 char tag[5] /* 'TMPFS' */
42"""
43
44DENTRY_NONE = """little:
45 uint8_t kind /* NONE */
46 uint32_t fname_len /* 0 */
47"""
48
49DENTRY_FILE = """little:
50 uint8_t kind /* FILE */
51 uint32_t fname_len /* filename length */
52 char fname[%d] /* filename */
53 uint32_t flen /* file length */
54"""
55
56DENTRY_DIRECTORY = """little:
57 uint8_t kind /* DIRECTORY */
58 uint32_t fname_len /* filename length */
59 char fname[%d] /* filename */
60"""
61
62TMPFS_NONE = 0
63TMPFS_FILE = 1
64TMPFS_DIRECTORY = 2
65
66def usage(prname):
67 "Print usage syntax"
68 print prname + " <PATH> <IMAGE>"
69
70def recursion(root, outf):
71 "Recursive directory walk"
72
73 for name in os.listdir(root):
74 canon = os.path.join(root, name)
75
76 if (os.path.isfile(canon) and (not name in exclude_names)):
77 size = os.path.getsize(canon)
78
79 dentry = xstruct.create(DENTRY_FILE % len(name))
80 dentry.kind = TMPFS_FILE
81 dentry.fname_len = len(name)
82 dentry.fname = name
83 dentry.flen = size
84
85 outf.write(dentry.pack())
86
87 inf = file(canon, "r")
88 rd = 0;
89 while (rd < size):
90 data = inf.read(4096);
91 outf.write(data)
92 rd += len(data)
93 inf.close()
94
95 if (os.path.isdir(canon) and (not name in exclude_names)):
96 dentry = xstruct.create(DENTRY_DIRECTORY % len(name))
97 dentry.kind = TMPFS_DIRECTORY
98 dentry.fname_len = len(name)
99 dentry.fname = name
100
101 outf.write(dentry.pack())
102
103 recursion(canon, outf)
104
105 dentry = xstruct.create(DENTRY_NONE)
106 dentry.kind = TMPFS_NONE
107 dentry.fname_len = 0
108
109 outf.write(dentry.pack())
110
111def main():
112 if (len(sys.argv) < 3):
113 usage(sys.argv[0])
114 return
115
116 path = os.path.abspath(sys.argv[1])
117 if (not os.path.isdir(path)):
118 print "<PATH> must be a directory"
119 return
120
121 outf = file(sys.argv[2], "w")
122
123 header = xstruct.create(HEADER)
124 header.tag = "TMPFS"
125
126 outf.write(header.pack())
127
128 recursion(path, outf)
129
130 dentry = xstruct.create(DENTRY_NONE)
131 dentry.kind = TMPFS_NONE
132 dentry.fname_len = 0
133
134 outf.write(dentry.pack())
135
136 outf.close()
137
138if __name__ == '__main__':
139 main()
Note: See TracBrowser for help on using the repository browser.