source: other-projects/playing-in-the-street/summer-2013/trunk/Playing-in-the-Street-WPF/Content/Web/mrdoob-three.js-4862f5f/utils/converters/ctm/join_ctm.py@ 28897

Last change on this file since 28897 was 28897, checked in by davidb, 10 years ago

GUI front-end to server base plus web page content

File size: 2.5 KB
Line 
1"""Join multiple binary files into single file and generate JSON snippet with offsets
2
3-------------------------------------
4How to use
5-------------------------------------
6
7python join_ctm.py -i "part_*.ctm" -o joined.ctm [-j offsets.js]
8
9Will read multiple files following wildcard pattern (ordered lexicographically):
10
11part_000.ctm
12part_001.ctm
13part_002.ctm
14
15...
16
17part_XXX.ctm
18
19And generate single concatenated files:
20
21joined.ctm
22offsets.js (optional, offsets are also dumped to standard output)
23
24"""
25
26import getopt
27import glob
28import sys
29import os
30
31# #####################################################
32# Templates
33# #####################################################
34TEMPLATE_JSON = u"""\
35"offsets": [ %(offsets)s ],
36"""
37
38# #############################################################################
39# Helpers
40# #############################################################################
41def 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# #####################################################
47if __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()
Note: See TracBrowser for help on using the repository browser.