Repository navigation
Expand file tree
/
Copy pathbldicures.py
More file actions
executable file
·191 lines (157 loc) · 5.79 KB
/
Copy pathbldicures.py
File metadata and controls
executable file
·191 lines (157 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/python -B
#
# Copyright (C) 2017 and later: Unicode, Inc. and others.
# License & terms of use: http://www.unicode.org/copyright.html
#
# Copyright (C) 2013-2014 IBM Corporation and Others. All Rights Reserved.
#
print "NOTE: this tool is a TECHNOLOGY PREVIEW and not a supported ICU tool."
#
# @author Steven R. Loomis <srl@icu-project.org>
#
# Yet Another Resource Builder
#
# Usage:
#
##$ mkdir loc
##$ echo 'root { hello { "Hello" } }' > loc/root.txt
##$ echo 'es { hello { "Hola" } }' > loc/es.txt
##$ rm -rf ./out
##$ bldicures.py --name myapp --from ./loc -d ./out
##$ ls out/myapp.dat
#
# use 'bldicures.py --help' for help.
#
# BUGS/TODO
# * dependency calculation
# * pathnames/PATH for genrb/pkgdata
# * cleanup of files, ./out, etc
# * document res_index
# * probably pathname munging
# * deuglify python
import sys
# for utf-8
#reload(sys)
#sys.setdefaultencoding("utf-8")
import argparse
import os
endian=sys.byteorder
parser = argparse.ArgumentParser(description='Yet Another ICU Resource Builder', epilog='ICU tool, http://icu-project.org - master copy at http://source.icu-project.org/repos/icu/tools/trunk/scripts/bldicures.py')
parser.add_argument('-f', '--from', action='append', dest='fromdirs', help='read .txt files from this dir', metavar='fromdir', required=True)
parser.add_argument('-n', '--name', action='store', help='set the bundle name, such as "myapp"', metavar='bundname', required=True)
parser.add_argument('-m', '--mode', action='store', help='pkgdata mode', metavar='mode', default="archive")
parser.add_argument('-d', '--dest', action='store', dest='destdir', help='dest dir, default is ".".', default=".", metavar='destdir')
parser.add_argument('-e', '--endian', action='store', dest='endian', help='endian, big, little or host, your default is "%s".' % endian, default=endian, metavar='endianness')
parser.add_argument('--verbose', '-v', action='count',default=0)
args = parser.parse_args()
if args.verbose>0:
print "Options: "+str(args)
if args.verbose > 0:
print "mkdir " + args.destdir
os.makedirs(args.destdir)
tmpdir = 'tmp'
os.makedirs('%s/%s/' % (args.destdir, tmpdir))
listname = '%s/%s/icufiles.lst' % (args.destdir, tmpdir)
if args.endian not in ("big","little","host"):
print "Unknown endianness: %s" % args.endian
sys.exit(1)
if args.endian is "host":
args.endian = endian
needswap = args.endian is not endian
if needswap and args.mode not in ("archive", "files"):
print "Don't know how to do swapping for mode=%s" % args.mode
sys.exit(1)
pkgmode = args.mode
if needswap and args.mode in ("files"):
pkgmode = "archive"
if args.verbose > 0:
print ">%s" % (listname)
listfn = open(listname, 'w')
print >>listfn, '# list for "%s" generated by %s on %s' % (args.name,parser.prog, '(now)')
print >>listfn, '# args: ' + str(args)
print >>listfn, '#'
idxname = '%s/%s/res_index.txt' % (args.destdir, tmpdir)
idxfn = open(idxname, 'w')
print >>idxfn, """// Warning, this file is autogenerated by %s
res_index:table(nofallback) {
InstalledLocales:table {""" % (parser.prog)
gens = {}
def add_res(resname,txtname, loc):
if args.verbose>0:
print "+ %s (from %s)" % (loc, txtname)
print >>listfn, "# %s" % (txtname)
print >>listfn, "%s.res" % (loc)
gens[loc] = { "loc": loc, "res": resname, "txt": txtname }
add_res('%s/%s/res_index.res' % (args.destdir,tmpdir), idxname, 'res_index')
def add_loc(path, loc):
resf = '%s/%s/%s.res' % (args.destdir,tmpdir,loc)
txtf = '%s/%s.txt' % (path, loc)
print >>idxfn, " %s {\"\"}" % loc
add_res(resf, txtf, loc)
for dir in args.fromdirs:
if args.verbose>0:
print "Collecting .txt files in %s" % (dir)
walks = os.walk(dir)
for ent in walks:
junk = (path,dirs,files) = ent
if args.verbose>3:
print junk
if (path.find("/.svn") != -1):
continue
for file in files:
if (file[-4:] != ".txt"):
if args.verbose>1:
print "Ignoring %s/%s with suffix %s" % (path,file, file[-4:])
continue
loc = file[:-4]
add_loc(path, loc)
print >>idxfn, " }"
print >>idxfn, "}"
idxfn.close()
listfn.close()
if (args.verbose>2):
print gens
if (args.verbose>3):
print "TODO: dependency tracking. For now, dont' care"
for gen in gens:
item = gens[gen]
cmd = 'genrb -d "%s/%s" "%s"' % (args.destdir, tmpdir, item["txt"])
if (args.verbose>1):
print "# " + cmd
os.system(cmd)
cmd = 'pkgdata -m "%s" -T "%s/%s" -p "%s" -s "%s/%s" -d "%s" "%s"' % (pkgmode,args.destdir,tmpdir,args.name,args.destdir,tmpdir,args.destdir,listname)
if (args.verbose>1):
cmd = cmd + " -v"
print "# " + cmd
rc = os.system(cmd)
if rc is not 0:
print "# Command failed: " + cmd
sys.exit(rc)
if needswap:
outfile = "%s/%s.dat" % (args.destdir, args.name)
tmpfile = "%s/%s/%s.dat" % (args.destdir, tmpdir, args.name)
if args.mode in ("files","archive"):
print "# %s -> %s" % (outfile, tmpfile)
os.rename(outfile,tmpfile)
# swap tmp back to out
cmd = 'icupkg -w -tb "%s" "%s"' % (tmpfile, outfile)
if (args.verbose>1):
cmd = cmd + " -v"
print "# " + cmd
rc = os.system(cmd)
if rc is not 0:
print "# Swap command failed: " + cmd
sys.exit(rc)
# fall through for files mode.
if args.mode in ("files"):
os.mkdir("%s/%s/" % (args.destdir, args.name))
# unpack
cmd = 'icupkg -tb -x "%s" -d "%s/%s/" "%s"' % (listname, args.destdir, args.name, outfile)
if (args.verbose>1):
cmd = cmd + " -v"
print "# " + cmd
rc = os.system(cmd)
if rc is not 0:
print "# Swap command failed: " + cmd
sys.exit(rc)
# todo cleanup??