Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 0a22ed4

Browse files
srl295trevnorris
authored andcommitted
build: i18n: py27 -> py26 dependency
Move from argparse to optparse for dependency management. Fixes: #7719 (comment) Reviewed-by: Trevor Norris <[email protected]>
1 parent f769d13 commit 0a22ed4

File tree

1 file changed

+73
-68
lines changed

1 file changed

+73
-68
lines changed

tools/icu/icutrim.py

Lines changed: 73 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,118 +17,123 @@
1717
reload(sys)
1818
sys.setdefaultencoding("utf-8")
1919

20-
import argparse
20+
import optparse
2121
import os
2222
import json
2323
import re
2424

2525
endian=sys.byteorder
2626

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" )
2928

30-
parser.add_argument("-P","--tool-path",
29+
parser.add_option("-P","--tool-path",
3130
action="store",
3231
dest="toolpath",
3332
help="set the prefix directory for ICU tools")
3433

35-
parser.add_argument("-D","--input-file",
34+
parser.add_option("-D","--input-file",
3635
action="store",
3736
dest="datfile",
3837
help="input data file (icudt__.dat)",
39-
required=True)
38+
) # required
4039

41-
parser.add_argument("-F","--filter-file",
40+
parser.add_option("-F","--filter-file",
4241
action="store",
4342
dest="filterfile",
4443
help="filter file (JSON format)",
45-
required=True)
44+
) # required
4645

47-
parser.add_argument("-T","--tmp-dir",
46+
parser.add_option("-T","--tmp-dir",
4847
action="store",
4948
dest="tmpdir",
5049
help="working directory.",
51-
required=True)
50+
) # required
5251

53-
parser.add_argument("--delete-tmp",
52+
parser.add_option("--delete-tmp",
5453
action="count",
5554
dest="deltmpdir",
5655
help="delete working directory.",
5756
default=0)
5857

59-
parser.add_argument("-O","--outfile",
58+
parser.add_option("-O","--outfile",
6059
action="store",
6160
dest="outfile",
6261
help="outfile (NOT a full path)",
63-
required=True)
62+
) # required
6463

65-
parser.add_argument("-v","--verbose",
64+
parser.add_option("-v","--verbose",
6665
action="count",
6766
default=0)
6867

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')
7069

70+
(options, args) = parser.parse_args()
7171

72-
args = parser.parse_args()
72+
optVars = vars(options)
7373

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)
7681

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)
8186

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)
8489
else:
85-
print "Please delete tmpdir %s before beginning." % args.tmpdir
90+
print "Please delete tmpdir %s before beginning." % options.tmpdir
8691
sys.exit(1)
8792

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
9095
sys.exit(1)
9196

92-
if args.endian is "host":
93-
args.endian = endian
97+
if options.endian is "host":
98+
options.endian = endian
9499

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)
97102
sys.exit(1)
98103

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)
101106
sys.exit(1)
102107

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)
105110
sys.exit(1)
106111

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)
109114
sys.exit(1)
110115

111-
outfile = os.path.join(args.tmpdir, args.outfile)
116+
outfile = os.path.join(options.tmpdir, options.outfile)
112117

113118
if os.path.isfile(outfile):
114119
print "Error, output file does exist: %s" % (outfile)
115120
sys.exit(1)
116121

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)
119124
sys.exit(1)
120125

121-
dataname=args.outfile[0:-4]
126+
dataname=options.outfile[0:-4]
122127

123128

124129
## TODO: need to improve this. Quotes, etc.
125130
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
128133
else:
129134
cmd = tool + " " + cmd
130135

131-
if(args.verbose>4):
136+
if(options.verbose>4):
132137
print "# " + cmd
133138

134139
rc = os.system(cmd)
@@ -138,24 +143,24 @@ def runcmd(tool, cmd, doContinue=False):
138143
return rc
139144

140145
## STEP 0 - read in json config
141-
fi= open(args.filterfile, "rb")
146+
fi= open(options.filterfile, "rb")
142147
config=json.load(fi)
143148
fi.close()
144149

145-
if (args.verbose > 6):
150+
if (options.verbose > 6):
146151
print config
147152

148153
if(config.has_key("comment")):
149-
print "%s: %s" % (args.filterfile, config["comment"])
154+
print "%s: %s" % (options.filterfile, config["comment"])
150155

151156
## STEP 1 - copy the data file, swapping endianness
152157
endian_letter = "l"
153158

154159

155-
runcmd("icupkg", "-t%s %s %s""" % (endian_letter, args.datfile, outfile))
160+
runcmd("icupkg", "-t%s %s %s""" % (endian_letter, options.datfile, outfile))
156161

157162
## STEP 2 - get listing
158-
listfile = os.path.join(args.tmpdir,"icudata.lst")
163+
listfile = os.path.join(options.tmpdir,"icudata.lst")
159164
runcmd("icupkg", "-l %s > %s""" % (outfile, listfile))
160165

161166
fi = open(listfile, 'rb')
@@ -165,7 +170,7 @@ def runcmd(tool, cmd, doContinue=False):
165170

166171
itemset = set(items)
167172

168-
if (args.verbose>1):
173+
if (options.verbose>1):
169174
print "input file: %d items" % (len(items))
170175

171176
# list of all trees
@@ -192,23 +197,23 @@ def queueForRemoval(tree):
192197
if not config["trees"].has_key(tree):
193198
return
194199
mytree = trees[tree]
195-
if(args.verbose>0):
200+
if(options.verbose>0):
196201
print "* %s: %d items" % (tree, len(mytree["locs"]))
197202
# do varible substitution for this tree here
198203
if type(config["trees"][tree]) == str or type(config["trees"][tree]) == unicode:
199204
treeStr = config["trees"][tree]
200-
if(args.verbose>5):
205+
if(options.verbose>5):
201206
print " Substituting $%s for tree %s" % (treeStr, tree)
202207
if(not config.has_key("variables") or not config["variables"].has_key(treeStr)):
203208
print " ERROR: no variable: variables.%s for tree %s" % (treeStr, tree)
204209
sys.exit(1)
205210
config["trees"][tree] = config["variables"][treeStr]
206211
myconfig = config["trees"][tree]
207-
if(args.verbose>4):
212+
if(options.verbose>4):
208213
print " Config: %s" % (myconfig)
209214
# Process this tree
210215
if(len(myconfig)==0 or len(mytree["locs"])==0):
211-
if(args.verbose>2):
216+
if(options.verbose>2):
212217
print " No processing for %s - skipping" % (tree)
213218
else:
214219
only = None
@@ -217,7 +222,7 @@ def queueForRemoval(tree):
217222
if (len(only)==0) and (mytree["treeprefix"] != ""):
218223
thePool = "%spool.res" % (mytree["treeprefix"])
219224
if (thePool in itemset):
220-
if(args.verbose>0):
225+
if(options.verbose>0):
221226
print "Removing %s because tree %s is empty." % (thePool, tree)
222227
remove.add(thePool)
223228
else:
@@ -227,12 +232,12 @@ def queueForRemoval(tree):
227232
if (only is not None) and not loc in only:
228233
# REMOVE loc
229234
toRemove = "%s%s%s" % (mytree["treeprefix"], loc, mytree["extension"])
230-
if(args.verbose>6):
235+
if(options.verbose>6):
231236
print "Queueing for removal: %s" % toRemove
232237
remove.add(toRemove)
233238

234239
def addTreeByType(tree, mytree):
235-
if(args.verbose>1):
240+
if(options.verbose>1):
236241
print "(considering %s): %s" % (tree, mytree)
237242
trees[tree] = mytree
238243
mytree["locs"]=[]
@@ -259,18 +264,18 @@ def addTreeByType(tree, mytree):
259264
tree = "ROOT"
260265
else:
261266
tree = treeprefix[0:-1]
262-
if(args.verbose>6):
267+
if(options.verbose>6):
263268
print "procesing %s" % (tree)
264269
trees[tree] = { "extension": ".res", "treeprefix": treeprefix, "hasIndex": True }
265270
# 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)
267272
runcmd("iculslocs", "-i %s -N %s -T %s -l > %s" % (outfile, dataname, tree, treelistfile))
268273
fi = open(treelistfile, 'rb')
269274
treeitems = fi.readlines()
270275
trees[tree]["locs"] = [treeitems[i].strip() for i in range(len(treeitems))]
271276
fi.close()
272277
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)
274279
else:
275280
queueForRemoval(tree)
276281

@@ -281,19 +286,19 @@ def removeList(count=0):
281286
if(count > 10):
282287
print "Giving up - %dth attempt at removal." % count
283288
sys.exit(1)
284-
if(args.verbose>1):
289+
if(options.verbose>1):
285290
print "%d items to remove - try #%d" % (len(remove),count)
286291
if(len(remove)>0):
287292
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")
290295
fi = open(removefile, 'wb')
291296
for i in remove:
292297
print >>fi, i
293298
fi.close()
294299
rc = runcmd("icupkg","-r %s %s 2> %s" % (removefile,outfile,hackerrfile),True)
295300
if rc is not 0:
296-
if(args.verbose>5):
301+
if(options.verbose>5):
297302
print "## Damage control, trying to parse stderr from icupkg.."
298303
fi = open(hackerrfile, 'rb')
299304
erritems = fi.readlines()
@@ -305,13 +310,13 @@ def removeList(count=0):
305310
m = pat.match(line)
306311
if m:
307312
toDelete = m.group(1)
308-
if(args.verbose > 5):
313+
if(options.verbose > 5):
309314
print "<< %s added to delete" % toDelete
310315
remove.add(toDelete)
311316
else:
312317
print "ERROR: could not match errline: %s" % line
313318
sys.exit(1)
314-
if(args.verbose > 5):
319+
if(options.verbose > 5):
315320
print " now %d items to remove" % len(remove)
316321
if(oldcount == len(remove)):
317322
print " ERROR: could not add any mor eitems to remove. Fail."
@@ -326,7 +331,7 @@ def removeList(count=0):
326331
# skip trees that don't have res_index
327332
if not trees[tree].has_key("hasIndex"):
328333
continue
329-
treebunddir = args.tmpdir
334+
treebunddir = options.tmpdir
330335
if(trees[tree]["treeprefix"]):
331336
treebunddir = os.path.join(treebunddir, trees[tree]["treeprefix"])
332337
if not (os.path.isdir(treebunddir)):
@@ -335,4 +340,4 @@ def removeList(count=0):
335340
treebundtxt = "%s.txt" % (treebundres[0:-4])
336341
runcmd("iculslocs", "-i %s -N %s -T %s -b %s" % (outfile, dataname, tree, treebundtxt))
337342
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

Comments
 (0)