xref: /aosp_15_r20/external/icu/tools/updatecldrdata.py (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1*0e209d39SAndroid Build Coastguard Worker#!/usr/bin/python3 -B
2*0e209d39SAndroid Build Coastguard Worker# Copyright 2018 The Android Open Source Project
3*0e209d39SAndroid Build Coastguard Worker#
4*0e209d39SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
5*0e209d39SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
6*0e209d39SAndroid Build Coastguard Worker# You may obtain a copy of the License at
7*0e209d39SAndroid Build Coastguard Worker#
8*0e209d39SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
9*0e209d39SAndroid Build Coastguard Worker#
10*0e209d39SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
11*0e209d39SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
12*0e209d39SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*0e209d39SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
14*0e209d39SAndroid Build Coastguard Worker# limitations under the License.
15*0e209d39SAndroid Build Coastguard Worker
16*0e209d39SAndroid Build Coastguard Worker"""Regenerates (just) ICU data source files used to build ICU data files."""
17*0e209d39SAndroid Build Coastguard Worker
18*0e209d39SAndroid Build Coastguard Workerfrom __future__ import print_function
19*0e209d39SAndroid Build Coastguard Worker
20*0e209d39SAndroid Build Coastguard Workerimport os
21*0e209d39SAndroid Build Coastguard Workerimport shutil
22*0e209d39SAndroid Build Coastguard Workerimport subprocess
23*0e209d39SAndroid Build Coastguard Workerimport sys
24*0e209d39SAndroid Build Coastguard Workerfrom pathlib import Path
25*0e209d39SAndroid Build Coastguard Worker
26*0e209d39SAndroid Build Coastguard Workerimport i18nutil
27*0e209d39SAndroid Build Coastguard Workerimport icuutil
28*0e209d39SAndroid Build Coastguard Worker
29*0e209d39SAndroid Build Coastguard Worker
30*0e209d39SAndroid Build Coastguard Worker# Run with no arguments from any directory, with no special setup required.
31*0e209d39SAndroid Build Coastguard Worker# See icu4c/source/data/cldr-icu-readme.txt for the upstream ICU instructions.
32*0e209d39SAndroid Build Coastguard Workerdef main():
33*0e209d39SAndroid Build Coastguard Worker  if subprocess.call(["which", "mvn"]) != 0 or subprocess.call(["which", "ant"]) != 0:
34*0e209d39SAndroid Build Coastguard Worker    print("Can't find the required tools. Run `sudo apt-get install maven ant` to install")
35*0e209d39SAndroid Build Coastguard Worker    exit(1)
36*0e209d39SAndroid Build Coastguard Worker
37*0e209d39SAndroid Build Coastguard Worker  if not os.path.exists(os.path.join(Path.home(), ".m2/settings.xml")):
38*0e209d39SAndroid Build Coastguard Worker    print("Can\'t find `~/.m2/settings.xml`. Please follow the instructions at "
39*0e209d39SAndroid Build Coastguard Worker          "http://cldr.unicode.org/development/maven to create one and the github token.")
40*0e209d39SAndroid Build Coastguard Worker    exit(1)
41*0e209d39SAndroid Build Coastguard Worker
42*0e209d39SAndroid Build Coastguard Worker  cldr_dir = icuutil.cldrDir()
43*0e209d39SAndroid Build Coastguard Worker  print('Found cldr in %s ...' % cldr_dir)
44*0e209d39SAndroid Build Coastguard Worker  icu_dir = icuutil.icuDir()
45*0e209d39SAndroid Build Coastguard Worker  print('Found icu in %s ...' % icu_dir)
46*0e209d39SAndroid Build Coastguard Worker
47*0e209d39SAndroid Build Coastguard Worker  # Ant doesn't have any mechanism for using a build directory separate from the
48*0e209d39SAndroid Build Coastguard Worker  # source directory so this build script creates a temporary directory and then
49*0e209d39SAndroid Build Coastguard Worker  # copies all necessary ICU4J and CLDR source code to here before building it:
50*0e209d39SAndroid Build Coastguard Worker  i18nutil.SwitchToNewTemporaryDirectory()
51*0e209d39SAndroid Build Coastguard Worker  build_dir = os.getcwd()
52*0e209d39SAndroid Build Coastguard Worker  cldr_build_dir = os.path.join(build_dir, 'cldr')
53*0e209d39SAndroid Build Coastguard Worker  icu4c_build_dir = os.path.join(build_dir, 'icu4c')
54*0e209d39SAndroid Build Coastguard Worker  icu4j_build_dir = os.path.join(build_dir, 'icu4j')
55*0e209d39SAndroid Build Coastguard Worker  icu_tools_build_dir = os.path.join(build_dir, 'icu_tools')
56*0e209d39SAndroid Build Coastguard Worker
57*0e209d39SAndroid Build Coastguard Worker  print('Copying CLDR source code ...')
58*0e209d39SAndroid Build Coastguard Worker  shutil.copytree(cldr_dir, cldr_build_dir, symlinks=True)
59*0e209d39SAndroid Build Coastguard Worker  print('Copying ICU4C source code ...')
60*0e209d39SAndroid Build Coastguard Worker  shutil.copytree(os.path.join(icu_dir, 'icu4c'), icu4c_build_dir, symlinks=True)
61*0e209d39SAndroid Build Coastguard Worker  print('Copying ICU4J source code ...')
62*0e209d39SAndroid Build Coastguard Worker  shutil.copytree(os.path.join(icu_dir, 'icu4j'), icu4j_build_dir, symlinks=True)
63*0e209d39SAndroid Build Coastguard Worker  print('Copying ICU tools source code ...')
64*0e209d39SAndroid Build Coastguard Worker  shutil.copytree(os.path.join(icu_dir, 'tools'), icu_tools_build_dir, symlinks=True)
65*0e209d39SAndroid Build Coastguard Worker
66*0e209d39SAndroid Build Coastguard Worker  # Setup environment variables for all subshell
67*0e209d39SAndroid Build Coastguard Worker  os.environ['ANT_OPTS'] = '-Xmx8192m'
68*0e209d39SAndroid Build Coastguard Worker  os.environ['MAVEN_ARGS'] = '--no-transfer-progress'
69*0e209d39SAndroid Build Coastguard Worker
70*0e209d39SAndroid Build Coastguard Worker  # This is the location of the original CLDR source tree (not the temporary
71*0e209d39SAndroid Build Coastguard Worker  # copy of the tools source code) from where the data files are to be read:
72*0e209d39SAndroid Build Coastguard Worker  os.environ['CLDR_DIR'] = cldr_build_dir  # os.path.join(os.getcwd(), 'cldr')
73*0e209d39SAndroid Build Coastguard Worker
74*0e209d39SAndroid Build Coastguard Worker  os.environ['ICU4C_ROOT'] = icu4c_build_dir
75*0e209d39SAndroid Build Coastguard Worker  os.environ['ICU4J_ROOT'] = icu4j_build_dir
76*0e209d39SAndroid Build Coastguard Worker  os.environ['TOOLS_ROOT'] = icu_tools_build_dir
77*0e209d39SAndroid Build Coastguard Worker  cldr_tmp_dir = os.path.join(build_dir, 'cldr-staging')
78*0e209d39SAndroid Build Coastguard Worker  os.environ['CLDR_TMP_DIR'] = cldr_tmp_dir
79*0e209d39SAndroid Build Coastguard Worker  cldr_production_tmp_dir = os.path.join(cldr_tmp_dir, 'production')
80*0e209d39SAndroid Build Coastguard Worker  os.environ['CLDR_DATA_DIR'] = cldr_production_tmp_dir
81*0e209d39SAndroid Build Coastguard Worker
82*0e209d39SAndroid Build Coastguard Worker  icu_tools_cldr_dir = os.path.join(icu_tools_build_dir, 'cldr')
83*0e209d39SAndroid Build Coastguard Worker  print('Installing CLDR tools ...')
84*0e209d39SAndroid Build Coastguard Worker  os.chdir(icu_tools_cldr_dir)
85*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call(['ant', 'install-cldr-libs'])
86*0e209d39SAndroid Build Coastguard Worker
87*0e209d39SAndroid Build Coastguard Worker  print('Building ICU data...')
88*0e209d39SAndroid Build Coastguard Worker  icu4c_data_build_dir = os.path.join(icu4c_build_dir, 'source/data')
89*0e209d39SAndroid Build Coastguard Worker  os.chdir(icu4c_data_build_dir)
90*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call(['ant', 'cleanprod'])
91*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call(['ant', 'setup'])
92*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call(['ant', 'proddata'])
93*0e209d39SAndroid Build Coastguard Worker
94*0e209d39SAndroid Build Coastguard Worker  # Finally we "compile" CLDR-data to a "production" form and place it in ICU
95*0e209d39SAndroid Build Coastguard Worker  os.chdir(os.path.join(icu_tools_build_dir, 'cldr', 'cldr-to-icu'))
96*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call([
97*0e209d39SAndroid Build Coastguard Worker    'ant',
98*0e209d39SAndroid Build Coastguard Worker    '-f',
99*0e209d39SAndroid Build Coastguard Worker    'build-icu-data.xml',
100*0e209d39SAndroid Build Coastguard Worker    '-DcldrDataDir=' + cldr_production_tmp_dir,
101*0e209d39SAndroid Build Coastguard Worker    '-DforceDelete=true',
102*0e209d39SAndroid Build Coastguard Worker    '-DincludePseudoLocales=true'
103*0e209d39SAndroid Build Coastguard Worker  ])
104*0e209d39SAndroid Build Coastguard Worker
105*0e209d39SAndroid Build Coastguard Worker  os.chdir(icu_tools_cldr_dir)
106*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call([
107*0e209d39SAndroid Build Coastguard Worker    'ant',
108*0e209d39SAndroid Build Coastguard Worker    'copy-cldr-testdata',
109*0e209d39SAndroid Build Coastguard Worker  ])
110*0e209d39SAndroid Build Coastguard Worker
111*0e209d39SAndroid Build Coastguard Worker  # Copy the generated data files from the temporary directory into AOSP.
112*0e209d39SAndroid Build Coastguard Worker  icu4c_data_source_dir = os.path.join(icu_dir, 'icu4c/source/data')
113*0e209d39SAndroid Build Coastguard Worker  rmAndCopyTree(icu4c_data_build_dir, icu4c_data_source_dir)
114*0e209d39SAndroid Build Coastguard Worker
115*0e209d39SAndroid Build Coastguard Worker  # Copy test data. It mirrors the copy-cldr-testdata steps in tools/cldr/build.xml.
116*0e209d39SAndroid Build Coastguard Worker  rmAndCopyTree(
117*0e209d39SAndroid Build Coastguard Worker    os.path.join(icu4c_build_dir, 'source/test/testdata/cldr'),
118*0e209d39SAndroid Build Coastguard Worker    os.path.join(icu_dir, 'icu4c/source/test/testdata/cldr'))
119*0e209d39SAndroid Build Coastguard Worker  rmAndCopyTree(
120*0e209d39SAndroid Build Coastguard Worker    os.path.join(icu4j_build_dir, 'main/core/src/test/resources/com/ibm/icu/dev/data/cldr'),
121*0e209d39SAndroid Build Coastguard Worker    os.path.join(icu_dir, 'icu4j/main/core/src/test/resources/com/ibm/icu/dev/data/cldr'))
122*0e209d39SAndroid Build Coastguard Worker
123*0e209d39SAndroid Build Coastguard Worker  # Copy the generated localefallback_data.h and LocaleFallbackData.java
124*0e209d39SAndroid Build Coastguard Worker  shutil.copy(
125*0e209d39SAndroid Build Coastguard Worker    os.path.join(icu4c_build_dir, 'source/common/localefallback_data.h'),
126*0e209d39SAndroid Build Coastguard Worker    os.path.join(icu_dir, 'icu4c/source/common/localefallback_data.h'))
127*0e209d39SAndroid Build Coastguard Worker  shutil.copy(
128*0e209d39SAndroid Build Coastguard Worker    os.path.join(icu4j_build_dir,
129*0e209d39SAndroid Build Coastguard Worker                 'main/core/src/main/java/com/ibm/icu/impl/LocaleFallbackData.java'),
130*0e209d39SAndroid Build Coastguard Worker    os.path.join(icu_dir, 'icu4j/main/core/src/main/java/com/ibm/icu/impl/LocaleFallbackData.java'))
131*0e209d39SAndroid Build Coastguard Worker
132*0e209d39SAndroid Build Coastguard Worker  print('Look in %s for new data source files' % icu4c_data_source_dir)
133*0e209d39SAndroid Build Coastguard Worker  sys.exit(0)
134*0e209d39SAndroid Build Coastguard Worker
135*0e209d39SAndroid Build Coastguard Worker
136*0e209d39SAndroid Build Coastguard Workerdef rmAndCopyTree(src, dst):
137*0e209d39SAndroid Build Coastguard Worker  if os.path.exists(dst):
138*0e209d39SAndroid Build Coastguard Worker    shutil.rmtree(dst)
139*0e209d39SAndroid Build Coastguard Worker  shutil.copytree(src, dst)
140*0e209d39SAndroid Build Coastguard Worker
141*0e209d39SAndroid Build Coastguard Worker
142*0e209d39SAndroid Build Coastguard Workerif __name__ == '__main__':
143*0e209d39SAndroid Build Coastguard Worker  main()
144