17
17
reload (sys )
18
18
sys .setdefaultencoding ("utf-8" )
19
19
20
- import argparse
20
+ import optparse
21
21
import os
22
22
import json
23
23
import re
24
24
25
25
endian = sys .byteorder
26
26
27
- parser = argparse .ArgumentParser (description = "ICU Datafile repackager. Example of use: \" mkdir tmp ; python icutrim.py -D ~/Downloads/icudt53l.dat -T tmp -F trim_en.json -O icudt53l.dat\" you will then find a smaller icudt53l.dat in 'tmp'. " ,
28
- epilog = "ICU tool, http://icu-project.org - master copy at http://source.icu-project.org/repos/icu/tools/trunk/scripts/icutrim.py" )
27
+ parser = optparse .OptionParser (usage = "usage: mkdir tmp ; %prog -D ~/Downloads/icudt53l.dat -T tmp -F trim_en.json -O icudt53l.dat" )
29
28
30
- parser .add_argument ("-P" ,"--tool-path" ,
29
+ parser .add_option ("-P" ,"--tool-path" ,
31
30
action = "store" ,
32
31
dest = "toolpath" ,
33
32
help = "set the prefix directory for ICU tools" )
34
33
35
- parser .add_argument ("-D" ,"--input-file" ,
34
+ parser .add_option ("-D" ,"--input-file" ,
36
35
action = "store" ,
37
36
dest = "datfile" ,
38
37
help = "input data file (icudt__.dat)" ,
39
- required = True )
38
+ ) # required
40
39
41
- parser .add_argument ("-F" ,"--filter-file" ,
40
+ parser .add_option ("-F" ,"--filter-file" ,
42
41
action = "store" ,
43
42
dest = "filterfile" ,
44
43
help = "filter file (JSON format)" ,
45
- required = True )
44
+ ) # required
46
45
47
- parser .add_argument ("-T" ,"--tmp-dir" ,
46
+ parser .add_option ("-T" ,"--tmp-dir" ,
48
47
action = "store" ,
49
48
dest = "tmpdir" ,
50
49
help = "working directory." ,
51
- required = True )
50
+ ) # required
52
51
53
- parser .add_argument ("--delete-tmp" ,
52
+ parser .add_option ("--delete-tmp" ,
54
53
action = "count" ,
55
54
dest = "deltmpdir" ,
56
55
help = "delete working directory." ,
57
56
default = 0 )
58
57
59
- parser .add_argument ("-O" ,"--outfile" ,
58
+ parser .add_option ("-O" ,"--outfile" ,
60
59
action = "store" ,
61
60
dest = "outfile" ,
62
61
help = "outfile (NOT a full path)" ,
63
- required = True )
62
+ ) # required
64
63
65
- parser .add_argument ("-v" ,"--verbose" ,
64
+ parser .add_option ("-v" ,"--verbose" ,
66
65
action = "count" ,
67
66
default = 0 )
68
67
69
- parser .add_argument ('-e' , '--endian' , action = 'store' , dest = 'endian' , help = 'endian, big, little or host, your default is "%s".' % endian , default = endian , metavar = 'endianness' )
68
+ parser .add_option ('-e' , '--endian' , action = 'store' , dest = 'endian' , help = 'endian, big, little or host, your default is "%s".' % endian , default = endian , metavar = 'endianness' )
70
69
70
+ (options , args ) = parser .parse_args ()
71
71
72
- args = parser . parse_args ( )
72
+ optVars = vars ( options )
73
73
74
- if args .verbose > 0 :
75
- print "Options: " + str (args )
74
+ for opt in [ "datfile" , "filterfile" , "tmpdir" , "outfile" ]:
75
+ if optVars [opt ] is None :
76
+ print "Missing required option: %s" % opt
77
+ sys .exit (1 )
78
+
79
+ if options .verbose > 0 :
80
+ print "Options: " + str (options )
76
81
77
- if (os .path .isdir (args .tmpdir ) and args .deltmpdir ):
78
- if args .verbose > 1 :
79
- print "Deleting tmp dir %s.." % (args .tmpdir )
80
- shutil .rmtree (args .tmpdir )
82
+ if (os .path .isdir (options .tmpdir ) and options .deltmpdir ):
83
+ if options .verbose > 1 :
84
+ print "Deleting tmp dir %s.." % (options .tmpdir )
85
+ shutil .rmtree (options .tmpdir )
81
86
82
- if not (os .path .isdir (args .tmpdir )):
83
- os .mkdir (args .tmpdir )
87
+ if not (os .path .isdir (options .tmpdir )):
88
+ os .mkdir (options .tmpdir )
84
89
else :
85
- print "Please delete tmpdir %s before beginning." % args .tmpdir
90
+ print "Please delete tmpdir %s before beginning." % options .tmpdir
86
91
sys .exit (1 )
87
92
88
- if args .endian not in ("big" ,"little" ,"host" ):
89
- print "Unknown endianness: %s" % args .endian
93
+ if options .endian not in ("big" ,"little" ,"host" ):
94
+ print "Unknown endianness: %s" % options .endian
90
95
sys .exit (1 )
91
96
92
- if args .endian is "host" :
93
- args .endian = endian
97
+ if options .endian is "host" :
98
+ options .endian = endian
94
99
95
- if not os .path .isdir (args .tmpdir ):
96
- print "Error, tmpdir not a directory: %s" % (args .tmpdir )
100
+ if not os .path .isdir (options .tmpdir ):
101
+ print "Error, tmpdir not a directory: %s" % (options .tmpdir )
97
102
sys .exit (1 )
98
103
99
- if not os .path .isfile (args .filterfile ):
100
- print "Filterfile doesn't exist: %s" % (args .filterfile )
104
+ if not os .path .isfile (options .filterfile ):
105
+ print "Filterfile doesn't exist: %s" % (options .filterfile )
101
106
sys .exit (1 )
102
107
103
- if not os .path .isfile (args .datfile ):
104
- print "Datfile doesn't exist: %s" % (args .datfile )
108
+ if not os .path .isfile (options .datfile ):
109
+ print "Datfile doesn't exist: %s" % (options .datfile )
105
110
sys .exit (1 )
106
111
107
- if not args .datfile .endswith (".dat" ):
108
- print "Datfile doesn't end with .dat: %s" % (args .datfile )
112
+ if not options .datfile .endswith (".dat" ):
113
+ print "Datfile doesn't end with .dat: %s" % (options .datfile )
109
114
sys .exit (1 )
110
115
111
- outfile = os .path .join (args .tmpdir , args .outfile )
116
+ outfile = os .path .join (options .tmpdir , options .outfile )
112
117
113
118
if os .path .isfile (outfile ):
114
119
print "Error, output file does exist: %s" % (outfile )
115
120
sys .exit (1 )
116
121
117
- if not args .outfile .endswith (".dat" ):
118
- print "Outfile doesn't end with .dat: %s" % (args .outfile )
122
+ if not options .outfile .endswith (".dat" ):
123
+ print "Outfile doesn't end with .dat: %s" % (options .outfile )
119
124
sys .exit (1 )
120
125
121
- dataname = args .outfile [0 :- 4 ]
126
+ dataname = options .outfile [0 :- 4 ]
122
127
123
128
124
129
## TODO: need to improve this. Quotes, etc.
125
130
def runcmd (tool , cmd , doContinue = False ):
126
- if (args .toolpath ):
127
- cmd = os .path .join (args .toolpath , tool ) + " " + cmd
131
+ if (options .toolpath ):
132
+ cmd = os .path .join (options .toolpath , tool ) + " " + cmd
128
133
else :
129
134
cmd = tool + " " + cmd
130
135
131
- if (args .verbose > 4 ):
136
+ if (options .verbose > 4 ):
132
137
print "# " + cmd
133
138
134
139
rc = os .system (cmd )
@@ -138,24 +143,24 @@ def runcmd(tool, cmd, doContinue=False):
138
143
return rc
139
144
140
145
## STEP 0 - read in json config
141
- fi = open (args .filterfile , "rb" )
146
+ fi = open (options .filterfile , "rb" )
142
147
config = json .load (fi )
143
148
fi .close ()
144
149
145
- if (args .verbose > 6 ):
150
+ if (options .verbose > 6 ):
146
151
print config
147
152
148
153
if (config .has_key ("comment" )):
149
- print "%s: %s" % (args .filterfile , config ["comment" ])
154
+ print "%s: %s" % (options .filterfile , config ["comment" ])
150
155
151
156
## STEP 1 - copy the data file, swapping endianness
152
157
endian_letter = "l"
153
158
154
159
155
- runcmd ("icupkg" , "-t%s %s %s" "" % (endian_letter , args .datfile , outfile ))
160
+ runcmd ("icupkg" , "-t%s %s %s" "" % (endian_letter , options .datfile , outfile ))
156
161
157
162
## STEP 2 - get listing
158
- listfile = os .path .join (args .tmpdir ,"icudata.lst" )
163
+ listfile = os .path .join (options .tmpdir ,"icudata.lst" )
159
164
runcmd ("icupkg" , "-l %s > %s" "" % (outfile , listfile ))
160
165
161
166
fi = open (listfile , 'rb' )
@@ -165,7 +170,7 @@ def runcmd(tool, cmd, doContinue=False):
165
170
166
171
itemset = set (items )
167
172
168
- if (args .verbose > 1 ):
173
+ if (options .verbose > 1 ):
169
174
print "input file: %d items" % (len (items ))
170
175
171
176
# list of all trees
@@ -192,23 +197,23 @@ def queueForRemoval(tree):
192
197
if not config ["trees" ].has_key (tree ):
193
198
return
194
199
mytree = trees [tree ]
195
- if (args .verbose > 0 ):
200
+ if (options .verbose > 0 ):
196
201
print "* %s: %d items" % (tree , len (mytree ["locs" ]))
197
202
# do varible substitution for this tree here
198
203
if type (config ["trees" ][tree ]) == str or type (config ["trees" ][tree ]) == unicode :
199
204
treeStr = config ["trees" ][tree ]
200
- if (args .verbose > 5 ):
205
+ if (options .verbose > 5 ):
201
206
print " Substituting $%s for tree %s" % (treeStr , tree )
202
207
if (not config .has_key ("variables" ) or not config ["variables" ].has_key (treeStr )):
203
208
print " ERROR: no variable: variables.%s for tree %s" % (treeStr , tree )
204
209
sys .exit (1 )
205
210
config ["trees" ][tree ] = config ["variables" ][treeStr ]
206
211
myconfig = config ["trees" ][tree ]
207
- if (args .verbose > 4 ):
212
+ if (options .verbose > 4 ):
208
213
print " Config: %s" % (myconfig )
209
214
# Process this tree
210
215
if (len (myconfig )== 0 or len (mytree ["locs" ])== 0 ):
211
- if (args .verbose > 2 ):
216
+ if (options .verbose > 2 ):
212
217
print " No processing for %s - skipping" % (tree )
213
218
else :
214
219
only = None
@@ -217,7 +222,7 @@ def queueForRemoval(tree):
217
222
if (len (only )== 0 ) and (mytree ["treeprefix" ] != "" ):
218
223
thePool = "%spool.res" % (mytree ["treeprefix" ])
219
224
if (thePool in itemset ):
220
- if (args .verbose > 0 ):
225
+ if (options .verbose > 0 ):
221
226
print "Removing %s because tree %s is empty." % (thePool , tree )
222
227
remove .add (thePool )
223
228
else :
@@ -227,12 +232,12 @@ def queueForRemoval(tree):
227
232
if (only is not None ) and not loc in only :
228
233
# REMOVE loc
229
234
toRemove = "%s%s%s" % (mytree ["treeprefix" ], loc , mytree ["extension" ])
230
- if (args .verbose > 6 ):
235
+ if (options .verbose > 6 ):
231
236
print "Queueing for removal: %s" % toRemove
232
237
remove .add (toRemove )
233
238
234
239
def addTreeByType (tree , mytree ):
235
- if (args .verbose > 1 ):
240
+ if (options .verbose > 1 ):
236
241
print "(considering %s): %s" % (tree , mytree )
237
242
trees [tree ] = mytree
238
243
mytree ["locs" ]= []
@@ -259,18 +264,18 @@ def addTreeByType(tree, mytree):
259
264
tree = "ROOT"
260
265
else :
261
266
tree = treeprefix [0 :- 1 ]
262
- if (args .verbose > 6 ):
267
+ if (options .verbose > 6 ):
263
268
print "procesing %s" % (tree )
264
269
trees [tree ] = { "extension" : ".res" , "treeprefix" : treeprefix , "hasIndex" : True }
265
270
# read in the resource list for the tree
266
- treelistfile = os .path .join (args .tmpdir ,"%s.lst" % tree )
271
+ treelistfile = os .path .join (options .tmpdir ,"%s.lst" % tree )
267
272
runcmd ("iculslocs" , "-i %s -N %s -T %s -l > %s" % (outfile , dataname , tree , treelistfile ))
268
273
fi = open (treelistfile , 'rb' )
269
274
treeitems = fi .readlines ()
270
275
trees [tree ]["locs" ] = [treeitems [i ].strip () for i in range (len (treeitems ))]
271
276
fi .close ()
272
277
if (not config .has_key ("trees" ) or not config ["trees" ].has_key (tree )):
273
- print " Warning: filter file %s does not mention trees.%s - will be kept as-is" % (args .filterfile , tree )
278
+ print " Warning: filter file %s does not mention trees.%s - will be kept as-is" % (options .filterfile , tree )
274
279
else :
275
280
queueForRemoval (tree )
276
281
@@ -281,19 +286,19 @@ def removeList(count=0):
281
286
if (count > 10 ):
282
287
print "Giving up - %dth attempt at removal." % count
283
288
sys .exit (1 )
284
- if (args .verbose > 1 ):
289
+ if (options .verbose > 1 ):
285
290
print "%d items to remove - try #%d" % (len (remove ),count )
286
291
if (len (remove )> 0 ):
287
292
oldcount = len (remove )
288
- hackerrfile = os .path .join (args .tmpdir , "REMOVE.err" )
289
- removefile = os .path .join (args .tmpdir , "REMOVE.lst" )
293
+ hackerrfile = os .path .join (options .tmpdir , "REMOVE.err" )
294
+ removefile = os .path .join (options .tmpdir , "REMOVE.lst" )
290
295
fi = open (removefile , 'wb' )
291
296
for i in remove :
292
297
print >> fi , i
293
298
fi .close ()
294
299
rc = runcmd ("icupkg" ,"-r %s %s 2> %s" % (removefile ,outfile ,hackerrfile ),True )
295
300
if rc is not 0 :
296
- if (args .verbose > 5 ):
301
+ if (options .verbose > 5 ):
297
302
print "## Damage control, trying to parse stderr from icupkg.."
298
303
fi = open (hackerrfile , 'rb' )
299
304
erritems = fi .readlines ()
@@ -305,13 +310,13 @@ def removeList(count=0):
305
310
m = pat .match (line )
306
311
if m :
307
312
toDelete = m .group (1 )
308
- if (args .verbose > 5 ):
313
+ if (options .verbose > 5 ):
309
314
print "<< %s added to delete" % toDelete
310
315
remove .add (toDelete )
311
316
else :
312
317
print "ERROR: could not match errline: %s" % line
313
318
sys .exit (1 )
314
- if (args .verbose > 5 ):
319
+ if (options .verbose > 5 ):
315
320
print " now %d items to remove" % len (remove )
316
321
if (oldcount == len (remove )):
317
322
print " ERROR: could not add any mor eitems to remove. Fail."
@@ -326,7 +331,7 @@ def removeList(count=0):
326
331
# skip trees that don't have res_index
327
332
if not trees [tree ].has_key ("hasIndex" ):
328
333
continue
329
- treebunddir = args .tmpdir
334
+ treebunddir = options .tmpdir
330
335
if (trees [tree ]["treeprefix" ]):
331
336
treebunddir = os .path .join (treebunddir , trees [tree ]["treeprefix" ])
332
337
if not (os .path .isdir (treebunddir )):
@@ -335,4 +340,4 @@ def removeList(count=0):
335
340
treebundtxt = "%s.txt" % (treebundres [0 :- 4 ])
336
341
runcmd ("iculslocs" , "-i %s -N %s -T %s -b %s" % (outfile , dataname , tree , treebundtxt ))
337
342
runcmd ("genrb" ,"-d %s -s %s res_index.txt" % (treebunddir , treebunddir ))
338
- runcmd ("icupkg" ,"-s %s -a %s%s %s" % (args .tmpdir , trees [tree ]["treeprefix" ], RES_INDX , outfile ))
343
+ runcmd ("icupkg" ,"-s %s -a %s%s %s" % (options .tmpdir , trees [tree ]["treeprefix" ], RES_INDX , outfile ))
0 commit comments