1#!/usr/bin/python -B
2
3"""Regenerates (just) ICU data source files used to build ICU data files."""
4
5from __future__ import print_function
6
7import os
8import pipes
9import shutil
10import subprocess
11import sys
12
13import i18nutil
14import icuutil
15
16
17# Run with no arguments from any directory, with no special setup required.
18# See icu4c/source/data/cldr-icu-readme.txt for the upstream ICU instructions.
19def main():
20  cldr_dir = icuutil.cldrDir()
21  print('Found cldr in %s ...' % cldr_dir)
22  icu_dir = icuutil.icuDir()
23  print('Found icu in %s ...' % icu_dir)
24
25  # Ant doesn't have any mechanism for using a build directory separate from the
26  # source directory so this build script creates a temporary directory and then
27  # copies all necessary ICU4J and CLDR source code to here before building it:
28  i18nutil.SwitchToNewTemporaryDirectory()
29
30  print('Copying ICU4J source code ...')
31  shutil.copytree(os.path.join(icu_dir, 'icu4j'), 'icu4j', True)
32  print('Building ICU4J ...')
33  subprocess.check_call([
34      'ant',
35      '-f', 'icu4j/build.xml',
36      'jar',
37      'cldrUtil',
38  ])
39
40  # Append the newly built JAR files to the Java CLASSPATH to use these instead
41  # of the pre-built JAR files in the CLDR source tree, to ensure that the same
42  # version of ICU is being used to build the data as will use the data:
43  cp = []
44  try:
45    cp.append(os.environ['CLASSPATH'])
46  except KeyError:
47    pass
48  cp.append('icu4j/icu4j.jar')
49  cp.append('icu4j/out/cldr_util/lib/utilities.jar')
50  os.environ['CLASSPATH'] = ':'.join(cp)
51
52  # This is the location of the original CLDR source tree (not the temporary
53  # copy of the tools source code) from where the data files are to be read:
54  os.environ['CLDR_DIR'] = cldr_dir
55
56  print('Copying CLDR source code ...')
57  shutil.copytree(os.path.join(cldr_dir, 'tools/java'), 'cldr-tools-java', True)
58  print('Building CLDR tools ...')
59  subprocess.check_call([
60      'ant',
61      '-f', 'cldr-tools-java/build.xml',
62      'tool',
63      'AddPseudolocales',
64  ])
65
66  # This is the temporary directory in which the CLDR tools have been built:
67  os.environ['CLDR_CLASSES'] = os.path.join(
68      os.getcwd(), 'cldr-tools-java/classes')
69
70  # It's essential to set CLDR_DTD_CACHE for otherwise the repeated requests for
71  # the same file will cause the unicode.org web server to block this machine:
72  os.makedirs('cldr-dtd-cache')
73  os.environ['ANT_OPTS'] = '-DCLDR_DTD_CACHE=%s' % pipes.quote(os.path.join(
74      os.getcwd(), 'cldr-dtd-cache'))
75
76  print('Building ICU data source files ...')
77  subprocess.check_call([
78      'ant',
79      '-f', os.path.join(icu_dir, 'icu4c/source/data/build.xml'),
80      'clean',
81      'all',
82  ])
83
84  print('Look in %s/icu4c/source/data for new data source files' % icu_dir)
85  sys.exit(0)
86
87if __name__ == '__main__':
88  main()
89