source: mainline/tools/mktmpfs.py@ 458619f7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 458619f7 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
RevLine 
[5a55ae6]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#
[3c80f2b]29
[5a55ae6]30"""
31TMPFS creator
32"""
33
34import sys
35import os
[8c25a4a]36import xstruct
37
[24edc18]38exclude_names = set(['.svn', '.bzr'])
[9df7918]39
[0516fd7]40HEADER = """little:
[8f2a852]41 char tag[5] /* 'TMPFS' */
[0516fd7]42"""
43
44DENTRY_NONE = """little:
45 uint8_t kind /* NONE */
46 uint32_t fname_len /* 0 */
47"""
[b3105ff6]48
[0516fd7]49DENTRY_FILE = """little:
50 uint8_t kind /* FILE */
51 uint32_t fname_len /* filename length */
[8f2a852]52 char fname[%d] /* filename */
[0516fd7]53 uint32_t flen /* file length */
54"""
[b3105ff6]55
[0516fd7]56DENTRY_DIRECTORY = """little:
57 uint8_t kind /* DIRECTORY */
58 uint32_t fname_len /* filename length */
[8f2a852]59 char fname[%d] /* filename */
[0516fd7]60"""
[8c25a4a]61
[0516fd7]62TMPFS_NONE = 0
63TMPFS_FILE = 1
64TMPFS_DIRECTORY = 2
[5a55ae6]65
66def usage(prname):
67 "Print usage syntax"
[8c25a4a]68 print prname + " <PATH> <IMAGE>"
[5a55ae6]69
[9b70394]70def recursion(root, outf):
71 "Recursive directory walk"
72
73 for name in os.listdir(root):
74 canon = os.path.join(root, name)
75
[9df7918]76 if (os.path.isfile(canon) and (not name in exclude_names)):
[9b70394]77 size = os.path.getsize(canon)
[0516fd7]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())
[9b70394]86
87 inf = file(canon, "r")
[0516fd7]88 rd = 0;
[9b70394]89 while (rd < size):
90 data = inf.read(4096);
91 outf.write(data)
92 rd += len(data)
93 inf.close()
94
[9df7918]95 if (os.path.isdir(canon) and (not name in exclude_names)):
[0516fd7]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
[8c25a4a]103 recursion(canon, outf)
[0516fd7]104
105 dentry = xstruct.create(DENTRY_NONE)
106 dentry.kind = TMPFS_NONE
107 dentry.fname_len = 0
108
109 outf.write(dentry.pack())
[9b70394]110
[5a55ae6]111def main():
[677f620]112 if (len(sys.argv) < 3):
[5a55ae6]113 usage(sys.argv[0])
114 return
115
[677f620]116 path = os.path.abspath(sys.argv[1])
[5a55ae6]117 if (not os.path.isdir(path)):
118 print "<PATH> must be a directory"
119 return
120
[677f620]121 outf = file(sys.argv[2], "w")
[5a55ae6]122
[0516fd7]123 header = xstruct.create(HEADER)
124 header.tag = "TMPFS"
125
126 outf.write(header.pack())
127
[677f620]128 recursion(path, outf)
[0516fd7]129
130 dentry = xstruct.create(DENTRY_NONE)
131 dentry.kind = TMPFS_NONE
132 dentry.fname_len = 0
133
134 outf.write(dentry.pack())
135
[677f620]136 outf.close()
[0516fd7]137
[5a55ae6]138if __name__ == '__main__':
139 main()
Note: See TracBrowser for help on using the repository browser.