12e5b6d6dSopenharmony_ci#!/usr/bin/python -B
22e5b6d6dSopenharmony_ci#
32e5b6d6dSopenharmony_ci# Copyright (C) 2017 and later: Unicode, Inc. and others.
42e5b6d6dSopenharmony_ci# License & terms of use: http://www.unicode.org/copyright.html
52e5b6d6dSopenharmony_ci#
62e5b6d6dSopenharmony_ci# Copyright (C) 2013-2014 IBM Corporation and Others. All Rights Reserved.
72e5b6d6dSopenharmony_ci#
82e5b6d6dSopenharmony_ciprint("NOTE: this tool is a TECHNOLOGY PREVIEW and not a supported ICU tool.")
92e5b6d6dSopenharmony_ci#
102e5b6d6dSopenharmony_ci# @author Steven R. Loomis <srl@icu-project.org>
112e5b6d6dSopenharmony_ci#
122e5b6d6dSopenharmony_ci# Yet Another Resource Builder
132e5b6d6dSopenharmony_ci#
142e5b6d6dSopenharmony_ci# Usage:
152e5b6d6dSopenharmony_ci#
162e5b6d6dSopenharmony_ci##$ mkdir loc
172e5b6d6dSopenharmony_ci##$ echo 'root { hello { "Hello" } }' > loc/root.txt
182e5b6d6dSopenharmony_ci##$ echo 'es { hello { "Hola" } }' > loc/es.txt
192e5b6d6dSopenharmony_ci##$ rm -rf ./out
202e5b6d6dSopenharmony_ci##$ bldicures.py --name myapp --from ./loc -d ./out
212e5b6d6dSopenharmony_ci##$ ls out/myapp.dat
222e5b6d6dSopenharmony_ci#
232e5b6d6dSopenharmony_ci# use 'bldicures.py --help' for help.
242e5b6d6dSopenharmony_ci#
252e5b6d6dSopenharmony_ci# BUGS/TODO
262e5b6d6dSopenharmony_ci# * dependency calculation
272e5b6d6dSopenharmony_ci# * pathnames/PATH for genrb/pkgdata
282e5b6d6dSopenharmony_ci# * cleanup of files, ./out, etc
292e5b6d6dSopenharmony_ci# * document res_index
302e5b6d6dSopenharmony_ci# * probably pathname munging
312e5b6d6dSopenharmony_ci# * deuglify python
322e5b6d6dSopenharmony_ci
332e5b6d6dSopenharmony_ci
342e5b6d6dSopenharmony_ciimport sys
352e5b6d6dSopenharmony_ci
362e5b6d6dSopenharmony_ci# for utf-8
372e5b6d6dSopenharmony_ci#reload(sys)
382e5b6d6dSopenharmony_ci#sys.setdefaultencoding("utf-8")
392e5b6d6dSopenharmony_ci
402e5b6d6dSopenharmony_ciimport argparse
412e5b6d6dSopenharmony_ciimport os
422e5b6d6dSopenharmony_ci
432e5b6d6dSopenharmony_ciendian=sys.byteorder
442e5b6d6dSopenharmony_ci
452e5b6d6dSopenharmony_ciparser = 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')
462e5b6d6dSopenharmony_ciparser.add_argument('-f', '--from', action='append', dest='fromdirs', help='read .txt files from this dir', metavar='fromdir', required=True)
472e5b6d6dSopenharmony_ciparser.add_argument('-n', '--name', action='store', help='set the bundle name, such as "myapp"', metavar='bundname', required=True)
482e5b6d6dSopenharmony_ciparser.add_argument('-m', '--mode', action='store', help='pkgdata mode', metavar='mode', default="archive")
492e5b6d6dSopenharmony_ciparser.add_argument('-d', '--dest', action='store', dest='destdir', help='dest dir, default is ".".', default=".", metavar='destdir')
502e5b6d6dSopenharmony_ciparser.add_argument('-e', '--endian', action='store', dest='endian', help='endian, big, little or host, your default is "%s".' % endian, default=endian, metavar='endianness')
512e5b6d6dSopenharmony_ciparser.add_argument('--verbose', '-v', action='count',default=0)
522e5b6d6dSopenharmony_ci
532e5b6d6dSopenharmony_ciargs = parser.parse_args()
542e5b6d6dSopenharmony_ciif args.verbose>0:
552e5b6d6dSopenharmony_ci    print("Options: "+str(args))
562e5b6d6dSopenharmony_ci
572e5b6d6dSopenharmony_ciif args.verbose > 0:
582e5b6d6dSopenharmony_ci    print("mkdir " + args.destdir)
592e5b6d6dSopenharmony_cios.makedirs(args.destdir)
602e5b6d6dSopenharmony_citmpdir = 'tmp'
612e5b6d6dSopenharmony_cios.makedirs('%s/%s/' % (args.destdir, tmpdir))
622e5b6d6dSopenharmony_ci
632e5b6d6dSopenharmony_cilistname = '%s/%s/icufiles.lst' % (args.destdir, tmpdir)
642e5b6d6dSopenharmony_ci
652e5b6d6dSopenharmony_ci
662e5b6d6dSopenharmony_ci
672e5b6d6dSopenharmony_ciif args.endian not in ("big","little","host"):
682e5b6d6dSopenharmony_ci    print("Unknown endianness: %s" % args.endian)
692e5b6d6dSopenharmony_ci    sys.exit(1)
702e5b6d6dSopenharmony_ci
712e5b6d6dSopenharmony_ciif args.endian == "host":
722e5b6d6dSopenharmony_ci    args.endian = endian
732e5b6d6dSopenharmony_ci
742e5b6d6dSopenharmony_cineedswap = args.endian is not endian
752e5b6d6dSopenharmony_ci
762e5b6d6dSopenharmony_ciif needswap and args.mode not in ("archive", "files"):
772e5b6d6dSopenharmony_ci    print("Don't know how to do swapping for mode=%s" % args.mode)
782e5b6d6dSopenharmony_ci    sys.exit(1)
792e5b6d6dSopenharmony_ci
802e5b6d6dSopenharmony_cipkgmode = args.mode
812e5b6d6dSopenharmony_ci
822e5b6d6dSopenharmony_ciif needswap and args.mode in ("files"):
832e5b6d6dSopenharmony_ci    pkgmode = "archive"
842e5b6d6dSopenharmony_ci
852e5b6d6dSopenharmony_ciif args.verbose > 0:
862e5b6d6dSopenharmony_ci    print(">%s" % (listname))
872e5b6d6dSopenharmony_cilistfn = open(listname, 'w')
882e5b6d6dSopenharmony_ci
892e5b6d6dSopenharmony_ciprint('# list for "%s" generated by %s on %s' % (args.name,parser.prog, '(now)'), file = listfn)
902e5b6d6dSopenharmony_ciprint('# args: ' + str(args), file = listfn)
912e5b6d6dSopenharmony_ciprint('#', file = listfn)
922e5b6d6dSopenharmony_ci
932e5b6d6dSopenharmony_ciidxname = '%s/%s/res_index.txt' % (args.destdir, tmpdir)
942e5b6d6dSopenharmony_ciidxfn = open(idxname, 'w')
952e5b6d6dSopenharmony_ciprint("""// Warning, this file is autogenerated by %s
962e5b6d6dSopenharmony_cires_index:table(nofallback) {
972e5b6d6dSopenharmony_ci InstalledLocales:table {""" % (parser.prog), file = idxfn)
982e5b6d6dSopenharmony_ci
992e5b6d6dSopenharmony_cigens = {}
1002e5b6d6dSopenharmony_ci
1012e5b6d6dSopenharmony_cidef add_res(resname,txtname, loc):
1022e5b6d6dSopenharmony_ci    if args.verbose>0:
1032e5b6d6dSopenharmony_ci        print("+ %s (from %s)" % (loc, txtname))
1042e5b6d6dSopenharmony_ci    print("# %s" % (txtname), file = listfn)
1052e5b6d6dSopenharmony_ci    print("%s.res" % (loc), file = listfn)
1062e5b6d6dSopenharmony_ci    gens[loc] = { "loc": loc, "res": resname, "txt": txtname }
1072e5b6d6dSopenharmony_ci
1082e5b6d6dSopenharmony_ciadd_res('%s/%s/res_index.res' % (args.destdir,tmpdir), idxname, 'res_index')
1092e5b6d6dSopenharmony_ci
1102e5b6d6dSopenharmony_cidef add_loc(path, loc):
1112e5b6d6dSopenharmony_ci    resf = '%s/%s/%s.res' % (args.destdir,tmpdir,loc)
1122e5b6d6dSopenharmony_ci    txtf = '%s/%s.txt' % (path, loc)
1132e5b6d6dSopenharmony_ci    print("  %s {\"\"}" % loc, file = idxfn)
1142e5b6d6dSopenharmony_ci    add_res(resf, txtf, loc)
1152e5b6d6dSopenharmony_ci
1162e5b6d6dSopenharmony_cifor dir in args.fromdirs:
1172e5b6d6dSopenharmony_ci    if args.verbose>0:
1182e5b6d6dSopenharmony_ci        print("Collecting .txt files in %s" % (dir))
1192e5b6d6dSopenharmony_ci
1202e5b6d6dSopenharmony_ci    walks = os.walk(dir)
1212e5b6d6dSopenharmony_ci    for ent in walks:
1222e5b6d6dSopenharmony_ci            junk = (path,dirs,files) = ent
1232e5b6d6dSopenharmony_ci            if args.verbose>3:
1242e5b6d6dSopenharmony_ci                print(junk)
1252e5b6d6dSopenharmony_ci            if (path.find("/.svn") != -1):
1262e5b6d6dSopenharmony_ci                continue
1272e5b6d6dSopenharmony_ci            for file in files:
1282e5b6d6dSopenharmony_ci                if (file[-4:] != ".txt"):
1292e5b6d6dSopenharmony_ci                    if args.verbose>1:
1302e5b6d6dSopenharmony_ci                        print("Ignoring %s/%s with suffix %s" % (path,file, file[-4:]))
1312e5b6d6dSopenharmony_ci                    continue
1322e5b6d6dSopenharmony_ci                loc = file[:-4]
1332e5b6d6dSopenharmony_ci                add_loc(path, loc)
1342e5b6d6dSopenharmony_ci
1352e5b6d6dSopenharmony_ciprint(" }", file = idxfn)
1362e5b6d6dSopenharmony_ciprint("}", file = idxfn)
1372e5b6d6dSopenharmony_ci
1382e5b6d6dSopenharmony_ciidxfn.close()
1392e5b6d6dSopenharmony_cilistfn.close()
1402e5b6d6dSopenharmony_ci
1412e5b6d6dSopenharmony_ciif (args.verbose>2):
1422e5b6d6dSopenharmony_ci    print(gens)
1432e5b6d6dSopenharmony_ci
1442e5b6d6dSopenharmony_ciif (args.verbose>3):
1452e5b6d6dSopenharmony_ci    print("TODO: dependency tracking. For now, don't care")
1462e5b6d6dSopenharmony_ci
1472e5b6d6dSopenharmony_cifor gen in gens:
1482e5b6d6dSopenharmony_ci    item = gens[gen]
1492e5b6d6dSopenharmony_ci    cmd = 'genrb -d "%s/%s" "%s"' % (args.destdir, tmpdir, item["txt"])
1502e5b6d6dSopenharmony_ci    if (args.verbose>1):
1512e5b6d6dSopenharmony_ci        print("# " + cmd)
1522e5b6d6dSopenharmony_ci    os.system(cmd)
1532e5b6d6dSopenharmony_ci
1542e5b6d6dSopenharmony_cicmd = '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)
1552e5b6d6dSopenharmony_ciif (args.verbose>1):
1562e5b6d6dSopenharmony_ci    cmd = cmd + " -v"
1572e5b6d6dSopenharmony_ci    print("# " + cmd)
1582e5b6d6dSopenharmony_circ = os.system(cmd)
1592e5b6d6dSopenharmony_ciif rc != 0:
1602e5b6d6dSopenharmony_ci    print("# Command failed: " + cmd)
1612e5b6d6dSopenharmony_ci    sys.exit(rc)
1622e5b6d6dSopenharmony_ci
1632e5b6d6dSopenharmony_ciif needswap:
1642e5b6d6dSopenharmony_ci    outfile = "%s/%s.dat" % (args.destdir, args.name)
1652e5b6d6dSopenharmony_ci    tmpfile =  "%s/%s/%s.dat" % (args.destdir, tmpdir, args.name)
1662e5b6d6dSopenharmony_ci    if args.mode in ("files","archive"):
1672e5b6d6dSopenharmony_ci        print("# %s -> %s" % (outfile, tmpfile))
1682e5b6d6dSopenharmony_ci        os.rename(outfile,tmpfile)
1692e5b6d6dSopenharmony_ci        # swap tmp back to out
1702e5b6d6dSopenharmony_ci        cmd = 'icupkg -w -tb "%s" "%s"' % (tmpfile, outfile)
1712e5b6d6dSopenharmony_ci        if (args.verbose>1):
1722e5b6d6dSopenharmony_ci            cmd = cmd + " -v"
1732e5b6d6dSopenharmony_ci            print("# " + cmd)
1742e5b6d6dSopenharmony_ci        rc = os.system(cmd)
1752e5b6d6dSopenharmony_ci        if rc != 0:
1762e5b6d6dSopenharmony_ci            print("# Swap command failed: " + cmd)
1772e5b6d6dSopenharmony_ci            sys.exit(rc)
1782e5b6d6dSopenharmony_ci    # fall through for files mode.
1792e5b6d6dSopenharmony_ci    if args.mode in ("files"):
1802e5b6d6dSopenharmony_ci        os.mkdir("%s/%s/" % (args.destdir, args.name))
1812e5b6d6dSopenharmony_ci        # unpack
1822e5b6d6dSopenharmony_ci        cmd = 'icupkg -tb -x "%s" -d "%s/%s/" "%s"' % (listname, args.destdir, args.name, outfile)
1832e5b6d6dSopenharmony_ci        if (args.verbose>1):
1842e5b6d6dSopenharmony_ci            cmd = cmd + " -v"
1852e5b6d6dSopenharmony_ci            print("# " + cmd)
1862e5b6d6dSopenharmony_ci        rc = os.system(cmd)
1872e5b6d6dSopenharmony_ci        if rc != 0:
1882e5b6d6dSopenharmony_ci            print("# Swap command failed: " + cmd)
1892e5b6d6dSopenharmony_ci            sys.exit(rc)
1902e5b6d6dSopenharmony_ci        # todo cleanup??
191