Move langInfo.txt regeneration away from updatecldrdata.py

The regeneration tool depends on ICU4J, and thus the data
ICU4J data resources files need to be regenerated before
generating langInfo.txt.

Thus, the regeneration step from updatecldrdata.py to
updateicudata.py

Test: tools/updateicudata.py
Change-Id: I947c8e277796126c3abbc803a74c9e8a50887f9a
diff --git a/tools/icuutil.py b/tools/icuutil.py
index f44a086..7c84b26 100644
--- a/tools/icuutil.py
+++ b/tools/icuutil.py
@@ -18,6 +18,7 @@
 
 from __future__ import print_function
 
+import filecmp
 import glob
 import os
 import shutil
@@ -245,6 +246,36 @@
   # Switch back to the original working cwd.
   os.chdir(original_working_dir)
 
+def RequiredToMakeLangInfo():
+  """ Returns true if icu4c/source/data/misc/langInfo.txt has been re-generated.
+  Returns false if re-generation is not needed.
+  """
+
+  # Generate icu4c/source/data/misc/langInfo.txt by a ICU4J tool
+  langInfo_dst_path = os.path.join(icu4cDir(), 'data/misc/langInfo.txt')
+  print('Building %s' % langInfo_dst_path)
+  langInfo_out_path = '/tmp/langInfo.txt'  # path hardcoded in the LocaleDistanceBuilder tool
+  if os.path.exists(langInfo_out_path):
+    os.remove(langInfo_out_path)
+
+  icu4j_dir = icu4jDir()
+  os.chdir(icu4j_dir)
+  subprocess.check_call(['ant', 'icu4jJar'])
+  os.chdir(os.path.join(icu4j_dir, 'tools', 'misc'))
+  subprocess.check_call(['ant', 'jar'])
+  subprocess.check_call([
+    'java',
+    '-cp',
+    'out/lib/icu4j-tools.jar:../../icu4j.jar',
+    'com.ibm.icu.dev.tool.locale.LocaleDistanceBuilder',
+  ])
+  if (filecmp.cmp(langInfo_dst_path, langInfo_out_path)):
+    print('The files {src} and {dst} are the same'.format(src=langInfo_out_path, dst=langInfo_dst_path))
+    return False
+
+  print('Copying {src} to {dst}'.format(src=langInfo_out_path, dst=langInfo_dst_path))
+  shutil.copyfile(langInfo_out_path, langInfo_dst_path)
+  return True
 
 def CopyLicenseFiles(target_dir):
   """Copies ICU license files to the target_dir"""
diff --git a/tools/updatecldrdata.py b/tools/updatecldrdata.py
index 6697d72..10b169d 100755
--- a/tools/updatecldrdata.py
+++ b/tools/updatecldrdata.py
@@ -77,6 +77,7 @@
     '-f',
     'build-icu-data.xml',
     '-DcldrDataDir=' + cldr_production_tmp_dir,
+    '-DforceDelete=true',
     '-DincludePseudoLocales=true'
   ])
 
@@ -103,26 +104,6 @@
   shutil.copy(localeCanonicalization_src, os.path.join(
     icu_dir, 'icu4j/main/tests/core/src/com/ibm/icu/dev/data/unicode/localeCanonicalization.txt'))
 
-  # Generate icu4c/source/data/misc/langInfo.txt by a ICU4J tool
-  langInfo_dst_path = os.path.join(icu4c_data_source_dir, 'misc', 'langInfo.txt')
-  print('Building %s' % langInfo_dst_path)
-  langInfo_out_path = '/tmp/langInfo.txt'  # path hardcoded in the LocaleDistanceBuilder tool
-  if os.path.exists(langInfo_out_path):
-    os.remove(langInfo_out_path)
-
-  os.chdir(icu4j_build_dir)
-  subprocess.check_call(['ant', 'icu4jJar'])
-  os.chdir(os.path.join(icu4j_build_dir, 'tools', 'misc'))
-  subprocess.check_call(['ant', 'jar'])
-  subprocess.check_call([
-    'java',
-    '-cp',
-    'out/lib/icu4j-tools.jar:../../icu4j.jar',
-    'com.ibm.icu.dev.tool.locale.LocaleDistanceBuilder',
-  ])
-  print('Copying {src} to {dst}'.format(src=langInfo_out_path, dst=langInfo_dst_path))
-  shutil.copyfile(langInfo_out_path, langInfo_dst_path)
-
   print('Look in %s for new data source files' % icu4c_data_source_dir)
   sys.exit(0)
 
diff --git a/tools/updateicudata.py b/tools/updateicudata.py
index 01122c1..1757619 100755
--- a/tools/updateicudata.py
+++ b/tools/updateicudata.py
@@ -13,18 +13,29 @@
 
 # Run with no arguments from any directory, with no special setup required.
 def main():
-  i18nutil.SwitchToNewTemporaryDirectory()
-  icu_build_dir = '%s/icu' % os.getcwd()
-
   icu_dir = icuutil.icuDir()
   print('Found icu in %s ...' % icu_dir)
 
+  makeIcuDataFiles()
+
+  # if icu4c/source/data/misc/langInfo.txt is re-generated, the binary data files need to be
+  # re-generated. makeIcuDataFiles() are called until it coverages because the re-generation
+  # depends icu4j, and icu4j depends on the bigit nary data files.
+  while (icuutil.RequiredToMakeLangInfo()):
+    makeIcuDataFiles()
+
+
+  print('Look in %s for new data files' % icu_dir)
+  sys.exit(0)
+
+def makeIcuDataFiles():
+  i18nutil.SwitchToNewTemporaryDirectory()
+  icu_build_dir = '%s/icu' % os.getcwd()
+
   icuutil.PrepareIcuBuild(icu_build_dir)
 
   icuutil.MakeAndCopyIcuDataFiles(icu_build_dir)
 
-  print('Look in %s for new data files' % icu_dir)
-  sys.exit(0)
 
 if __name__ == '__main__':
   main()