1 | """Join multiple binary files into single file and generate JSON snippet with offsets
|
---|
2 |
|
---|
3 | -------------------------------------
|
---|
4 | How to use
|
---|
5 | -------------------------------------
|
---|
6 |
|
---|
7 | python join_ctm.py -i "part_*.ctm" -o joined.ctm [-j offsets.js]
|
---|
8 |
|
---|
9 | Will read multiple files following wildcard pattern (ordered lexicographically):
|
---|
10 |
|
---|
11 | part_000.ctm
|
---|
12 | part_001.ctm
|
---|
13 | part_002.ctm
|
---|
14 |
|
---|
15 | ...
|
---|
16 |
|
---|
17 | part_XXX.ctm
|
---|
18 |
|
---|
19 | And generate single concatenated files:
|
---|
20 |
|
---|
21 | joined.ctm
|
---|
22 | offsets.js (optional, offsets are also dumped to standard output)
|
---|
23 |
|
---|
24 | """
|
---|
25 |
|
---|
26 | import getopt
|
---|
27 | import glob
|
---|
28 | import sys
|
---|
29 | import os
|
---|
30 |
|
---|
31 | # #####################################################
|
---|
32 | # Templates
|
---|
33 | # #####################################################
|
---|
34 | TEMPLATE_JSON = u"""\
|
---|
35 | "offsets": [ %(offsets)s ],
|
---|
36 | """
|
---|
37 |
|
---|
38 | # #############################################################################
|
---|
39 | # Helpers
|
---|
40 | # #############################################################################
|
---|
41 | def usage():
|
---|
42 | print 'Usage: %s -i "filename_*.ctm" -o filename.ctm [-j offsets.js]' % os.path.basename(sys.argv[0])
|
---|
43 |
|
---|
44 | # #####################################################
|
---|
45 | # Main
|
---|
46 | # #####################################################
|
---|
47 | if __name__ == "__main__":
|
---|
48 |
|
---|
49 | # get parameters from the command line
|
---|
50 |
|
---|
51 | try:
|
---|
52 | opts, args = getopt.getopt(sys.argv[1:], "hi:o:j:", ["help", "input=", "output=", "json="])
|
---|
53 |
|
---|
54 | except getopt.GetoptError:
|
---|
55 | usage()
|
---|
56 | sys.exit(2)
|
---|
57 |
|
---|
58 |
|
---|
59 | inpattern = ""
|
---|
60 | outname = ""
|
---|
61 | jsonname = ""
|
---|
62 |
|
---|
63 | for o, a in opts:
|
---|
64 | if o in ("-h", "--help"):
|
---|
65 | usage()
|
---|
66 | sys.exit()
|
---|
67 |
|
---|
68 | elif o in ("-i", "--input"):
|
---|
69 | inpattern = a
|
---|
70 |
|
---|
71 | elif o in ("-o", "--output"):
|
---|
72 | outname = a
|
---|
73 |
|
---|
74 | elif o in ("-j", "--json"):
|
---|
75 | jsonname = a
|
---|
76 |
|
---|
77 | # quit if required parameters are missing
|
---|
78 |
|
---|
79 | if inpattern == "" or outname == "":
|
---|
80 | usage()
|
---|
81 | sys.exit(2)
|
---|
82 |
|
---|
83 | outfile = open(outname, "wb")
|
---|
84 |
|
---|
85 | matches = glob.glob(inpattern)
|
---|
86 | matches.sort()
|
---|
87 |
|
---|
88 | total = 0
|
---|
89 | offsets = []
|
---|
90 |
|
---|
91 | for filename in matches:
|
---|
92 | filesize = os.path.getsize(filename)
|
---|
93 | offsets.append(total)
|
---|
94 | total += filesize
|
---|
95 |
|
---|
96 | print filename, filesize
|
---|
97 |
|
---|
98 | infile = open(filename, "rb")
|
---|
99 | buffer = infile.read()
|
---|
100 | outfile.write(buffer)
|
---|
101 | infile.close()
|
---|
102 |
|
---|
103 | outfile.close()
|
---|
104 |
|
---|
105 | json_str = TEMPLATE_JSON % {
|
---|
106 | "offsets" : ", ".join(["%d" % o for o in offsets])
|
---|
107 | }
|
---|
108 |
|
---|
109 | print json_str
|
---|
110 |
|
---|
111 | if jsonname:
|
---|
112 | jsonfile = open(jsonname, "w")
|
---|
113 | jsonfile.write(json_str)
|
---|
114 | jsonfile.close() |
---|