Rev 3017 | Rev 3021 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3017 | Rev 3019 | ||
---|---|---|---|
1 | #!/usr/bin/env python |
1 | #!/usr/bin/env python |
2 | # |
2 | # |
3 | # Copyright (c) 2008 Martin Decky |
3 | # Copyright (c) 2008 Martin Decky |
4 | # All rights reserved. |
4 | # All rights reserved. |
5 | # |
5 | # |
6 | # Redistribution and use in source and binary forms, with or without |
6 | # Redistribution and use in source and binary forms, with or without |
7 | # modification, are permitted provided that the following conditions |
7 | # modification, are permitted provided that the following conditions |
8 | # are met: |
8 | # are met: |
9 | # |
9 | # |
10 | # - Redistributions of source code must retain the above copyright |
10 | # - Redistributions of source code must retain the above copyright |
11 | # notice, this list of conditions and the following disclaimer. |
11 | # notice, this list of conditions and the following disclaimer. |
12 | # - Redistributions in binary form must reproduce the above copyright |
12 | # - Redistributions in binary form must reproduce the above copyright |
13 | # notice, this list of conditions and the following disclaimer in the |
13 | # notice, this list of conditions and the following disclaimer in the |
14 | # documentation and/or other materials provided with the distribution. |
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 |
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. |
16 | # derived from this software without specific prior written permission. |
17 | # |
17 | # |
18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
19 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | # |
28 | # |
29 | """ |
29 | """ |
30 | TMPFS creator |
30 | TMPFS creator |
31 | """ |
31 | """ |
32 | 32 | ||
33 | import sys |
33 | import sys |
34 | import os |
34 | import os |
35 | import struct |
35 | import struct |
36 | 36 | ||
37 | def usage(prname): |
37 | def usage(prname): |
38 | "Print usage syntax" |
38 | "Print usage syntax" |
39 | print prname + " <PATH> <IMAGE>" |
39 | print prname + " <PATH> <IMAGE>" |
40 | 40 | ||
41 | def main(): |
41 | def main(): |
42 | if (len(sys.argv) < 3): |
42 | if (len(sys.argv) < 3): |
43 | usage(sys.argv[0]) |
43 | usage(sys.argv[0]) |
44 | return |
44 | return |
45 | 45 | ||
46 | path = os.path.abspath(sys.argv[1]); |
46 | path = os.path.abspath(sys.argv[1]); |
47 | if (not os.path.isdir(path)): |
47 | if (not os.path.isdir(path)): |
48 | print "<PATH> must be a directory" |
48 | print "<PATH> must be a directory" |
49 | return |
49 | return |
50 | 50 | ||
- | 51 | header_size = 18 |
|
- | 52 | payload_size = 0 |
|
51 | outf = file(sys.argv[2], "w"); |
53 | outf = file(sys.argv[2], "w") |
- | 54 | outf.write(struct.pack("<" + ("%d" % header_size) + "x")) |
|
52 | 55 | ||
53 | for root, dirs, files in os.walk(path): |
56 | for root, dirs, files in os.walk(path): |
54 | relpath = root[len(path):len(root)] |
57 | relpath = root[len(path):len(root)] |
55 | for name in files: |
58 | for name in files: |
56 | canon = os.path.join(relpath, name) |
59 | canon = os.path.join(relpath, name) |
57 | outf.write(struct.pack("<BL" + ("%d" % len(canon)) + "s", 1, len(canon), canon)) |
60 | outf.write(struct.pack("<BL" + ("%d" % len(canon)) + "s", 1, len(canon), canon)) |
- | 61 | payload_size += 5 + len(canon) |
|
58 | 62 | ||
59 | fn = os.path.join(root, name) |
63 | fn = os.path.join(root, name) |
60 | size = os.path.getsize(fn) |
64 | size = os.path.getsize(fn) |
61 | rd = 0; |
65 | rd = 0; |
62 | outf.write(struct.pack("<L", size)) |
66 | outf.write(struct.pack("<L", size)) |
- | 67 | payload_size += 4 |
|
63 | 68 | ||
64 | inf = file(fn, "r") |
69 | inf = file(fn, "r") |
65 | while (rd < size): |
70 | while (rd < size): |
66 | data = inf.read(4096); |
71 | data = inf.read(4096); |
67 | outf.write(data) |
72 | outf.write(data) |
- | 73 | payload_size += len(data) |
|
68 | rd += len(data) |
74 | rd += len(data) |
69 | inf.close() |
75 | inf.close() |
70 | 76 | ||
71 | for name in dirs: |
77 | for name in dirs: |
72 | canon = os.path.join(relpath, name) |
78 | canon = os.path.join(relpath, name) |
73 | outf.write(struct.pack("<BL" + ("%d" % len(canon)) + "s", 2, len(canon), canon)) |
79 | outf.write(struct.pack("<BL" + ("%d" % len(canon)) + "s", 2, len(canon), canon)) |
- | 80 | payload_size += 5 + len(canon) |
|
74 | 81 | ||
- | 82 | outf.seek(0) |
|
- | 83 | outf.write(struct.pack("<4sBBLQ", "HORD", 1, 1, header_size, payload_size)) |
|
75 | outf.close() |
84 | outf.close() |
76 | 85 | ||
77 | if __name__ == '__main__': |
86 | if __name__ == '__main__': |
78 | main() |
87 | main() |
79 | 88 |