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