Rev 3042 | Rev 3425 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3042 | Rev 3424 | ||
---|---|---|---|
Line 40... | Line 40... | ||
40 | 40 | ||
41 | def usage(prname): |
41 | def usage(prname): |
42 | "Print usage syntax" |
42 | "Print usage syntax" |
43 | print prname + " <ALIGNMENT> <PATH> <IMAGE>" |
43 | print prname + " <ALIGNMENT> <PATH> <IMAGE>" |
44 | 44 | ||
- | 45 | def recursion(root, outf): |
|
- | 46 | "Recursive directory walk" |
|
- | 47 | ||
- | 48 | payload_size = 0 |
|
- | 49 | ||
- | 50 | for name in os.listdir(root): |
|
- | 51 | canon = os.path.join(root, name) |
|
- | 52 | ||
- | 53 | if (os.path.isfile(canon)): |
|
- | 54 | outf.write(struct.pack("<BL" + ("%d" % len(name)) + "s", 1, len(name), name)) |
|
- | 55 | payload_size += 5 + len(name) |
|
- | 56 | size = os.path.getsize(canon) |
|
- | 57 | rd = 0; |
|
- | 58 | outf.write(struct.pack("<L", size)) |
|
- | 59 | payload_size += 4 |
|
- | 60 | ||
- | 61 | inf = file(canon, "r") |
|
- | 62 | while (rd < size): |
|
- | 63 | data = inf.read(4096); |
|
- | 64 | outf.write(data) |
|
- | 65 | payload_size += len(data) |
|
- | 66 | rd += len(data) |
|
- | 67 | inf.close() |
|
- | 68 | ||
- | 69 | if (os.path.isdir(canon)): |
|
- | 70 | outf.write(struct.pack("<BL" + ("%d" % len(name)) + "s", 2, len(name), name)) |
|
- | 71 | payload_size += 5 + len(name) |
|
- | 72 | payload_size += recursion(canon, outf) |
|
- | 73 | outf.write(struct.pack("<BL", 0, 0)) |
|
- | 74 | payload_size += 5 |
|
- | 75 | ||
- | 76 | return payload_size |
|
- | 77 | ||
45 | def main(): |
78 | def main(): |
46 | if (len(sys.argv) < 4): |
79 | if (len(sys.argv) < 4): |
47 | usage(sys.argv[0]) |
80 | usage(sys.argv[0]) |
48 | return |
81 | return |
49 | 82 | ||
Line 56... | Line 89... | ||
56 | if (not os.path.isdir(path)): |
89 | if (not os.path.isdir(path)): |
57 | print "<PATH> must be a directory" |
90 | print "<PATH> must be a directory" |
58 | return |
91 | return |
59 | 92 | ||
60 | header_size = align_up(18, align) |
93 | header_size = align_up(18, align) |
61 | payload_size = 0 |
- | |
62 | outf = file(sys.argv[3], "w") |
94 | outf = file(sys.argv[3], "w") |
63 | outf.write(struct.pack("<" + ("%d" % header_size) + "x")) |
95 | outf.write(struct.pack("<" + ("%d" % header_size) + "x")) |
64 | 96 | ||
65 | for root, dirs, files in os.walk(path): |
- | |
66 | relpath = root[len(path):len(root)] |
- | |
67 | for name in files: |
- | |
68 | canon = os.path.join(relpath, name) |
- | |
69 | outf.write(struct.pack("<BL" + ("%d" % len(canon)) + "s", 1, len(canon), canon)) |
- | |
70 | payload_size += 5 + len(canon) |
- | |
71 | - | ||
72 | fn = os.path.join(root, name) |
- | |
73 | size = os.path.getsize(fn) |
- | |
74 | rd = 0; |
- | |
75 | outf.write(struct.pack("<L", size)) |
97 | outf.write(struct.pack("<5s", "TMPFS")) |
76 | payload_size += 4 |
98 | payload_size = 5 |
77 | 99 | ||
78 | inf = file(fn, "r") |
- | |
79 | while (rd < size): |
- | |
80 | data = inf.read(4096); |
- | |
81 | outf.write(data) |
- | |
82 | payload_size += len(data) |
100 | payload_size += recursion(path, outf) |
83 | rd += len(data) |
- | |
84 | inf.close() |
- | |
85 | 101 | ||
86 | for name in dirs: |
- | |
87 | canon = os.path.join(relpath, name) |
- | |
88 | outf.write(struct.pack("<BL" + ("%d" % len(canon)) + "s", 2, len(canon), canon)) |
102 | outf.write(struct.pack("<BL", 0, 0)) |
89 | payload_size += 5 + len(canon) |
103 | payload_size += 5 |
90 | 104 | ||
91 | aligned_size = align_up(payload_size, align) |
105 | aligned_size = align_up(payload_size, align) |
92 | 106 | ||
93 | if (aligned_size - payload_size > 0): |
107 | if (aligned_size - payload_size > 0): |
94 | outf.write(struct.pack("<" + ("%d" % (aligned_size - payload_size)) + "x")) |
108 | outf.write(struct.pack("<" + ("%d" % (aligned_size - payload_size)) + "x")) |
95 | 109 | ||
96 | outf.seek(0) |
110 | outf.seek(0) |
97 | outf.write(struct.pack("<4sBBLQ", "HORD", 1, 1, header_size, aligned_size)) |
111 | outf.write(struct.pack("<4sBBLQ", "HORD", 1, 1, header_size, aligned_size)) |
98 | outf.close() |
112 | outf.close() |
99 | 113 | ||
100 | if __name__ == '__main__': |
114 | if __name__ == '__main__': |