Home | History | Annotate | Download | only in tools
      1 #!/usr/bin/python -B
      2 
      3 """Regenerates ICU data files."""
      4 
      5 import glob
      6 import os
      7 import shutil
      8 import subprocess
      9 import sys
     10 
     11 import i18nutil
     12 
     13 # Find the icu directory.
     14 android_build_top = i18nutil.GetAndroidRootOrDie()
     15 icu_dir = os.path.realpath('%s/external/icu' % android_build_top)
     16 icu4c_dir = os.path.realpath('%s/icu4c/source' % icu_dir)
     17 icu4j_dir = os.path.realpath('%s/icu4j' % icu_dir)
     18 i18nutil.CheckDirExists(icu4c_dir, 'external/icu/icu4c/source')
     19 i18nutil.CheckDirExists(icu4j_dir, 'external/icu/icu4j')
     20 
     21 def PrepareIcuBuild(icu_build_dir):
     22   # Keep track of the original cwd so we can go back to it at the end.
     23   original_working_dir = os.getcwd()
     24 
     25   # Create a directory to run 'make' from.
     26   os.mkdir(icu_build_dir)
     27   os.chdir(icu_build_dir)
     28 
     29   # Build the ICU tools.
     30   print 'Configuring ICU tools...'
     31   subprocess.check_call(['%s/runConfigureICU' % icu4c_dir, 'Linux'])
     32 
     33   os.chdir(original_working_dir)
     34 
     35 def icuDir():
     36   return icu_dir
     37 
     38 def MakeTzDataFiles(icu_build_dir, data_filename):
     39   # Keep track of the original cwd so we can go back to it at the end.
     40   original_working_dir = os.getcwd()
     41 
     42   # Fix missing files.
     43   os.chdir('%s/tools/tzcode' % icu_build_dir)
     44 
     45   # The tz2icu tool only picks up icuregions and icuzones in they are in the CWD
     46   for icu_data_file in [ 'icuregions', 'icuzones']:
     47     icu_data_file_source = '%s/tools/tzcode/%s' % (icu4c_dir, icu_data_file)
     48     icu_data_file_symlink = './%s' % icu_data_file
     49     os.symlink(icu_data_file_source, icu_data_file_symlink)
     50 
     51   shutil.copyfile('%s/%s' % (original_working_dir, data_filename),
     52                   data_filename)
     53 
     54   print 'Making ICU tz data files...'
     55   # The Makefile assumes the existence of the bin directory.
     56   os.mkdir('%s/bin' % icu_build_dir)
     57   subprocess.check_call(['make'])
     58 
     59   # Copy the source file to its ultimate destination.
     60   icu_txt_data_dir = '%s/data/misc' % icu4c_dir
     61   print 'Copying zoneinfo64.txt to %s ...' % icu_txt_data_dir
     62   shutil.copy('zoneinfo64.txt', icu_txt_data_dir)
     63 
     64   os.chdir(original_working_dir)
     65 
     66 
     67 def MakeAndCopyIcuDataFiles(icu_build_dir):
     68   # Keep track of the original cwd so we can go back to it at the end.
     69   original_working_dir = os.getcwd()
     70 
     71   # Regenerate the .dat file.
     72   os.chdir(icu_build_dir)
     73   subprocess.check_call(['make', 'INCLUDE_UNI_CORE_DATA=1', '-j32'])
     74 
     75   # Copy the .dat file to its ultimate destination.
     76   icu_dat_data_dir = '%s/stubdata' % icu4c_dir
     77   datfiles = glob.glob('data/out/tmp/icudt??l.dat')
     78   if len(datfiles) != 1:
     79     print 'ERROR: Unexpectedly found %d .dat files (%s). Halting.' % (len(datfiles), datfiles)
     80     sys.exit(1)
     81   datfile = datfiles[0]
     82   print 'Copying %s to %s ...' % (datfile, icu_dat_data_dir)
     83   shutil.copy(datfile, icu_dat_data_dir)
     84 
     85   # Generate the ICU4J .jar files
     86   os.chdir('%s/data' % icu_build_dir)
     87   subprocess.check_call(['make', 'icu4j-data'])
     88 
     89   # Copy the ICU4J .jar files to their ultimate destination.
     90   icu_jar_data_dir = '%s/main/shared/data' % icu4j_dir
     91   jarfiles = glob.glob('out/icu4j/*.jar')
     92   if len(jarfiles) != 2:
     93     print 'ERROR: Unexpectedly found %d .jar files (%s). Halting.' % (len(jarfiles), jarfiles)
     94     sys.exit(1)
     95   for jarfile in jarfiles:
     96     print 'Copying %s to %s ...' % (jarfile, icu_jar_data_dir)
     97     shutil.copy(jarfile, icu_jar_data_dir)
     98 
     99   # Switch back to the original working cwd.
    100   os.chdir(original_working_dir)
    101 
    102 # Run with no arguments from any directory, with no special setup required.
    103 def main():
    104   i18nutil.SwitchToNewTemporaryDirectory()
    105   icu_build_dir = '%s/icu' % os.getcwd()
    106 
    107   print 'Found icu in %s ...' % icu_dir
    108 
    109   PrepareIcuBuild(icu_build_dir)
    110 
    111   MakeAndCopyIcuDataFiles(icu_build_dir)
    112 
    113   print 'Look in %s for new data files' % icu_dir
    114   sys.exit(0)
    115 
    116 if __name__ == '__main__':
    117   main()
    118