Fix Collator.getAvailableLocales.

Only ucol_getAvailable was confused by us listing InstalledLocales
as things like "coll/en_US.res" rather than "en_us", but we were
getting them all wrong.

(cherry-pick of d94915e444b0467eb371720677d033f0d20b99f5.)

Change-Id: I5942cb79740b81405c26583e80cc8a5b21b0d843
diff --git a/stubdata/icu-data-default.txt b/stubdata/icu-data-default.txt
index ce357f7..99730f2 100644
--- a/stubdata/icu-data-default.txt
+++ b/stubdata/icu-data-default.txt
@@ -17,10 +17,6 @@
 coll/res_index.res
 coll/root.res
 coll/ucadata.icu
-# coll/he.res is needed for iw.
-coll/he.res
-# coll/id.res is needed for in.
-coll/id.res
 # coll/ps.res is needed for fa_AF.
 coll/ps.res
 
diff --git a/stubdata/icu_dat_generator.py b/stubdata/icu_dat_generator.py
index 47af51a..d24e662 100755
--- a/stubdata/icu_dat_generator.py
+++ b/stubdata/icu_dat_generator.py
@@ -73,13 +73,24 @@
   InvokeIcuTool("icupkg", TMP_DAT_PATH, args)
 
 
+def ResFilesToLocales(res_files):
+  locales = []
+  for res_file in res_files:
+    # res_file is something like 'coll/en_US.res'.
+    if not '/' in res_file:
+      locales.append(res_file)
+    else:
+      locales.append(res_file.split('/')[1].replace('.res', ''))
+  return locales
+
+
 def WriteIndex(path, locales):
   empty_value = " {\"\"}\n"  # key-value pair for all locale entries
 
   f = open(path, "w")
   f.write("res_index:table(nofallback) {\n")
   f.write("  InstalledLocales {\n")
-  for locale in locales:
+  for locale in sorted(locales):
     f.write(locale + empty_value)
 
   f.write("  }\n")
@@ -101,7 +112,7 @@
   pattern1 = '%s/data/%s/%s.txt' % (ICU4C_DIR, dir_name, language)
   pattern2 = '%s/data/%s/%s_*.txt' % (ICU4C_DIR, dir_name, language)
   for path in glob.glob(pattern1) + glob.glob(pattern2):
-    if 'TRADITIONAL' in path:
+    if 'TRADITIONAL' in path or 'PHONEBOOK' in path:
       continue
     parts = path.split('/')
     if dir_name == 'locales':
@@ -111,6 +122,12 @@
     collection.add(path)
 
 
+def DumpFile(filename):
+  print ' ----------------- %s' % filename
+  os.system("cat %s" % filename)
+  print ' ----------------- END'
+
+
 # Open input file (such as icu-data-default.txt).
 # Go through the list and generate res_index.res for locales, brkitr,
 # coll, et cetera.
@@ -242,7 +259,7 @@
       if end <= 3 or (line.find("_") <= 3 and line.find("_") > 0):
         locales.add(line[:end])
 
-  kind_to_locales = {
+  kind_to_res_files = {
       "brkitr": brkitrs,
       "coll": colls,
       "curr": currs,
@@ -253,40 +270,45 @@
   }
 
   # Merge the machine-generated list into the human-generated list.
-  for kind, res_files in kind_to_locales.items():
+  for kind, res_files in kind_to_res_files.items():
     for res_file in sorted(res_files):
       if '.' not in res_file:
         res_file = res_file + '.res'
       new_add_list.append(res_file)
 
   if VERBOSE:
-    for kind, locales in kind_to_locales.items():
-      print "%s=%s" % (kind, sorted(locales))
+    for kind, res_files in kind_to_res_files.items():
+      print "%s=%s" % (kind, sorted(res_files))
 
   # Write the genrb input files.
 
   # First add_list.txt, the argument to icupkg -a...
   f = open(os.path.join(TMP_DAT_PATH, "add_list.txt"), "w")
   for line in new_add_list:
+    if line.startswith('#'):
+      continue
     f.write("%s\n" % line)
   f.close()
 
   # Second res_index.txt, used below by genrb.
   res_index = "res_index.txt"
   WriteIndex(os.path.join(TMP_DAT_PATH, res_index), locales)
-  for kind, locales in kind_to_locales.items():
+  for kind, res_files in kind_to_res_files.items():
     if kind == "locales":
       continue
-    WriteIndex(os.path.join(TMP_DAT_PATH, kind, res_index), locales)
+    res_index_filename = os.path.join(TMP_DAT_PATH, kind, res_index)
+    WriteIndex(res_index_filename, ResFilesToLocales(res_files))
+    if VERY_VERBOSE:
+      DumpFile(res_index_filename)
 
   # Useful if you need to see the temporary input files we generated.
-  if False:
-    os.system("cat %s/add_list.txt" % TMP_DAT_PATH)
-    os.system("cat %s/res_index.txt" % TMP_DAT_PATH)
+  if VERY_VERBOSE:
+    DumpFile('%s/add_list.txt' % TMP_DAT_PATH)
+    DumpFile('%s/res_index.txt' % TMP_DAT_PATH)
 
   # Call genrb to generate new res_index.res.
   InvokeIcuTool("genrb", TMP_DAT_PATH, [res_index])
-  for kind, locales in kind_to_locales.items():
+  for kind, res_files in kind_to_res_files.items():
     if kind == "locales":
       continue
     InvokeIcuTool("genrb", os.path.join(TMP_DAT_PATH, kind), [res_index])
@@ -315,13 +337,13 @@
   global ICU_DATA           # e.g. "icudt50l"
   global TMP_DAT_PATH       # Temporary directory to store all resource files and
                             # intermediate dat files.
-  global VERBOSE
+  global VERBOSE, VERY_VERBOSE
 
-  VERBOSE = False
+  VERBOSE = VERY_VERBOSE = False
 
   show_help = False
   try:
-    opts, args = getopt.getopt(sys.argv[1:], "hv", ["help", "verbose"])
+    opts, args = getopt.getopt(sys.argv[1:], "hv", ["help", "verbose", "very-verbose"])
   except getopt.error:
     PrintHelpAndExit()
   for opt, _ in opts:
@@ -329,6 +351,8 @@
       show_help = True
     elif opt in ("-v", "--verbose"):
       VERBOSE = True
+    elif opt in ("--very-verbose"):
+      VERY_VERBOSE = VERBOSE = True
   if args:
     show_help = True
 
diff --git a/stubdata/icudt51l-default.dat b/stubdata/icudt51l-default.dat
index 27fda78..a754289 100644
--- a/stubdata/icudt51l-default.dat
+++ b/stubdata/icudt51l-default.dat
Binary files differ