Skip to content

Commit

Permalink
tools: fix Python 3 issues in tools/icu/icutrim.py
Browse files Browse the repository at this point in the history
PR-URL: #29213
Reviewed-By: Steven R Loomis <srloomis@us.ibm.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
cclauss authored and BridgeAR committed Sep 3, 2019
1 parent eceebd3 commit a123a20
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions tools/icu/icutrim.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,15 @@ def runcmd(tool, cmd, doContinue=False):
config=json.load(fi)
fi.close()

if (options.locales):
if not config.has_key("variables"):
config["variables"] = {}
if not config["variables"].has_key("locales"):
config["variables"]["locales"] = {}
config["variables"]["locales"]["only"] = options.locales.split(',')

if (options.verbose > 6):
if options.locales:
config["variables"] = config.get("variables", {})
config["variables"]["locales"] = config["variables"].get("locales", {})
config["variables"]["locales"]["only"] = options.locales.split(',')

if options.verbose > 6:
print(config)

if(config.has_key("comment")):
if "comment" in config:
print("%s: %s" % (options.filterfile, config["comment"]))

## STEP 1 - copy the data file, swapping endianness
Expand All @@ -186,61 +184,55 @@ def runcmd(tool, cmd, doContinue=False):
listfile = os.path.join(options.tmpdir,"icudata.lst")
runcmd("icupkg", "-l %s > %s""" % (outfile, listfile))

fi = open(listfile, 'rb')
items = fi.readlines()
items = [items[i].strip() for i in range(len(items))]
fi.close()

with open(listfile, 'rb') as fi:
items = [line.strip() for line in fi.read().decode("utf-8").splitlines()]
itemset = set(items)

if (options.verbose>1):
print("input file: %d items" % (len(items)))
if options.verbose > 1:
print("input file: %d items" % len(items))

# list of all trees
trees = {}
RES_INDX = "res_index.res"
remove = None
# remove - always remove these
if config.has_key("remove"):
if "remove" in config:
remove = set(config["remove"])
else:
remove = set()

# keep - always keep these
if config.has_key("keep"):
if "keep" in config:
keep = set(config["keep"])
else:
keep = set()

def queueForRemoval(tree):
global remove
if not config.has_key("trees"):
# no config
return
if not config["trees"].has_key(tree):
if tree not in config.get("trees", {}):
return
mytree = trees[tree]
if(options.verbose>0):
if options.verbose > 0:
print("* %s: %d items" % (tree, len(mytree["locs"])))
# do varible substitution for this tree here
if isinstance(config["trees"][tree], basestring):
treeStr = config["trees"][tree]
if(options.verbose>5):
if options.verbose > 5:
print(" Substituting $%s for tree %s" % (treeStr, tree))
if(not config.has_key("variables") or not config["variables"].has_key(treeStr)):
if treeStr not in config.get("variables", {}):
print(" ERROR: no variable: variables.%s for tree %s" % (treeStr, tree))
sys.exit(1)
config["trees"][tree] = config["variables"][treeStr]
myconfig = config["trees"][tree]
if(options.verbose>4):
if options.verbose > 4:
print(" Config: %s" % (myconfig))
# Process this tree
if(len(myconfig)==0 or len(mytree["locs"])==0):
if(options.verbose>2):
print(" No processing for %s - skipping" % (tree))
else:
only = None
if myconfig.has_key("only"):
if "only" in myconfig:
only = set(myconfig["only"])
if (len(only)==0) and (mytree["treeprefix"] != ""):
thePool = "%spool.res" % (mytree["treeprefix"])
Expand Down Expand Up @@ -297,7 +289,7 @@ def addTreeByType(tree, mytree):
treeitems = fi.readlines()
trees[tree]["locs"] = [treeitems[i].strip() for i in range(len(treeitems))]
fi.close()
if(not config.has_key("trees") or not config["trees"].has_key(tree)):
if tree not in config.get("trees", {}):
print(" Warning: filter file %s does not mention trees.%s - will be kept as-is" % (options.filterfile, tree))
else:
queueForRemoval(tree)
Expand All @@ -315,10 +307,8 @@ def removeList(count=0):
oldcount = len(remove)
hackerrfile=os.path.join(options.tmpdir, "REMOVE.err")
removefile = os.path.join(options.tmpdir, "REMOVE.lst")
fi = open(removefile, 'wb')
for i in remove:
print(i, file=fi)
fi.close()
with open(removefile, 'wb') as fi:
fi.write('\n'.join(remove).encode("utf-8") + b'\n')
rc = runcmd("icupkg","-r %s %s 2> %s" % (removefile,outfile,hackerrfile),True)
if rc != 0:
if(options.verbose>5):
Expand Down Expand Up @@ -352,7 +342,7 @@ def removeList(count=0):
# now, fixup res_index, one at a time
for tree in trees:
# skip trees that don't have res_index
if not trees[tree].has_key("hasIndex"):
if "hasIndex" not in trees[tree]:
continue
treebunddir = options.tmpdir
if(trees[tree]["treeprefix"]):
Expand Down

0 comments on commit a123a20

Please sign in to comment.